@eventista/ticketing-common 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cluster/services/cluster.service.js +2 -2
- package/dist/cluster/services/cluster.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -3
- package/src/cluster/config/cluster.config.ts +0 -6
- package/src/cluster/index.ts +0 -2
- package/src/cluster/modules/cluster.module.ts +0 -9
- package/src/cluster/services/cluster.service.ts +0 -76
- package/src/database/index.ts +0 -2
- package/src/database/mongodb/index.ts +0 -2
- package/src/database/mongodb/mongodb.module.ts +0 -42
- package/src/database/mongodb/mongodb.service.ts +0 -111
- package/src/database/redis/index.ts +0 -2
- package/src/database/redis/redis.module.ts +0 -45
- package/src/database/redis/redis.service.ts +0 -142
- package/src/exception/exception.filter.ts +0 -36
- package/src/generic-repository/index.ts +0 -7
- package/src/generic-repository/repositories/base.repository.interface.ts +0 -78
- package/src/generic-repository/repositories/base.repository.ts +0 -199
- package/src/generic-repository/services/base.service.interface.ts +0 -78
- package/src/generic-repository/services/base.service.ts +0 -165
- package/src/index.ts +0 -13
- package/src/logger/custom.logger.ts +0 -83
- package/src/logger/index.ts +0 -2
- package/src/logger/logger.module.ts +0 -14
- package/src/pipes/custom-validation.pipe.ts +0 -61
- package/src/response/response.interceptor.ts +0 -26
- package/src/schemas/base.schema.ts +0 -19
- package/src/schemas/event/event.interfaces.ts +0 -74
- package/src/schemas/event/event.schema.ts +0 -195
- package/src/schemas/index.ts +0 -16
- package/src/schemas/order/order.interfaces.ts +0 -78
- package/src/schemas/order/order.schema.ts +0 -120
- package/src/schemas/promotions/promotions.interfaces.ts +0 -34
- package/src/schemas/promotions/promotions.schema.ts +0 -71
- package/src/schemas/rows/rows.interfaces.ts +0 -14
- package/src/schemas/rows/rows.schema.ts +0 -47
- package/src/schemas/schema.helper.ts +0 -103
- package/src/schemas/ticket-classes/ticket-classes.interfaces.ts +0 -18
- package/src/schemas/ticket-classes/ticket-classes.schemas.ts +0 -51
- package/src/schemas/ticket-summary/ticket-summary.interfaces.ts +0 -20
- package/src/schemas/ticket-summary/ticket-summary.schema.ts +0 -46
- package/src/schemas/user-fanpass/user-fanpass.interfaces.ts +0 -28
- package/src/schemas/user-fanpass/user-fanpass.schemas.ts +0 -86
- package/tsconfig.json +0 -17
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import { Document, FilterQuery, Model, UpdateQuery, QueryOptions, PipelineStage } from 'mongoose';
|
|
2
|
-
import { NotFoundException } from '@nestjs/common';
|
|
3
|
-
import { IBaseRepository } from './base.repository.interface';
|
|
4
|
-
|
|
5
|
-
export class BaseRepository<T extends Document> implements IBaseRepository<T> {
|
|
6
|
-
constructor(protected readonly model: Model<T>) {}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Tìm một document theo điều kiện
|
|
10
|
-
*/
|
|
11
|
-
async findOne(
|
|
12
|
-
filterQuery: FilterQuery<T>,
|
|
13
|
-
projection?: Record<string, unknown>,
|
|
14
|
-
options?: QueryOptions,
|
|
15
|
-
): Promise<T | null> {
|
|
16
|
-
return this.model.findOne(filterQuery, projection, options).exec();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Tìm một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
21
|
-
*/
|
|
22
|
-
async findOneOrFail(
|
|
23
|
-
filterQuery: FilterQuery<T>,
|
|
24
|
-
projection?: Record<string, unknown>,
|
|
25
|
-
options?: QueryOptions,
|
|
26
|
-
): Promise<T> {
|
|
27
|
-
const document = await this.findOne(filterQuery, projection, options);
|
|
28
|
-
if (!document) {
|
|
29
|
-
throw new NotFoundException(`${this.model.modelName} not found`);
|
|
30
|
-
}
|
|
31
|
-
return document;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Tìm nhiều documents theo điều kiện
|
|
36
|
-
*/
|
|
37
|
-
async find(
|
|
38
|
-
filterQuery: FilterQuery<T>,
|
|
39
|
-
projection?: Record<string, unknown>,
|
|
40
|
-
options?: QueryOptions,
|
|
41
|
-
): Promise<T[]> {
|
|
42
|
-
return this.model.find(filterQuery, projection, options).exec();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Tạo một document mới
|
|
47
|
-
*/
|
|
48
|
-
async create(createDto: Partial<T>): Promise<T> {
|
|
49
|
-
const newDocument = new this.model(createDto);
|
|
50
|
-
return newDocument.save();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Tạo nhiều documents
|
|
55
|
-
*/
|
|
56
|
-
async createMany(createDtos: Partial<T>[]): Promise<T[]> {
|
|
57
|
-
const result = await this.model.insertMany(createDtos);
|
|
58
|
-
return result as unknown as T[];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Cập nhật một document theo điều kiện
|
|
63
|
-
*/
|
|
64
|
-
async findOneAndUpdate(
|
|
65
|
-
filterQuery: FilterQuery<T>,
|
|
66
|
-
updateQuery: UpdateQuery<T>,
|
|
67
|
-
options: QueryOptions = { new: true },
|
|
68
|
-
): Promise<T | null> {
|
|
69
|
-
return this.model.findOneAndUpdate(filterQuery, updateQuery, options).exec();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Cập nhật một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
74
|
-
*/
|
|
75
|
-
async findOneAndUpdateOrFail(
|
|
76
|
-
filterQuery: FilterQuery<T>,
|
|
77
|
-
updateQuery: UpdateQuery<T>,
|
|
78
|
-
options: QueryOptions = { new: true },
|
|
79
|
-
): Promise<T> {
|
|
80
|
-
const document = await this.findOneAndUpdate(filterQuery, updateQuery, options);
|
|
81
|
-
if (!document) {
|
|
82
|
-
throw new NotFoundException(`${this.model.modelName} not found`);
|
|
83
|
-
}
|
|
84
|
-
return document;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Cập nhật nhiều documents theo điều kiện
|
|
89
|
-
*/
|
|
90
|
-
async updateMany(
|
|
91
|
-
filterQuery: FilterQuery<T>,
|
|
92
|
-
updateQuery: UpdateQuery<T>,
|
|
93
|
-
): Promise<{ matchedCount: number; modifiedCount: number }> {
|
|
94
|
-
const result = await this.model.updateMany(filterQuery, updateQuery).exec();
|
|
95
|
-
return {
|
|
96
|
-
matchedCount: result.matchedCount,
|
|
97
|
-
modifiedCount: result.modifiedCount,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Xóa một document theo điều kiện
|
|
103
|
-
*/
|
|
104
|
-
async findOneAndDelete(
|
|
105
|
-
filterQuery: FilterQuery<T>,
|
|
106
|
-
options?: QueryOptions,
|
|
107
|
-
): Promise<T | null> {
|
|
108
|
-
return this.model.findOneAndDelete(filterQuery, options).exec();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Xóa một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
113
|
-
*/
|
|
114
|
-
async findOneAndDeleteOrFail(
|
|
115
|
-
filterQuery: FilterQuery<T>,
|
|
116
|
-
options?: QueryOptions,
|
|
117
|
-
): Promise<T> {
|
|
118
|
-
const document = await this.findOneAndDelete(filterQuery, options);
|
|
119
|
-
if (!document) {
|
|
120
|
-
throw new NotFoundException(`${this.model.modelName} not found`);
|
|
121
|
-
}
|
|
122
|
-
return document;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Xóa nhiều documents theo điều kiện
|
|
127
|
-
*/
|
|
128
|
-
async deleteMany(filterQuery: FilterQuery<T>): Promise<{ deletedCount: number }> {
|
|
129
|
-
const result = await this.model.deleteMany(filterQuery).exec();
|
|
130
|
-
return { deletedCount: result.deletedCount || 0 };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Đếm số lượng documents theo điều kiện
|
|
135
|
-
*/
|
|
136
|
-
async count(filterQuery: FilterQuery<T>): Promise<number> {
|
|
137
|
-
return this.model.countDocuments(filterQuery).exec();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Kiểm tra document có tồn tại không
|
|
142
|
-
*/
|
|
143
|
-
async exists(filterQuery: FilterQuery<T>): Promise<boolean> {
|
|
144
|
-
const count = await this.count(filterQuery);
|
|
145
|
-
return count > 0;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Thực hiện aggregation pipeline
|
|
150
|
-
*/
|
|
151
|
-
async aggregate(pipeline: PipelineStage[]): Promise<any[]> {
|
|
152
|
-
return this.model.aggregate(pipeline).exec();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Phân trang
|
|
157
|
-
*/
|
|
158
|
-
async paginate(
|
|
159
|
-
filterQuery: FilterQuery<T>,
|
|
160
|
-
options: {
|
|
161
|
-
page?: number;
|
|
162
|
-
limit?: number;
|
|
163
|
-
sort?: Record<string, 1 | -1>;
|
|
164
|
-
projection?: Record<string, unknown>;
|
|
165
|
-
} = {},
|
|
166
|
-
): Promise<{
|
|
167
|
-
data: T[];
|
|
168
|
-
pagination: {
|
|
169
|
-
total: number;
|
|
170
|
-
page: number;
|
|
171
|
-
limit: number;
|
|
172
|
-
pages: number;
|
|
173
|
-
};
|
|
174
|
-
}> {
|
|
175
|
-
const page = options.page || 1;
|
|
176
|
-
const limit = options.limit || 10;
|
|
177
|
-
const skip = (page - 1) * limit;
|
|
178
|
-
|
|
179
|
-
const [data, total] = await Promise.all([
|
|
180
|
-
this.model
|
|
181
|
-
.find(filterQuery, options.projection)
|
|
182
|
-
.sort(options.sort)
|
|
183
|
-
.skip(skip)
|
|
184
|
-
.limit(limit)
|
|
185
|
-
.exec(),
|
|
186
|
-
this.count(filterQuery),
|
|
187
|
-
]);
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
data,
|
|
191
|
-
pagination: {
|
|
192
|
-
total,
|
|
193
|
-
page,
|
|
194
|
-
limit,
|
|
195
|
-
pages: Math.ceil(total / limit),
|
|
196
|
-
},
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Document, FilterQuery, UpdateQuery, QueryOptions, PipelineStage } from 'mongoose';
|
|
2
|
-
|
|
3
|
-
export interface IBaseService<T extends Document> {
|
|
4
|
-
findOne(
|
|
5
|
-
filterQuery: FilterQuery<T>,
|
|
6
|
-
projection?: Record<string, unknown>,
|
|
7
|
-
options?: QueryOptions,
|
|
8
|
-
): Promise<T | null>;
|
|
9
|
-
|
|
10
|
-
findOneOrFail(
|
|
11
|
-
filterQuery: FilterQuery<T>,
|
|
12
|
-
projection?: Record<string, unknown>,
|
|
13
|
-
options?: QueryOptions,
|
|
14
|
-
): Promise<T>;
|
|
15
|
-
|
|
16
|
-
find(
|
|
17
|
-
filterQuery: FilterQuery<T>,
|
|
18
|
-
projection?: Record<string, unknown>,
|
|
19
|
-
options?: QueryOptions,
|
|
20
|
-
): Promise<T[]>;
|
|
21
|
-
|
|
22
|
-
create(createDto: Partial<T>): Promise<T>;
|
|
23
|
-
|
|
24
|
-
createMany(createDtos: Partial<T>[]): Promise<T[]>;
|
|
25
|
-
|
|
26
|
-
update(
|
|
27
|
-
filterQuery: FilterQuery<T>,
|
|
28
|
-
updateQuery: UpdateQuery<T>,
|
|
29
|
-
options?: QueryOptions,
|
|
30
|
-
): Promise<T | null>;
|
|
31
|
-
|
|
32
|
-
updateOrFail(
|
|
33
|
-
filterQuery: FilterQuery<T>,
|
|
34
|
-
updateQuery: UpdateQuery<T>,
|
|
35
|
-
options?: QueryOptions,
|
|
36
|
-
): Promise<T>;
|
|
37
|
-
|
|
38
|
-
updateMany(
|
|
39
|
-
filterQuery: FilterQuery<T>,
|
|
40
|
-
updateQuery: UpdateQuery<T>,
|
|
41
|
-
): Promise<{ matchedCount: number; modifiedCount: number }>;
|
|
42
|
-
|
|
43
|
-
delete(
|
|
44
|
-
filterQuery: FilterQuery<T>,
|
|
45
|
-
options?: QueryOptions,
|
|
46
|
-
): Promise<T | null>;
|
|
47
|
-
|
|
48
|
-
deleteOrFail(
|
|
49
|
-
filterQuery: FilterQuery<T>,
|
|
50
|
-
options?: QueryOptions,
|
|
51
|
-
): Promise<T>;
|
|
52
|
-
|
|
53
|
-
deleteMany(filterQuery: FilterQuery<T>): Promise<{ deletedCount: number }>;
|
|
54
|
-
|
|
55
|
-
count(filterQuery: FilterQuery<T>): Promise<number>;
|
|
56
|
-
|
|
57
|
-
exists(filterQuery: FilterQuery<T>): Promise<boolean>;
|
|
58
|
-
|
|
59
|
-
aggregate(pipeline: PipelineStage[]): Promise<any[]>;
|
|
60
|
-
|
|
61
|
-
paginate(
|
|
62
|
-
filterQuery: FilterQuery<T>,
|
|
63
|
-
options?: {
|
|
64
|
-
page?: number;
|
|
65
|
-
limit?: number;
|
|
66
|
-
sort?: Record<string, 1 | -1>;
|
|
67
|
-
projection?: Record<string, unknown>;
|
|
68
|
-
},
|
|
69
|
-
): Promise<{
|
|
70
|
-
data: T[];
|
|
71
|
-
pagination: {
|
|
72
|
-
total: number;
|
|
73
|
-
page: number;
|
|
74
|
-
limit: number;
|
|
75
|
-
pages: number;
|
|
76
|
-
};
|
|
77
|
-
}>;
|
|
78
|
-
}
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { Document, FilterQuery, UpdateQuery, QueryOptions, PipelineStage } from 'mongoose';
|
|
2
|
-
import { IBaseRepository } from '../repositories/base.repository.interface';
|
|
3
|
-
import { Logger } from '@nestjs/common';
|
|
4
|
-
import { IBaseService } from './base.service.interface';
|
|
5
|
-
|
|
6
|
-
export class BaseService<T extends Document> implements IBaseService<T> {
|
|
7
|
-
protected readonly logger: Logger;
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
protected readonly repository: IBaseRepository<T>,
|
|
11
|
-
context?: string,
|
|
12
|
-
) {
|
|
13
|
-
this.logger = new Logger(context || this.constructor.name);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Tìm một document theo điều kiện
|
|
18
|
-
*/
|
|
19
|
-
async findOne(
|
|
20
|
-
filterQuery: FilterQuery<T>,
|
|
21
|
-
projection?: Record<string, unknown>,
|
|
22
|
-
options?: QueryOptions,
|
|
23
|
-
): Promise<T | null> {
|
|
24
|
-
return this.repository.findOne(filterQuery, projection, options);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Tìm một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
29
|
-
*/
|
|
30
|
-
async findOneOrFail(
|
|
31
|
-
filterQuery: FilterQuery<T>,
|
|
32
|
-
projection?: Record<string, unknown>,
|
|
33
|
-
options?: QueryOptions,
|
|
34
|
-
): Promise<T> {
|
|
35
|
-
return this.repository.findOneOrFail(filterQuery, projection, options);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Tìm nhiều documents theo điều kiện
|
|
40
|
-
*/
|
|
41
|
-
async find(
|
|
42
|
-
filterQuery: FilterQuery<T>,
|
|
43
|
-
projection?: Record<string, unknown>,
|
|
44
|
-
options?: QueryOptions,
|
|
45
|
-
): Promise<T[]> {
|
|
46
|
-
return this.repository.find(filterQuery, projection, options);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Tạo một document mới
|
|
51
|
-
*/
|
|
52
|
-
async create(createDto: Partial<T>): Promise<T> {
|
|
53
|
-
return this.repository.create(createDto);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Tạo nhiều documents
|
|
58
|
-
*/
|
|
59
|
-
async createMany(createDtos: Partial<T>[]): Promise<T[]> {
|
|
60
|
-
return this.repository.createMany(createDtos);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Cập nhật một document theo điều kiện
|
|
65
|
-
*/
|
|
66
|
-
async update(
|
|
67
|
-
filterQuery: FilterQuery<T>,
|
|
68
|
-
updateQuery: UpdateQuery<T>,
|
|
69
|
-
options: QueryOptions = { new: true },
|
|
70
|
-
): Promise<T | null> {
|
|
71
|
-
return this.repository.findOneAndUpdate(filterQuery, updateQuery, options);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Cập nhật một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
76
|
-
*/
|
|
77
|
-
async updateOrFail(
|
|
78
|
-
filterQuery: FilterQuery<T>,
|
|
79
|
-
updateQuery: UpdateQuery<T>,
|
|
80
|
-
options: QueryOptions = { new: true },
|
|
81
|
-
): Promise<T> {
|
|
82
|
-
return this.repository.findOneAndUpdateOrFail(filterQuery, updateQuery, options);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Cập nhật nhiều documents theo điều kiện
|
|
87
|
-
*/
|
|
88
|
-
async updateMany(
|
|
89
|
-
filterQuery: FilterQuery<T>,
|
|
90
|
-
updateQuery: UpdateQuery<T>,
|
|
91
|
-
): Promise<{ matchedCount: number; modifiedCount: number }> {
|
|
92
|
-
return this.repository.updateMany(filterQuery, updateQuery);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Xóa một document theo điều kiện
|
|
97
|
-
*/
|
|
98
|
-
async delete(
|
|
99
|
-
filterQuery: FilterQuery<T>,
|
|
100
|
-
options?: QueryOptions,
|
|
101
|
-
): Promise<T | null> {
|
|
102
|
-
return this.repository.findOneAndDelete(filterQuery, options);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Xóa một document theo điều kiện hoặc throw exception nếu không tìm thấy
|
|
107
|
-
*/
|
|
108
|
-
async deleteOrFail(
|
|
109
|
-
filterQuery: FilterQuery<T>,
|
|
110
|
-
options?: QueryOptions,
|
|
111
|
-
): Promise<T> {
|
|
112
|
-
return this.repository.findOneAndDeleteOrFail(filterQuery, options);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Xóa nhiều documents theo điều kiện
|
|
117
|
-
*/
|
|
118
|
-
async deleteMany(filterQuery: FilterQuery<T>): Promise<{ deletedCount: number }> {
|
|
119
|
-
return this.repository.deleteMany(filterQuery);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Đếm số lượng documents theo điều kiện
|
|
124
|
-
*/
|
|
125
|
-
async count(filterQuery: FilterQuery<T>): Promise<number> {
|
|
126
|
-
return this.repository.count(filterQuery);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Kiểm tra document có tồn tại không
|
|
131
|
-
*/
|
|
132
|
-
async exists(filterQuery: FilterQuery<T>): Promise<boolean> {
|
|
133
|
-
return this.repository.exists(filterQuery);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Thực hiện aggregation pipeline
|
|
138
|
-
*/
|
|
139
|
-
async aggregate(pipeline: PipelineStage[]): Promise<any[]> {
|
|
140
|
-
return this.repository.aggregate(pipeline);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Phân trang
|
|
145
|
-
*/
|
|
146
|
-
async paginate(
|
|
147
|
-
filterQuery: FilterQuery<T>,
|
|
148
|
-
options: {
|
|
149
|
-
page?: number;
|
|
150
|
-
limit?: number;
|
|
151
|
-
sort?: Record<string, 1 | -1>;
|
|
152
|
-
projection?: Record<string, unknown>;
|
|
153
|
-
} = {},
|
|
154
|
-
): Promise<{
|
|
155
|
-
data: T[];
|
|
156
|
-
pagination: {
|
|
157
|
-
total: number;
|
|
158
|
-
page: number;
|
|
159
|
-
limit: number;
|
|
160
|
-
pages: number;
|
|
161
|
-
};
|
|
162
|
-
}> {
|
|
163
|
-
return this.repository.paginate(filterQuery, options);
|
|
164
|
-
}
|
|
165
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
export * from './database';
|
|
3
|
-
export * from './exception/exception.filter';
|
|
4
|
-
export * from './generic-repository';
|
|
5
|
-
|
|
6
|
-
export * from './logger';
|
|
7
|
-
|
|
8
|
-
export * from './pipes/custom-validation.pipe';
|
|
9
|
-
export * from './response/response.interceptor'
|
|
10
|
-
|
|
11
|
-
export * from './schemas';
|
|
12
|
-
|
|
13
|
-
export * from './cluster/index';
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { LoggerService } from '@nestjs/common';
|
|
2
|
-
import { blue, cyan, gray, green, red, yellow } from 'colorette';
|
|
3
|
-
|
|
4
|
-
export class CustomLogger implements LoggerService {
|
|
5
|
-
private readonly isProduction: boolean;
|
|
6
|
-
|
|
7
|
-
constructor(private readonly context?: string) {
|
|
8
|
-
this.isProduction = process.env.NODE_ENV === 'production';
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
private getTimestamp(): string {
|
|
13
|
-
const localeStringOptions = {
|
|
14
|
-
year: 'numeric',
|
|
15
|
-
hour: 'numeric',
|
|
16
|
-
minute: 'numeric',
|
|
17
|
-
second: 'numeric',
|
|
18
|
-
day: '2-digit',
|
|
19
|
-
month: '2-digit',
|
|
20
|
-
};
|
|
21
|
-
return new Date().toLocaleString(undefined, localeStringOptions as Intl.DateTimeFormatOptions);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
private formatMessage(level: string, message: string, context?: string): string {
|
|
25
|
-
if (this.isProduction) {
|
|
26
|
-
return JSON.stringify({
|
|
27
|
-
timestamp: this.getTimestamp(),
|
|
28
|
-
pid: process.pid,
|
|
29
|
-
level,
|
|
30
|
-
context: context || this.context,
|
|
31
|
-
message
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const timestamp = gray(`[Nest] ${process.pid} - ${this.getTimestamp()}`);
|
|
36
|
-
const formattedLevel = this.colorize(level);
|
|
37
|
-
const contextStr = yellow(`[${context || this.context}] `);
|
|
38
|
-
return `${timestamp} ${formattedLevel} ${contextStr}${message}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private colorize(level: string): string {
|
|
42
|
-
switch (level) {
|
|
43
|
-
case 'LOG':
|
|
44
|
-
return green(`${level} `);
|
|
45
|
-
case 'ERROR':
|
|
46
|
-
return red(`${level}`);
|
|
47
|
-
case 'WARN':
|
|
48
|
-
return yellow(`${level} `);
|
|
49
|
-
case 'DEBUG':
|
|
50
|
-
return blue(`${level}`);
|
|
51
|
-
case 'VERBOSE':
|
|
52
|
-
return cyan(`${level}`);
|
|
53
|
-
default:
|
|
54
|
-
return level;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
log(message: string, context?: string) {
|
|
59
|
-
console.log(this.formatMessage('LOG', message, context));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
error(message: string, metadata?: any) {
|
|
63
|
-
const baseLog = this.formatMessage('ERROR', message);
|
|
64
|
-
if (metadata) {
|
|
65
|
-
console.error(baseLog);
|
|
66
|
-
console.error(red('Details:'), metadata);
|
|
67
|
-
} else {
|
|
68
|
-
console.error(baseLog);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
warn(message: string, context?: string) {
|
|
73
|
-
console.warn(this.formatMessage('WARN', message, context));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
debug(message: string, context?: string) {
|
|
77
|
-
console.debug(this.formatMessage('DEBUG', message, context));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
verbose(message: string, context?: string) {
|
|
81
|
-
console.log(this.formatMessage('VERBOSE', message, context));
|
|
82
|
-
}
|
|
83
|
-
}
|
package/src/logger/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Global, Module } from '@nestjs/common';
|
|
2
|
-
import { CustomLogger } from './custom.logger';
|
|
3
|
-
|
|
4
|
-
@Global()
|
|
5
|
-
@Module({
|
|
6
|
-
providers: [
|
|
7
|
-
{
|
|
8
|
-
provide: 'Logger',
|
|
9
|
-
useValue: new CustomLogger(), // Initialize with no context
|
|
10
|
-
},
|
|
11
|
-
],
|
|
12
|
-
exports: ['Logger'],
|
|
13
|
-
})
|
|
14
|
-
export class LoggerModule {}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ArgumentMetadata,
|
|
3
|
-
BadRequestException,
|
|
4
|
-
Injectable,
|
|
5
|
-
ValidationPipe,
|
|
6
|
-
ValidationPipeOptions,
|
|
7
|
-
} from '@nestjs/common';
|
|
8
|
-
|
|
9
|
-
@Injectable()
|
|
10
|
-
export class CustomValidationPipe extends ValidationPipe {
|
|
11
|
-
constructor(options?: ValidationPipeOptions) {
|
|
12
|
-
super({
|
|
13
|
-
whitelist: true, // Loại bỏ các trường không có trong DTO
|
|
14
|
-
transform: true, // Tự động chuyển đổi dữ liệu đầu vào thành class instance của DTO
|
|
15
|
-
...options, // Cho phép truyền thêm tùy chọn nếu cần
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
public async transform(value, metadata: ArgumentMetadata) {
|
|
19
|
-
try {
|
|
20
|
-
return await super.transform(value, metadata);
|
|
21
|
-
} catch (e) {
|
|
22
|
-
if (e instanceof BadRequestException) {
|
|
23
|
-
const errors: any = [];
|
|
24
|
-
for (const err of e.getResponse()['message']) {
|
|
25
|
-
errors.push({
|
|
26
|
-
field: err.property,
|
|
27
|
-
constraints: this.getConstraints(err),
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
throw new BadRequestException({
|
|
32
|
-
errorCode: 400,
|
|
33
|
-
message: 'Validation failed',
|
|
34
|
-
errors,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getConstraints(err) {
|
|
41
|
-
if (typeof err == 'string') {
|
|
42
|
-
return err;
|
|
43
|
-
}
|
|
44
|
-
if (typeof err == 'object' && err.constraints) {
|
|
45
|
-
return err.constraints;
|
|
46
|
-
}
|
|
47
|
-
return this.deepReduce(err.children);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
deepReduce(array) {
|
|
51
|
-
return array.reduce((accumulator, currentValue) => {
|
|
52
|
-
if (currentValue.children && currentValue.children.length > 0) {
|
|
53
|
-
return this.deepReduce(currentValue.children);
|
|
54
|
-
}
|
|
55
|
-
if (currentValue.constraints) {
|
|
56
|
-
return currentValue.constraints;
|
|
57
|
-
}
|
|
58
|
-
return accumulator;
|
|
59
|
-
}, {});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CallHandler,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
Injectable,
|
|
5
|
-
NestInterceptor,
|
|
6
|
-
} from '@nestjs/common';
|
|
7
|
-
import { Observable } from 'rxjs';
|
|
8
|
-
import { map } from 'rxjs/operators';
|
|
9
|
-
|
|
10
|
-
@Injectable()
|
|
11
|
-
export class ResponseInterceptor implements NestInterceptor {
|
|
12
|
-
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
|
13
|
-
// Chỉ áp dụng cho HTTP hoặc HTTPS requests
|
|
14
|
-
if (context.getType() !== 'http') {
|
|
15
|
-
return next.handle();
|
|
16
|
-
}
|
|
17
|
-
return next.handle().pipe(
|
|
18
|
-
map((data) => ({
|
|
19
|
-
errorCode: 0,
|
|
20
|
-
message: 'Success',
|
|
21
|
-
data,
|
|
22
|
-
timestamp: new Date().toISOString(),
|
|
23
|
-
})),
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Prop, Schema } from '@nestjs/mongoose';
|
|
2
|
-
import { Document } from 'mongoose';
|
|
3
|
-
|
|
4
|
-
export interface BaseDocument extends Document {
|
|
5
|
-
createdAt: Date;
|
|
6
|
-
updatedAt: Date;
|
|
7
|
-
isDeleted: boolean;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class BaseSchema {
|
|
11
|
-
@Prop({ type: Date, default: Date.now })
|
|
12
|
-
createdAt: Date;
|
|
13
|
-
|
|
14
|
-
@Prop({ type: Date, default: Date.now })
|
|
15
|
-
updatedAt: Date;
|
|
16
|
-
|
|
17
|
-
@Prop({ type: Boolean, default: false })
|
|
18
|
-
isDeleted: boolean;
|
|
19
|
-
}
|