@eventista/ticketing-common 1.0.3 → 1.0.4

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.
Files changed (163) hide show
  1. package/package.json +6 -3
  2. package/dist/cluster/config/cluster.config.d.ts +0 -8
  3. package/dist/cluster/config/cluster.config.js +0 -8
  4. package/dist/cluster/config/cluster.config.js.map +0 -1
  5. package/dist/cluster/index.d.ts +0 -2
  6. package/dist/cluster/index.js +0 -19
  7. package/dist/cluster/index.js.map +0 -1
  8. package/dist/cluster/modules/cluster.module.d.ts +0 -2
  9. package/dist/cluster/modules/cluster.module.js +0 -22
  10. package/dist/cluster/modules/cluster.module.js.map +0 -1
  11. package/dist/cluster/services/cluster.service.d.ts +0 -7
  12. package/dist/cluster/services/cluster.service.js +0 -63
  13. package/dist/cluster/services/cluster.service.js.map +0 -1
  14. package/dist/database/index.d.ts +0 -2
  15. package/dist/database/index.js +0 -19
  16. package/dist/database/index.js.map +0 -1
  17. package/dist/database/mongodb/index.d.ts +0 -2
  18. package/dist/database/mongodb/index.js +0 -19
  19. package/dist/database/mongodb/index.js.map +0 -1
  20. package/dist/database/mongodb/mongodb.module.d.ts +0 -2
  21. package/dist/database/mongodb/mongodb.module.js +0 -50
  22. package/dist/database/mongodb/mongodb.module.js.map +0 -1
  23. package/dist/database/mongodb/mongodb.service.d.ts +0 -29
  24. package/dist/database/mongodb/mongodb.service.js +0 -114
  25. package/dist/database/mongodb/mongodb.service.js.map +0 -1
  26. package/dist/database/redis/index.d.ts +0 -2
  27. package/dist/database/redis/index.js +0 -19
  28. package/dist/database/redis/index.js.map +0 -1
  29. package/dist/database/redis/redis.module.d.ts +0 -2
  30. package/dist/database/redis/redis.module.js +0 -52
  31. package/dist/database/redis/redis.module.js.map +0 -1
  32. package/dist/database/redis/redis.service.d.ts +0 -26
  33. package/dist/database/redis/redis.service.js +0 -126
  34. package/dist/database/redis/redis.service.js.map +0 -1
  35. package/dist/exception/exception.filter.d.ts +0 -4
  36. package/dist/exception/exception.filter.js +0 -35
  37. package/dist/exception/exception.filter.js.map +0 -1
  38. package/dist/generic-repository/index.d.ts +0 -4
  39. package/dist/generic-repository/index.js +0 -21
  40. package/dist/generic-repository/index.js.map +0 -1
  41. package/dist/generic-repository/repositories/base.repository.d.ts +0 -39
  42. package/dist/generic-repository/repositories/base.repository.interface.d.ts +0 -36
  43. package/dist/generic-repository/repositories/base.repository.interface.js +0 -3
  44. package/dist/generic-repository/repositories/base.repository.interface.js.map +0 -1
  45. package/dist/generic-repository/repositories/base.repository.js +0 -96
  46. package/dist/generic-repository/repositories/base.repository.js.map +0 -1
  47. package/dist/generic-repository/services/base.service.d.ts +0 -42
  48. package/dist/generic-repository/services/base.service.interface.d.ts +0 -36
  49. package/dist/generic-repository/services/base.service.interface.js +0 -3
  50. package/dist/generic-repository/services/base.service.interface.js.map +0 -1
  51. package/dist/generic-repository/services/base.service.js +0 -57
  52. package/dist/generic-repository/services/base.service.js.map +0 -1
  53. package/dist/index.d.ts +0 -8
  54. package/dist/index.js +0 -25
  55. package/dist/index.js.map +0 -1
  56. package/dist/logger/custom.logger.d.ts +0 -14
  57. package/dist/logger/custom.logger.js +0 -76
  58. package/dist/logger/custom.logger.js.map +0 -1
  59. package/dist/logger/index.d.ts +0 -2
  60. package/dist/logger/index.js +0 -19
  61. package/dist/logger/index.js.map +0 -1
  62. package/dist/logger/logger.module.d.ts +0 -2
  63. package/dist/logger/logger.module.js +0 -27
  64. package/dist/logger/logger.module.js.map +0 -1
  65. package/dist/pipes/custom-validation.pipe.d.ts +0 -7
  66. package/dist/pipes/custom-validation.pipe.js +0 -69
  67. package/dist/pipes/custom-validation.pipe.js.map +0 -1
  68. package/dist/response/response.interceptor.d.ts +0 -5
  69. package/dist/response/response.interceptor.js +0 -29
  70. package/dist/response/response.interceptor.js.map +0 -1
  71. package/dist/schemas/base.schema.d.ts +0 -11
  72. package/dist/schemas/base.schema.js +0 -29
  73. package/dist/schemas/base.schema.js.map +0 -1
  74. package/dist/schemas/event/event.interfaces.d.ts +0 -69
  75. package/dist/schemas/event/event.interfaces.js +0 -9
  76. package/dist/schemas/event/event.interfaces.js.map +0 -1
  77. package/dist/schemas/event/event.schema.d.ts +0 -70
  78. package/dist/schemas/event/event.schema.js +0 -242
  79. package/dist/schemas/event/event.schema.js.map +0 -1
  80. package/dist/schemas/index.d.ts +0 -14
  81. package/dist/schemas/index.js +0 -31
  82. package/dist/schemas/index.js.map +0 -1
  83. package/dist/schemas/order/order.interfaces.d.ts +0 -72
  84. package/dist/schemas/order/order.interfaces.js +0 -31
  85. package/dist/schemas/order/order.interfaces.js.map +0 -1
  86. package/dist/schemas/order/order.schema.d.ts +0 -39
  87. package/dist/schemas/order/order.schema.js +0 -162
  88. package/dist/schemas/order/order.schema.js.map +0 -1
  89. package/dist/schemas/promotions/promotions.interfaces.d.ts +0 -30
  90. package/dist/schemas/promotions/promotions.interfaces.js +0 -14
  91. package/dist/schemas/promotions/promotions.interfaces.js.map +0 -1
  92. package/dist/schemas/promotions/promotions.schema.d.ts +0 -25
  93. package/dist/schemas/promotions/promotions.schema.js +0 -92
  94. package/dist/schemas/promotions/promotions.schema.js.map +0 -1
  95. package/dist/schemas/rows/rows.interfaces.d.ts +0 -14
  96. package/dist/schemas/rows/rows.interfaces.js +0 -3
  97. package/dist/schemas/rows/rows.interfaces.js.map +0 -1
  98. package/dist/schemas/rows/rows.schema.d.ts +0 -22
  99. package/dist/schemas/rows/rows.schema.js +0 -65
  100. package/dist/schemas/rows/rows.schema.js.map +0 -1
  101. package/dist/schemas/schema.helper.d.ts +0 -4
  102. package/dist/schemas/schema.helper.js +0 -84
  103. package/dist/schemas/schema.helper.js.map +0 -1
  104. package/dist/schemas/ticket-classes/ticket-classes.interfaces.d.ts +0 -18
  105. package/dist/schemas/ticket-classes/ticket-classes.interfaces.js +0 -3
  106. package/dist/schemas/ticket-classes/ticket-classes.interfaces.js.map +0 -1
  107. package/dist/schemas/ticket-classes/ticket-classes.schemas.d.ts +0 -26
  108. package/dist/schemas/ticket-classes/ticket-classes.schemas.js +0 -75
  109. package/dist/schemas/ticket-classes/ticket-classes.schemas.js.map +0 -1
  110. package/dist/schemas/ticket-summary/ticket-summary.interfaces.d.ts +0 -19
  111. package/dist/schemas/ticket-summary/ticket-summary.interfaces.js +0 -9
  112. package/dist/schemas/ticket-summary/ticket-summary.interfaces.js.map +0 -1
  113. package/dist/schemas/ticket-summary/ticket-summary.schema.d.ts +0 -23
  114. package/dist/schemas/ticket-summary/ticket-summary.schema.js +0 -65
  115. package/dist/schemas/ticket-summary/ticket-summary.schema.js.map +0 -1
  116. package/dist/schemas/user-fanpass/user-fanpass.interfaces.d.ts +0 -26
  117. package/dist/schemas/user-fanpass/user-fanpass.interfaces.js +0 -9
  118. package/dist/schemas/user-fanpass/user-fanpass.interfaces.js.map +0 -1
  119. package/dist/schemas/user-fanpass/user-fanpass.schemas.d.ts +0 -29
  120. package/dist/schemas/user-fanpass/user-fanpass.schemas.js +0 -113
  121. package/dist/schemas/user-fanpass/user-fanpass.schemas.js.map +0 -1
  122. package/dist/tsconfig.tsbuildinfo +0 -1
  123. package/src/cluster/config/cluster.config.ts +0 -6
  124. package/src/cluster/index.ts +0 -2
  125. package/src/cluster/modules/cluster.module.ts +0 -9
  126. package/src/cluster/services/cluster.service.ts +0 -76
  127. package/src/database/index.ts +0 -2
  128. package/src/database/mongodb/index.ts +0 -2
  129. package/src/database/mongodb/mongodb.module.ts +0 -42
  130. package/src/database/mongodb/mongodb.service.ts +0 -111
  131. package/src/database/redis/index.ts +0 -2
  132. package/src/database/redis/redis.module.ts +0 -45
  133. package/src/database/redis/redis.service.ts +0 -142
  134. package/src/exception/exception.filter.ts +0 -36
  135. package/src/generic-repository/index.ts +0 -7
  136. package/src/generic-repository/repositories/base.repository.interface.ts +0 -78
  137. package/src/generic-repository/repositories/base.repository.ts +0 -199
  138. package/src/generic-repository/services/base.service.interface.ts +0 -78
  139. package/src/generic-repository/services/base.service.ts +0 -165
  140. package/src/index.ts +0 -13
  141. package/src/logger/custom.logger.ts +0 -83
  142. package/src/logger/index.ts +0 -2
  143. package/src/logger/logger.module.ts +0 -14
  144. package/src/pipes/custom-validation.pipe.ts +0 -61
  145. package/src/response/response.interceptor.ts +0 -26
  146. package/src/schemas/base.schema.ts +0 -19
  147. package/src/schemas/event/event.interfaces.ts +0 -74
  148. package/src/schemas/event/event.schema.ts +0 -195
  149. package/src/schemas/index.ts +0 -16
  150. package/src/schemas/order/order.interfaces.ts +0 -78
  151. package/src/schemas/order/order.schema.ts +0 -120
  152. package/src/schemas/promotions/promotions.interfaces.ts +0 -34
  153. package/src/schemas/promotions/promotions.schema.ts +0 -71
  154. package/src/schemas/rows/rows.interfaces.ts +0 -14
  155. package/src/schemas/rows/rows.schema.ts +0 -47
  156. package/src/schemas/schema.helper.ts +0 -103
  157. package/src/schemas/ticket-classes/ticket-classes.interfaces.ts +0 -18
  158. package/src/schemas/ticket-classes/ticket-classes.schemas.ts +0 -51
  159. package/src/schemas/ticket-summary/ticket-summary.interfaces.ts +0 -20
  160. package/src/schemas/ticket-summary/ticket-summary.schema.ts +0 -46
  161. package/src/schemas/user-fanpass/user-fanpass.interfaces.ts +0 -28
  162. package/src/schemas/user-fanpass/user-fanpass.schemas.ts +0 -86
  163. 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
- }
@@ -1,2 +0,0 @@
1
- export * from './logger.module';
2
- export * from './custom.logger';
@@ -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
- }