@solidxai/core 0.1.9-beta.6 → 0.1.9-beta.7
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/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.js +5 -5
- package/dist/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.js.map +1 -1
- package/dist/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.d.ts.map +1 -1
- package/dist/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.js +2 -2
- package/dist/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.js.map +1 -1
- package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.js +5 -5
- package/dist/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.js.map +1 -1
- package/dist/helpers/module-metadata-helper.service.js +3 -3
- package/dist/helpers/module-metadata-helper.service.js.map +1 -1
- package/dist/helpers/string.helper.d.ts +1 -0
- package/dist/helpers/string.helper.d.ts.map +1 -1
- package/dist/helpers/string.helper.js +4 -1
- package/dist/helpers/string.helper.js.map +1 -1
- package/dist/repository/chatter-message-details.repository.d.ts.map +1 -1
- package/dist/repository/chatter-message-details.repository.js +7 -6
- package/dist/repository/chatter-message-details.repository.js.map +1 -1
- package/dist/repository/chatter-message.repository.d.ts.map +1 -1
- package/dist/repository/chatter-message.repository.js +4 -4
- package/dist/repository/chatter-message.repository.js.map +1 -1
- package/dist/repository/solid-base.repository.js +2 -2
- package/dist/repository/solid-base.repository.js.map +1 -1
- package/dist/seeders/module-test-data.service.js +4 -4
- package/dist/seeders/module-test-data.service.js.map +1 -1
- package/dist/services/chatter-message.service.d.ts.map +1 -1
- package/dist/services/chatter-message.service.js +7 -7
- package/dist/services/chatter-message.service.js.map +1 -1
- package/dist/services/computed-fields/entity/sequence-num-computed-field-provider.d.ts.map +1 -1
- package/dist/services/computed-fields/entity/sequence-num-computed-field-provider.js +2 -2
- package/dist/services/computed-fields/entity/sequence-num-computed-field-provider.js.map +1 -1
- package/dist/services/crud-helper.service.d.ts.map +1 -1
- package/dist/services/crud-helper.service.js +9 -9
- package/dist/services/crud-helper.service.js.map +1 -1
- package/dist/services/export-transaction.service.d.ts.map +1 -1
- package/dist/services/export-transaction.service.js +4 -3
- package/dist/services/export-transaction.service.js.map +1 -1
- package/dist/services/field-metadata.service.d.ts.map +1 -1
- package/dist/services/field-metadata.service.js +7 -7
- package/dist/services/field-metadata.service.js.map +1 -1
- package/dist/services/fixtures.service.js +2 -2
- package/dist/services/fixtures.service.js.map +1 -1
- package/dist/services/genai/mcp-handlers/mcp-handler-factory.service.d.ts.map +1 -1
- package/dist/services/genai/mcp-handlers/mcp-handler-factory.service.js +2 -2
- package/dist/services/genai/mcp-handlers/mcp-handler-factory.service.js.map +1 -1
- package/dist/services/import-transaction.service.d.ts.map +1 -1
- package/dist/services/import-transaction.service.js +2 -2
- package/dist/services/import-transaction.service.js.map +1 -1
- package/dist/services/menu-item-metadata.service.js +2 -2
- package/dist/services/menu-item-metadata.service.js.map +1 -1
- package/dist/services/model-metadata.service.d.ts.map +1 -1
- package/dist/services/model-metadata.service.js +11 -10
- package/dist/services/model-metadata.service.js.map +1 -1
- package/dist/services/module-metadata.service.d.ts.map +1 -1
- package/dist/services/module-metadata.service.js +2 -2
- package/dist/services/module-metadata.service.js.map +1 -1
- package/dist/services/queues/publisher-factory.service.d.ts.map +1 -1
- package/dist/services/queues/publisher-factory.service.js +2 -2
- package/dist/services/queues/publisher-factory.service.js.map +1 -1
- package/dist/services/selection-providers/pseudo-foreign-key-selection-provider.service.js +3 -3
- package/dist/services/selection-providers/pseudo-foreign-key-selection-provider.service.js.map +1 -1
- package/dist/services/solid-introspect.service.js +2 -2
- package/dist/services/solid-introspect.service.js.map +1 -1
- package/dist/services/view-metadata.service.d.ts.map +1 -1
- package/dist/services/view-metadata.service.js +2 -2
- package/dist/services/view-metadata.service.js.map +1 -1
- package/dist/subscribers/computed-entity-field.subscriber.js +6 -6
- package/dist/subscribers/computed-entity-field.subscriber.js.map +1 -1
- package/package.json +1 -2
- package/src/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.ts +1 -1
- package/src/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.ts +1 -2
- package/src/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.ts +1 -1
- package/src/helpers/module-metadata-helper.service.ts +3 -3
- package/src/helpers/string.helper.ts +3 -0
- package/src/repository/chatter-message-details.repository.ts +4 -3
- package/src/repository/chatter-message.repository.ts +4 -4
- package/src/repository/solid-base.repository.ts +2 -2
- package/src/seeders/module-test-data.service.ts +1 -1
- package/src/services/chatter-message.service.ts +1 -3
- package/src/services/computed-fields/entity/sequence-num-computed-field-provider.ts +1 -2
- package/src/services/crud-helper.service.ts +1 -4
- package/src/services/export-transaction.service.ts +3 -4
- package/src/services/field-metadata.service.ts +1 -4
- package/src/services/fixtures.service.ts +1 -1
- package/src/services/genai/mcp-handlers/mcp-handler-factory.service.ts +1 -2
- package/src/services/import-transaction.service.ts +1 -1
- package/src/services/menu-item-metadata.service.ts +2 -2
- package/src/services/model-metadata.service.ts +10 -12
- package/src/services/module-metadata.service.ts +1 -2
- package/src/services/queues/publisher-factory.service.ts +1 -2
- package/src/services/selection-providers/pseudo-foreign-key-selection-provider.service.ts +1 -1
- package/src/services/solid-introspect.service.ts +1 -1
- package/src/services/view-metadata.service.ts +1 -5
- package/src/subscribers/computed-entity-field.subscriber.ts +6 -6
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ManyToManyRelationFieldCrudManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const string_helper_1 = require("../string.helper");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const class_validator_1 = require("class-validator");
|
|
7
7
|
const create_field_metadata_dto_1 = require("../../dtos/create-field-metadata.dto");
|
|
@@ -47,9 +47,9 @@ class ManyToManyRelationFieldCrudManager {
|
|
|
47
47
|
return errors;
|
|
48
48
|
}
|
|
49
49
|
async transformForCreate(dto) {
|
|
50
|
-
const currentEntityName = (0,
|
|
50
|
+
const currentEntityName = (0, string_helper_1.classify)(this.options.modelSingularName);
|
|
51
51
|
const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);
|
|
52
|
-
const relatedEntityName = (0,
|
|
52
|
+
const relatedEntityName = (0, string_helper_1.classify)(this.options.relationCoModelSingularName);
|
|
53
53
|
const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName);
|
|
54
54
|
const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);
|
|
55
55
|
if (result !== undefined) {
|
|
@@ -108,7 +108,7 @@ class ManyToManyRelationFieldCrudManager {
|
|
|
108
108
|
}
|
|
109
109
|
await this.options.entityManager
|
|
110
110
|
.createQueryBuilder()
|
|
111
|
-
.relation((0,
|
|
111
|
+
.relation((0, string_helper_1.classify)(this.options.modelSingularName), this.valueFieldName)
|
|
112
112
|
.of(this.options.entityId)
|
|
113
113
|
.add(ids);
|
|
114
114
|
return undefined;
|
|
@@ -119,7 +119,7 @@ class ManyToManyRelationFieldCrudManager {
|
|
|
119
119
|
}
|
|
120
120
|
await this.options.entityManager
|
|
121
121
|
.createQueryBuilder()
|
|
122
|
-
.relation((0,
|
|
122
|
+
.relation((0, string_helper_1.classify)(this.options.modelSingularName), this.valueFieldName)
|
|
123
123
|
.of(this.options.entityId)
|
|
124
124
|
.remove(ids);
|
|
125
125
|
return undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ManyToManyRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,oEAAkE;AAClE,2CAAwC;AACxC,qDAA8D;AAC9D,oFAA2E;AAE3E,qCAA4C;AAe5C,MAAM,YAAY,GAAG,CAAC,iDAAqB,CAAC,IAAI,EAAE,iDAAqB,CAA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M,wBAAwB,GAAG,EAAE,CAAC;QACpC,KAAK,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;YAC1B,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAClE,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;iBACI,CAAC;gBACF,MAAM,WAAW,GAAG,uBAAuB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC3D,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAED,OAAO,wBAAwB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,yBAAyB,CAAC,MAAa,EAAE,uBAA4B;QAE/E,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAG5C,MAAM,uBAAuB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAA;IACf,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CASJ;AA3LD,gFA2LC","sourcesContent":["import { classify } from \"@angular-devkit/core/src/utils/strings\";\nimport { Logger } from \"@nestjs/common\";\nimport { isEmpty, isEnum, isNotEmpty } from \"class-validator\";\nimport { RelationFieldsCommand } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\nimport { EntityManager, In } from \"typeorm\";\n\nexport interface ManyToManyRelationFieldOptions {\n // Add options for relation field\n required: boolean | undefined | null;\n relationCoModelSingularName: string | undefined | null;\n modelSingularName: string | undefined | null;\n isInverseSide: boolean;\n entityManager: EntityManager;\n fieldName: string;\n relationCoModelFieldName?: string;\n isUpdate?: boolean;\n entityId?: number;\n}\n\nconst linkCommands = [RelationFieldsCommand.link, RelationFieldsCommand.unlink, RelationFieldsCommand.set];\n\n// This implementation is meant to be used for many-to-one relation field\nexport class ManyToManyRelationFieldCrudManager implements FieldCrudManager {\n private readonly logger = new Logger(ManyToManyRelationFieldCrudManager.name);\n\n private valueFieldName: string;\n private idFieldName: string;\n private commandFieldName: string;\n\n constructor(private readonly options: ManyToManyRelationFieldOptions) {\n if (!this.options.isInverseSide) {\n this.valueFieldName = `${this.options.fieldName}`;\n this.idFieldName = `${this.options.fieldName}Ids`;\n this.commandFieldName = `${this.options.fieldName}Command`;\n }\n else {\n this.valueFieldName = `${this.options.relationCoModelFieldName}`;\n this.idFieldName = `${this.options.relationCoModelFieldName}Ids`;\n this.commandFieldName = `${this.options.relationCoModelFieldName}Command`;\n }\n }\n\n validate(dto: any) {\n return this.applyValidations(dto);\n }\n\n private applyValidations(dto: any): ValidationError[] { \n const errors: ValidationError[] = [];\n\n const commandFieldName = this.commandFieldName;\n const commandValue = dto[commandFieldName];\n this.isApplyRequiredValidation() && isEmpty(commandValue) ? errors.push({ field: commandFieldName, error: 'Command field is required' }) : \"no errors\";\n if (isNotEmpty(commandValue)) {\n !isEnum(commandValue, RelationFieldsCommand)? errors.push({ field: this.options.fieldName, error: 'Command Field has invalid value' }) : \"no errors\";\n } \n \n var fieldValue = null\n if (commandValue === RelationFieldsCommand.clear) {\n return errors;\n } else if (linkCommands.includes(commandValue)) {\n fieldValue = dto[this.idFieldName];\n } \n else {\n fieldValue = dto[this.valueFieldName];\n }\n\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n return errors;\n }\n\n async transformForCreate(dto: any): Promise<any> {\n const currentEntityName = classify(this.options.modelSingularName);\n const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);\n\n const relatedEntityName = classify(this.options.relationCoModelSingularName);\n const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName)\n\n const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);\n if (result !== undefined) {\n dto[this.valueFieldName] = result;\n } else {\n delete dto[this.valueFieldName];\n }\n return dto;\n }\n\n private async transformByCommand(dto: any, relatedEntityRepository: any, currentEntityRepository: any): Promise<any[] | undefined> {\n // TODO : Need to add support for the multiple commands\n const command = dto[this.commandFieldName];\n const values = dto[this.valueFieldName];\n const ids = dto[this.idFieldName];\n\n switch (command) {\n case RelationFieldsCommand.create:\n return await this.transformForCommandCreate(values, relatedEntityRepository);\n case RelationFieldsCommand.update:\n return await this.transformForCommandUpdate(values, relatedEntityRepository, dto, currentEntityRepository);\n case RelationFieldsCommand.delete:\n return await this.transformForCommandDelete(values, relatedEntityRepository);\n case RelationFieldsCommand.clear:\n return this.transformForCommandClear();\n case RelationFieldsCommand.set:\n return await this.transformForCommandSet(ids, relatedEntityRepository);\n case RelationFieldsCommand.link:\n return await this.tranformForCommandLink(ids, relatedEntityRepository);\n case RelationFieldsCommand.unlink:\n return await this.transformForCommandUnLink(ids);\n default:\n this.logger.log(`Invalid command ${command}`);\n return null;\n\n }\n }\n\n private transformForCommandClear() {\n return []\n }\n\n private async transformForCommandSet(ids: any[], relatedEntityRepository: any): Promise<any[]> {\n const loadedEntities: any[] = await relatedEntityRepository.find({\n where : {id: In(ids) }\n })\n if (loadedEntities.length !== ids.length) {\n throw new Error('Invalid entity ids provided for set');\n }\n\n return loadedEntities;\n }\n\n private async tranformForCommandLink(ids: any[], relatedEntityRepository: any): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Link command is only supported for update operations');\n }\n\n const count: number = await relatedEntityRepository.count({\n where: { id: In(ids) }\n });\n if (count !== ids.length) {\n throw new Error('Invalid entity ids provided for linking');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .add(ids);\n\n return undefined;\n }\n\n private async transformForCommandUnLink(ids: any[]): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Unlink command is only supported for update operations');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .remove(ids);\n\n return undefined;\n }\n\n private async transformForCommandCreate(values: any[], relatedEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n return transformedRelatedFields;\n }\n\n private async transformForCommandUpdate(values: any[], relatedEntityRepository: any, dto: any, currentEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n if (entity.id) {\n const transformed = await relatedEntityRepository.preload(entity);\n transformedRelatedFields.push(transformed);\n }\n else {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n }\n\n return transformedRelatedFields;\n }\n\n private async transformForCommandDelete(values: any[], relatedEntityRepository: any): Promise<any[]> {\n // Map and get the ids from the values\n const ids = values.map((value) => value.id);\n\n // Delete the ids linked to the associated entity\n await relatedEntityRepository.delete(ids);\n return null\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n\n // TODO: We have moved this to SolidRegistry service, refactor to use that service.\n // Returns the entity target class from the entity name\n // private getEntityTarget(entityName: string): any { //TODO Can be refactored to use this function from crud helper service\n // const entityMetadatas = this.options.entityManager.connection.entityMetadatas;\n // const entityMetadata = entityMetadatas.find(em => em.name === entityName);\n // return entityMetadata.target;\n // }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ManyToManyRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/ManyToManyRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,oDAA4C;AAC5C,2CAAwC;AACxC,qDAA8D;AAC9D,oFAA2E;AAE3E,qCAA4C;AAe5C,MAAM,YAAY,GAAG,CAAC,iDAAqB,CAAC,IAAI,EAAE,iDAAqB,CAAC,MAAM,EAAE,iDAAqB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,CAAC;QACpC,KAAK,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;YAC1B,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAClE,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;iBACI,CAAC;gBACF,MAAM,WAAW,GAAG,uBAAuB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC3D,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAED,OAAO,wBAAwB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,yBAAyB,CAAC,MAAa,EAAE,uBAA4B;QAE/E,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAG5C,MAAM,uBAAuB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAA;IACf,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CASJ;AA3LD,gFA2LC","sourcesContent":["import { classify } from '../string.helper';\nimport { Logger } from \"@nestjs/common\";\nimport { isEmpty, isEnum, isNotEmpty } from \"class-validator\";\nimport { RelationFieldsCommand } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\nimport { EntityManager, In } from \"typeorm\";\n\nexport interface ManyToManyRelationFieldOptions {\n // Add options for relation field\n required: boolean | undefined | null;\n relationCoModelSingularName: string | undefined | null;\n modelSingularName: string | undefined | null;\n isInverseSide: boolean;\n entityManager: EntityManager;\n fieldName: string;\n relationCoModelFieldName?: string;\n isUpdate?: boolean;\n entityId?: number;\n}\n\nconst linkCommands = [RelationFieldsCommand.link, RelationFieldsCommand.unlink, RelationFieldsCommand.set];\n\n// This implementation is meant to be used for many-to-one relation field\nexport class ManyToManyRelationFieldCrudManager implements FieldCrudManager {\n private readonly logger = new Logger(ManyToManyRelationFieldCrudManager.name);\n\n private valueFieldName: string;\n private idFieldName: string;\n private commandFieldName: string;\n\n constructor(private readonly options: ManyToManyRelationFieldOptions) {\n if (!this.options.isInverseSide) {\n this.valueFieldName = `${this.options.fieldName}`;\n this.idFieldName = `${this.options.fieldName}Ids`;\n this.commandFieldName = `${this.options.fieldName}Command`;\n }\n else {\n this.valueFieldName = `${this.options.relationCoModelFieldName}`;\n this.idFieldName = `${this.options.relationCoModelFieldName}Ids`;\n this.commandFieldName = `${this.options.relationCoModelFieldName}Command`;\n }\n }\n\n validate(dto: any) {\n return this.applyValidations(dto);\n }\n\n private applyValidations(dto: any): ValidationError[] { \n const errors: ValidationError[] = [];\n\n const commandFieldName = this.commandFieldName;\n const commandValue = dto[commandFieldName];\n this.isApplyRequiredValidation() && isEmpty(commandValue) ? errors.push({ field: commandFieldName, error: 'Command field is required' }) : \"no errors\";\n if (isNotEmpty(commandValue)) {\n !isEnum(commandValue, RelationFieldsCommand)? errors.push({ field: this.options.fieldName, error: 'Command Field has invalid value' }) : \"no errors\";\n } \n \n var fieldValue = null\n if (commandValue === RelationFieldsCommand.clear) {\n return errors;\n } else if (linkCommands.includes(commandValue)) {\n fieldValue = dto[this.idFieldName];\n } \n else {\n fieldValue = dto[this.valueFieldName];\n }\n\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required` }) : \"no errors\";\n return errors;\n }\n\n async transformForCreate(dto: any): Promise<any> {\n const currentEntityName = classify(this.options.modelSingularName);\n const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);\n\n const relatedEntityName = classify(this.options.relationCoModelSingularName);\n const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName)\n\n const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);\n if (result !== undefined) {\n dto[this.valueFieldName] = result;\n } else {\n delete dto[this.valueFieldName];\n }\n return dto;\n }\n\n private async transformByCommand(dto: any, relatedEntityRepository: any, currentEntityRepository: any): Promise<any[] | undefined> {\n // TODO : Need to add support for the multiple commands\n const command = dto[this.commandFieldName];\n const values = dto[this.valueFieldName];\n const ids = dto[this.idFieldName];\n\n switch (command) {\n case RelationFieldsCommand.create:\n return await this.transformForCommandCreate(values, relatedEntityRepository);\n case RelationFieldsCommand.update:\n return await this.transformForCommandUpdate(values, relatedEntityRepository, dto, currentEntityRepository);\n case RelationFieldsCommand.delete:\n return await this.transformForCommandDelete(values, relatedEntityRepository);\n case RelationFieldsCommand.clear:\n return this.transformForCommandClear();\n case RelationFieldsCommand.set:\n return await this.transformForCommandSet(ids, relatedEntityRepository);\n case RelationFieldsCommand.link:\n return await this.tranformForCommandLink(ids, relatedEntityRepository);\n case RelationFieldsCommand.unlink:\n return await this.transformForCommandUnLink(ids);\n default:\n this.logger.log(`Invalid command ${command}`);\n return null;\n\n }\n }\n\n private transformForCommandClear() {\n return []\n }\n\n private async transformForCommandSet(ids: any[], relatedEntityRepository: any): Promise<any[]> {\n const loadedEntities: any[] = await relatedEntityRepository.find({\n where : {id: In(ids) }\n })\n if (loadedEntities.length !== ids.length) {\n throw new Error('Invalid entity ids provided for set');\n }\n\n return loadedEntities;\n }\n\n private async tranformForCommandLink(ids: any[], relatedEntityRepository: any): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Link command is only supported for update operations');\n }\n\n const count: number = await relatedEntityRepository.count({\n where: { id: In(ids) }\n });\n if (count !== ids.length) {\n throw new Error('Invalid entity ids provided for linking');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .add(ids);\n\n return undefined;\n }\n\n private async transformForCommandUnLink(ids: any[]): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Unlink command is only supported for update operations');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .remove(ids);\n\n return undefined;\n }\n\n private async transformForCommandCreate(values: any[], relatedEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n return transformedRelatedFields;\n }\n\n private async transformForCommandUpdate(values: any[], relatedEntityRepository: any, dto: any, currentEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n if (entity.id) {\n const transformed = await relatedEntityRepository.preload(entity);\n transformedRelatedFields.push(transformed);\n }\n else {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n }\n\n return transformedRelatedFields;\n }\n\n private async transformForCommandDelete(values: any[], relatedEntityRepository: any): Promise<any[]> {\n // Map and get the ids from the values\n const ids = values.map((value) => value.id);\n\n // Delete the ids linked to the associated entity\n await relatedEntityRepository.delete(ids);\n return null\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n\n // TODO: We have moved this to SolidRegistry service, refactor to use that service.\n // Returns the entity target class from the entity name\n // private getEntityTarget(entityName: string): any { //TODO Can be refactored to use this function from crud helper service\n // const entityMetadatas = this.options.entityManager.connection.entityMetadatas;\n // const entityMetadata = entityMetadatas.find(em => em.name === entityName);\n // return entityMetadata.target;\n // }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ManyToOneRelationFieldCrudManager.d.ts","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEnE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,WAAW,6BAA6B;IAE1C,QAAQ,EAAE,OAAO,GAAG,SAAS,GAA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qBAAa,iCAAkC,YAAW,gBAAgB;IAG1D,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,gBAAgB,CAAS;gBACJ,OAAO,EAAE,6BAA6B;IAKnE,QAAQ,CAAC,GAAG,EAAE,GAAG;IAMjB,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,6BAA6B;IAS/B,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAgChD,OAAO,CAAC,yBAAyB;CAWpC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ManyToOneRelationFieldCrudManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const string_helper_1 = require("../string.helper");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const class_validator_1 = require("class-validator");
|
|
7
7
|
const is_parsable_int_1 = require("../../validators/is-parsable-int");
|
|
@@ -48,7 +48,7 @@ class ManyToOneRelationFieldCrudManager {
|
|
|
48
48
|
if (!this.options.relationCoModelSingularName) {
|
|
49
49
|
throw new common_1.BadRequestException(`ManyToOneRelationFieldCrudManager: relationCoModelSingularName is not defined in the field: ${this.options.fieldName}`);
|
|
50
50
|
}
|
|
51
|
-
const coModelEntityName = (0,
|
|
51
|
+
const coModelEntityName = (0, string_helper_1.classify)(this.options.relationCoModelSingularName);
|
|
52
52
|
if ((0, class_validator_1.isNotEmpty)(fieldId)) {
|
|
53
53
|
dto[this.options.fieldName] = await this.options.entityManager.getRepository(coModelEntityName).findOneBy({ id: fieldId });
|
|
54
54
|
if (this.options.required && (0, class_validator_1.isEmpty)(dto[this.options.fieldName])) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ManyToOneRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"ManyToOneRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/ManyToOneRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,oDAA4C;AAC5C,2CAAqD;AACrD,qDAAgE;AAEhE,sEAA+D;AAe/D,MAAa,iCAAiC;IAG1C,YAA6B,OAAsC;QAAtC,YAAO,GAAP,OAAO,CAA+B;QAE/D,IAAI,CAAC,WAAW,GAAG,GAAG,OAAO,CAAC,SAAS,IAAI,CAAC;QAC5C,IAAI,CAAC,gBAAgB,GAAG,GAAG,OAAO,CAAC,SAAS,SAAS,CAAC;IAC1D,CAAC;IACD,QAAQ,CAAC,GAAQ;QACb,MAAM,OAAO,GAAW,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAC9C,MAAM,YAAY,GAAW,GAAG,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;QACxD,OAAO,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;IACxD,CAAC;IAEO,gBAAgB,CAAC,OAAY,EAAE,YAAoB;QACvD,MAAM,MAAM,GAAsB,EAAE,CAAC;QACrC,IAAI,CAAC,yBAAyB,EAAE,IAAI,IAAA,yBAAO,EAAC,OAAO,CAAC,IAAI,IAAA,yBAAO,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,UAAU,IAAI,CAAC,OAAO,CAAC,SAAS,6BAA6B,IAAI,CAAC,WAAW,OAAO,IAAI,CAAC,gBAAgB,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;QAClQ,IAAI,IAAA,4BAAU,EAAC,OAAO,CAAC,EAAE,CAAC;YACtB,MAAM,CAAC,IAAI,CAAC,GA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L,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,IAAI,IAAA,yBAAO,EAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;gBAChE,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,SAAS,4CAA4C,IAAI,CAAC,OAAO,CAAC,+BAA+B,KAAK,iBAAiB,iBAAiB,IAAI,CAAC,OAAO,CAAC,2BAA2B,EAAE,CAAC,CAAC;YAC9N,CAAC;QACL,CAAC;QAED,OAAO,GAAG,CAAC;IACf,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CASJ;AApFD,8EAoFC","sourcesContent":["import { classify } from '../string.helper';\nimport { BadRequestException } from \"@nestjs/common\";\nimport { isEmpty, isNotEmpty, isString } from \"class-validator\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\nimport { IsParsableInt } from \"src/validators/is-parsable-int\";\nimport { EntityManager } from \"typeorm\";\n\nexport interface ManyToOneRelationFieldOptions {\n // Add options for relation field\n required: boolean | undefined | null;\n relationCoModelSingularName: string | undefined | null;\n fieldName: string | undefined | null;\n // modelUserKeyFieldName: string | undefined | null;\n modelSingularName: string | undefined | null;\n entityManager: EntityManager;\n relationCoModelUserKeyFieldName: string | undefined | null; \n}\n\n// This implementation is meant to be used for many-to-one relation field\nexport class ManyToOneRelationFieldCrudManager implements FieldCrudManager {\n private idFieldName: string;\n private userKeyFieldName: string;\n constructor(private readonly options: ManyToOneRelationFieldOptions) {\n // this.options = options;\n this.idFieldName = `${options.fieldName}Id`;\n this.userKeyFieldName = `${options.fieldName}UserKey`;\n }\n validate(dto: any) {\n const fieldId: number = dto[this.idFieldName];\n const fieldUserKey: string = dto[this.userKeyFieldName];\n return this.applyValidations(fieldId, fieldUserKey);\n }\n\n private applyValidations(fieldId: any, fieldUserKey: string): ValidationError[] {\n const errors: ValidationError[] = [];\n this.isApplyRequiredValidation() && isEmpty(fieldId) && isEmpty(fieldUserKey) ? errors.push({ field: this.options.fieldName, error: `Field: ${this.options.fieldName} is required. Either pass ${this.idFieldName} or ${this.userKeyFieldName}.` }) : \"no errors\";\n if (isNotEmpty(fieldId)) {\n errors.push(...this.applyIdFormatValidations(fieldId));\n }\n if (isNotEmpty(fieldUserKey)) {\n errors.push(...this.applyUserKeyFormatValidations(fieldUserKey));\n }\n return errors;\n }\n\n private applyIdFormatValidations(fieldId: any): ValidationError[] { //FIXME fieldId is any because it can be string or number. Keeping it any for compatibility with isParsableInt. \n const errors: ValidationError[] = [];\n !IsParsableInt(fieldId) ? errors.push({ field: this.options.fieldName, error: 'Field is not a integer' }) : \"no errors\";\n return errors;\n }\n\n private applyUserKeyFormatValidations(fieldUserKey: string): ValidationError[] {\n const errors: ValidationError[] = [];\n !isString(fieldUserKey) ? errors.push({ field: this.options.fieldName, error: 'Field is not a string' }) : \"no errors\";\n if (isEmpty(this.options.relationCoModelUserKeyFieldName)) {\n errors.push({ field: this.options.fieldName, error: `UserKey field name is not defined in the model ${this.options.relationCoModelSingularName}` });\n }\n return errors;\n }\n\n async transformForCreate(dto: any): Promise<any> {\n const fieldId: number = dto[this.idFieldName];\n const fieldUserKeyValue: string = dto[this.userKeyFieldName];\n\n // Avoid transforming if both fieldId and fieldUserKey is empty\n if ((isEmpty(fieldId)) && isEmpty(fieldUserKeyValue)) return dto;\n\n if (!this.options.relationCoModelSingularName) {\n throw new BadRequestException(`ManyToOneRelationFieldCrudManager: relationCoModelSingularName is not defined in the field: ${this.options.fieldName}`);\n }\n // // Load the related entity from the database, using the repository of the related entity\n //const entityTarget = this.getRelatedEntityTarget(classify(this.options.relationCoModelSingularName));\n const coModelEntityName = classify(this.options.relationCoModelSingularName);\n\n //Load the related entity from the database, using the repository of the related entity\n //const entityTarget = this.getRelatedEntityTarget(classify(this.options.relationCoModelSingularName));\n if (isNotEmpty(fieldId)) {\n dto[this.options.fieldName] = await this.options.entityManager.getRepository(coModelEntityName).findOneBy({ id: fieldId });\n if (this.options.required && isEmpty(dto[this.options.fieldName])) {\n throw new Error(`Field ${this.options.fieldName} resolution failed. Record with id: ${fieldId} not found in ${this.options.relationCoModelSingularName}`);\n }\n }\n else {\n dto[this.options.fieldName] = await this.options.entityManager.getRepository(coModelEntityName).findOneBy({ [this.options.relationCoModelUserKeyFieldName]: fieldUserKeyValue });\n if (this.options.required && isEmpty(dto[this.options.fieldName])) {\n throw new Error(`Field ${this.options.fieldName} resolution failed. Record with userKey: ${this.options.relationCoModelUserKeyFieldName}: ${fieldUserKeyValue} not found in ${this.options.relationCoModelSingularName}`);\n }\n }\n\n return dto;\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n\n // Returns the entity target class from the entity name\n // private getRelatedEntityTarget(relatedEntityName: string): any {\n // const entityMetadatas = this.options.entityManager.connection.entityMetadatas;\n // const relatedEntityMetadata = entityMetadatas.find(em => em.name === relatedEntityName);\n // return relatedEntityMetadata.target;\n // }\n\n}"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OneToManyRelationFieldCrudManager = void 0;
|
|
4
|
-
const
|
|
4
|
+
const string_helper_1 = require("../string.helper");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const class_validator_1 = require("class-validator");
|
|
7
7
|
const create_field_metadata_dto_1 = require("../../dtos/create-field-metadata.dto");
|
|
@@ -54,9 +54,9 @@ class OneToManyRelationFieldCrudManager {
|
|
|
54
54
|
return errors;
|
|
55
55
|
}
|
|
56
56
|
async transformForCreate(dto) {
|
|
57
|
-
const currentEntityName = (0,
|
|
57
|
+
const currentEntityName = (0, string_helper_1.classify)(this.options.modelSingularName);
|
|
58
58
|
const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);
|
|
59
|
-
const relatedEntityName = (0,
|
|
59
|
+
const relatedEntityName = (0, string_helper_1.classify)(this.options.relationCoModelSingularName);
|
|
60
60
|
const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName);
|
|
61
61
|
const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);
|
|
62
62
|
if (result !== undefined) {
|
|
@@ -115,7 +115,7 @@ class OneToManyRelationFieldCrudManager {
|
|
|
115
115
|
}
|
|
116
116
|
await this.options.entityManager
|
|
117
117
|
.createQueryBuilder()
|
|
118
|
-
.relation((0,
|
|
118
|
+
.relation((0, string_helper_1.classify)(this.options.modelSingularName), this.valueFieldName)
|
|
119
119
|
.of(this.options.entityId)
|
|
120
120
|
.add(ids);
|
|
121
121
|
return undefined;
|
|
@@ -126,7 +126,7 @@ class OneToManyRelationFieldCrudManager {
|
|
|
126
126
|
}
|
|
127
127
|
await this.options.entityManager
|
|
128
128
|
.createQueryBuilder()
|
|
129
|
-
.relation((0,
|
|
129
|
+
.relation((0, string_helper_1.classify)(this.options.modelSingularName), this.valueFieldName)
|
|
130
130
|
.of(this.options.entityId)
|
|
131
131
|
.remove(ids);
|
|
132
132
|
return undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OneToManyRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,oEAAkE;AAClE,2CAAwC;AACxC,qDAAqE;AACrE,oFAA2E;AAE3E,qCAA4C;AAc5C,MAAM,YAAY,GAAG,CAAC,iDAAqB,CAAC,IAAI,EAAE,iDAAqB,CAAC,MAAM,EAAE,iDAAqB,CAAC,GAAG,CAAC,CAAC;AAG3G,MAAa,iCAAiC;IAO1C,YAA6B,OAAsC;QAAtC,YAAO,GAAP,OAAO,CAA+B;QANlD,WAAM,GAAG,IAAI,eAAM,CAAC,iCAAiC,CAAC,IAAI,CAAC,CAAC;QAOzE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,IAAI,GA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AwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAChD,CAAC;QACA,OAAO,wBAAwB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,yBAAyB,CAAC,MAAa,EAAE,uBAA4B,EAAE,GAAQ,EAAE,uBAA4B;QACvH,MAAM,wBAAwB,GAAG,EAAE,CAAC;QACpC,KAAK,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;YAC1B,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAClE,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;iBACI,CAAC;gBACF,MAAM,WAAW,GAAG,uBAAuB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAC3D,wBAAwB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAED,OAAO,wBAAwB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,yBAAyB,CAAC,MAAa,EAAE,uBAA4B;QAE/E,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAG5C,MAAM,uBAAuB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAA;IACf,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CASJ;AAlMD,8EAkMC","sourcesContent":["import { classify } from \"@angular-devkit/core/src/utils/strings\";\nimport { Logger } from \"@nestjs/common\";\nimport { isEmpty, isEnum, isInt, isNotEmpty } from \"class-validator\";\nimport { RelationFieldsCommand } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\nimport { EntityManager, In } from \"typeorm\";\n\nexport interface OneToManyRelationFieldOptions {\n // Add options for relation field\n required: boolean | undefined | null;\n relationCoModelSingularName: string | undefined | null;\n inverseRelationCoModelFieldName: string | undefined | null;\n modelSingularName: string | undefined | null;\n inverseFieldName: string | undefined | null;\n entityManager: EntityManager | undefined | null;\n isUpdate?: boolean;\n entityId?: number;\n}\n\nconst linkCommands = [RelationFieldsCommand.link, RelationFieldsCommand.unlink, RelationFieldsCommand.set];\n\n// This implementation is meant to be used for many-to-one relation field\nexport class OneToManyRelationFieldCrudManager implements FieldCrudManager {\n private readonly logger = new Logger(OneToManyRelationFieldCrudManager.name);\n\n private readonly valueFieldName: string;\n private readonly idFieldName: string;\n private readonly commandFieldName: string;\n\n constructor(private readonly options: OneToManyRelationFieldOptions) {\n this.valueFieldName = this.options.inverseRelationCoModelFieldName ?? `${this.options.relationCoModelSingularName}s`;\n this.idFieldName = `${this.options.inverseRelationCoModelFieldName ?? this.options.relationCoModelSingularName}Ids`;\n this.commandFieldName = `${this.options.inverseRelationCoModelFieldName ?? this.options.relationCoModelSingularName}Command`;\n }\n\n validate(dto: any) {\n return this.applyValidations(dto);\n }\n\n private applyValidations(dto: any): ValidationError[] { \n const errors: ValidationError[] = [];\n\n const commandFieldName = this.commandFieldName;\n const commandValue = dto[commandFieldName];\n this.isApplyRequiredValidation() && isEmpty(commandValue) ? errors.push({ field: commandFieldName, error: 'Command field is required' }) : \"no errors\";\n if (isNotEmpty(commandValue)) {\n !isEnum(commandValue, RelationFieldsCommand)? errors.push({ field: this.options.inverseFieldName, error: 'Command Field has invalid value' }) : \"no errors\";\n } \n \n var fieldValue = null\n if (commandValue === RelationFieldsCommand.clear) {\n return errors;\n } else if (linkCommands.includes(commandValue)) {\n fieldValue = dto[this.idFieldName];\n } \n else {\n fieldValue = dto[this.valueFieldName];\n }\n\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.inverseFieldName, error: `Field: ${this.options.inverseFieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n errors.push(...this.applyFormatValidations(fieldValue, commandValue, commandFieldName));\n }\n return errors;\n }\n\n private applyFormatValidations(fieldValue: any[], commandValue: RelationFieldsCommand, commandFieldName: string): ValidationError[] {\n const errors: ValidationError[] = [];\n if (linkCommands.includes(commandValue)) {\n for (const id of fieldValue) {\n if (!isInt(id)) {\n errors.push({ field: this.options.inverseFieldName, error: `Invalid ids in ${commandFieldName}` });\n }\n }\n }\n return errors;\n }\n\n async transformForCreate(dto: any): Promise<any> {\n const currentEntityName = classify(this.options.modelSingularName);\n const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);\n\n const relatedEntityName = classify(this.options.relationCoModelSingularName);\n const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName);\n\n const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);\n if (result !== undefined) {\n dto[this.valueFieldName] = result;\n } else {\n delete dto[this.valueFieldName];\n }\n return dto;\n }\n\n private async transformByCommand(dto: any, relatedEntityRepository: any, currentEntityRepository: any): Promise<any[] | undefined> {\n // TODO : Need to add support for the multiple commands\n const command = dto[this.commandFieldName];\n const values = dto[this.valueFieldName];\n const ids = dto[this.idFieldName];\n \n switch (command) {\n case RelationFieldsCommand.create:\n return await this.transformForCommandCreate(values, relatedEntityRepository);\n case RelationFieldsCommand.update:\n return await this.transformForCommandUpdate(values, relatedEntityRepository, dto, currentEntityRepository);\n case RelationFieldsCommand.delete:\n return await this.transformForCommandDelete(values, relatedEntityRepository);\n case RelationFieldsCommand.clear:\n return this.transformForCommandClear();\n case RelationFieldsCommand.set:\n return await this.transformForCommandSet(ids, relatedEntityRepository);\n case RelationFieldsCommand.link:\n return await this.tranformForCommandLink(ids, relatedEntityRepository);\n case RelationFieldsCommand.unlink:\n return await this.transformForCommandUnLink(ids);\n default:\n this.logger.log(`Invalid command ${command}`);\n return null;\n }\n }\n\n private transformForCommandClear() {\n return []\n }\n\n private async transformForCommandSet(ids: any[], relatedEntityRepository: any): Promise<any[]> {\n const loadedEntities: any[] = await relatedEntityRepository.find({\n where : {id: In(ids) }\n })\n if (loadedEntities.length !== ids.length) {\n throw new Error('Invalid entity ids provided for set');\n }\n\n return loadedEntities;\n }\n\n private async tranformForCommandLink(ids: any[], relatedEntityRepository: any): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Link command is only supported for update operations');\n }\n\n const count: number = await relatedEntityRepository.count({\n where: { id: In(ids) }\n });\n if (count !== ids.length) {\n throw new Error('Invalid entity ids provided for linking');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .add(ids);\n\n return undefined;\n }\n\n private async transformForCommandUnLink(ids: any[]): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Unlink command is only supported for update operations');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .remove(ids);\n\n return undefined;\n }\n\n private async transformForCommandCreate(values: any[], relatedEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n return transformedRelatedFields;\n }\n\n private async transformForCommandUpdate(values: any[], relatedEntityRepository: any, dto: any, currentEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n if (entity.id) {\n const transformed = await relatedEntityRepository.preload(entity);\n transformedRelatedFields.push(transformed);\n }\n else {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n }\n\n return transformedRelatedFields;\n }\n\n private async transformForCommandDelete(values: any[], relatedEntityRepository: any): Promise<any[]> {\n // Map and get the ids from the values\n const ids = values.map((value) => value.id);\n\n // Delete the ids linked to the associated entity\n await relatedEntityRepository.delete(ids);\n return null\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n\n // TODO: We have moved this to SolidRegistry service, refactor to use that service.\n // Returns the entity target class from the entity name\n // private getEntityTarget(entityName: string): any { //TODO Can be refactored to use this function from crud helper service\n // const entityMetadatas = this.options.entityManager.connection.entityMetadatas;\n // const entityMetadata = entityMetadatas.find(em => em.name === entityName);\n // return entityMetadata.target;\n // }\n}"]}
|
|
1
|
+
{"version":3,"file":"OneToManyRelationFieldCrudManager.js","sourceRoot":"","sources":["../../../src/helpers/field-crud-managers/OneToManyRelationFieldCrudManager.ts"],"names":[],"mappings":";;;AAAA,oDAA4C;AAC5C,2CAAwC;AACxC,qDAAqE;AACrE,oFAA2E;AAE3E,qCAA4C;AAc5C,MAAM,YAAY,GAAG,CAAC,iDAAqB,CAAC,IAAI,EAAE,iDAAqB,CAAC,MAAM,EAAE,iDAAqB,CAAC,GAAG,CAAC,CAAC;AAG3G,MAAa,iCAAiC;IAO1C,YAA6B,OAAsC;QAAtC,YAAO,GAAP,OAAO,CAA+B;QANlD,WAAM,GAAG,IAAI,eAAM,CAAC,iCAAiC,CAAC,IAAI,CAAC,CAAC;QAOzE,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,2BAA2B,GAAG,CAAC;QACrH,IAAI,CAAC,WAAW,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,IAAI,IAAI,CAAC,OAAO,CAAC,2BAA2B,KAAK,CAAC;QACpH,IAAI,CAAC,gBAAgB,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,+BAA+B,IAAI,IAAI,CAAC,OAAO,CAAC,2BAA2B,SAAS,CAAC;IACjI,CAAC;IAED,QAAQ,CAAC,GAAQ;QACb,OAAO,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACtC,CAAC;IAEO,gBAAgB,CAAC,GAAQ;QAC7B,MAAM,MAAM,GAAsB,EAAE,CAAC;QAErC,MAAM,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,CAAC;QAC/C,MAAM,YAAY,GAAG,GAA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AC,IAAI,CAAC,WAAW,CAAC,CAAC;YAC/C,CAAC;QACL,CAAC;QAED,OAAO,wBAAwB,CAAC;IACpC,CAAC;IAEO,KAAK,CAAC,yBAAyB,CAAC,MAAa,EAAE,uBAA4B;QAE/E,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAG5C,MAAM,uBAAuB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAA;IACf,CAAC;IAEO,yBAAyB;QAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;IACjC,CAAC;CASJ;AAlMD,8EAkMC","sourcesContent":["import { classify } from '../string.helper';\nimport { Logger } from \"@nestjs/common\";\nimport { isEmpty, isEnum, isInt, isNotEmpty } from \"class-validator\";\nimport { RelationFieldsCommand } from \"src/dtos/create-field-metadata.dto\";\nimport { FieldCrudManager, ValidationError } from \"src/interfaces\";\nimport { EntityManager, In } from \"typeorm\";\n\nexport interface OneToManyRelationFieldOptions {\n // Add options for relation field\n required: boolean | undefined | null;\n relationCoModelSingularName: string | undefined | null;\n inverseRelationCoModelFieldName: string | undefined | null;\n modelSingularName: string | undefined | null;\n inverseFieldName: string | undefined | null;\n entityManager: EntityManager | undefined | null;\n isUpdate?: boolean;\n entityId?: number;\n}\n\nconst linkCommands = [RelationFieldsCommand.link, RelationFieldsCommand.unlink, RelationFieldsCommand.set];\n\n// This implementation is meant to be used for many-to-one relation field\nexport class OneToManyRelationFieldCrudManager implements FieldCrudManager {\n private readonly logger = new Logger(OneToManyRelationFieldCrudManager.name);\n\n private readonly valueFieldName: string;\n private readonly idFieldName: string;\n private readonly commandFieldName: string;\n\n constructor(private readonly options: OneToManyRelationFieldOptions) {\n this.valueFieldName = this.options.inverseRelationCoModelFieldName ?? `${this.options.relationCoModelSingularName}s`;\n this.idFieldName = `${this.options.inverseRelationCoModelFieldName ?? this.options.relationCoModelSingularName}Ids`;\n this.commandFieldName = `${this.options.inverseRelationCoModelFieldName ?? this.options.relationCoModelSingularName}Command`;\n }\n\n validate(dto: any) {\n return this.applyValidations(dto);\n }\n\n private applyValidations(dto: any): ValidationError[] { \n const errors: ValidationError[] = [];\n\n const commandFieldName = this.commandFieldName;\n const commandValue = dto[commandFieldName];\n this.isApplyRequiredValidation() && isEmpty(commandValue) ? errors.push({ field: commandFieldName, error: 'Command field is required' }) : \"no errors\";\n if (isNotEmpty(commandValue)) {\n !isEnum(commandValue, RelationFieldsCommand)? errors.push({ field: this.options.inverseFieldName, error: 'Command Field has invalid value' }) : \"no errors\";\n } \n \n var fieldValue = null\n if (commandValue === RelationFieldsCommand.clear) {\n return errors;\n } else if (linkCommands.includes(commandValue)) {\n fieldValue = dto[this.idFieldName];\n } \n else {\n fieldValue = dto[this.valueFieldName];\n }\n\n this.isApplyRequiredValidation() && isEmpty(fieldValue) ? errors.push({ field: this.options.inverseFieldName, error: `Field: ${this.options.inverseFieldName} is required` }) : \"no errors\";\n if (isNotEmpty(fieldValue)) {\n errors.push(...this.applyFormatValidations(fieldValue, commandValue, commandFieldName));\n }\n return errors;\n }\n\n private applyFormatValidations(fieldValue: any[], commandValue: RelationFieldsCommand, commandFieldName: string): ValidationError[] {\n const errors: ValidationError[] = [];\n if (linkCommands.includes(commandValue)) {\n for (const id of fieldValue) {\n if (!isInt(id)) {\n errors.push({ field: this.options.inverseFieldName, error: `Invalid ids in ${commandFieldName}` });\n }\n }\n }\n return errors;\n }\n\n async transformForCreate(dto: any): Promise<any> {\n const currentEntityName = classify(this.options.modelSingularName);\n const currentEntityRepository = this.options.entityManager.getRepository(currentEntityName);\n\n const relatedEntityName = classify(this.options.relationCoModelSingularName);\n const relatedEntityRepository = this.options.entityManager.getRepository(relatedEntityName);\n\n const result = await this.transformByCommand(dto, relatedEntityRepository, currentEntityRepository);\n if (result !== undefined) {\n dto[this.valueFieldName] = result;\n } else {\n delete dto[this.valueFieldName];\n }\n return dto;\n }\n\n private async transformByCommand(dto: any, relatedEntityRepository: any, currentEntityRepository: any): Promise<any[] | undefined> {\n // TODO : Need to add support for the multiple commands\n const command = dto[this.commandFieldName];\n const values = dto[this.valueFieldName];\n const ids = dto[this.idFieldName];\n \n switch (command) {\n case RelationFieldsCommand.create:\n return await this.transformForCommandCreate(values, relatedEntityRepository);\n case RelationFieldsCommand.update:\n return await this.transformForCommandUpdate(values, relatedEntityRepository, dto, currentEntityRepository);\n case RelationFieldsCommand.delete:\n return await this.transformForCommandDelete(values, relatedEntityRepository);\n case RelationFieldsCommand.clear:\n return this.transformForCommandClear();\n case RelationFieldsCommand.set:\n return await this.transformForCommandSet(ids, relatedEntityRepository);\n case RelationFieldsCommand.link:\n return await this.tranformForCommandLink(ids, relatedEntityRepository);\n case RelationFieldsCommand.unlink:\n return await this.transformForCommandUnLink(ids);\n default:\n this.logger.log(`Invalid command ${command}`);\n return null;\n }\n }\n\n private transformForCommandClear() {\n return []\n }\n\n private async transformForCommandSet(ids: any[], relatedEntityRepository: any): Promise<any[]> {\n const loadedEntities: any[] = await relatedEntityRepository.find({\n where : {id: In(ids) }\n })\n if (loadedEntities.length !== ids.length) {\n throw new Error('Invalid entity ids provided for set');\n }\n\n return loadedEntities;\n }\n\n private async tranformForCommandLink(ids: any[], relatedEntityRepository: any): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Link command is only supported for update operations');\n }\n\n const count: number = await relatedEntityRepository.count({\n where: { id: In(ids) }\n });\n if (count !== ids.length) {\n throw new Error('Invalid entity ids provided for linking');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .add(ids);\n\n return undefined;\n }\n\n private async transformForCommandUnLink(ids: any[]): Promise<undefined> {\n if (!this.options.isUpdate) {\n throw new Error('Unlink command is only supported for update operations');\n }\n\n await this.options.entityManager\n .createQueryBuilder()\n .relation(classify(this.options.modelSingularName), this.valueFieldName)\n .of(this.options.entityId)\n .remove(ids);\n\n return undefined;\n }\n\n private async transformForCommandCreate(values: any[], relatedEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n return transformedRelatedFields;\n }\n\n private async transformForCommandUpdate(values: any[], relatedEntityRepository: any, dto: any, currentEntityRepository: any): Promise<any[]> {\n const transformedRelatedFields = [];\n for (const entity of values) {\n if (entity.id) {\n const transformed = await relatedEntityRepository.preload(entity);\n transformedRelatedFields.push(transformed);\n }\n else {\n const transformed = relatedEntityRepository.create(entity);\n transformedRelatedFields.push(transformed);\n }\n }\n\n return transformedRelatedFields;\n }\n\n private async transformForCommandDelete(values: any[], relatedEntityRepository: any): Promise<any[]> {\n // Map and get the ids from the values\n const ids = values.map((value) => value.id);\n\n // Delete the ids linked to the associated entity\n await relatedEntityRepository.delete(ids);\n return null\n }\n\n private isApplyRequiredValidation(): boolean {\n return this.options.required;\n }\n\n // TODO: We have moved this to SolidRegistry service, refactor to use that service.\n // Returns the entity target class from the entity name\n // private getEntityTarget(entityName: string): any { //TODO Can be refactored to use this function from crud helper service\n // const entityMetadatas = this.options.entityManager.connection.entityMetadatas;\n // const entityMetadata = entityMetadatas.find(em => em.name === entityName);\n // return entityMetadata.target;\n // }\n}"]}
|
|
@@ -44,7 +44,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
44
44
|
var ModuleMetadataHelperService_1;
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
46
|
exports.ModuleMetadataHelperService = void 0;
|
|
47
|
-
const
|
|
47
|
+
const lodash_1 = require("lodash");
|
|
48
48
|
const common_1 = require("@nestjs/common");
|
|
49
49
|
const fs = __importStar(require("fs/promises"));
|
|
50
50
|
const path = __importStar(require("path"));
|
|
@@ -78,7 +78,7 @@ let ModuleMetadataHelperService = ModuleMetadataHelperService_1 = class ModuleMe
|
|
|
78
78
|
if (!moduleName) {
|
|
79
79
|
return '';
|
|
80
80
|
}
|
|
81
|
-
const dashModuleName = (0,
|
|
81
|
+
const dashModuleName = (0, lodash_1.kebabCase)(moduleName);
|
|
82
82
|
const folderPath = path.resolve(process.cwd(), 'module-metadata', dashModuleName);
|
|
83
83
|
const filePath = path.join(folderPath, `${dashModuleName}-metadata.json`);
|
|
84
84
|
const fileExists = await this.fileService.exists(filePath);
|
|
@@ -95,7 +95,7 @@ let ModuleMetadataHelperService = ModuleMetadataHelperService_1 = class ModuleMe
|
|
|
95
95
|
if (!moduleName) {
|
|
96
96
|
return '';
|
|
97
97
|
}
|
|
98
|
-
const dashModuleName = (0,
|
|
98
|
+
const dashModuleName = (0, lodash_1.kebabCase)(moduleName);
|
|
99
99
|
const folderPath = path.resolve(process.cwd(), 'module-metadata', dashModuleName);
|
|
100
100
|
const exists = await this.fileService.exists(folderPath);
|
|
101
101
|
return exists ? folderPath : '';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module-metadata-helper.service.js","sourceRoot":"","sources":["../../src/helpers/module-metadata-helper.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"module-metadata-helper.service.js","sourceRoot":"","sources":["../../src/helpers/module-metadata-helper.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAAmC;AACnC,2CAAoD;AACpD,gDAAkC;AAClC,2CAA6B;AAC7B,4CAAuD;AACvD,2CAAoD;AAG7C,IAAM,2BAA2B,mCAAjC,MAAM,2BAA2B;IAEpC,YAA6B,WAA4B;QAA5B,gBAAW,GAAX,WAAW,CAAiB;QADxC,WAAM,GAAG,IAAI,eAAM,CAAC,6BAA2B,CAAC,IAAI,CAAC,CAAC;IACV,CAAC;IAO9D,KAAK,CAAC,8BAA8B,CAAC,cAAsB;QACvD,IAAI,CAAC,cAA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WAAW,EAAE,GAAG,cAAc,gBAAgB,CAAC,CAAC;gBACnH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,kBAAkB,YAAY,EAAE,CAAC,CAAC;gBACpD,OAAO,YAAY,CAAC;YACxB,CAAC;QACL,CAAC;QACD,OAAO,QAAQ,CAAC;IACpB,CAAC;IACD,KAAK,CAAC,2BAA2B,CAAC,UAAkB;QAChD,IAAI,CAAC,UAAU,EAAE,CAAC;YACd,OAAO,EAAE,CAAC;QACd,CAAC;QAED,MAAM,cAAc,GAAG,IAAA,kBAAS,EAAC,UAAU,CAAC,CAAC;QAE7C,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAC3B,OAAO,CAAC,GAAG,EAAE,EACb,iBAAiB,EACjB,cAAc,CACjB,CAAC;QAEF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACzD,OAAO,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;IACpC,CAAC;CAEJ,CAAA;AApEY,kEAA2B;sCAA3B,2BAA2B;IADvC,IAAA,mBAAU,GAAE;qCAGiC,sBAAe;GAFhD,2BAA2B,CAoEvC","sourcesContent":["import { kebabCase } from 'lodash';\nimport { Injectable, Logger } from \"@nestjs/common\";\nimport * as fs from 'fs/promises'; // Use the Promise-based version of fs for async/await\nimport * as path from 'path'; // To handle file paths\nimport { SOLID_CORE_MODULE_NAME } from \"src/constants\";\nimport { DiskFileService } from \"src/services/file\";\n\n@Injectable()\nexport class ModuleMetadataHelperService {\n private readonly logger = new Logger(ModuleMetadataHelperService.name);\n constructor(private readonly fileService: DiskFileService) { }\n // async getModuleMetadataConfig(moduleName: string): Promise<ModuleMetadata> {\n // const filePath = this.getModuleMetadataFilePath(moduleName);\n // const metadata = await this.getModuleMetadata(filePath);\n // return metadata;\n // }\n\n async getModuleMetadataConfiguration(configFilePath: string): Promise<any> {\n if (!configFilePath) {\n return null;\n }\n try {\n const fileContent = await fs.readFile(configFilePath, 'utf8');\n return JSON.parse(fileContent);\n }\n catch (error) {\n this.logger.error(`module metadata configuration non existent at: ${configFilePath}`);\n }\n\n return null;\n }\n\n async getModulePath(moduleName: string): Promise<string> {\n if (!moduleName) {\n return '';\n }\n return path.resolve(process.cwd(), 'src', moduleName);\n }\n\n async getModuleMetadataFilePath(moduleName: string): Promise<string> {\n if (!moduleName) {\n return '';\n }\n const dashModuleName = kebabCase(moduleName);\n const folderPath = path.resolve(process.cwd(), 'module-metadata', dashModuleName);\n const filePath = path.join(folderPath, `${dashModuleName}-metadata.json`);\n // Check if filePath exists\n const fileExists = await this.fileService.exists(filePath);\n // this.logger.debug(`File exists: ${fileExists} at ${filePath}`);\n if (!fileExists) {\n // If the module is solid-core, try the fallback path, in case the current root directory is the solid core project\n if (dashModuleName === SOLID_CORE_MODULE_NAME) {\n const fallbackPath = path.resolve(process.cwd(), 'src', 'seeders', 'seed-data', `${dashModuleName}-metadata.json`);\n this.logger.debug(`Fallback path: ${fallbackPath}`);\n return fallbackPath;\n }\n }\n return filePath;\n }\n async getModuleMetadataFolderPath(moduleName: string): Promise<string> {\n if (!moduleName) {\n return '';\n }\n\n const dashModuleName = kebabCase(moduleName);\n\n const folderPath = path.resolve(\n process.cwd(),\n 'module-metadata',\n dashModuleName,\n );\n\n const exists = await this.fileService.exists(folderPath);\n return exists ? folderPath : '';\n }\n\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.helper.d.ts","sourceRoot":"","sources":["../../src/helpers/string.helper.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,MAGxC,CAAA"}
|
|
1
|
+
{"version":3,"file":"string.helper.d.ts","sourceRoot":"","sources":["../../src/helpers/string.helper.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,MAGxC,CAAA;AAGD,eAAO,MAAM,QAAQ,MAAO,MAAM,KAAG,MAAkC,CAAC"}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.lowerFirst = void 0;
|
|
3
|
+
exports.classify = exports.lowerFirst = void 0;
|
|
4
4
|
const lowerFirst = (str) => {
|
|
5
5
|
if (!str)
|
|
6
6
|
return str;
|
|
7
7
|
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
8
8
|
};
|
|
9
9
|
exports.lowerFirst = lowerFirst;
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
11
|
+
const classify = (s) => (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(s));
|
|
12
|
+
exports.classify = classify;
|
|
10
13
|
//# sourceMappingURL=string.helper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.helper.js","sourceRoot":"","sources":["../../src/helpers/string.helper.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CAAC,GAAW,EAAU,EAAE;IAC9C,IAAI,CAAC,GAAG;QAAE,OAAO,GAAG,CAAC;IACrB,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC,CAAA;AAHY,QAAA,UAAU,cAGtB","sourcesContent":["export const lowerFirst = (str: string): string => {\n if (!str) return str;\n return str.charAt(0).toLowerCase() + str.slice(1);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"string.helper.js","sourceRoot":"","sources":["../../src/helpers/string.helper.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CAAC,GAAW,EAAU,EAAE;IAC9C,IAAI,CAAC,GAAG;QAAE,OAAO,GAAG,CAAC;IACrB,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC,CAAA;AAHY,QAAA,UAAU,cAGtB;AAED,mCAA+C;AACxC,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAU,EAAE,CAAC,IAAA,mBAAU,EAAC,IAAA,kBAAS,EAAC,CAAC,CAAC,CAAC,CAAC;AAA3D,QAAA,QAAQ,YAAmD","sourcesContent":["export const lowerFirst = (str: string): string => {\n if (!str) return str;\n return str.charAt(0).toLowerCase() + str.slice(1);\n}\n\nimport { upperFirst, camelCase } from 'lodash';\nexport const classify = (s: string): string => upperFirst(camelCase(s));\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatter-message-details.repository.d.ts","sourceRoot":"","sources":["../../src/repository/chatter-message-details.repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,kBAAkB,EACnB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"chatter-message-details.repository.d.ts","sourceRoot":"","sources":["../../src/repository/chatter-message-details.repository.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,WAAW,EACX,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAIjB,OAAO,EAAE,qBAAqB,EAAE,MAAM,6CAA6C,CAAC;AAEpF,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAI9D,qBACa,+BAAgC,SAAQ,mBAAmB,CAAC,qBAAqB,CAAC;IAE3F,QAAQ,CAAC,UAAU,EAAE,UAAU;IAC/B,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB;IACrD,QAAQ,CAAC,sBAAsB,EAAE,sBAAsB;gBAF9C,UAAU,EAAE,UAAU,EACtB,qBAAqB,EAAE,qBAAqB,EAC5C,sBAAsB,EAAE,sBAAsB;IAIvD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA4C;IAQlE,mCAAmC,CAChD,KAAK,SAAW,EAChB,WAAW,CAAC,EAAE,WAAW,GACxB,OAAO,CAAC,kBAAkB,CAAC,qBAAqB,CAAC,CAAC;IAqCrD,eAAe,CAAC,CAAC,EACf,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC,EACzB,WAAW,EAAE,MAAM,EACnB,YAAY,GAAE,MAAkB,GAC/B,kBAAkB,CAAC,CAAC,CAAC;IAqBxB,OAAO,CAAC,sBAAsB;CAW/B"}
|
|
@@ -12,12 +12,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.ChatterMessageDetailsRepository = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
14
|
const typeorm_1 = require("typeorm");
|
|
15
|
-
const
|
|
15
|
+
const lodash_1 = require("lodash");
|
|
16
|
+
const string_helper_1 = require("../helpers/string.helper");
|
|
16
17
|
const chatter_message_details_entity_1 = require("../entities/chatter-message-details.entity");
|
|
17
18
|
const request_context_service_1 = require("../services/request-context.service");
|
|
18
19
|
const security_rule_repository_1 = require("./security-rule.repository");
|
|
19
20
|
const solid_base_repository_1 = require("./solid-base.repository");
|
|
20
|
-
const
|
|
21
|
+
const lodash_2 = require("lodash");
|
|
21
22
|
let ChatterMessageDetailsRepository = class ChatterMessageDetailsRepository extends solid_base_repository_1.SolidBaseRepository {
|
|
22
23
|
constructor(dataSource, requestContextService, securityRuleRepository) {
|
|
23
24
|
super(chatter_message_details_entity_1.ChatterMessageDetails, dataSource, requestContextService, securityRuleRepository);
|
|
@@ -39,9 +40,9 @@ let ChatterMessageDetailsRepository = class ChatterMessageDetailsRepository exte
|
|
|
39
40
|
return this.securityRuleRepository.applySecurityRules(qb, coModelName, activeUser, coModelAlias);
|
|
40
41
|
}
|
|
41
42
|
leftJoinCoModel(qb, coModelName, messageAlias = 'message') {
|
|
42
|
-
const entityName = (0,
|
|
43
|
+
const entityName = (0, string_helper_1.classify)(coModelName);
|
|
43
44
|
const meta = this.dataSource.getMetadata(entityName);
|
|
44
|
-
const coAlias = (0,
|
|
45
|
+
const coAlias = (0, lodash_1.camelCase)(meta.name);
|
|
45
46
|
qb.leftJoin(entityName, coAlias, `"${coAlias}"."id" = "${messageAlias}"."co_model_entity_id" AND "${messageAlias}"."co_model_name" = :model`, { model: coModelName });
|
|
46
47
|
return qb;
|
|
47
48
|
}
|
|
@@ -49,10 +50,10 @@ let ChatterMessageDetailsRepository = class ChatterMessageDetailsRepository exte
|
|
|
49
50
|
const requestFilter = this.requestContextService.getRequestFilter();
|
|
50
51
|
if (!requestFilter)
|
|
51
52
|
return [undefined, undefined];
|
|
52
|
-
const coModelName = (0,
|
|
53
|
+
const coModelName = (0, lodash_2.get)(requestFilter, this.CO_MODEL_NAME_PATH);
|
|
53
54
|
if (!coModelName)
|
|
54
55
|
return [undefined, undefined];
|
|
55
|
-
const alias = (0,
|
|
56
|
+
const alias = (0, lodash_1.camelCase)(coModelName);
|
|
56
57
|
return [coModelName, alias];
|
|
57
58
|
}
|
|
58
59
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatter-message-details.repository.js","sourceRoot":"","sources":["../../src/repository/chatter-message-details.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,qCAIiB;AACjB,
|
|
1
|
+
{"version":3,"file":"chatter-message-details.repository.js","sourceRoot":"","sources":["../../src/repository/chatter-message-details.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,qCAIiB;AACjB,mCAA+C;AAC/C,4DAAoD;AAEpD,+FAAoF;AAEpF,iFAA6E;AAC7E,yEAAoE;AACpE,mEAA8D;AAC9D,mCAA0B;AAInB,IAAM,+BAA+B,GAArC,MAAM,+BAAgC,SAAQ,2CAA0C;IAC7F,YACW,UAAsB,EACtB,qBAA4C,EAC5C,sBAA8C;QAEvD,KAAK,CAAC,sDAAqB,EAAE,UAAU,EAAE,qBAAqB,EAAE,sBAAsB,CA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A4B,EAC3G,EAAE,KAAK,EAAE,WAAW,EAAE,CACvB,CAAC;QAEF,OAAO,EAAE,CAAC;IACZ,CAAC;IAGO,sBAAsB;QAC5B,MAAM,aAAa,GAAG,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,EAAE,CAAC;QACpE,IAAI,CAAC,aAAa;YAAE,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAElD,MAAM,WAAW,GAAG,IAAA,YAAG,EAAC,aAAa,EAAE,IAAI,CAAC,kBAAkB,CAAC,CAAC;QAChE,IAAI,CAAC,WAAW;YAAE,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAEhD,MAAM,KAAK,GAAG,IAAA,kBAAS,EAAC,WAAW,CAAC,CAAC;QACrC,OAAO,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;CAEF,CAAA;AA5FY,0EAA+B;0CAA/B,+BAA+B;IAD3C,IAAA,mBAAU,GAAE;qCAGY,oBAAU;QACC,+CAAqB;QACpB,iDAAsB;GAJ9C,+BAA+B,CA4F3C","sourcesContent":["import { Injectable } from '@nestjs/common';\nimport {\n DataSource,\n QueryRunner,\n SelectQueryBuilder,\n} from 'typeorm';\nimport { upperFirst, camelCase } from 'lodash';\nimport { classify } from '../helpers/string.helper';\n\nimport { ChatterMessageDetails } from 'src/entities/chatter-message-details.entity';\nimport { ActiveUserData } from 'src/interfaces/active-user-data.interface';\nimport { RequestContextService } from 'src/services/request-context.service';\nimport { SecurityRuleRepository } from './security-rule.repository';\nimport { SolidBaseRepository } from './solid-base.repository';\nimport {get} from \"lodash\"\n\n\n@Injectable()\nexport class ChatterMessageDetailsRepository extends SolidBaseRepository<ChatterMessageDetails> {\n constructor(\n readonly dataSource: DataSource,\n readonly requestContextService: RequestContextService,\n readonly securityRuleRepository: SecurityRuleRepository,\n ) {\n super(ChatterMessageDetails, dataSource, requestContextService, securityRuleRepository);\n }\n private readonly CO_MODEL_NAME_PATH = 'filters.chatterMessage.coModelName.$eq';\n\n /**\n * Build a security-aware QB:\n * - join the real relation to ChatterMessage (alias: \"message\")\n * - left join the polymorphic co-model table using message.co_model_* fields\n * - (optionally) apply security rules on the co-model alias\n */\n override async createSecurityRuleAwareQueryBuilder(\n alias = 'detail',\n queryRunner?: QueryRunner,\n ): Promise<SelectQueryBuilder<ChatterMessageDetails>> {\n const activeUser = this.requestContextService.getActiveUser();\n let qb = await super.createSecurityRuleAwareQueryBuilder(alias, queryRunner);\n if (!activeUser) return qb;\n\n // Example: join the \"client\" co-model (pass whatever co-model name you need)\n const [coModelName, coModelAlias] = this.getCoModelNameAndAlias();\n if (!coModelName) return qb;\n\n // Join the real relation so we can access co_model_* fields\n qb = qb.leftJoin(`${alias}.chatterMessage`, 'chatterMessage');\n qb = this.leftJoinCoModel(qb, coModelName, 'chatterMessage');\n\n // If your security rules should apply to the co-model rows, pass the co-model alias.\n // Here we use the co-model name \"client\" both as model key and alias base for consistency.\n return this.securityRuleRepository.applySecurityRules(\n qb,\n coModelName, // modelSingularName (or whatever your rules expect)\n activeUser as ActiveUserData,\n coModelAlias, // the alias we used inside leftJoinCoModel\n );\n }\n\n /**\n * Left-join the polymorphic co-model table, matching:\n * <coModelAlias>.id = <messageAlias>.co_model_entity_id\n * AND <messageAlias>.co_model_name = :model\n *\n * @param qb QB built on ChatterMessageDetails\n * @param coModelName e.g. \"client\" | \"invoice\" | ...\n * @param messageAlias alias used for joined ChatterMessage (default: \"message\")\n *\n * Notes:\n * - We resolve the entity metadata from the classified name (e.g., \"Client\"),\n * then use metadata.tablePath to get schema-qualified table.\n * - We build a stable alias from metadata.name (camelized).\n */\n leftJoinCoModel<T>(\n qb: SelectQueryBuilder<T>,\n coModelName: string,\n messageAlias: string = 'message',\n ): SelectQueryBuilder<T> {\n // Resolve entity metadata from your naming convention\n const entityName = classify(coModelName); // \"client\" -> \"Client\"\n const meta = this.dataSource.getMetadata(entityName); // throws if not registered\n // const table = meta.tablePath; // schema-qualified\n const coAlias = camelCase(meta.name); // stable alias, e.g., \"client\"\n\n // LEFT JOIN \"<schema>\".\"<table>\" \"<coAlias>\"\n // ON \"<coAlias>\".\"id\" = \"message\".\"co_model_entity_id\"\n // AND \"message\".\"co_model_name\" = :model\n qb.leftJoin(\n entityName,\n coAlias,\n `\"${coAlias}\".\"id\" = \"${messageAlias}\".\"co_model_entity_id\" AND \"${messageAlias}\".\"co_model_name\" = :model`,\n { model: coModelName },\n );\n\n return qb;\n }\n\n // This uses the requestContextService.getRequestFilter method and extracts the coModelName and creates the alias and returns the name and alias tuple\n private getCoModelNameAndAlias() {\n const requestFilter = this.requestContextService.getRequestFilter();\n if (!requestFilter) return [undefined, undefined];\n\n const coModelName = get(requestFilter, this.CO_MODEL_NAME_PATH);\n if (!coModelName) return [undefined, undefined];\n\n const alias = camelCase(coModelName);\n return [coModelName, alias];\n }\n\n}"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatter-message.repository.d.ts","sourceRoot":"","sources":["../../src/repository/chatter-message.repository.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"chatter-message.repository.d.ts","sourceRoot":"","sources":["../../src/repository/chatter-message.repository.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AAErE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAI9D,qBACa,wBAAyB,SAAQ,mBAAmB,CAAC,cAAc,CAAC;IAEzE,QAAQ,CAAC,UAAU,EAAE,UAAU;IAC/B,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB;IACrD,QAAQ,CAAC,sBAAsB,EAAE,sBAAsB;gBAF9C,UAAU,EAAE,UAAU,EACtB,qBAAqB,EAAE,qBAAqB,EAC5C,sBAAsB,EAAE,sBAAsB;IAK3D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA6B;IACjD,mCAAmC,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC;IAkB1I,OAAO,CAAC,eAAe;IAoBvB,OAAO,CAAC,sBAAsB;CAUjC"}
|
|
@@ -10,7 +10,6 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ChatterMessageRepository = void 0;
|
|
13
|
-
const strings_1 = require("@angular-devkit/core/src/utils/strings");
|
|
14
13
|
const common_1 = require("@nestjs/common");
|
|
15
14
|
const chatter_message_entity_1 = require("../entities/chatter-message.entity");
|
|
16
15
|
const request_context_service_1 = require("../services/request-context.service");
|
|
@@ -18,6 +17,7 @@ const typeorm_1 = require("typeorm");
|
|
|
18
17
|
const security_rule_repository_1 = require("./security-rule.repository");
|
|
19
18
|
const solid_base_repository_1 = require("./solid-base.repository");
|
|
20
19
|
const lodash_1 = require("lodash");
|
|
20
|
+
const string_helper_1 = require("../helpers/string.helper");
|
|
21
21
|
let ChatterMessageRepository = class ChatterMessageRepository extends solid_base_repository_1.SolidBaseRepository {
|
|
22
22
|
constructor(dataSource, requestContextService, securityRuleRepository) {
|
|
23
23
|
super(chatter_message_entity_1.ChatterMessage, dataSource, requestContextService, securityRuleRepository);
|
|
@@ -38,9 +38,9 @@ let ChatterMessageRepository = class ChatterMessageRepository extends solid_base
|
|
|
38
38
|
return this.securityRuleRepository.applySecurityRules(qb, coModelName, activeUserOrUndefined, coModelAlias);
|
|
39
39
|
}
|
|
40
40
|
leftJoinCoModel(qb, coModelName) {
|
|
41
|
-
const entityName = (0,
|
|
41
|
+
const entityName = (0, string_helper_1.classify)(coModelName);
|
|
42
42
|
const meta = this.dataSource.getMetadata(entityName);
|
|
43
|
-
const alias = (0,
|
|
43
|
+
const alias = (0, lodash_1.camelCase)(meta.name);
|
|
44
44
|
qb.leftJoin(entityName, alias, `${alias}.id = entity.co_model_entity_id AND entity.co_model_name = :model`, { model: coModelName });
|
|
45
45
|
return qb;
|
|
46
46
|
}
|
|
@@ -51,7 +51,7 @@ let ChatterMessageRepository = class ChatterMessageRepository extends solid_base
|
|
|
51
51
|
const coModelName = (0, lodash_1.get)(requestFilter, this.CO_MODEL_NAME_PATH);
|
|
52
52
|
if (!coModelName)
|
|
53
53
|
return [undefined, undefined];
|
|
54
|
-
const alias = (0,
|
|
54
|
+
const alias = (0, lodash_1.camelCase)(coModelName);
|
|
55
55
|
return [coModelName, alias];
|
|
56
56
|
}
|
|
57
57
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chatter-message.repository.js","sourceRoot":"","sources":["../../src/repository/chatter-message.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"chatter-message.repository.js","sourceRoot":"","sources":["../../src/repository/chatter-message.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+EAAqE;AAErE,iFAA6E;AAC7E,qCAAsE;AACtE,yEAAoE;AACpE,mEAA8D;AAC9D,mCAAwC;AACxC,4DAAoD;AAG7C,IAAM,wBAAwB,GAA9B,MAAM,wBAAyB,SAAQ,2CAAmC;IAC7E,YACa,UAAsB,EACtB,qBAA4C,EAC5C,sBAA8C;QAEvD,KAAK,CAAC,uCAAc,EAAE,UAAU,EAAE,qBAAqB,EAAE,sBAAsB,CAAC,CAAC;QAJxE,eAAU,GAAV,UAAU,CAAY;QACtB,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,2BAAsB,GAAtB,sBAAsB,CAAwB;QAK1C,uBAAkB,GAAG,yBAAyB,CAAC;IAFhE,CAAC;IAGQ,KAAK,CAAC,mCAA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C,WAAW;YAAE,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAChD,MAAM,KAAK,GAAG,IAAA,kBAAS,EAAC,WAAW,CAAC,CAAC;QACrC,OAAO,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;CAEJ,CAAA;AA1DY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;qCAGgB,oBAAU;QACC,+CAAqB;QACpB,iDAAsB;GAJlD,wBAAwB,CA0DpC","sourcesContent":["import { Injectable } from \"@nestjs/common\";\nimport { ChatterMessage } from \"src/entities/chatter-message.entity\";\nimport { ActiveUserData } from \"src/interfaces/active-user-data.interface\";\nimport { RequestContextService } from \"src/services/request-context.service\";\nimport { DataSource, QueryRunner, SelectQueryBuilder } from \"typeorm\";\nimport { SecurityRuleRepository } from \"./security-rule.repository\";\nimport { SolidBaseRepository } from \"./solid-base.repository\";\nimport { get, camelCase } from 'lodash';\nimport { classify } from '../helpers/string.helper';\n\n@Injectable()\nexport class ChatterMessageRepository extends SolidBaseRepository<ChatterMessage> {\n constructor(\n readonly dataSource: DataSource,\n readonly requestContextService: RequestContextService,\n readonly securityRuleRepository: SecurityRuleRepository,\n ) {\n super(ChatterMessage, dataSource, requestContextService, securityRuleRepository);\n }\n\n private readonly CO_MODEL_NAME_PATH = 'filters.coModelName.$eq';\n override async createSecurityRuleAwareQueryBuilder(alias?: string, queryRunner?: QueryRunner): Promise<SelectQueryBuilder<ChatterMessage>> {\n const activeUserOrUndefined = this.requestContextService.getActiveUser();\n let qb = await super.createSecurityRuleAwareQueryBuilder(alias, queryRunner);\n if (!activeUserOrUndefined) return qb;\n \n //Left join on the associated chatter model entity\n const [coModelName, coModelAlias] = this.getCoModelNameAndAlias();\n if (!coModelName) return qb;\n qb = this.leftJoinCoModel(qb, coModelName);\n \n return this.securityRuleRepository.applySecurityRules(\n qb,\n coModelName,\n activeUserOrUndefined as ActiveUserData,\n coModelAlias\n );\n }\n\n private leftJoinCoModel<ChatterMessage>(\n qb: SelectQueryBuilder<ChatterMessage>,\n coModelName: string\n ) {\n // const Target = resolveEntityFromCoModelName(coModelName); // your mapping\n const entityName = classify(coModelName)\n const meta = this.dataSource.getMetadata(entityName);\n const alias = camelCase(meta.name);\n qb.leftJoin(\n entityName,\n alias,\n // `${alias}.id = entity.co_model_entity_id`,\n `${alias}.id = entity.co_model_entity_id AND entity.co_model_name = :model`,\n { model: coModelName }\n );\n return qb;\n // return qb.leftJoin(Target, alias, `${alias}.id = entity.co_model_entity_id`);\n }\n\n // This uses the requestContextService.getRequestFilter method and extracts the coModelName and creates the alias and returns the name and alias tuple\n private getCoModelNameAndAlias() {\n const requestFilter = this.requestContextService.getRequestFilter();\n if (!requestFilter) return [undefined, undefined];\n\n const coModelName = get(requestFilter, this.CO_MODEL_NAME_PATH);\n if (!coModelName) return [undefined, undefined];\n const alias = camelCase(coModelName);\n return [coModelName, alias];\n }\n\n}"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SolidBaseRepository = void 0;
|
|
4
|
-
const
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const model_metadata_entity_1 = require("../entities/model-metadata.entity");
|
|
7
7
|
const typeorm_1 = require("typeorm");
|
|
@@ -13,7 +13,7 @@ class SolidBaseRepository extends typeorm_1.Repository {
|
|
|
13
13
|
this.logger = new common_1.Logger(this.constructor.name);
|
|
14
14
|
}
|
|
15
15
|
modelSingularName() {
|
|
16
|
-
return (0,
|
|
16
|
+
return (0, lodash_1.camelCase)(this.metadata.name);
|
|
17
17
|
}
|
|
18
18
|
async findOneByUserKey(userKeyValue) {
|
|
19
19
|
const modelSingularName = this.modelSingularName();
|