@mondart/nestjs-common-module 2.6.0 → 2.6.6

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.
@@ -9,18 +9,23 @@ exports.IamContext = (0, common_1.createParamDecorator)((data, ctx) => {
9
9
  return (0, exports.getIamContext)(request);
10
10
  });
11
11
  const getIamContext = (request) => {
12
- const injectedPayload = request?.headers?.['injectedpayload'];
13
- const ip = request?.headers['x-forwarded-for'] ||
12
+ const headers = typeof request?.headers === 'string'
13
+ ? JSON.parse(request?.headers)
14
+ : request?.headers;
15
+ const injectedPayload = headers?.['injectedpayload'];
16
+ const ip = headers?.['x-forwarded-for'] ||
17
+ headers?.['host'] ||
18
+ headers?.address ||
14
19
  request?.socket?.remoteAddress ||
15
20
  null;
16
- const requestId = request?.headers['request-id'] ?? (0, crypto_1.randomUUID)();
17
- const timestamp = new Date(request?.headers?.['timestamp']);
21
+ const requestId = headers?.['request-id'] ?? (0, crypto_1.randomUUID)();
22
+ const timestamp = new Date(headers?.['timestamp'] || headers?.['time'] || Date.now());
18
23
  let isAgent = false;
19
24
  let user;
20
25
  let agent;
21
26
  if (injectedPayload) {
22
27
  const data = JSON.parse(injectedPayload);
23
- const token = request?.headers?.['authorization'];
28
+ const token = headers?.['authorization'];
24
29
  if (data) {
25
30
  if (!data?.is_agent && (data?.contact_id || data?.store_id)) {
26
31
  user = {
@@ -40,7 +45,7 @@ const getIamContext = (request) => {
40
45
  };
41
46
  }
42
47
  }
43
- delete request?.headers?.['authorization'];
48
+ delete headers?.['authorization'];
44
49
  }
45
50
  return new iam_context_dto_1.IamContextDto({
46
51
  ip,
@@ -29,9 +29,18 @@ let GlobalExceptionFilter = class GlobalExceptionFilter {
29
29
  Sentry.captureException(exception, exception?.response);
30
30
  const context = host.switchToHttp();
31
31
  const type = host?.getType();
32
- this.logger.error(`exceptionName: ${exception?.name}`, JSON.stringify(exception) ??
33
- exception?.message ??
34
- 'Unknown exception occurred.');
32
+ const message = typeof exception?.message === 'string'
33
+ ? exception?.message !== ''
34
+ ? exception?.message
35
+ : 'Unknown exception occurred.'
36
+ : (typeof exception?.message === 'object' &&
37
+ Object.keys(exception?.message)?.length !== 0) ||
38
+ (Array.isArray(exception?.message) &&
39
+ exception?.message?.length !== 0)
40
+ ? JSON.stringify(exception?.message)
41
+ : 'Unknown exception occurred.';
42
+ this.logger.error(`exceptionName: ${exception?.name} - ${message}`);
43
+ this.logger.debug(exception?.stack);
35
44
  if (type === 'rpc') {
36
45
  const exceptionsName = Object.values(exceptions).map((exception) => exception.name);
37
46
  if (exceptionsName.includes(exception.name)) {
@@ -33,15 +33,18 @@ let CaptchaGuard = class CaptchaGuard {
33
33
  const captchaSettings = this.getCaptchaDetails(context);
34
34
  const request = context.switchToHttp().getRequest();
35
35
  const iamContext = (0, decorators_1.getIamContext)(request);
36
- if (iamContext?.agentId || iamContext?.userId)
36
+ if (iamContext?.agentId || iamContext?.userId || !captchaSettings)
37
37
  return true;
38
38
  if (captchaSettings && captchaSettings.executeAfter) {
39
- const ip = request.headers['x-forwarded-for'] || request.socket.remoteAddress;
39
+ const ip = iamContext?.ip || null;
40
40
  const result = await this.incExecutionTimes(ip, captchaSettings.executeAfter);
41
41
  if (result)
42
42
  return true;
43
43
  }
44
- const text = request.headers['captcha-token'];
44
+ const headers = typeof request?.headers === 'string'
45
+ ? JSON.parse(request?.headers)
46
+ : request?.headers;
47
+ const text = headers?.['captcha-token'];
45
48
  if (!text)
46
49
  throw new captcha_exception_1.InvalidCaptcha();
47
50
  await this.isCaptchaValid(text);
@@ -11,39 +11,42 @@ class CoreCrudController {
11
11
  CoreCrudController.responseDto = responseDto;
12
12
  }
13
13
  async findAllWithPagination(query, paginateConfig, options, ...args) {
14
- const response = await this.coreService.findAllWithPagination(query, paginateConfig, options);
14
+ const response = await this.coreService.findAllWithPagination(query, paginateConfig, options, ...args);
15
15
  if (!response)
16
16
  throw new common_1.NotFoundException(enums_1.SharedMessages.FETCH_FAILED);
17
17
  const serializedData = response.data.map((item) => new this.responseDto(item));
18
- return new dto_1.SuccessResponse(serializedData, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK, { ...response.meta, links: response.links });
18
+ return new dto_1.SuccessResponse(serializedData, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK, {
19
+ ...response.meta,
20
+ links: response.links,
21
+ });
19
22
  }
20
23
  async findOneById(id, options, ...args) {
21
- const response = await this.coreService.findOneById(id, options);
24
+ const response = await this.coreService.findOneById(id, options, ...args);
22
25
  if (!response)
23
26
  throw new common_1.NotFoundException(enums_1.SharedMessages.RESOURCE_NOT_FOUND);
24
27
  return new dto_1.SuccessResponse(new this.responseDto(response), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
25
28
  }
26
29
  async create(createDto, options, ...args) {
27
- const response = await this.coreService.create(createDto, options);
30
+ const response = await this.coreService.create(createDto, options, ...args);
28
31
  if (!response)
29
32
  throw new common_1.BadRequestException(enums_1.SharedMessages.CREATE_FAILED);
30
33
  return new dto_1.SuccessResponse(new this.responseDto(response), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.CREATED);
31
34
  }
32
35
  async update({ id }, updateDto, options, ...args) {
33
- const response = await this.coreService.update(id, updateDto, options);
36
+ const response = await this.coreService.update(id, updateDto, options, ...args);
34
37
  if (!response)
35
38
  throw new common_1.BadRequestException(enums_1.SharedMessages.UPDATE_FAILED);
36
39
  const foundItem = await this.coreService.findOneById(id, options);
37
40
  return new dto_1.SuccessResponse(new this.responseDto(foundItem), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
38
41
  }
39
42
  async deleteById({ id }, options, ...args) {
40
- const result = await this.coreService.deleteById(id, options);
43
+ const result = await this.coreService.deleteById(id, options, ...args);
41
44
  if (!result)
42
45
  throw new common_1.BadRequestException(enums_1.SharedMessages.DELETE_FAILED);
43
46
  return new dto_1.SuccessResponse(undefined, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
44
47
  }
45
48
  async softDeleteById({ id }, options, ...args) {
46
- const result = await this.coreService.softDeleteById(id, options);
49
+ const result = await this.coreService.softDeleteById(id, options, ...args);
47
50
  if (!result)
48
51
  throw new common_1.BadRequestException(enums_1.SharedMessages.DELETE_FAILED);
49
52
  return new dto_1.SuccessResponse(undefined, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
@@ -11,22 +11,22 @@ export declare abstract class CoreCrudService<T extends BaseModelEntity, CreateD
11
11
  protected readonly repository: Repository<T>;
12
12
  private readonly relationsPath;
13
13
  protected constructor(repository: Repository<T>);
14
- create(createDto: CreateDto, options?: CoreBaseServiceOption): Promise<T>;
15
- update(id: number, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>): Promise<UpdateResult>;
16
- updateMany(query: FindManyOptions<T>, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>): Promise<void>;
17
- upsert(upsertDto: QueryDeepPartialEntity<T>, upsertOptions: UpsertOptions<T>, options?: CoreBaseServiceOption): Promise<InsertResult>;
18
- findAllWithPagination(query: PaginationQueryCustom, paginateConfig: PaginateConfig<T>, options?: CoreFindAllWithPaginationServiceOption<T>): Promise<Paginated<T>>;
19
- findAll(query: FindManyOptions<T>, options?: CoreFindAllServiceOption): Promise<T[]>;
20
- findOneById(id: number, options?: CoreFindOneByIdServiceOption<T>): Promise<T>;
21
- findOne(query: FindManyOptions<T>, options?: CoreFindOneServiceOption): Promise<T>;
22
- isExists(query: FindManyOptions<T>, options?: CoreBaseServiceOption): Promise<boolean>;
23
- count(query: FindManyOptions<T>, options?: CoreBaseServiceOption): Promise<number>;
24
- softDelete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<UpdateResult>;
25
- softDeleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<UpdateResult>;
26
- restore(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<UpdateResult>;
27
- restoreById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<UpdateResult>;
28
- delete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<DeleteResult>;
29
- deleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<DeleteResult>;
14
+ create(createDto: CreateDto, options?: CoreBaseServiceOption, ...args: any[]): Promise<T>;
15
+ update(id: number, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>, ...args: any[]): Promise<UpdateResult>;
16
+ updateMany(query: FindManyOptions<T>, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>, ...args: any[]): Promise<void>;
17
+ upsert(upsertDto: QueryDeepPartialEntity<T>, upsertOptions: UpsertOptions<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<InsertResult>;
18
+ findAllWithPagination(query: PaginationQueryCustom, paginateConfig: PaginateConfig<T>, options?: CoreFindAllWithPaginationServiceOption<T>, ...args: any[]): Promise<Paginated<T>>;
19
+ findAll(query: FindManyOptions<T>, options?: CoreFindAllServiceOption, ...args: any[]): Promise<T[]>;
20
+ findOneById(id: number, options?: CoreFindOneByIdServiceOption<T>, ...args: any[]): Promise<T>;
21
+ findOne(query: FindManyOptions<T>, options?: CoreFindOneServiceOption, ...args: any[]): Promise<T>;
22
+ isExists(query: FindManyOptions<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<boolean>;
23
+ count(query: FindManyOptions<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<number>;
24
+ softDelete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<UpdateResult>;
25
+ softDeleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption, ...args: any[]): Promise<UpdateResult>;
26
+ restore(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<UpdateResult>;
27
+ restoreById(id: string | string[] | number | number[], options?: CoreBaseServiceOption, ...args: any[]): Promise<UpdateResult>;
28
+ delete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption, ...args: any[]): Promise<DeleteResult>;
29
+ deleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption, ...args: any[]): Promise<DeleteResult>;
30
30
  private relatedPropertyTransformer;
31
31
  private whereQueryTransformer;
32
32
  private isObject;
@@ -12,7 +12,7 @@ class CoreCrudService {
12
12
  const metadata = this.repository.metadata;
13
13
  this.relationsPath = metadata.relations.map((relation) => relation.propertyPath);
14
14
  }
15
- async create(createDto, options) {
15
+ async create(createDto, options, ...args) {
16
16
  createDto = this.relatedPropertyTransformer(createDto);
17
17
  const entity = this.repository.create(createDto);
18
18
  if (options?.entityManager) {
@@ -22,7 +22,7 @@ class CoreCrudService {
22
22
  return await this.repository.save(entity);
23
23
  }
24
24
  }
25
- async update(id, updateDto, options) {
25
+ async update(id, updateDto, options, ...args) {
26
26
  updateDto = this.relatedPropertyTransformer(updateDto);
27
27
  if (!options?.entityManager) {
28
28
  const selectFields = Object.keys(updateDto);
@@ -67,13 +67,13 @@ class CoreCrudService {
67
67
  };
68
68
  }
69
69
  }
70
- async updateMany(query, updateDto, options) {
70
+ async updateMany(query, updateDto, options, ...args) {
71
71
  const foundEntities = await this.findAll(query, options);
72
72
  for await (const entity of foundEntities) {
73
73
  await this.update(entity.id, updateDto, options).catch((err) => console.log(err));
74
74
  }
75
75
  }
76
- async upsert(upsertDto, upsertOptions, options) {
76
+ async upsert(upsertDto, upsertOptions, options, ...args) {
77
77
  try {
78
78
  upsertDto = this.relatedPropertyTransformer(upsertDto);
79
79
  let conflictPaths = [];
@@ -110,7 +110,7 @@ class CoreCrudService {
110
110
  throw new common_1.BadRequestException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.UPSERT_FAILED, error));
111
111
  }
112
112
  }
113
- async findAllWithPagination(query, paginateConfig, options) {
113
+ async findAllWithPagination(query, paginateConfig, options, ...args) {
114
114
  return await (0, nestjs_paginate_1.paginate)(query, options?.selectQueryBuilder
115
115
  ? options.selectQueryBuilder
116
116
  : this.repository, {
@@ -122,7 +122,7 @@ class CoreCrudService {
122
122
  : undefined,
123
123
  });
124
124
  }
125
- async findAll(query, options) {
125
+ async findAll(query, options, ...args) {
126
126
  let result;
127
127
  query.where = this.whereQueryTransformer(query.where) ?? undefined;
128
128
  if (!options?.entityManager)
@@ -144,7 +144,7 @@ class CoreCrudService {
144
144
  }
145
145
  return result;
146
146
  }
147
- async findOneById(id, options) {
147
+ async findOneById(id, options, ...args) {
148
148
  let result;
149
149
  if (options?.entityManager) {
150
150
  result = await options.entityManager.findOne(this.repository.target, {
@@ -175,7 +175,7 @@ class CoreCrudService {
175
175
  }
176
176
  return result;
177
177
  }
178
- async findOne(query, options) {
178
+ async findOne(query, options, ...args) {
179
179
  let result;
180
180
  query.where = this.whereQueryTransformer(query.where) ?? undefined;
181
181
  if (options?.entityManager) {
@@ -199,7 +199,7 @@ class CoreCrudService {
199
199
  }
200
200
  return result;
201
201
  }
202
- async isExists(query, options) {
202
+ async isExists(query, options, ...args) {
203
203
  let result;
204
204
  query.where = this.whereQueryTransformer(query.where) ?? undefined;
205
205
  if (options?.entityManager) {
@@ -210,7 +210,7 @@ class CoreCrudService {
210
210
  }
211
211
  return result;
212
212
  }
213
- async count(query, options) {
213
+ async count(query, options, ...args) {
214
214
  let result;
215
215
  query.where = this.whereQueryTransformer(query.where) ?? undefined;
216
216
  if (options?.entityManager) {
@@ -221,7 +221,7 @@ class CoreCrudService {
221
221
  }
222
222
  return result;
223
223
  }
224
- async softDelete(query, options) {
224
+ async softDelete(query, options, ...args) {
225
225
  const formatedQuery = this.whereQueryTransformer(query) ?? undefined;
226
226
  if (options?.entityManager) {
227
227
  return await options?.entityManager.softDelete(this.repository.target, formatedQuery);
@@ -230,7 +230,7 @@ class CoreCrudService {
230
230
  return await this.repository.softDelete(formatedQuery);
231
231
  }
232
232
  }
233
- async softDeleteById(id, options) {
233
+ async softDeleteById(id, options, ...args) {
234
234
  if (options?.entityManager) {
235
235
  return await options?.entityManager.softDelete(this.repository.target, id);
236
236
  }
@@ -238,7 +238,7 @@ class CoreCrudService {
238
238
  return await this.repository.softDelete(id);
239
239
  }
240
240
  }
241
- async restore(query, options) {
241
+ async restore(query, options, ...args) {
242
242
  const formatedQuery = this.whereQueryTransformer(query) ?? undefined;
243
243
  if (options?.entityManager) {
244
244
  return await options?.entityManager.restore(this.repository.target, formatedQuery);
@@ -247,7 +247,7 @@ class CoreCrudService {
247
247
  return await this.repository.restore(formatedQuery);
248
248
  }
249
249
  }
250
- async restoreById(id, options) {
250
+ async restoreById(id, options, ...args) {
251
251
  if (options?.entityManager) {
252
252
  return await options?.entityManager.restore(this.repository.target, id);
253
253
  }
@@ -255,7 +255,7 @@ class CoreCrudService {
255
255
  return await this.repository.restore(id);
256
256
  }
257
257
  }
258
- async delete(query, options) {
258
+ async delete(query, options, ...args) {
259
259
  const formatedQuery = this.whereQueryTransformer(query) ?? undefined;
260
260
  if (options?.entityManager) {
261
261
  return await options?.entityManager.delete(this.repository.target, formatedQuery);
@@ -264,7 +264,7 @@ class CoreCrudService {
264
264
  return await this.repository.delete(formatedQuery);
265
265
  }
266
266
  }
267
- async deleteById(id, options) {
267
+ async deleteById(id, options, ...args) {
268
268
  if (options?.entityManager) {
269
269
  return await options?.entityManager.delete(this.repository.target, id);
270
270
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SagaInvocationException = void 0;
4
4
  class SagaInvocationException extends Error {
5
5
  constructor(originalError, step) {
6
- super(originalError.message);
6
+ super(`${step}: ${originalError.message}`);
7
7
  this.originalError = originalError;
8
8
  this.step = step;
9
9
  Object.setPrototypeOf(this, SagaInvocationException.prototype);
@@ -4,3 +4,4 @@ export { SagaStatus } from './enums';
4
4
  export { SagaBuilder } from './services';
5
5
  export { SagaModule } from './saga.module';
6
6
  export { Saga } from './decorators';
7
+ export { SagaStep } from './types';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Saga = exports.SagaModule = exports.SagaBuilder = exports.SagaStatus = exports.SagaInvocationException = exports.SagaCompensationException = void 0;
3
+ exports.SagaStep = exports.Saga = exports.SagaModule = exports.SagaBuilder = exports.SagaStatus = exports.SagaInvocationException = exports.SagaCompensationException = void 0;
4
4
  var exceptions_1 = require("./exceptions");
5
5
  Object.defineProperty(exports, "SagaCompensationException", { enumerable: true, get: function () { return exceptions_1.SagaCompensationException; } });
6
6
  Object.defineProperty(exports, "SagaInvocationException", { enumerable: true, get: function () { return exceptions_1.SagaInvocationException; } });
@@ -12,3 +12,5 @@ var saga_module_1 = require("./saga.module");
12
12
  Object.defineProperty(exports, "SagaModule", { enumerable: true, get: function () { return saga_module_1.SagaModule; } });
13
13
  var decorators_1 = require("./decorators");
14
14
  Object.defineProperty(exports, "Saga", { enumerable: true, get: function () { return decorators_1.Saga; } });
15
+ var types_1 = require("./types");
16
+ Object.defineProperty(exports, "SagaStep", { enumerable: true, get: function () { return types_1.SagaStep; } });