@solidstarters/solid-core 1.2.151 → 1.2.153

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 (36) hide show
  1. package/dist/dtos/create-scheduled-job.dto.d.ts +1 -0
  2. package/dist/dtos/create-scheduled-job.dto.d.ts.map +1 -1
  3. package/dist/dtos/create-scheduled-job.dto.js +10 -2
  4. package/dist/dtos/create-scheduled-job.dto.js.map +1 -1
  5. package/dist/dtos/update-scheduled-job.dto.d.ts +1 -0
  6. package/dist/dtos/update-scheduled-job.dto.d.ts.map +1 -1
  7. package/dist/dtos/update-scheduled-job.dto.js +10 -2
  8. package/dist/dtos/update-scheduled-job.dto.js.map +1 -1
  9. package/dist/entities/scheduled-job.entity.d.ts.map +1 -1
  10. package/dist/entities/scheduled-job.entity.js +1 -0
  11. package/dist/entities/scheduled-job.entity.js.map +1 -1
  12. package/dist/repository/scheduled-job.repository.d.ts +1 -15
  13. package/dist/repository/scheduled-job.repository.d.ts.map +1 -1
  14. package/dist/repository/scheduled-job.repository.js +28 -20
  15. package/dist/repository/scheduled-job.repository.js.map +1 -1
  16. package/dist/seeders/module-metadata-seeder.service.d.ts +2 -1
  17. package/dist/seeders/module-metadata-seeder.service.d.ts.map +1 -1
  18. package/dist/seeders/module-metadata-seeder.service.js.map +1 -1
  19. package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.d.ts +2 -1
  20. package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.d.ts.map +1 -1
  21. package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.js +13 -5
  22. package/dist/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.js.map +1 -1
  23. package/dist/subscribers/scheduled-job.subscriber.d.ts +1 -1
  24. package/dist/subscribers/scheduled-job.subscriber.d.ts.map +1 -1
  25. package/dist/subscribers/scheduled-job.subscriber.js +5 -4
  26. package/dist/subscribers/scheduled-job.subscriber.js.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +1 -1
  29. package/rebuild.sh +1 -1
  30. package/src/dtos/create-scheduled-job.dto.ts +7 -13
  31. package/src/dtos/update-scheduled-job.dto.ts +7 -14
  32. package/src/entities/scheduled-job.entity.ts +1 -0
  33. package/src/repository/scheduled-job.repository.ts +46 -35
  34. package/src/seeders/module-metadata-seeder.service.ts +2 -1
  35. package/src/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.ts +34 -16
  36. package/src/subscribers/scheduled-job.subscriber.ts +9 -9
@@ -17,6 +17,7 @@ const common_1 = require("@nestjs/common");
17
17
  const typeorm_1 = require("@nestjs/typeorm");
18
18
  const computed_field_provider_decorator_1 = require("../../../decorators/computed-field-provider.decorator");
19
19
  const typeorm_2 = require("typeorm");
20
+ const lodash_1 = require("lodash");
20
21
  let AlphaNumExternalIdComputationProvider = class AlphaNumExternalIdComputationProvider {
21
22
  constructor(entityManager) {
22
23
  this.entityManager = entityManager;
@@ -25,13 +26,20 @@ let AlphaNumExternalIdComputationProvider = class AlphaNumExternalIdComputationP
25
26
  return this.constructor.name;
26
27
  }
27
28
  help() {
28
- return 'Provider used to compute external ID for a CommonEntity.';
29
+ return 'Provider used to compute external ID for a CommonEntity with support for static or dynamic prefix.';
29
30
  }
30
31
  async preComputeValue(triggerEntity, computedFieldMetadata) {
31
- const prefix = computedFieldMetadata.computedFieldValueProviderCtxt.prefix;
32
- const codeLength = computedFieldMetadata.computedFieldValueProviderCtxt.length || 5;
32
+ const { prefix, length, dynamicFieldPrefix } = computedFieldMetadata.computedFieldValueProviderCtxt;
33
+ const codeLength = length || 5;
34
+ let resolvedPrefix = prefix || '';
35
+ if (dynamicFieldPrefix) {
36
+ const dynamicValue = (0, lodash_1.get)(triggerEntity, dynamicFieldPrefix);
37
+ if (dynamicValue) {
38
+ resolvedPrefix = String(dynamicValue).trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9\-]/g, '');
39
+ }
40
+ }
33
41
  const uniqueCode = await this.generateUniqueExternalId(codeLength, triggerEntity, computedFieldMetadata.fieldName);
34
- triggerEntity[computedFieldMetadata.fieldName] = `${prefix}-${uniqueCode}`;
42
+ triggerEntity[computedFieldMetadata.fieldName] = resolvedPrefix ? `${resolvedPrefix}-${uniqueCode}` : uniqueCode;
35
43
  }
36
44
  generateRandomCode(length = 5) {
37
45
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
@@ -42,7 +50,7 @@ let AlphaNumExternalIdComputationProvider = class AlphaNumExternalIdComputationP
42
50
  return result;
43
51
  }
44
52
  async isExternalIdUnique(externalId, triggerEntity, fieldName) {
45
- const count = await this.entityManager.count(triggerEntity.constructor.name, {
53
+ const count = await this.entityManager.count(triggerEntity.constructor, {
46
54
  where: { [fieldName]: externalId },
47
55
  });
48
56
  return count === 0;
@@ -1 +1 @@
1
- {"version":3,"file":"alpha-num-external-id-computed-field-provider.js","sourceRoot":"","sources":["../../../../src/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,6CAAsD;AACtD,6GAAyF;AAIzF,qCAAwC;AASjC,IAAM,qCAAqC,GAA3C,MAAM,qCAAqC;IAChD,YAEmB,aAA4B;QAA5B,kBAAa,GAAb,aAAa,CAAe;IAC3C,CAAC;IAEL,IAAI;QACF,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC/B,CAAC;IAED,IAAI;QACF,OAAO,0DAA0D,CAAC;IACpE,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,aAAgB,EAAE,qBAAuE;QAC7G,MAAM,MAAM,GAAG,qBAAqB,CAAC,8BAA8B,CAAC,MAAM,CAAC;QAC3E,MAAM,UAAU,GAAG,qBAAqB,CAAC,8BAA8B,CAAC,MAAM,IAAI,CAAC,CAAC;QACpF,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,wBAAwB,CAAC,UAAU,EAAE,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AAK,CAAC;YACf,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;IACrF,CAAC;CACF,CAAA;AApDY,sFAAqC;gDAArC,qCAAqC;IAFjD,IAAA,yDAAqB,GAAE;IACvB,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,6BAAmB,GAAE,CAAA;qCACU,uBAAa;GAHpC,qCAAqC,CAoDjD","sourcesContent":["import { Injectable } from '@nestjs/common';\nimport { InjectEntityManager } from '@nestjs/typeorm';\nimport { ComputedFieldProvider } from 'src/decorators/computed-field-provider.decorator';\nimport { CommonEntity } from 'src/entities/common.entity';\nimport { ComputedFieldMetadata } from 'src/helpers/solid-registry';\nimport { IEntityPreComputeFieldProvider } from 'src/interfaces';\nimport { EntityManager } from 'typeorm';\n\nexport interface AlphaNumExternalIdContext {\n prefix: string; // The prefix to use for the external ID\n length?: number; // Optional: length of the unique code to generate, default is 5\n}\n\n@ComputedFieldProvider()\n@Injectable()\nexport class AlphaNumExternalIdComputationProvider<T extends CommonEntity> implements IEntityPreComputeFieldProvider<T, AlphaNumExternalIdContext> {\n constructor(\n @InjectEntityManager()\n private readonly entityManager: EntityManager\n ) { }\n\n name(): string {\n return this.constructor.name;\n }\n\n help(): string {\n return 'Provider used to compute external ID for a CommonEntity.';\n }\n\n async preComputeValue(triggerEntity: T, computedFieldMetadata: ComputedFieldMetadata<AlphaNumExternalIdContext>) {\n const prefix = computedFieldMetadata.computedFieldValueProviderCtxt.prefix;\n const codeLength = computedFieldMetadata.computedFieldValueProviderCtxt.length || 5;\n const uniqueCode = await this.generateUniqueExternalId(codeLength, triggerEntity, computedFieldMetadata.fieldName);\n triggerEntity[computedFieldMetadata.fieldName] = `${prefix}-${uniqueCode}`;\n }\n\n private generateRandomCode(length = 5): string {\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\n let result = '';\n for (let i = 0; i < length; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length));\n }\n return result;\n }\n\n private async isExternalIdUnique(externalId: string, triggerEntity: T, fieldName: string): Promise<boolean> {\n const count = await this.entityManager.count(triggerEntity.constructor.name, {\n where: { [fieldName]: externalId },\n });\n return count === 0;\n }\n\n private async generateUniqueExternalId(codeLength: number, triggerEntity: T, fieldName: string): Promise<string> {\n const maxAttempts = 10;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n const newId = this.generateRandomCode(codeLength);\n\n const isUnique = await this.isExternalIdUnique(newId, triggerEntity, fieldName);\n\n if (isUnique) {\n return newId;\n }\n }\n\n throw new Error('Failed to generate a unique external ID after multiple attempts');\n }\n}"]}
1
+ {"version":3,"file":"alpha-num-external-id-computed-field-provider.js","sourceRoot":"","sources":["../../../../src/services/computed-fields/entity/alpha-num-external-id-computed-field-provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,6CAAsD;AACtD,6GAAyF;AAIzF,qCAAwC;AACxC,mCAA6B;AAStB,IAAM,qCAAqC,GAA3C,MAAM,qCAAqC;IAEhD,YAEmB,aAA4B;QAA5B,kBAAa,GAAb,aAAa,CAAe;IAC3C,CAAC;IAEL,IAAI;QACF,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC/B,CAAC;IAED,IAAI;QACF,OAAO,oGAAoG,CAAC;IAC9G,CAAC;IAED,KAAK,CAAC,eAAe,CAAE,aAAgB,EAAE,qBAAuE;QAE9G,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,GAC1C,qBAAqB,CAAC,8BAA8B,CAAC;QAEvD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,CAAC;QAG/B,IAAI,cAAc,GAAG,MAAM,IAAI,EAAE,CAAC;QAElC,IAAI,kBAAkB,EAAE,CAAC;YACvB,MAAM,YAAY,GAAG,IAAA,YAAG,EAAC,aAAoB,EAAE,kBAAkB,CAAC,CAAC;YACnE,IAAI,YAAY,EAAE,CA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KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,GAAG,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;YACvD,MAAM,KAAK,GAAG,IAAI,CAAC,kBAAkB,CAAC,UAAU,CAAC,CAAC;YAElD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAC,aAAa,EAAC,SAAS,CAAC,CAAC;YAE9E,IAAI,QAAQ,EAAE,CAAC;gBACb,OAAO,KAAK,CAAC;YACf,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;IACrF,CAAC;CACF,CAAA;AArEY,sFAAqC;gDAArC,qCAAqC;IAFjD,IAAA,yDAAqB,GAAE;IACvB,IAAA,mBAAU,GAAE;IAIR,WAAA,IAAA,6BAAmB,GAAE,CAAA;qCACU,uBAAa;GAJpC,qCAAqC,CAqEjD","sourcesContent":["import { Injectable } from '@nestjs/common';\nimport { InjectEntityManager } from '@nestjs/typeorm';\nimport { ComputedFieldProvider } from 'src/decorators/computed-field-provider.decorator';\nimport { CommonEntity } from 'src/entities/common.entity';\nimport { ComputedFieldMetadata } from 'src/helpers/solid-registry';\nimport { IEntityPreComputeFieldProvider } from 'src/interfaces';\nimport { EntityManager } from 'typeorm';\nimport { get } from \"lodash\";\nexport interface AlphaNumExternalIdContext {\n prefix?: string; // alias -> staticPrefix\n length?: number; // Optional: length of the unique code to generate, default is 5\n dynamicFieldPrefix?: string; // NEW: field name on the entity\n}\n\n@ComputedFieldProvider()\n@Injectable()\nexport class AlphaNumExternalIdComputationProvider<T extends CommonEntity> implements IEntityPreComputeFieldProvider<T, AlphaNumExternalIdContext>\n{\n constructor(\n @InjectEntityManager()\n private readonly entityManager: EntityManager\n ) { }\n\n name(): string {\n return this.constructor.name;\n }\n\n help(): string {\n return 'Provider used to compute external ID for a CommonEntity with support for static or dynamic prefix.';\n }\n\n async preComputeValue( triggerEntity: T, computedFieldMetadata: ComputedFieldMetadata<AlphaNumExternalIdContext>\n ) {\n const { prefix, length, dynamicFieldPrefix } =\n computedFieldMetadata.computedFieldValueProviderCtxt;\n\n const codeLength = length || 5;\n\n // Determine prefix\n let resolvedPrefix = prefix || '';\n\n if (dynamicFieldPrefix) {\n const dynamicValue = get(triggerEntity as any, dynamicFieldPrefix);\n if (dynamicValue) {\n resolvedPrefix = String(dynamicValue).trim().toLowerCase().replace(/\\s+/g, '-').replace(/[^a-z0-9\\-]/g, '');\n }\n }\n\n const uniqueCode = await this.generateUniqueExternalId( codeLength, triggerEntity, computedFieldMetadata.fieldName );\n triggerEntity[computedFieldMetadata.fieldName] = resolvedPrefix ? `${resolvedPrefix}-${uniqueCode}` : uniqueCode;\n }\n\n private generateRandomCode(length = 5): string {\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\n let result = '';\n for (let i = 0; i < length; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length));\n }\n return result;\n }\n\n private async isExternalIdUnique( externalId: string, triggerEntity: T, fieldName: string ): Promise<boolean> {\n const count = await this.entityManager.count(triggerEntity.constructor as any,\n {\n where: { [fieldName]: externalId },\n }\n );\n return count === 0;\n }\n\n private async generateUniqueExternalId(codeLength: number, triggerEntity: T, fieldName: string): Promise<string> {\n const maxAttempts = 10;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n const newId = this.generateRandomCode(codeLength);\n\n const isUnique = await this.isExternalIdUnique(newId,triggerEntity,fieldName);\n\n if (isUnique) {\n return newId;\n }\n }\n\n throw new Error('Failed to generate a unique external ID after multiple attempts');\n }\n}\n"]}
@@ -1,7 +1,7 @@
1
- import { DataSource, EntitySubscriberInterface, InsertEvent, UpdateEvent, RemoveEvent } from "typeorm";
2
1
  import { ScheduledJob } from "src/entities/scheduled-job.entity";
3
2
  import { ModuleMetadataHelperService } from "src/helpers/module-metadata-helper.service";
4
3
  import { ScheduledJobRepository } from "src/repository/scheduled-job.repository";
4
+ import { DataSource, EntitySubscriberInterface, InsertEvent, RemoveEvent, UpdateEvent } from "typeorm";
5
5
  export declare class ScheduledJobSubscriber implements EntitySubscriberInterface<ScheduledJob> {
6
6
  private readonly dataSource;
7
7
  private readonly moduleMetadataHelperService;
@@ -1 +1 @@
1
- {"version":3,"file":"scheduled-job.subscriber.d.ts","sourceRoot":"","sources":["../../src/subscribers/scheduled-job.subscriber.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,UAAU,EACV,yBAAyB,EACzB,WAAW,EACX,WAAW,EACX,WAAW,EAEZ,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AACzF,OAAO,EAAyB,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AAIxG,qBACa,sBACX,YAAW,yBAAyB,CAAC,YAAY,CAAC;IAW5B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC/C,OAAO,CAAC,QAAQ,CAAC,2BAA2B;IAC5C,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IAZnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAGlE,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAIlC;gBAGqC,UAAU,EAAE,UAAU,EAC1C,2BAA2B,EAAE,2BAA2B,EACxD,gBAAgB,EAAE,sBAAsB;IAK3D,QAAQ;IAIF,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;IAQ5C,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;IAqB5C,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;YAIpC,cAAc;YAkDd,cAAc;CAmD7B"}
1
+ {"version":3,"file":"scheduled-job.subscriber.d.ts","sourceRoot":"","sources":["../../src/subscribers/scheduled-job.subscriber.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AACzF,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AACjF,OAAO,EACL,UAAU,EAEV,yBAAyB,EACzB,WAAW,EACX,WAAW,EACX,WAAW,EACZ,MAAM,SAAS,CAAC;AAEjB,qBACa,sBACX,YAAW,yBAAyB,CAAC,YAAY,CAAC;IAW5B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC/C,OAAO,CAAC,QAAQ,CAAC,2BAA2B;IAC5C,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IAZnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA2C;IAGlE,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAIlC;gBAGqC,UAAU,EAAE,UAAU,EAC1C,2BAA2B,EAAE,2BAA2B,EACxD,gBAAgB,EAAE,sBAAsB;IAK3D,QAAQ;IAIF,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;IAQ5C,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;IAqB5C,WAAW,CAAC,KAAK,EAAE,WAAW,CAAC,YAAY,CAAC;YAIpC,cAAc;YAkDd,cAAc;CAoD7B"}
@@ -50,11 +50,11 @@ exports.ScheduledJobSubscriber = void 0;
50
50
  const common_1 = require("@nestjs/common");
51
51
  const typeorm_1 = require("@nestjs/typeorm");
52
52
  const fs = __importStar(require("fs/promises"));
53
- const typeorm_2 = require("typeorm");
53
+ const module_metadata_entity_1 = require("../entities/module-metadata.entity");
54
54
  const scheduled_job_entity_1 = require("../entities/scheduled-job.entity");
55
55
  const module_metadata_helper_service_1 = require("../helpers/module-metadata-helper.service");
56
56
  const scheduled_job_repository_1 = require("../repository/scheduled-job.repository");
57
- const module_metadata_entity_1 = require("../entities/module-metadata.entity");
57
+ const typeorm_2 = require("typeorm");
58
58
  let ScheduledJobSubscriber = ScheduledJobSubscriber_1 = class ScheduledJobSubscriber {
59
59
  constructor(dataSource, moduleMetadataHelperService, scheduledJobRepo) {
60
60
  this.dataSource = dataSource;
@@ -151,12 +151,13 @@ let ScheduledJobSubscriber = ScheduledJobSubscriber_1 = class ScheduledJobSubscr
151
151
  const jobName = jobEntity.scheduleName;
152
152
  const existingIndex = metaData.scheduledJobs.findIndex((job) => job.scheduleName === jobName);
153
153
  const jobDto = await this.scheduledJobRepo.toDto(populatedScheduleJob);
154
+ const { moduleId, ...dtoToWrite } = jobDto;
154
155
  if (existingIndex !== -1) {
155
- metaData.scheduledJobs[existingIndex] = jobDto;
156
+ metaData.scheduledJobs[existingIndex] = dtoToWrite;
156
157
  this.logger.log(`Updated scheduled job ${jobName} in metadata`);
157
158
  }
158
159
  else {
159
- metaData.scheduledJobs.push(jobDto);
160
+ metaData.scheduledJobs.push(dtoToWrite);
160
161
  this.logger.log(`Added scheduled job ${jobName} to metadata`);
161
162
  }
162
163
  const updatedContent = JSON.stringify(metaData, null, 2);
@@ -1 +1 @@
1
- {"version":3,"file":"scheduled-job.subscriber.js","sourceRoot":"","sources":["../../src/subscribers/scheduled-job.subscriber.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,6CAAmD;AACnD,gDAAkC;AAClC,qCAOiB;AACjB,2EAAiE;AACjE,8FAAyF;AACzF,qFAAwG;AACxG,+EAAqE;AAI9D,IAAM,sBAAsB,8BAA5B,MAAM,sBAAsB;IAWjC,YACsB,UAAuC,EAC1C,2BAAwD,EACxD,gBAAwC;QAFpB,eAAU,GAAV,UAAU,CAAY;QAC1C,gCAA2B,GAA3B,2BAA2B,CAA6B;QACxD,qBAAgB,GAAhB,gBAAgB,CAAwB;QAZ1C,WAAM,GAAG,IAAI,eAAM,CAAC,wBAAsB,CAAC,IAAI,CAAC,CAAC;QAGjD,wBAAmB,GAAuC;YACzE,WAAW;YACX,WAAW;YACX,WAAW;SACZ,CAAC;QAOA,IAAI,CAAC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\";\nimport { InjectDataSource } from \"@nestjs/typeorm\";\nimport * as fs from \"fs/promises\";\nimport {\n DataSource,\n EntitySubscriberInterface,\n InsertEvent,\n UpdateEvent,\n RemoveEvent,\n EntityManager,\n} from \"typeorm\";\nimport { ScheduledJob } from \"src/entities/scheduled-job.entity\";\nimport { ModuleMetadataHelperService } from \"src/helpers/module-metadata-helper.service\";\nimport { CreateScheduledJobDto, ScheduledJobRepository } from \"src/repository/scheduled-job.repository\";\nimport { ModuleMetadata } from \"src/entities/module-metadata.entity\";\nimport { where } from \"locale-codes\";\n\n@Injectable()\nexport class ScheduledJobSubscriber\n implements EntitySubscriberInterface<ScheduledJob> {\n private readonly logger = new Logger(ScheduledJobSubscriber.name);\n\n /** Fields that, when changed (and only these changed), should NOT trigger metadata update. */\n private readonly ignoredUpdateFields: Array<keyof ScheduledJob | string> = [\n \"lastRunAt\",\n \"nextRunAt\",\n \"updatedAt\",\n ];\n\n constructor(\n @InjectDataSource() private readonly dataSource: DataSource,\n private readonly moduleMetadataHelperService: ModuleMetadataHelperService,\n private readonly scheduledJobRepo: ScheduledJobRepository\n ) {\n this.dataSource.subscribers.push(this);\n }\n\n listenTo() {\n return ScheduledJob;\n }\n\n async afterInsert(event: InsertEvent<ScheduledJob>) {\n if (!event.entity) {\n this.logger.debug('No schedule Job entity found in the afterInsert method');\n return;\n }\n await this.updateMetadata(event.entity, event.queryRunner.manager);\n }\n\n async afterUpdate(event: UpdateEvent<ScheduledJob>) {\n if (!event.databaseEntity) {\n this.logger.debug('No schedule Job entity found in the afterUpdate method');\n return;\n }\n\n // get hold of the changed field names \n const changedProps = (event.updatedColumns ?? []).map((c) => c.propertyName);\n\n // Decide whether to skip: only skip when *all* changed fields are in the ignore list\n const onlyIgnoredChanged = changedProps.every((p) => this.ignoredUpdateFields.includes(p));\n\n if (onlyIgnoredChanged) {\n this.logger.debug(`Skipping metadata update for ScheduledJob#${(event.databaseEntity as ScheduledJob).id}; only ignored fields changed: ${changedProps.join(\", \")}`\n );\n return;\n }\n\n await this.updateMetadata(event.databaseEntity, event.queryRunner.manager);\n }\n\n async afterRemove(event: RemoveEvent<ScheduledJob>) {\n await this.removeMetadata(event);\n }\n\n private async removeMetadata(event: RemoveEvent<ScheduledJob>) {\n const jobEntity = event.entity;\n const moduleMetadata = jobEntity?.module;\n\n if (!moduleMetadata) {\n this.logger.error(\n `Module metadata not found for scheduled job with ID ${jobEntity?.id}`\n );\n return;\n }\n\n const moduleMetadataRepo = this.dataSource.getRepository(ModuleMetadata);\n const populatedModuleMetadata = await moduleMetadataRepo.findOne({\n where: { id: moduleMetadata.id },\n });\n\n if (!populatedModuleMetadata) {\n this.logger.error(\n `Could not find ModuleMetadata with ID ${moduleMetadata.id}`\n );\n return;\n }\n const filePath =\n await this.moduleMetadataHelperService.getModuleMetadataFilePath(\n populatedModuleMetadata.name\n );\n try {\n await fs.access(filePath);\n } catch {\n this.logger.error(`Metadata file not found: ${filePath}`);\n return;\n }\n const metaData =\n await this.moduleMetadataHelperService.getModuleMetadataConfiguration(\n filePath\n );\n // Remove, update or insert logic\n const jobName = jobEntity.scheduleName;\n const existingIndex = metaData.scheduledJobs.findIndex(\n (job) => job.scheduleName === jobName\n );\n if (existingIndex !== -1) {\n metaData.scheduledJobs.splice(existingIndex, 1);\n this.logger.log(`Removed scheduled job ${jobName} from metadata`);\n }\n const updatedContent = JSON.stringify(metaData, null, 2);\n await fs.writeFile(filePath, updatedContent);\n this.logger.log(`Updated scheduledJobs in ${filePath}`);\n }\n\n private async updateMetadata(jobEntity: ScheduledJob, entityManager: EntityManager) {\n // populate the job with its relation\n const populatedScheduleJob = await entityManager.findOne(ScheduledJob, {\n where: { id: jobEntity.id },\n relations: ['module'],\n });\n\n if (!populatedScheduleJob) {\n throw new Error(`ScheduleJob not found for id ${jobEntity.id}`);\n }\n const filePath =\n await this.moduleMetadataHelperService.getModuleMetadataFilePath(\n populatedScheduleJob.module?.name\n );\n\n try {\n await fs.access(filePath);\n } catch {\n this.logger.error(`Metadata file not found: ${filePath}`);\n return;\n }\n\n const metaData =\n await this.moduleMetadataHelperService.getModuleMetadataConfiguration(\n filePath\n );\n\n // Ensure scheduledJobs exists\n if (!metaData.scheduledJobs) {\n metaData.scheduledJobs = [];\n }\n\n // Remove, update or insert logic\n const jobName = jobEntity.scheduleName;\n const existingIndex = metaData.scheduledJobs.findIndex(\n (job) => job.scheduleName === jobName\n );\n // Insert or update job in metadata\n const jobDto = await this.scheduledJobRepo.toDto(populatedScheduleJob as ScheduledJob);\n if (existingIndex !== -1) {\n metaData.scheduledJobs[existingIndex] = jobDto;\n this.logger.log(`Updated scheduled job ${jobName} in metadata`);\n } else {\n metaData.scheduledJobs.push(jobDto);\n this.logger.log(`Added scheduled job ${jobName} to metadata`);\n }\n\n const updatedContent = JSON.stringify(metaData, null, 2);\n await fs.writeFile(filePath, updatedContent);\n this.logger.log(`Updated scheduledJobs in ${filePath}`);\n }\n}\n"]}
1
+ {"version":3,"file":"scheduled-job.subscriber.js","sourceRoot":"","sources":["../../src/subscribers/scheduled-job.subscriber.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,6CAAmD;AACnD,gDAAkC;AAClC,+EAAqE;AACrE,2EAAiE;AACjE,8FAAyF;AACzF,qFAAiF;AACjF,qCAOiB;AAGV,IAAM,sBAAsB,8BAA5B,MAAM,sBAAsB;IAWjC,YACsB,UAAuC,EAC1C,2BAAwD,EACxD,gBAAwC;QAFpB,eAAU,GAAV,UAAU,CAAY;QAC1C,gCAA2B,GAA3B,2BAA2B,CAA6B;QACxD,qBAAgB,GAAhB,gBAAgB,CAAwB;QAZ1C,WAAM,GAAG,IAAI,eAAM,CAAC,wBAAsB,CAAC,IAAI,CAAC,CAAC;QAGjD,wBAAmB,GAAuC;YACzE,WAAW;YACX,WAAW;YACX,WAAW;SACZ,CAAC;QAOA,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI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","sourcesContent":["import { Injectable, Logger } from \"@nestjs/common\";\nimport { InjectDataSource } from \"@nestjs/typeorm\";\nimport * as fs from \"fs/promises\";\nimport { ModuleMetadata } from \"src/entities/module-metadata.entity\";\nimport { ScheduledJob } from \"src/entities/scheduled-job.entity\";\nimport { ModuleMetadataHelperService } from \"src/helpers/module-metadata-helper.service\";\nimport { ScheduledJobRepository } from \"src/repository/scheduled-job.repository\";\nimport {\n DataSource,\n EntityManager,\n EntitySubscriberInterface,\n InsertEvent,\n RemoveEvent,\n UpdateEvent,\n} from \"typeorm\";\n\n@Injectable()\nexport class ScheduledJobSubscriber\n implements EntitySubscriberInterface<ScheduledJob> {\n private readonly logger = new Logger(ScheduledJobSubscriber.name);\n\n /** Fields that, when changed (and only these changed), should NOT trigger metadata update. */\n private readonly ignoredUpdateFields: Array<keyof ScheduledJob | string> = [\n \"lastRunAt\",\n \"nextRunAt\",\n \"updatedAt\",\n ];\n\n constructor(\n @InjectDataSource() private readonly dataSource: DataSource,\n private readonly moduleMetadataHelperService: ModuleMetadataHelperService,\n private readonly scheduledJobRepo: ScheduledJobRepository\n ) {\n this.dataSource.subscribers.push(this);\n }\n\n listenTo() {\n return ScheduledJob;\n }\n\n async afterInsert(event: InsertEvent<ScheduledJob>) {\n if (!event.entity) {\n this.logger.debug('No schedule Job entity found in the afterInsert method');\n return;\n }\n await this.updateMetadata(event.entity, event.queryRunner.manager);\n }\n\n async afterUpdate(event: UpdateEvent<ScheduledJob>) {\n if (!event.databaseEntity) {\n this.logger.debug('No schedule Job entity found in the afterUpdate method');\n return;\n }\n\n // get hold of the changed field names \n const changedProps = (event.updatedColumns ?? []).map((c) => c.propertyName);\n\n // Decide whether to skip: only skip when *all* changed fields are in the ignore list\n const onlyIgnoredChanged = changedProps.every((p) => this.ignoredUpdateFields.includes(p));\n\n if (onlyIgnoredChanged) {\n this.logger.debug(`Skipping metadata update for ScheduledJob#${(event.databaseEntity as ScheduledJob).id}; only ignored fields changed: ${changedProps.join(\", \")}`\n );\n return;\n }\n\n await this.updateMetadata(event.databaseEntity, event.queryRunner.manager);\n }\n\n async afterRemove(event: RemoveEvent<ScheduledJob>) {\n await this.removeMetadata(event);\n }\n\n private async removeMetadata(event: RemoveEvent<ScheduledJob>) {\n const jobEntity = event.entity;\n const moduleMetadata = jobEntity?.module;\n\n if (!moduleMetadata) {\n this.logger.error(\n `Module metadata not found for scheduled job with ID ${jobEntity?.id}`\n );\n return;\n }\n\n const moduleMetadataRepo = this.dataSource.getRepository(ModuleMetadata);\n const populatedModuleMetadata = await moduleMetadataRepo.findOne({\n where: { id: moduleMetadata.id },\n });\n\n if (!populatedModuleMetadata) {\n this.logger.error(\n `Could not find ModuleMetadata with ID ${moduleMetadata.id}`\n );\n return;\n }\n const filePath =\n await this.moduleMetadataHelperService.getModuleMetadataFilePath(\n populatedModuleMetadata.name\n );\n try {\n await fs.access(filePath);\n } catch {\n this.logger.error(`Metadata file not found: ${filePath}`);\n return;\n }\n const metaData =\n await this.moduleMetadataHelperService.getModuleMetadataConfiguration(\n filePath\n );\n // Remove, update or insert logic\n const jobName = jobEntity.scheduleName;\n const existingIndex = metaData.scheduledJobs.findIndex(\n (job) => job.scheduleName === jobName\n );\n if (existingIndex !== -1) {\n metaData.scheduledJobs.splice(existingIndex, 1);\n this.logger.log(`Removed scheduled job ${jobName} from metadata`);\n }\n const updatedContent = JSON.stringify(metaData, null, 2);\n await fs.writeFile(filePath, updatedContent);\n this.logger.log(`Updated scheduledJobs in ${filePath}`);\n }\n\n private async updateMetadata(jobEntity: ScheduledJob, entityManager: EntityManager) {\n // populate the job with its relation\n const populatedScheduleJob = await entityManager.findOne(ScheduledJob, {\n where: { id: jobEntity.id },\n relations: ['module'],\n });\n\n if (!populatedScheduleJob) {\n throw new Error(`ScheduleJob not found for id ${jobEntity.id}`);\n }\n const filePath =\n await this.moduleMetadataHelperService.getModuleMetadataFilePath(\n populatedScheduleJob.module?.name\n );\n\n try {\n await fs.access(filePath);\n } catch {\n this.logger.error(`Metadata file not found: ${filePath}`);\n return;\n }\n\n const metaData =\n await this.moduleMetadataHelperService.getModuleMetadataConfiguration(\n filePath\n );\n\n // Ensure scheduledJobs exists\n if (!metaData.scheduledJobs) {\n metaData.scheduledJobs = [];\n }\n\n // Remove, update or insert logic\n const jobName = jobEntity.scheduleName;\n const existingIndex = metaData.scheduledJobs.findIndex(\n (job) => job.scheduleName === jobName\n );\n // Insert or update job in metadata\n const jobDto = await this.scheduledJobRepo.toDto(populatedScheduleJob as ScheduledJob);\n const {moduleId, ...dtoToWrite} = jobDto\n if (existingIndex !== -1) {\n metaData.scheduledJobs[existingIndex] = dtoToWrite;\n this.logger.log(`Updated scheduled job ${jobName} in metadata`);\n } else {\n metaData.scheduledJobs.push(dtoToWrite);\n this.logger.log(`Added scheduled job ${jobName} to metadata`);\n }\n\n const updatedContent = JSON.stringify(metaData, null, 2);\n await fs.writeFile(filePath, updatedContent);\n this.logger.log(`Updated scheduledJobs in ${filePath}`);\n }\n}\n"]}