@autofleet/sadot 1.2.16-beta-e2ec42dc.0 → 1.2.16-beta-ce490ac0.0

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.
@@ -1,2 +1,2 @@
1
- const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../utils/logger/index.cjs`),n=require(`../utils/constants/index.cjs`),r=require(`../utils/validations/validators/index.cjs`),i=require(`../events/index.cjs`),a=require(`../errors/index.cjs`),o=require(`../utils/validations/index.cjs`),s=require(`../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs`),c=require(`../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs`),l=require(`./CustomFieldValue.cjs`);let u=require(`sequelize-typescript`);var d,f,p,m,h,g;let _=class extends u.Model{get modelTypeIds(){return this._modelTypeIds}set modelTypeIds(e){this._modelTypeIds=e,this.setDataValue(`modelTypeIds`,e)}static displayNameDefaultValue(e){if(e?.displayName||(e.displayName=e.name),![null,void 0].includes(e.defaultValue)){let t=o.validateValue(e.defaultValue,e.fieldType,e.validation);if(t.error)throw new a.InvalidValueError(e.defaultValue,e.name,t.error)}}static afterSaveHandler(e,t){t.transaction?t.transaction.afterCommit(()=>i.sendDimEvent(e)):i.sendDimEvent(e)}toJSON(){let e={...this.get()};return delete e.modelTypeMappings,e}};c.__decorate([u.PrimaryKey,(0,u.Column)({type:u.DataType.UUID,defaultValue:u.DataType.UUIDV4,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`id`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`name`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING}),s.__decorateMetadata(`design:type`,String)],_.prototype,`displayName`,void 0),c.__decorate([(0,u.Is)(`SupportedType`,e=>{if(!Object.values(n.CustomFieldDefinitionType).includes(e))throw new a.UnsupportedCustomFieldTypeError(`"${e}" is not a supported type.`)}),(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,typeof(d=n.CustomFieldDefinitionType!==void 0&&n.CustomFieldDefinitionType)==`function`?d:Object)],_.prototype,`fieldType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.JSONB}),s.__decorateMetadata(`design:type`,Object)],_.prototype,`validation`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.UUID,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`entityId`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`entityType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`modelType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.TEXT}),s.__decorateMetadata(`design:type`,String)],_.prototype,`description`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`required`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`disabled`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.JSONB,allowNull:!0}),s.__decorateMetadata(`design:type`,Object)],_.prototype,`defaultValue`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!1}),s.__decorateMetadata(`design:type`,typeof(f=typeof Date<`u`&&Date)==`function`?f:Object)],_.prototype,`createdAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!0}),s.__decorateMetadata(`design:type`,typeof(p=typeof Date<`u`&&Date)==`function`?p:Object)],_.prototype,`updatedAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!0}),s.__decorateMetadata(`design:type`,typeof(m=typeof Date<`u`&&Date)==`function`?m:Object)],_.prototype,`deletedAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1,allowNull:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`blockEditingFromUI`,void 0),c.__decorate([(0,u.HasMany)(()=>l.default),s.__decorateMetadata(`design:type`,Array)],_.prototype,`values`,void 0),c.__decorate([u.BeforeCreate,s.__decorateMetadata(`design:type`,Function),s.__decorateMetadata(`design:paramtypes`,[typeof(h=_!==void 0&&_)==`function`?h:Object]),s.__decorateMetadata(`design:returntype`,void 0)],_,`displayNameDefaultValue`,null),c.__decorate([u.AfterSave,s.__decorateMetadata(`design:type`,Function),s.__decorateMetadata(`design:paramtypes`,[typeof(g=_!==void 0&&_)==`function`?g:Object,Object]),s.__decorateMetadata(`design:returntype`,void 0)],_,`afterSaveHandler`,null),_=c.__decorate([(0,u.DefaultScope)(()=>({where:{disabled:!1}})),(0,u.Table)({tableName:`custom_field_definitions`,modelName:`CustomFieldDefinition`,indexes:[{name:`unique_name_model_type`,fields:[`model_type`,`entity_id`,`name`],unique:!0}],timestamps:!0,validate:{validationByType(){if(!this.validation&&r.CustomValidationTypes[this.fieldType])throw t.default.error(`No custom validation for custom field type ${this.fieldType} found`),new a.UnsupportedCustomValidationError(`Validation provided for "${this.fieldType}" is not supported`)}}})],_);var v=_;exports.default=v;
1
+ const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../utils/logger/index.cjs`),n=require(`../utils/constants/index.cjs`),r=require(`../utils/validations/validators/index.cjs`),i=require(`../events/index.cjs`),a=require(`../errors/index.cjs`),o=require(`../utils/validations/index.cjs`),s=require(`../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.cjs`),c=require(`../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.cjs`),l=require(`./CustomFieldValue.cjs`);let u=require(`sequelize-typescript`);var d,f,p,m,h,g;let _=class extends u.Model{get modelTypeIds(){return this._modelTypeIds}set modelTypeIds(e){this._modelTypeIds=e,this.setDataValue(`modelTypeIds`,e)}static displayNameDefaultValue(e){if(e?.displayName||(e.displayName=e.name),![null,void 0].includes(e.defaultValue)){let t=o.validateValue(e.defaultValue,e.fieldType,e.validation);if(t.error)throw new a.InvalidValueError(e.defaultValue,e.name,t.error)}}static afterSaveHandler(e,t){t.transaction?t.transaction.afterCommit(()=>i.sendDimEvent(e)):i.sendDimEvent(e)}toJSON(){let{modelTypeMappings:e,...t}=this.get();return t}};c.__decorate([u.PrimaryKey,(0,u.Column)({type:u.DataType.UUID,defaultValue:u.DataType.UUIDV4,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`id`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`name`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING}),s.__decorateMetadata(`design:type`,String)],_.prototype,`displayName`,void 0),c.__decorate([(0,u.Is)(`SupportedType`,e=>{if(!Object.values(n.CustomFieldDefinitionType).includes(e))throw new a.UnsupportedCustomFieldTypeError(`"${e}" is not a supported type.`)}),(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,typeof(d=n.CustomFieldDefinitionType!==void 0&&n.CustomFieldDefinitionType)==`function`?d:Object)],_.prototype,`fieldType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.JSONB}),s.__decorateMetadata(`design:type`,Object)],_.prototype,`validation`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.UUID,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`entityId`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`entityType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.STRING,allowNull:!1}),s.__decorateMetadata(`design:type`,String)],_.prototype,`modelType`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.TEXT}),s.__decorateMetadata(`design:type`,String)],_.prototype,`description`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`required`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`disabled`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.JSONB,allowNull:!0}),s.__decorateMetadata(`design:type`,Object)],_.prototype,`defaultValue`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!1}),s.__decorateMetadata(`design:type`,typeof(f=typeof Date<`u`&&Date)==`function`?f:Object)],_.prototype,`createdAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!0}),s.__decorateMetadata(`design:type`,typeof(p=typeof Date<`u`&&Date)==`function`?p:Object)],_.prototype,`updatedAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.DATE,allowNull:!0}),s.__decorateMetadata(`design:type`,typeof(m=typeof Date<`u`&&Date)==`function`?m:Object)],_.prototype,`deletedAt`,void 0),c.__decorate([(0,u.Column)({type:u.DataType.BOOLEAN,defaultValue:!1,allowNull:!1}),s.__decorateMetadata(`design:type`,Boolean)],_.prototype,`blockEditingFromUI`,void 0),c.__decorate([(0,u.HasMany)(()=>l.default),s.__decorateMetadata(`design:type`,Array)],_.prototype,`values`,void 0),c.__decorate([u.BeforeCreate,s.__decorateMetadata(`design:type`,Function),s.__decorateMetadata(`design:paramtypes`,[typeof(h=_!==void 0&&_)==`function`?h:Object]),s.__decorateMetadata(`design:returntype`,void 0)],_,`displayNameDefaultValue`,null),c.__decorate([u.AfterSave,s.__decorateMetadata(`design:type`,Function),s.__decorateMetadata(`design:paramtypes`,[typeof(g=_!==void 0&&_)==`function`?g:Object,Object]),s.__decorateMetadata(`design:returntype`,void 0)],_,`afterSaveHandler`,null),_=c.__decorate([(0,u.DefaultScope)(()=>({where:{disabled:!1}})),(0,u.Table)({tableName:`custom_field_definitions`,modelName:`CustomFieldDefinition`,indexes:[{name:`unique_name_model_type`,fields:[`model_type`,`entity_id`,`name`],unique:!0}],timestamps:!0,validate:{validationByType(){if(!this.validation&&r.CustomValidationTypes[this.fieldType])throw t.default.error(`No custom validation for custom field type ${this.fieldType} found`),new a.UnsupportedCustomValidationError(`Validation provided for "${this.fieldType}" is not supported`)}}})],_);var v=_;exports.default=v;
2
2
  //# sourceMappingURL=CustomFieldDefinition.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CustomFieldDefinition.cjs","names":["CustomFieldDefinition","Model","validateValue","InvalidValueError","sendDimEvent","PrimaryKey","DataType","CustomFieldDefinitionType","UnsupportedCustomFieldTypeError","CustomFieldValue","BeforeCreate","AfterSave","CustomValidationTypes","UnsupportedCustomValidationError"],"sources":["../../src/models/CustomFieldDefinition.ts"],"sourcesContent":["import {\n Table,\n Column,\n Model,\n DataType,\n HasMany,\n PrimaryKey,\n BeforeCreate,\n DefaultScope,\n AfterSave,\n Is,\n} from 'sequelize-typescript';\nimport type { CreateOptions } from 'sequelize';\nimport { CustomFieldDefinitionType } from '../utils/constants';\nimport { CustomValidationTypes } from '../utils/validations/validators';\nimport CustomFieldValue from './CustomFieldValue';\nimport CustomFieldModelTypeMap from './CustomFieldModelTypeMap';\nimport { sendDimEvent } from '../events';\nimport { InvalidValueError, UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';\nimport logger from '../utils/logger';\nimport { validateValue } from '../utils/validations';\n\n@DefaultScope(() => ({ where: { disabled: false } }))\n@Table({\n tableName: 'custom_field_definitions',\n modelName: 'CustomFieldDefinition',\n indexes: [\n {\n name: 'unique_name_model_type',\n fields: ['model_type', 'entity_id', 'name'],\n unique: true,\n },\n ],\n timestamps: true,\n validate: {\n validationByType(this: CustomFieldDefinition) {\n // Verify the validation is provided if needed\n if (!this.validation && CustomValidationTypes[this.fieldType as keyof typeof CustomValidationTypes]) {\n // TODO: enforce the validation-object schema based on the fieldType\n logger.error(`No custom validation for custom field type ${this.fieldType} found`);\n throw new UnsupportedCustomValidationError(`Validation provided for \"${this.fieldType}\" is not supported`);\n }\n },\n },\n})\nclass CustomFieldDefinition extends Model {\n @PrimaryKey\n @Column({\n type: DataType.UUID,\n defaultValue: DataType.UUIDV4,\n allowNull: false,\n })\n declare id: string;\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n name!: string;\n\n @Column({\n type: DataType.STRING,\n })\n displayName?: string; // Defaulted to name with beforeCreate hook\n\n @Is('SupportedType', (value) => {\n if (!Object.values(CustomFieldDefinitionType).includes(value as CustomFieldDefinitionType)) {\n throw new UnsupportedCustomFieldTypeError(`\"${value}\" is not a supported type.`);\n }\n })\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n fieldType!: CustomFieldDefinitionType;\n\n @Column({\n type: DataType.JSONB,\n })\n validation?: any;\n\n @Column({\n type: DataType.UUID,\n allowNull: false,\n })\n entityId!: string; /** Client association entity id */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n entityType!: string; /** Client association entity type (demand source / fleet / etc.) */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n modelType!: string; /** Model type. e.g. Vehicle / StopPoint / etc. */\n\n @Column({\n type: DataType.TEXT,\n })\n description?: string;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n required?: boolean;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n disabled?: boolean;\n\n @Column({\n type: DataType.JSONB,\n allowNull: true,\n })\n defaultValue?: any;\n\n @Column({\n type: DataType.DATE,\n allowNull: false,\n })\n declare createdAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare updatedAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare deletedAt?: Date;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n allowNull: false,\n })\n blockEditingFromUI!: boolean;\n\n @HasMany(() => CustomFieldValue)\n declare values: CustomFieldValue[];\n\n // Association added programmatically when useModelTypeMapping is enabled\n declare modelTypeMappings?: CustomFieldModelTypeMap[];\n\n // Virtual property for modelTypeIds - set manually after fetch\n private _modelTypeIds?: string[];\n\n get modelTypeIds(): string[] | undefined {\n return this._modelTypeIds;\n }\n\n set modelTypeIds(value: string[] | undefined) {\n this._modelTypeIds = value;\n this.setDataValue('modelTypeIds', value);\n }\n\n @BeforeCreate\n static displayNameDefaultValue(instance: CustomFieldDefinition): void {\n if (!instance?.displayName) {\n instance.displayName = instance.name;\n }\n if (![null, undefined].includes(instance.defaultValue)) {\n const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);\n if (isValid.error) {\n throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);\n }\n }\n }\n\n @AfterSave\n static afterSaveHandler(instance: CustomFieldDefinition, options: CreateOptions): void {\n if (options.transaction) {\n options.transaction.afterCommit(() => sendDimEvent(instance));\n } else {\n sendDimEvent(instance);\n }\n }\n\n /**\n * Override toJSON to exclude internal associations from API responses\n * The modelTypeMappings association is used internally to optimize queries\n * but should not be exposed in the API response (use modelTypeIds instead)\n */\n toJSON(): object {\n const values = { ...this.get() };\n // Remove the internal association used for query optimization\n delete values.modelTypeMappings;\n return values;\n }\n}\n\nexport default CustomFieldDefinition;\n"],"mappings":"oiBAsBA,IAAA,EAAA,cAuBoCC,EAAAA,KAAM,CA+GxC,IAAI,cAAqC,CACvC,OAAO,KAAK,cAGd,IAAI,aAAa,EAA6B,CAC5C,KAAK,cAAgB,EACrB,KAAK,aAAa,eAAgB,EAAM,CAG1C,OACO,wBAAwB,EAAuC,CAIpE,GAHK,GAAU,cACb,EAAS,YAAc,EAAS,MAE9B,CAAC,CAAC,KAAM,IAAA,GAAU,CAAC,SAAS,EAAS,aAAa,CAAE,CACtD,IAAM,EAAUC,EAAAA,cAAc,EAAS,aAAc,EAAS,UAAW,EAAS,WAAW,CAC7F,GAAI,EAAQ,MACV,MAAM,IAAIC,EAAAA,kBAAkB,EAAS,aAAc,EAAS,KAAM,EAAQ,MAAM,EAKtF,OACO,iBAAiB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,cAAA,MAAA,CAAA,CAAA,EAAA,UAAA,SAAA,IAAA,GAAA,eAkB/BC,EAAAA,qPAaAC,EAAAA,4QA5JkB,CAAE,MAAO,CAAE,SAAU,GAAO,CAAE,EAAE,EAAA,EAAA,EAAA,OAC9C,CACL,UAAW,2BACX,UAAW,wBACX,QAAS,CACP,CACE,KAAM,yBACN,OAAQ,CAAC,aAAc,YAAa,OAAO,CAC3C,OAAQ,GACT,CACF,CACD,WAAY,GACZ,SAAU,CACR,kBAA8C,CAE5C,GAAI,CAAC,KAAK,YAAcC,EAAAA,sBAAsB,KAAK,WAGjD,MADA,EAAA,QAAO,MAAM,8CAA8C,KAAK,UAAU,QAAQ,CAC5E,IAAIC,EAAAA,iCAAiC,4BAA4B,KAAK,UAAU,oBAAoB,EAG/G,CACF,CAAC,CAAA,CAAA,EAAA,CA4JF,IAAA,EAAe"}
1
+ {"version":3,"file":"CustomFieldDefinition.cjs","names":["CustomFieldDefinition","Model","validateValue","InvalidValueError","sendDimEvent","PrimaryKey","DataType","CustomFieldDefinitionType","UnsupportedCustomFieldTypeError","CustomFieldValue","BeforeCreate","AfterSave","CustomValidationTypes","UnsupportedCustomValidationError"],"sources":["../../src/models/CustomFieldDefinition.ts"],"sourcesContent":["import {\n Table,\n Column,\n Model,\n DataType,\n HasMany,\n PrimaryKey,\n BeforeCreate,\n DefaultScope,\n AfterSave,\n Is,\n} from 'sequelize-typescript';\nimport type { CreateOptions } from 'sequelize';\nimport { CustomFieldDefinitionType } from '../utils/constants';\nimport { CustomValidationTypes } from '../utils/validations/validators';\nimport CustomFieldValue from './CustomFieldValue';\nimport CustomFieldModelTypeMap from './CustomFieldModelTypeMap';\nimport { sendDimEvent } from '../events';\nimport { InvalidValueError, UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';\nimport logger from '../utils/logger';\nimport { validateValue } from '../utils/validations';\n\n@DefaultScope(() => ({ where: { disabled: false } }))\n@Table({\n tableName: 'custom_field_definitions',\n modelName: 'CustomFieldDefinition',\n indexes: [\n {\n name: 'unique_name_model_type',\n fields: ['model_type', 'entity_id', 'name'],\n unique: true,\n },\n ],\n timestamps: true,\n validate: {\n validationByType(this: CustomFieldDefinition) {\n // Verify the validation is provided if needed\n if (!this.validation && CustomValidationTypes[this.fieldType as keyof typeof CustomValidationTypes]) {\n // TODO: enforce the validation-object schema based on the fieldType\n logger.error(`No custom validation for custom field type ${this.fieldType} found`);\n throw new UnsupportedCustomValidationError(`Validation provided for \"${this.fieldType}\" is not supported`);\n }\n },\n },\n})\nclass CustomFieldDefinition extends Model {\n @PrimaryKey\n @Column({\n type: DataType.UUID,\n defaultValue: DataType.UUIDV4,\n allowNull: false,\n })\n declare id: string;\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n name!: string;\n\n @Column({\n type: DataType.STRING,\n })\n displayName?: string; // Defaulted to name with beforeCreate hook\n\n @Is('SupportedType', (value) => {\n if (!Object.values(CustomFieldDefinitionType).includes(value as CustomFieldDefinitionType)) {\n throw new UnsupportedCustomFieldTypeError(`\"${value}\" is not a supported type.`);\n }\n })\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n fieldType!: CustomFieldDefinitionType;\n\n @Column({\n type: DataType.JSONB,\n })\n validation?: any;\n\n @Column({\n type: DataType.UUID,\n allowNull: false,\n })\n entityId!: string; /** Client association entity id */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n entityType!: string; /** Client association entity type (demand source / fleet / etc.) */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n modelType!: string; /** Model type. e.g. Vehicle / StopPoint / etc. */\n\n @Column({\n type: DataType.TEXT,\n })\n description?: string;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n required?: boolean;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n disabled?: boolean;\n\n @Column({\n type: DataType.JSONB,\n allowNull: true,\n })\n defaultValue?: any;\n\n @Column({\n type: DataType.DATE,\n allowNull: false,\n })\n declare createdAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare updatedAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare deletedAt?: Date;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n allowNull: false,\n })\n blockEditingFromUI!: boolean;\n\n @HasMany(() => CustomFieldValue)\n declare values: CustomFieldValue[];\n\n // Association added programmatically when useModelTypeMapping is enabled\n declare modelTypeMappings?: CustomFieldModelTypeMap[];\n\n // Virtual property for modelTypeIds - set manually after fetch\n private _modelTypeIds?: string[];\n\n get modelTypeIds(): string[] | undefined {\n return this._modelTypeIds;\n }\n\n set modelTypeIds(value: string[] | undefined) {\n this._modelTypeIds = value;\n this.setDataValue('modelTypeIds', value);\n }\n\n @BeforeCreate\n static displayNameDefaultValue(instance: CustomFieldDefinition): void {\n if (!instance?.displayName) {\n instance.displayName = instance.name;\n }\n if (![null, undefined].includes(instance.defaultValue)) {\n const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);\n if (isValid.error) {\n throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);\n }\n }\n }\n\n @AfterSave\n static afterSaveHandler(instance: CustomFieldDefinition, options: CreateOptions): void {\n if (options.transaction) {\n options.transaction.afterCommit(() => sendDimEvent(instance));\n } else {\n sendDimEvent(instance);\n }\n }\n\n /**\n * Override toJSON to exclude internal associations from API responses\n * The modelTypeMappings association is used internally to optimize queries\n * but should not be exposed in the API response (use modelTypeIds instead)\n */\n toJSON(): object {\n // Remove the internal association used for query optimization\n const { modelTypeMappings: _modelTypeMappings, ...values } = this.get();\n return values;\n }\n}\n\nexport default CustomFieldDefinition;\n"],"mappings":"oiBAsBA,IAAA,EAAA,cAuBoCC,EAAAA,KAAM,CA+GxC,IAAI,cAAqC,CACvC,OAAO,KAAK,cAGd,IAAI,aAAa,EAA6B,CAC5C,KAAK,cAAgB,EACrB,KAAK,aAAa,eAAgB,EAAM,CAG1C,OACO,wBAAwB,EAAu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AAA,OAAA,EAAA,OAAA,KAAA,KAAA,OAAA,WAAA,EAAA,OAAA,CAAA,CAAA,EAAA,UAAA,YAAA,IAAA,GAAA,4BAGM,CACN,KAAMA,EAAAA,SAAS,QACf,aAAc,GACd,UAAW,GACZ,CAAC,CAAA,EAAA,mBAAA,cAAA,QAAA,CAAA,CAAA,EAAA,UAAA,qBAAA,IAAA,GAAA,iCAGaG,EAAAA,QAAiB,CAAA,EAAA,mBAAA,cAAA,MAAA,CAAA,CAAA,EAAA,UAAA,SAAA,IAAA,GAAA,eAkB/BC,EAAAA,qPAaAC,EAAAA,4QA5JkB,CAAE,MAAO,CAAE,SAAU,GAAO,CAAE,EAAE,EAAA,EAAA,EAAA,OAC9C,CACL,UAAW,2BACX,UAAW,wBACX,QAAS,CACP,CACE,KAAM,yBACN,OAAQ,CAAC,aAAc,YAAa,OAAO,CAC3C,OAAQ,GACT,CACF,CACD,WAAY,GACZ,SAAU,CACR,kBAA8C,CAE5C,GAAI,CAAC,KAAK,YAAcC,EAAAA,sBAAsB,KAAK,WAGjD,MADA,EAAA,QAAO,MAAM,8CAA8C,KAAK,UAAU,QAAQ,CAC5E,IAAIC,EAAAA,iCAAiC,4BAA4B,KAAK,UAAU,oBAAoB,EAG/G,CACF,CAAC,CAAA,CAAA,EAAA,CA2JF,IAAA,EAAe"}
@@ -1,2 +1,2 @@
1
- import e from"../utils/logger/index.js";import{CustomFieldDefinitionType as t}from"../utils/constants/index.js";import{CustomValidationTypes as n}from"../utils/validations/validators/index.js";import{sendDimEvent as r}from"../events/index.js";import{InvalidValueError as i,UnsupportedCustomFieldTypeError as a,UnsupportedCustomValidationError as o}from"../errors/index.js";import{validateValue as s}from"../utils/validations/index.js";import{__decorateMetadata as c}from"../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js";import{__decorate as l}from"../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js";import u from"./CustomFieldValue.js";import{AfterSave as d,BeforeCreate as f,Column as p,DataType as m,DefaultScope as h,HasMany as g,Is as _,Model as v,PrimaryKey as y,Table as b}from"sequelize-typescript";var x,S,C,w,T,E;let D=class extends v{get modelTypeIds(){return this._modelTypeIds}set modelTypeIds(e){this._modelTypeIds=e,this.setDataValue(`modelTypeIds`,e)}static displayNameDefaultValue(e){if(e?.displayName||(e.displayName=e.name),![null,void 0].includes(e.defaultValue)){let t=s(e.defaultValue,e.fieldType,e.validation);if(t.error)throw new i(e.defaultValue,e.name,t.error)}}static afterSaveHandler(e,t){t.transaction?t.transaction.afterCommit(()=>r(e)):r(e)}toJSON(){let e={...this.get()};return delete e.modelTypeMappings,e}};l([y,p({type:m.UUID,defaultValue:m.UUIDV4,allowNull:!1}),c(`design:type`,String)],D.prototype,`id`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`name`,void 0),l([p({type:m.STRING}),c(`design:type`,String)],D.prototype,`displayName`,void 0),l([_(`SupportedType`,e=>{if(!Object.values(t).includes(e))throw new a(`"${e}" is not a supported type.`)}),p({type:m.STRING,allowNull:!1}),c(`design:type`,typeof(x=t!==void 0&&t)==`function`?x:Object)],D.prototype,`fieldType`,void 0),l([p({type:m.JSONB}),c(`design:type`,Object)],D.prototype,`validation`,void 0),l([p({type:m.UUID,allowNull:!1}),c(`design:type`,String)],D.prototype,`entityId`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`entityType`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`modelType`,void 0),l([p({type:m.TEXT}),c(`design:type`,String)],D.prototype,`description`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1}),c(`design:type`,Boolean)],D.prototype,`required`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1}),c(`design:type`,Boolean)],D.prototype,`disabled`,void 0),l([p({type:m.JSONB,allowNull:!0}),c(`design:type`,Object)],D.prototype,`defaultValue`,void 0),l([p({type:m.DATE,allowNull:!1}),c(`design:type`,typeof(S=typeof Date<`u`&&Date)==`function`?S:Object)],D.prototype,`createdAt`,void 0),l([p({type:m.DATE,allowNull:!0}),c(`design:type`,typeof(C=typeof Date<`u`&&Date)==`function`?C:Object)],D.prototype,`updatedAt`,void 0),l([p({type:m.DATE,allowNull:!0}),c(`design:type`,typeof(w=typeof Date<`u`&&Date)==`function`?w:Object)],D.prototype,`deletedAt`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1,allowNull:!1}),c(`design:type`,Boolean)],D.prototype,`blockEditingFromUI`,void 0),l([g(()=>u),c(`design:type`,Array)],D.prototype,`values`,void 0),l([f,c(`design:type`,Function),c(`design:paramtypes`,[typeof(T=D!==void 0&&D)==`function`?T:Object]),c(`design:returntype`,void 0)],D,`displayNameDefaultValue`,null),l([d,c(`design:type`,Function),c(`design:paramtypes`,[typeof(E=D!==void 0&&D)==`function`?E:Object,Object]),c(`design:returntype`,void 0)],D,`afterSaveHandler`,null),D=l([h(()=>({where:{disabled:!1}})),b({tableName:`custom_field_definitions`,modelName:`CustomFieldDefinition`,indexes:[{name:`unique_name_model_type`,fields:[`model_type`,`entity_id`,`name`],unique:!0}],timestamps:!0,validate:{validationByType(){if(!this.validation&&n[this.fieldType])throw e.error(`No custom validation for custom field type ${this.fieldType} found`),new o(`Validation provided for "${this.fieldType}" is not supported`)}}})],D);var O=D;export{O as default};
1
+ import e from"../utils/logger/index.js";import{CustomFieldDefinitionType as t}from"../utils/constants/index.js";import{CustomValidationTypes as n}from"../utils/validations/validators/index.js";import{sendDimEvent as r}from"../events/index.js";import{InvalidValueError as i,UnsupportedCustomFieldTypeError as a,UnsupportedCustomValidationError as o}from"../errors/index.js";import{validateValue as s}from"../utils/validations/index.js";import{__decorateMetadata as c}from"../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorateMetadata.js";import{__decorate as l}from"../_virtual/_@oxc-project_runtime@0.103.0/helpers/decorate.js";import u from"./CustomFieldValue.js";import{AfterSave as d,BeforeCreate as f,Column as p,DataType as m,DefaultScope as h,HasMany as g,Is as _,Model as v,PrimaryKey as y,Table as b}from"sequelize-typescript";var x,S,C,w,T,E;let D=class extends v{get modelTypeIds(){return this._modelTypeIds}set modelTypeIds(e){this._modelTypeIds=e,this.setDataValue(`modelTypeIds`,e)}static displayNameDefaultValue(e){if(e?.displayName||(e.displayName=e.name),![null,void 0].includes(e.defaultValue)){let t=s(e.defaultValue,e.fieldType,e.validation);if(t.error)throw new i(e.defaultValue,e.name,t.error)}}static afterSaveHandler(e,t){t.transaction?t.transaction.afterCommit(()=>r(e)):r(e)}toJSON(){let{modelTypeMappings:e,...t}=this.get();return t}};l([y,p({type:m.UUID,defaultValue:m.UUIDV4,allowNull:!1}),c(`design:type`,String)],D.prototype,`id`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`name`,void 0),l([p({type:m.STRING}),c(`design:type`,String)],D.prototype,`displayName`,void 0),l([_(`SupportedType`,e=>{if(!Object.values(t).includes(e))throw new a(`"${e}" is not a supported type.`)}),p({type:m.STRING,allowNull:!1}),c(`design:type`,typeof(x=t!==void 0&&t)==`function`?x:Object)],D.prototype,`fieldType`,void 0),l([p({type:m.JSONB}),c(`design:type`,Object)],D.prototype,`validation`,void 0),l([p({type:m.UUID,allowNull:!1}),c(`design:type`,String)],D.prototype,`entityId`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`entityType`,void 0),l([p({type:m.STRING,allowNull:!1}),c(`design:type`,String)],D.prototype,`modelType`,void 0),l([p({type:m.TEXT}),c(`design:type`,String)],D.prototype,`description`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1}),c(`design:type`,Boolean)],D.prototype,`required`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1}),c(`design:type`,Boolean)],D.prototype,`disabled`,void 0),l([p({type:m.JSONB,allowNull:!0}),c(`design:type`,Object)],D.prototype,`defaultValue`,void 0),l([p({type:m.DATE,allowNull:!1}),c(`design:type`,typeof(S=typeof Date<`u`&&Date)==`function`?S:Object)],D.prototype,`createdAt`,void 0),l([p({type:m.DATE,allowNull:!0}),c(`design:type`,typeof(C=typeof Date<`u`&&Date)==`function`?C:Object)],D.prototype,`updatedAt`,void 0),l([p({type:m.DATE,allowNull:!0}),c(`design:type`,typeof(w=typeof Date<`u`&&Date)==`function`?w:Object)],D.prototype,`deletedAt`,void 0),l([p({type:m.BOOLEAN,defaultValue:!1,allowNull:!1}),c(`design:type`,Boolean)],D.prototype,`blockEditingFromUI`,void 0),l([g(()=>u),c(`design:type`,Array)],D.prototype,`values`,void 0),l([f,c(`design:type`,Function),c(`design:paramtypes`,[typeof(T=D!==void 0&&D)==`function`?T:Object]),c(`design:returntype`,void 0)],D,`displayNameDefaultValue`,null),l([d,c(`design:type`,Function),c(`design:paramtypes`,[typeof(E=D!==void 0&&D)==`function`?E:Object,Object]),c(`design:returntype`,void 0)],D,`afterSaveHandler`,null),D=l([h(()=>({where:{disabled:!1}})),b({tableName:`custom_field_definitions`,modelName:`CustomFieldDefinition`,indexes:[{name:`unique_name_model_type`,fields:[`model_type`,`entity_id`,`name`],unique:!0}],timestamps:!0,validate:{validationByType(){if(!this.validation&&n[this.fieldType])throw e.error(`No custom validation for custom field type ${this.fieldType} found`),new o(`Validation provided for "${this.fieldType}" is not supported`)}}})],D);var O=D;export{O as default};
2
2
  //# sourceMappingURL=CustomFieldDefinition.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CustomFieldDefinition.js","names":["CustomFieldDefinition","CustomFieldValue"],"sources":["../../src/models/CustomFieldDefinition.ts"],"sourcesContent":["import {\n Table,\n Column,\n Model,\n DataType,\n HasMany,\n PrimaryKey,\n BeforeCreate,\n DefaultScope,\n AfterSave,\n Is,\n} from 'sequelize-typescript';\nimport type { CreateOptions } from 'sequelize';\nimport { CustomFieldDefinitionType } from '../utils/constants';\nimport { CustomValidationTypes } from '../utils/validations/validators';\nimport CustomFieldValue from './CustomFieldValue';\nimport CustomFieldModelTypeMap from './CustomFieldModelTypeMap';\nimport { sendDimEvent } from '../events';\nimport { InvalidValueError, UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';\nimport logger from '../utils/logger';\nimport { validateValue } from '../utils/validations';\n\n@DefaultScope(() => ({ where: { disabled: false } }))\n@Table({\n tableName: 'custom_field_definitions',\n modelName: 'CustomFieldDefinition',\n indexes: [\n {\n name: 'unique_name_model_type',\n fields: ['model_type', 'entity_id', 'name'],\n unique: true,\n },\n ],\n timestamps: true,\n validate: {\n validationByType(this: CustomFieldDefinition) {\n // Verify the validation is provided if needed\n if (!this.validation && CustomValidationTypes[this.fieldType as keyof typeof CustomValidationTypes]) {\n // TODO: enforce the validation-object schema based on the fieldType\n logger.error(`No custom validation for custom field type ${this.fieldType} found`);\n throw new UnsupportedCustomValidationError(`Validation provided for \"${this.fieldType}\" is not supported`);\n }\n },\n },\n})\nclass CustomFieldDefinition extends Model {\n @PrimaryKey\n @Column({\n type: DataType.UUID,\n defaultValue: DataType.UUIDV4,\n allowNull: false,\n })\n declare id: string;\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n name!: string;\n\n @Column({\n type: DataType.STRING,\n })\n displayName?: string; // Defaulted to name with beforeCreate hook\n\n @Is('SupportedType', (value) => {\n if (!Object.values(CustomFieldDefinitionType).includes(value as CustomFieldDefinitionType)) {\n throw new UnsupportedCustomFieldTypeError(`\"${value}\" is not a supported type.`);\n }\n })\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n fieldType!: CustomFieldDefinitionType;\n\n @Column({\n type: DataType.JSONB,\n })\n validation?: any;\n\n @Column({\n type: DataType.UUID,\n allowNull: false,\n })\n entityId!: string; /** Client association entity id */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n entityType!: string; /** Client association entity type (demand source / fleet / etc.) */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n modelType!: string; /** Model type. e.g. Vehicle / StopPoint / etc. */\n\n @Column({\n type: DataType.TEXT,\n })\n description?: string;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n required?: boolean;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n disabled?: boolean;\n\n @Column({\n type: DataType.JSONB,\n allowNull: true,\n })\n defaultValue?: any;\n\n @Column({\n type: DataType.DATE,\n allowNull: false,\n })\n declare createdAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare updatedAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare deletedAt?: Date;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n allowNull: false,\n })\n blockEditingFromUI!: boolean;\n\n @HasMany(() => CustomFieldValue)\n declare values: CustomFieldValue[];\n\n // Association added programmatically when useModelTypeMapping is enabled\n declare modelTypeMappings?: CustomFieldModelTypeMap[];\n\n // Virtual property for modelTypeIds - set manually after fetch\n private _modelTypeIds?: string[];\n\n get modelTypeIds(): string[] | undefined {\n return this._modelTypeIds;\n }\n\n set modelTypeIds(value: string[] | undefined) {\n this._modelTypeIds = value;\n this.setDataValue('modelTypeIds', value);\n }\n\n @BeforeCreate\n static displayNameDefaultValue(instance: CustomFieldDefinition): void {\n if (!instance?.displayName) {\n instance.displayName = instance.name;\n }\n if (![null, undefined].includes(instance.defaultValue)) {\n const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);\n if (isValid.error) {\n throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);\n }\n }\n }\n\n @AfterSave\n static afterSaveHandler(instance: CustomFieldDefinition, options: CreateOptions): void {\n if (options.transaction) {\n options.transaction.afterCommit(() => sendDimEvent(instance));\n } else {\n sendDimEvent(instance);\n }\n }\n\n /**\n * Override toJSON to exclude internal associations from API responses\n * The modelTypeMappings association is used internally to optimize queries\n * but should not be exposed in the API response (use modelTypeIds instead)\n */\n toJSON(): object {\n const values = { ...this.get() };\n // Remove the internal association used for query optimization\n delete values.modelTypeMappings;\n return values;\n }\n}\n\nexport default CustomFieldDefinition;\n"],"mappings":"w1BAsBA,IA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OAAA,WAAA,EAAA,OAAA,CAAA,CAAA,EAAA,UAAA,YAAA,IAAA,GAAA,IAGD,EAAO,CACN,KAAM,EAAS,QACf,aAAc,GACd,UAAW,GACZ,CAAC,CAAA,EAAA,cAAA,QAAA,CAAA,CAAA,EAAA,UAAA,qBAAA,IAAA,GAAA,IAGD,MAAcC,EAAiB,CAAA,EAAA,cAAA,MAAA,CAAA,CAAA,EAAA,UAAA,SAAA,IAAA,GAAA,IAkB/B,sKAaA,wKA5JF,OAAoB,CAAE,MAAO,CAAE,SAAU,GAAO,CAAE,EAAE,CACpD,EAAM,CACL,UAAW,2BACX,UAAW,wBACX,QAAS,CACP,CACE,KAAM,yBACN,OAAQ,CAAC,aAAc,YAAa,OAAO,CAC3C,OAAQ,GACT,CACF,CACD,WAAY,GACZ,SAAU,CACR,kBAA8C,CAE5C,GAAI,CAAC,KAAK,YAAc,EAAsB,KAAK,WAGjD,MADA,EAAO,MAAM,8CAA8C,KAAK,UAAU,QAAQ,CAC5E,IAAI,EAAiC,4BAA4B,KAAK,UAAU,oBAAoB,EAG/G,CACF,CAAC,CAAA,CAAA,EAAA,CA4JF,IAAA,EAAe"}
1
+ {"version":3,"file":"CustomFieldDefinition.js","names":["CustomFieldDefinition","CustomFieldValue"],"sources":["../../src/models/CustomFieldDefinition.ts"],"sourcesContent":["import {\n Table,\n Column,\n Model,\n DataType,\n HasMany,\n PrimaryKey,\n BeforeCreate,\n DefaultScope,\n AfterSave,\n Is,\n} from 'sequelize-typescript';\nimport type { CreateOptions } from 'sequelize';\nimport { CustomFieldDefinitionType } from '../utils/constants';\nimport { CustomValidationTypes } from '../utils/validations/validators';\nimport CustomFieldValue from './CustomFieldValue';\nimport CustomFieldModelTypeMap from './CustomFieldModelTypeMap';\nimport { sendDimEvent } from '../events';\nimport { InvalidValueError, UnsupportedCustomFieldTypeError, UnsupportedCustomValidationError } from '../errors';\nimport logger from '../utils/logger';\nimport { validateValue } from '../utils/validations';\n\n@DefaultScope(() => ({ where: { disabled: false } }))\n@Table({\n tableName: 'custom_field_definitions',\n modelName: 'CustomFieldDefinition',\n indexes: [\n {\n name: 'unique_name_model_type',\n fields: ['model_type', 'entity_id', 'name'],\n unique: true,\n },\n ],\n timestamps: true,\n validate: {\n validationByType(this: CustomFieldDefinition) {\n // Verify the validation is provided if needed\n if (!this.validation && CustomValidationTypes[this.fieldType as keyof typeof CustomValidationTypes]) {\n // TODO: enforce the validation-object schema based on the fieldType\n logger.error(`No custom validation for custom field type ${this.fieldType} found`);\n throw new UnsupportedCustomValidationError(`Validation provided for \"${this.fieldType}\" is not supported`);\n }\n },\n },\n})\nclass CustomFieldDefinition extends Model {\n @PrimaryKey\n @Column({\n type: DataType.UUID,\n defaultValue: DataType.UUIDV4,\n allowNull: false,\n })\n declare id: string;\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n name!: string;\n\n @Column({\n type: DataType.STRING,\n })\n displayName?: string; // Defaulted to name with beforeCreate hook\n\n @Is('SupportedType', (value) => {\n if (!Object.values(CustomFieldDefinitionType).includes(value as CustomFieldDefinitionType)) {\n throw new UnsupportedCustomFieldTypeError(`\"${value}\" is not a supported type.`);\n }\n })\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n fieldType!: CustomFieldDefinitionType;\n\n @Column({\n type: DataType.JSONB,\n })\n validation?: any;\n\n @Column({\n type: DataType.UUID,\n allowNull: false,\n })\n entityId!: string; /** Client association entity id */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n entityType!: string; /** Client association entity type (demand source / fleet / etc.) */\n\n @Column({\n type: DataType.STRING,\n allowNull: false,\n })\n modelType!: string; /** Model type. e.g. Vehicle / StopPoint / etc. */\n\n @Column({\n type: DataType.TEXT,\n })\n description?: string;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n required?: boolean;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n })\n disabled?: boolean;\n\n @Column({\n type: DataType.JSONB,\n allowNull: true,\n })\n defaultValue?: any;\n\n @Column({\n type: DataType.DATE,\n allowNull: false,\n })\n declare createdAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare updatedAt?: Date;\n\n @Column({\n type: DataType.DATE,\n allowNull: true,\n })\n declare deletedAt?: Date;\n\n @Column({\n type: DataType.BOOLEAN,\n defaultValue: false,\n allowNull: false,\n })\n blockEditingFromUI!: boolean;\n\n @HasMany(() => CustomFieldValue)\n declare values: CustomFieldValue[];\n\n // Association added programmatically when useModelTypeMapping is enabled\n declare modelTypeMappings?: CustomFieldModelTypeMap[];\n\n // Virtual property for modelTypeIds - set manually after fetch\n private _modelTypeIds?: string[];\n\n get modelTypeIds(): string[] | undefined {\n return this._modelTypeIds;\n }\n\n set modelTypeIds(value: string[] | undefined) {\n this._modelTypeIds = value;\n this.setDataValue('modelTypeIds', value);\n }\n\n @BeforeCreate\n static displayNameDefaultValue(instance: CustomFieldDefinition): void {\n if (!instance?.displayName) {\n instance.displayName = instance.name;\n }\n if (![null, undefined].includes(instance.defaultValue)) {\n const isValid = validateValue(instance.defaultValue, instance.fieldType, instance.validation);\n if (isValid.error) {\n throw new InvalidValueError(instance.defaultValue, instance.name, isValid.error);\n }\n }\n }\n\n @AfterSave\n static afterSaveHandler(instance: CustomFieldDefinition, options: CreateOptions): void {\n if (options.transaction) {\n options.transaction.afterCommit(() => sendDimEvent(instance));\n } else {\n sendDimEvent(instance);\n }\n }\n\n /**\n * Override toJSON to exclude internal associations from API responses\n * The modelTypeMappings association is used internally to optimize queries\n * but should not be exposed in the API response (use modelTypeIds instead)\n */\n toJSON(): object {\n // Remove the internal association used for query optimization\n const { modelTypeMappings: _modelTypeMappings, ...values } = this.get();\n return values;\n }\n}\n\nexport default CustomFieldDefinition;\n"],"mappings":"w1BAsBA,IAAA,EAAA,cAuBoC,CAAM,CA+GxC,IAAI,cAAqC,CACvC,OAAO,KAAK,cAGd,IAAI,aAAa,EAA6B,CAC5C,KAAK,cAAgB,EACrB,KAAK,aA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A,EAAA,cAAA,QAAA,CAAA,CAAA,EAAA,UAAA,qBAAA,IAAA,GAAA,IAGD,MAAcC,EAAiB,CAAA,EAAA,cAAA,MAAA,CAAA,CAAA,EAAA,UAAA,SAAA,IAAA,GAAA,IAkB/B,sKAaA,wKA5JF,OAAoB,CAAE,MAAO,CAAE,SAAU,GAAO,CAAE,EAAE,CACpD,EAAM,CACL,UAAW,2BACX,UAAW,wBACX,QAAS,CACP,CACE,KAAM,yBACN,OAAQ,CAAC,aAAc,YAAa,OAAO,CAC3C,OAAQ,GACT,CACF,CACD,WAAY,GACZ,SAAU,CACR,kBAA8C,CAE5C,GAAI,CAAC,KAAK,YAAc,EAAsB,KAAK,WAGjD,MADA,EAAO,MAAM,8CAA8C,KAAK,UAAU,QAAQ,CAC5E,IAAI,EAAiC,4BAA4B,KAAK,UAAU,oBAAoB,EAG/G,CACF,CAAC,CAAA,CAAA,EAAA,CA2JF,IAAA,EAAe"}
@@ -1,2 +1,2 @@
1
- const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../errors/index.cjs`),n=require(`../models/CustomFieldDefinition.cjs`),r=require(`../models/CustomFieldModelTypeMap.cjs`);require(`../models/index.cjs`);let i=require(`sequelize`);const a=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),o=async e=>{let{modelTypeIds:t,...i}=e,a=await n.default.sequelize.transaction(async e=>{let a=await n.default.create(i,{transaction:e});return t?.length&&await r.default.bulkCreate(t.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=t??[],a},s=async(e,t={withDisabled:!1})=>{let r=t.withDisabled?n.default.unscoped():n.default,i=t.include;if(t.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};i=t.include?Array.isArray(t.include)?[...t.include,e]:[t.include,e]:e}let o=await r.scope(`userScope`).findAll({where:e,transaction:t.transaction,raw:!t.enrichWithModelTypeIds,include:i});return t.enrichWithModelTypeIds?o.map(e=>{let t=e;return e.modelTypeIds=a(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e}):o},c=(e,t={withDisabled:!1})=>s({id:{[i.Op.in]:e}},t),l=async(e,t={withDisabled:!1})=>{let{withDisabled:r}=t,i=await(r?n.default.unscoped().scope(`userScope`):n.default.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!i)return null;let o=i;return i.modelTypeIds=a(o.modelTypeMappings??o.dataValues?.modelTypeMappings),i},u=async(e,t,r={})=>{let{include:a,useEntityIdFromInclude:o}=r.modelOptions,s={modelType:e,...!o&&{entityId:{[i.Op.in]:t}}};return n.default.findAll({where:s,transaction:r.transaction,include:a?.(t),raw:!0})},d=e=>n.default.scope(`userScope`).findOne({where:e}),f=async(e,t)=>{let{modelTypeIds:i,...a}=t,o=await n.default.sequelize.transaction(async t=>{let o=(await n.default.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:t}))[1][0];if(i){let n=await r.default.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:t}),a=n.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=n.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await r.default.destroy({where:{id:c.map(e=>e.id)},transaction:t}),s.length>0&&await r.default.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:t}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:t}))}return o});return o.modelTypeIds=i??[],o},p=async(e,t={withDisabled:!1,modelType:``})=>{let{withDisabled:r,entityIds:o,modelType:s,modelOptions:c}=t,{include:l,useEntityIdFromInclude:u}=c??{},d={modelType:s,...!u&&o&&o.length>0&&{entityId:{[i.Op.in]:o}}},f=r?n.default.unscoped():n.default,p=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(u&&l&&o){let e=l(o);p.push(...e)}return(await f.scope(`userScope`).findAll({where:d,include:p,raw:!1,transaction:t.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=a(e.modelTypeMappings||e.dataValues?.modelTypeMappings),e))},m=async(e,{findAll:n,modelOptions:r={},withDisabled:a=!1,...o}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=e[0]?.dataValues??{},l=new Set,u=[],d=new Set;e.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[i.Op.in]:Array.from(d)},name:{[i.Op.in]:Array.from(l)}},p=await(n??s)(f,{withDisabled:a,modelOptions:r,...o}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new t.MissingDefinitionError(Array.from(l).filter(e=>!h[e]));return h};exports.create=o,exports.findAll=s,exports.findByEntityIds=u,exports.findById=l,exports.findByIds=c,exports.findByModelTypeId=p,exports.findByWhere=d,exports.getCustomFieldDefinitionsDictionary=m,exports.update=f;
1
+ const e=require(`../_virtual/rolldown_runtime.cjs`),t=require(`../errors/index.cjs`),n=require(`../models/CustomFieldDefinition.cjs`),r=require(`../models/CustomFieldModelTypeMap.cjs`);require(`../models/index.cjs`);let i=require(`sequelize`);const a=()=>`modelTypeMappings`in(n.default.associations||{}),o=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),s=e=>{let t=e;return e.modelTypeIds=o(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e},c=async e=>{let{modelTypeIds:t,...i}=e,a=await n.default.sequelize.transaction(async e=>{let a=await n.default.create(i,{transaction:e});return t?.length&&await r.default.bulkCreate(t.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=t??[],a},l=async(e,t={withDisabled:!1})=>{let r=t.withDisabled?n.default.unscoped():n.default,i=t.include;if(t.enrichWithModelTypeIds&&a()){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};i=t.include?Array.isArray(t.include)?[...t.include,e]:[t.include,e]:e}let o=await r.scope(`userScope`).findAll({where:e,transaction:t.transaction,raw:!t.enrichWithModelTypeIds,include:i});return t.enrichWithModelTypeIds&&a()?o.map(s):o},u=(e,t={withDisabled:!1})=>l({id:{[i.Op.in]:e}},t),d=async(e,t={withDisabled:!1})=>{let{withDisabled:r}=t,i=r?n.default.unscoped().scope(`userScope`):n.default.scope(`userScope`),o=a(),c=await i.findByPk(e,{include:o?[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]:void 0});return c?o?s(c):c:null},f=async(e,t,r={})=>{let{include:a,useEntityIdFromInclude:o}=r.modelOptions,s={modelType:e,...!o&&{entityId:{[i.Op.in]:t}}};return n.default.findAll({where:s,transaction:r.transaction,include:a?.(t),raw:!0})},p=e=>n.default.scope(`userScope`).findOne({where:e}),m=async(e,t)=>{let{modelTypeIds:i,...a}=t,o=await n.default.sequelize.transaction(async t=>{let o=(await n.default.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:t}))[1][0];if(i){let n=await r.default.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:t}),a=n.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=n.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await r.default.destroy({where:{id:c.map(e=>e.id)},transaction:t}),s.length>0&&await r.default.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:t}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:t}))}return o});return o.modelTypeIds=i??[],o},h=async(e,t={withDisabled:!1,modelType:``})=>{let{withDisabled:r,entityIds:o,modelType:c,modelOptions:l}=t,{include:u,useEntityIdFromInclude:d}=l??{},f={modelType:c,...!d&&o&&o.length>0&&{entityId:{[i.Op.in]:o}}},p=r?n.default.unscoped():n.default,m=a(),h=[];if(m&&h.push({association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}),d&&u&&o){let e=u(o);h.push(...e)}let g=await p.scope(`userScope`).findAll({where:f,include:h.length>0?h:void 0,raw:!1,transaction:t.transaction}),_=m?g.filter(t=>{let n=t.modelTypeMappings??t.dataValues?.modelTypeMappings??[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId??t.dataValues?.modelTypeId)===e)}):g;return m?_.map(s):_},g=async(e,{findAll:n,modelOptions:r={},withDisabled:a=!1,...o}={withDisabled:!1,modelOptions:{}})=>{let{modelType:s}=e[0]?.dataValues??{},c=new Set,u=[],d=new Set;e.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{c.add(e)})});let f={modelType:s,entityId:{[i.Op.in]:Array.from(d)},name:{[i.Op.in]:Array.from(c)}},p=await(n??l)(f,{withDisabled:a,modelOptions:r,...o}),m=p.filter(e=>c.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==c.size)throw new t.MissingDefinitionError(Array.from(c).filter(e=>!h[e]));return h};exports.create=c,exports.findAll=l,exports.findByEntityIds=f,exports.findById=d,exports.findByIds=u,exports.findByModelTypeId=h,exports.findByWhere=p,exports.getCustomFieldDefinitionsDictionary=g,exports.update=m;
2
2
  //# sourceMappingURL=definition.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"definition.cjs","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","Op","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]","MissingDefinitionError"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const defWithMappings = def as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"mPAuBA,MAAM,EAAmC,GACnC,CAAC,GAAY,CAAC,MAAM,QAAQ,EAAS,CAChC,EAAE,CAGJ,EACJ,IAAK,GAAmB,EAAE,aAAe,EAAE,YAAY,YAAY,CACnE,OAAO,GAAM,EAAQ,EAAI,CAGjB,EAAS,KACpB,IACmC,CACnC,GAAM,CAAE,eAAc,GAAG,GAAmB,EAEtC,EAAS,MAAMA,EAAAA,QAAsB,UAAW,YAAY,KAAO,IAA6B,CACpG,IAAM,EAAwB,MAAMA,EAAAA,QAAsB,OAAO,EAAuB,CACtF,cACD,CAAC,CAYF,OAVI,GAAc,QAChB,MAAMC,EAAAA,QAAwB,WAC5B,EAAa,IAAI,IAAgB,CAC/B,wBAAyB,EAAsB,GAC/C,cACD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AAG,GACsC,CAAE,aAAc,GAAO,aAAc,EAAE,CAAE,GACjC,CACnD,GAAM,CAAE,aAAc,EAAU,IAAI,YAAc,EAAE,CAC9C,EAAe,IAAI,IACnB,EAAW,EAAE,CACb,EAAY,IAAI,IACtB,EAAU,QAAS,GAAa,CAC9B,GAAM,CAAE,WAAY,CAAE,UAAS,WAAU,aAAc,IAA2B,EAClF,EAAS,KAAK,EAAQ,CACtB,EAAU,IAAI,EAAS,CAEvB,OAAO,KAAK,GAAwB,EAAE,CAAC,CAAC,QAAS,GAAc,CAC7D,EAAa,IAAI,EAAU,EAC3B,EACF,CAEF,IAAMF,EAAsB,CAC1B,YACA,SAAU,EAAGD,EAAAA,GAAG,IAAK,MAAM,KAAK,EAAU,CAAE,CAC5C,KAAM,EAAGA,EAAAA,GAAG,IAAK,MAAM,KAAK,EAAa,CAAE,CAC5C,CAKK,EAAc,MAHC,GAAY,GAGM,EAAO,CAAE,eAAc,eAAc,GAAG,EAAS,CAAC,CAEnF,EAAqB,EAAY,OAAO,GAAO,EAAa,IAAI,EAAI,KAAK,CAAC,CAC1E,EAA2B,OAAO,YAAY,EAAmB,IAAI,GAAc,CAAC,EAAW,KAAM,EAAW,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAII,EAAAA,uBADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
1
+ {"version":3,"file":"definition.cjs","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","Op","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]","MissingDefinitionError"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Check if the modelTypeMappings association exists on CustomFieldDefinition\n * The association is only created when useModelTypeMapping: true is passed to initTables\n */\nconst hasModelTypeMappingsAssociation = (): boolean =>\n 'modelTypeMappings' in (CustomFieldDefinition.associations || {});\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\n/**\n * Helper to extract modelTypeIds from a definition with loaded association\n * Handles the common pattern of getting mappings from a definition\n */\nconst enrichDefinitionWithModelTypeIds = (\n definition: CustomFieldDefinition,\n): CustomFieldDefinition & { modelTypeIds?: string[]; } => {\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested and the association exists, include it to avoid N+1 queries\n if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map(enrichDefinitionWithModelTypeIds);\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const includeModelTypeMappings = hasModelTypeMappingsAssociation();\n\n const definition = await queryModel.findByPk(id, {\n include: includeModelTypeMappings ? [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ] : undefined,\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data if the association exists\n if (includeModelTypeMappings) {\n return enrichDefinitionWithModelTypeIds(definition);\n }\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeModelTypeMappings = hasModelTypeMappingsAssociation();\n const includeArray: any[] = [];\n\n // Only include the association if it exists\n if (includeModelTypeMappings) {\n includeArray.push({\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n });\n }\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray.length > 0 ? includeArray : undefined,\n raw: false,\n transaction: options.transaction,\n });\n\n // Only filter by modelTypeId if the association exists\n const filteredDefinitions = includeModelTypeMappings ? definitions.filter((def: CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n }) => {\n const mappings = def.modelTypeMappings ?? def.dataValues?.modelTypeMappings ?? [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: MappingData) => {\n const mappingTypeId = mapping.modelTypeId ?? mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n }) : definitions;\n\n // Use association data to extract modelTypeIds if the association exists\n if (includeModelTypeMappings) {\n return filteredDefinitions.map(enrichDefinitionWithModelTypeIds);\n }\n\n return filteredDefinitions as (CustomFieldDefinition & { modelTypeIds?: string[]; })[];\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"mPAuBA,MAAM,MACJ,sBAAwBA,EAAAA,QAAsB,cAAgB,EAAE,EAM5D,EAAmC,GACnC,CAAC,GAAY,CAAC,MAAM,QAAQ,EAAS,CACh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W,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAII,EAAAA,uBADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
@@ -1,2 +1,2 @@
1
- import{MissingDefinitionError as e}from"../errors/index.js";import t from"../models/CustomFieldDefinition.js";import n from"../models/CustomFieldModelTypeMap.js";import"../models/index.js";import{Op as r}from"sequelize";const i=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),a=async e=>{let{modelTypeIds:r,...i}=e,a=await t.sequelize.transaction(async e=>{let a=await t.create(i,{transaction:e});return r?.length&&await n.bulkCreate(r.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=r??[],a},o=async(e,n={withDisabled:!1})=>{let r=n.withDisabled?t.unscoped():t,a=n.include;if(n.enrichWithModelTypeIds){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};a=n.include?Array.isArray(n.include)?[...n.include,e]:[n.include,e]:e}let o=await r.scope(`userScope`).findAll({where:e,transaction:n.transaction,raw:!n.enrichWithModelTypeIds,include:a});return n.enrichWithModelTypeIds?o.map(e=>{let t=e;return e.modelTypeIds=i(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e}):o},s=(e,t={withDisabled:!1})=>o({id:{[r.in]:e}},t),c=async(e,n={withDisabled:!1})=>{let{withDisabled:r}=n,a=await(r?t.unscoped().scope(`userScope`):t.scope(`userScope`)).findByPk(e,{include:[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]});if(!a)return null;let o=a;return a.modelTypeIds=i(o.modelTypeMappings??o.dataValues?.modelTypeMappings),a},l=async(e,n,i={})=>{let{include:a,useEntityIdFromInclude:o}=i.modelOptions,s={modelType:e,...!o&&{entityId:{[r.in]:n}}};return t.findAll({where:s,transaction:i.transaction,include:a?.(n),raw:!0})},u=e=>t.scope(`userScope`).findOne({where:e}),d=async(e,r)=>{let{modelTypeIds:i,...a}=r,o=await t.sequelize.transaction(async r=>{let o=(await t.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:r}))[1][0];if(i){let t=await n.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:r}),a=t.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=t.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await n.destroy({where:{id:c.map(e=>e.id)},transaction:r}),s.length>0&&await n.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:r}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:r}))}return o});return o.modelTypeIds=i??[],o},f=async(e,n={withDisabled:!1,modelType:``})=>{let{withDisabled:a,entityIds:o,modelType:s,modelOptions:c}=n,{include:l,useEntityIdFromInclude:u}=c??{},d={modelType:s,...!u&&o&&o.length>0&&{entityId:{[r.in]:o}}},f=a?t.unscoped():t,p=[{association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}];if(u&&l&&o){let e=l(o);p.push(...e)}return(await f.scope(`userScope`).findAll({where:d,include:p,raw:!1,transaction:n.transaction})).filter(t=>{let n=t.modelTypeMappings||t.dataValues?.modelTypeMappings||[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId||t.dataValues?.modelTypeId)===e)}).map(e=>(e.modelTypeIds=i(e.modelTypeMappings||e.dataValues?.modelTypeMappings),e))},p=async(t,{findAll:n,modelOptions:i={},withDisabled:a=!1,...s}={withDisabled:!1,modelOptions:{}})=>{let{modelType:c}=t[0]?.dataValues??{},l=new Set,u=[],d=new Set;t.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:c,entityId:{[r.in]:Array.from(d)},name:{[r.in]:Array.from(l)}},p=await(n??o)(f,{withDisabled:a,modelOptions:i,...s}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new e(Array.from(l).filter(e=>!h[e]));return h};export{a as create,o as findAll,l as findByEntityIds,c as findById,s as findByIds,f as findByModelTypeId,u as findByWhere,p as getCustomFieldDefinitionsDictionary,d as update};
1
+ import{MissingDefinitionError as e}from"../errors/index.js";import t from"../models/CustomFieldDefinition.js";import n from"../models/CustomFieldModelTypeMap.js";import"../models/index.js";import{Op as r}from"sequelize";const i=()=>`modelTypeMappings`in(t.associations||{}),a=e=>!e||!Array.isArray(e)?[]:e.map(e=>e.modelTypeId||e.dataValues?.modelTypeId).filter(e=>!!e),o=e=>{let t=e;return e.modelTypeIds=a(t.modelTypeMappings??t.dataValues?.modelTypeMappings),e},s=async e=>{let{modelTypeIds:r,...i}=e,a=await t.sequelize.transaction(async e=>{let a=await t.create(i,{transaction:e});return r?.length&&await n.bulkCreate(r.map(e=>({customFieldDefinitionId:a.id,modelTypeId:e})),{transaction:e}),a});return a.modelTypeIds=r??[],a},c=async(e,n={withDisabled:!1})=>{let r=n.withDisabled?t.unscoped():t,a=n.include;if(n.enrichWithModelTypeIds&&i()){let e={association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]};a=n.include?Array.isArray(n.include)?[...n.include,e]:[n.include,e]:e}let s=await r.scope(`userScope`).findAll({where:e,transaction:n.transaction,raw:!n.enrichWithModelTypeIds,include:a});return n.enrichWithModelTypeIds&&i()?s.map(o):s},l=(e,t={withDisabled:!1})=>c({id:{[r.in]:e}},t),u=async(e,n={withDisabled:!1})=>{let{withDisabled:r}=n,a=r?t.unscoped().scope(`userScope`):t.scope(`userScope`),s=i(),c=await a.findByPk(e,{include:s?[{association:`modelTypeMappings`,required:!1,attributes:[`modelTypeId`]}]:void 0});return c?s?o(c):c:null},d=async(e,n,i={})=>{let{include:a,useEntityIdFromInclude:o}=i.modelOptions,s={modelType:e,...!o&&{entityId:{[r.in]:n}}};return t.findAll({where:s,transaction:i.transaction,include:a?.(n),raw:!0})},f=e=>t.scope(`userScope`).findOne({where:e}),p=async(e,r)=>{let{modelTypeIds:i,...a}=r,o=await t.sequelize.transaction(async r=>{let o=(await t.scope(`userScope`).update(a,{where:{id:e},returning:!0,individualHooks:!0,transaction:r}))[1][0];if(i){let t=await n.findAll({where:{customFieldDefinitionId:e},attributes:[`id`,`modelTypeId`],raw:!0,transaction:r}),a=t.map(e=>e.modelTypeId),s=i.filter(e=>!a.includes(e)),c=t.filter(e=>!i.includes(e.modelTypeId)),l=c.length>0||s.length>0;c.length>0&&await n.destroy({where:{id:c.map(e=>e.id)},transaction:r}),s.length>0&&await n.bulkCreate(s.map(t=>({customFieldDefinitionId:e,modelTypeId:t})),{transaction:r}),l&&(o.changed(`updatedAt`,!0),await o.save({transaction:r}))}return o});return o.modelTypeIds=i??[],o},m=async(e,n={withDisabled:!1,modelType:``})=>{let{withDisabled:a,entityIds:s,modelType:c,modelOptions:l}=n,{include:u,useEntityIdFromInclude:d}=l??{},f={modelType:c,...!d&&s&&s.length>0&&{entityId:{[r.in]:s}}},p=a?t.unscoped():t,m=i(),h=[];if(m&&h.push({association:`modelTypeMappings`,required:!1,attributes:[`id`,`modelTypeId`]}),d&&u&&s){let e=u(s);h.push(...e)}let g=await p.scope(`userScope`).findAll({where:f,include:h.length>0?h:void 0,raw:!1,transaction:n.transaction}),_=m?g.filter(t=>{let n=t.modelTypeMappings??t.dataValues?.modelTypeMappings??[];return!Array.isArray(n)||n.length===0?!0:n.some(t=>(t.modelTypeId??t.dataValues?.modelTypeId)===e)}):g;return m?_.map(o):_},h=async(t,{findAll:n,modelOptions:i={},withDisabled:a=!1,...o}={withDisabled:!1,modelOptions:{}})=>{let{modelType:s}=t[0]?.dataValues??{},l=new Set,u=[],d=new Set;t.forEach(e=>{let{dataValues:{modelId:t,entityId:n,customFields:r}}=e;u.push(t),d.add(n),Object.keys(r??{}).forEach(e=>{l.add(e)})});let f={modelType:s,entityId:{[r.in]:Array.from(d)},name:{[r.in]:Array.from(l)}},p=await(n??c)(f,{withDisabled:a,modelOptions:i,...o}),m=p.filter(e=>l.has(e.name)),h=Object.fromEntries(m.map(e=>[e.name,e]));if(!p?.length||m.length!==l.size)throw new e(Array.from(l).filter(e=>!h[e]));return h};export{s as create,c as findAll,d as findByEntityIds,u as findById,l as findByIds,m as findByModelTypeId,f as findByWhere,h as getCustomFieldDefinitionsDictionary,p as update};
2
2
  //# sourceMappingURL=definition.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"definition.js","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested, include the association to avoid N+1 queries\n if (options.enrichWithModelTypeIds) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map((def: CustomFieldDefinition) => {\n const defWithMappings = def as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const definition = await queryModel.findByPk(id, {\n include: [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ],\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeArray: any[] = [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n },\n ];\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray,\n raw: false,\n transaction: options.transaction,\n });\n\n const filteredDefinitions = definitions.filter((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings || [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: any) => {\n const mappingTypeId = mapping.modelTypeId || mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n });\n\n // Use association data to extract modelTypeIds instead of making N additional queries\n return filteredDefinitions.map((def: any) => {\n const mappings = def.modelTypeMappings || def.dataValues?.modelTypeMappings;\n def.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return def as CustomFieldDefinition & { modelTypeIds?: string[]; };\n });\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n thro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AAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
1
+ {"version":3,"file":"definition.js","names":["CustomFieldDefinition","CustomFieldModelTypeMap","includeToUse: Includeable | Includeable[] | undefined","modelTypeMappingsInclude: Includeable","where: WhereOptions","baseWhere: WhereOptions","includeArray: any[]"],"sources":["../../src/repository/definition.ts"],"sourcesContent":["import {\n Op,\n type Includeable, type Transaction, type FindOptions, type WhereOptions, type Transactionable,\n} from 'sequelize';\nimport { CustomFieldDefinition, CustomFieldModelTypeMap, type CustomFieldEntries } from '../models';\nimport type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';\nimport type { ModelOptions } from '../types';\nimport { MissingDefinitionError } from '../errors';\n\n/**\n * Interface for mapping association data (can be raw or Sequelize model instance)\n */\ninterface MappingData {\n modelTypeId?: string;\n dataValues?: {\n modelTypeId?: string;\n };\n}\n\n/**\n * Check if the modelTypeMappings association exists on CustomFieldDefinition\n * The association is only created when useModelTypeMapping: true is passed to initTables\n */\nconst hasModelTypeMappingsAssociation = (): boolean =>\n 'modelTypeMappings' in (CustomFieldDefinition.associations || {});\n\n/**\n * Helper to extract modelTypeIds from the modelTypeMappings association data\n * Handles both raw and non-raw Sequelize results\n */\nconst extractModelTypeIdsFromMappings = (mappings: MappingData[]): string[] => {\n if (!mappings || !Array.isArray(mappings)) {\n return [];\n }\n\n return mappings\n .map((m: MappingData) => m.modelTypeId || m.dataValues?.modelTypeId)\n .filter(id => Boolean(id)) as string[];\n};\n\n/**\n * Helper to extract modelTypeIds from a definition with loaded association\n * Handles the common pattern of getting mappings from a definition\n */\nconst enrichDefinitionWithModelTypeIds = (\n definition: CustomFieldDefinition,\n): CustomFieldDefinition & { modelTypeIds?: string[]; } => {\n const defWithMappings = definition as CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n };\n const mappings = defWithMappings.modelTypeMappings ?? defWithMappings.dataValues?.modelTypeMappings;\n definition.modelTypeIds = extractModelTypeIdsFromMappings(mappings);\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const create = async (\n data: CreateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const customFieldDefinition = await CustomFieldDefinition.create(definitionData as any, {\n transaction,\n });\n\n if (modelTypeIds?.length) {\n await CustomFieldModelTypeMap.bulkCreate(\n modelTypeIds.map(modelTypeId => ({\n customFieldDefinitionId: customFieldDefinition.id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n return customFieldDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\ninterface SadotFindOptions {\n withDisabled?: boolean;\n transaction?: Transaction | null;\n include?: Includeable | Includeable[];\n enrichWithModelTypeIds?: boolean;\n}\n\ntype SadotGetDefinitionsByEntityIdsOptions = FindOptions & { modelOptions?: ModelOptions; findAll?: typeof findAll; } & Pick<SadotFindOptions, 'withDisabled'>;\n\nexport const findAll = async (\n where: WhereOptions,\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => {\n const queryModel = options.withDisabled\n ? CustomFieldDefinition.unscoped()\n : CustomFieldDefinition;\n\n // Build include array - ensure we maintain the type compatibility\n let includeToUse: Includeable | Includeable[] | undefined = options.include;\n\n // If enrichWithModelTypeIds is requested and the association exists, include it to avoid N+1 queries\n if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {\n const modelTypeMappingsInclude: Includeable = {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n };\n\n if (options.include) {\n // Combine existing includes with modelTypeMappings\n includeToUse = Array.isArray(options.include)\n ? [...options.include, modelTypeMappingsInclude]\n : [options.include, modelTypeMappingsInclude];\n } else {\n includeToUse = modelTypeMappingsInclude;\n }\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where,\n transaction: options.transaction,\n raw: !options.enrichWithModelTypeIds,\n include: includeToUse,\n });\n\n if (options.enrichWithModelTypeIds && hasModelTypeMappingsAssociation()) {\n // Extract modelTypeIds from the included association data instead of making N queries\n return definitions.map(enrichDefinitionWithModelTypeIds);\n }\n\n return definitions;\n};\n\nexport const findByIds = (\n ids: string[],\n options: SadotFindOptions = { withDisabled: false },\n): Promise<CustomFieldDefinition[]> => findAll({ id: { [Op.in]: ids } }, options);\n\nexport const findById = async (\n id: string,\n options: Pick<SadotFindOptions, 'withDisabled'> = { withDisabled: false },\n): Promise<CustomFieldDefinition | null> => {\n const { withDisabled } = options;\n\n const queryModel = withDisabled\n ? CustomFieldDefinition.unscoped().scope('userScope')\n : CustomFieldDefinition.scope('userScope');\n\n const includeModelTypeMappings = hasModelTypeMappingsAssociation();\n\n const definition = await queryModel.findByPk(id, {\n include: includeModelTypeMappings ? [\n {\n association: 'modelTypeMappings',\n required: false,\n attributes: ['modelTypeId'],\n },\n ] : undefined,\n });\n\n if (!definition) {\n return null;\n }\n\n // Extract modelTypeIds from the included association data if the association exists\n if (includeModelTypeMappings) {\n return enrichDefinitionWithModelTypeIds(definition);\n }\n\n return definition as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const findByEntityIds = async (\n modelType: string,\n entityIds: string[],\n options: FindOptions & { modelOptions?: ModelOptions; } = {},\n): Promise<CustomFieldDefinition[]> => {\n const { include, useEntityIdFromInclude } = options.modelOptions!;\n const where: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n return CustomFieldDefinition.findAll({\n where,\n transaction: options.transaction,\n include: include?.(entityIds),\n raw: true,\n });\n};\n\nexport const findByWhere = (where: WhereOptions<CustomFieldDefinition>): Promise<CustomFieldDefinition | null> =>\n CustomFieldDefinition.scope('userScope').findOne({\n where,\n });\n\nexport const findDefinitionsByModels = async (\n modelTypes: string[],\n options?: Transactionable,\n): Promise<CustomFieldDefinition[]> => {\n const query: WhereOptions<CreateCustomFieldDefinition> = { modelType: { [Op.in]: modelTypes } };\n return CustomFieldDefinition.findAll({\n where: query,\n transaction: options?.transaction,\n });\n};\n\nexport const update = async (\n id: string,\n data: UpdateCustomFieldDefinition,\n): Promise<CustomFieldDefinition> => {\n const { modelTypeIds, ...definitionData } = data;\n\n const result = await CustomFieldDefinition.sequelize!.transaction(async (transaction: Transaction) => {\n const updatedDefinition = (await CustomFieldDefinition.scope('userScope').update(definitionData, {\n where: { id },\n returning: true,\n individualHooks: true,\n transaction,\n }))[1][0];\n\n if (modelTypeIds) {\n const existingMappings = await CustomFieldModelTypeMap.findAll({\n where: { customFieldDefinitionId: id },\n attributes: ['id', 'modelTypeId'],\n raw: true,\n transaction,\n });\n\n const existingTypeIds = existingMappings.map(m => m.modelTypeId);\n\n const toAdd = modelTypeIds.filter(typeId => !existingTypeIds.includes(typeId));\n const toRemove = existingMappings.filter(m => !modelTypeIds.includes(m.modelTypeId));\n\n // Check if there are any mapping changes\n const hasMappingChanges = toRemove.length > 0 || toAdd.length > 0;\n\n if (toRemove.length > 0) {\n await CustomFieldModelTypeMap.destroy({\n where: {\n id: toRemove.map(m => m.id),\n },\n transaction,\n });\n }\n\n if (toAdd.length > 0) {\n await CustomFieldModelTypeMap.bulkCreate(\n toAdd.map(modelTypeId => ({\n customFieldDefinitionId: id,\n modelTypeId,\n })),\n { transaction },\n );\n }\n\n // Touch the definition to update its updatedAt timestamp after mapping changes\n if (hasMappingChanges) {\n updatedDefinition.changed('updatedAt', true);\n await updatedDefinition.save({ transaction });\n }\n }\n\n return updatedDefinition;\n });\n\n result.modelTypeIds = modelTypeIds ?? [];\n\n return result as CustomFieldDefinition & { modelTypeIds?: string[]; };\n};\n\nexport const disable = (id: string): Promise<[affectedCount: number]> =>\n CustomFieldDefinition.update(\n { disabled: true },\n { where: { id } },\n );\n\nexport const destroy = (id: string): Promise<number> =>\n CustomFieldDefinition.destroy({ where: { id } });\n\n/**\n * Return the names of the required fields for a given model\n */\nexport const getRequiredFields = async (\n modelType: string,\n modelId: string | string[],\n entityId: string | string[],\n modelOptions: ModelOptions = {},\n): Promise<string[]> => {\n const entityIds = Array.isArray(entityId) ? entityId : [entityId];\n const { include, useEntityIdFromInclude } = modelOptions;\n\n const where: WhereOptions = {\n modelType,\n required: true,\n ...(!useEntityIdFromInclude && { entityId: { [Op.in]: entityIds } }),\n };\n\n const requiredFields = await CustomFieldDefinition.findAll({\n where,\n include: include?.(entityIds),\n logging: true,\n });\n const requiredFieldsNames = requiredFields.map(definition => definition.name);\n return [...new Set(requiredFieldsNames)];\n};\n\n/**\n * @returns A promise resolving with a dictionary of custom field definitions by name.\n * @throws A {@link MissingDefinitionError} if any of the custom fields doesn't have a definition.\n */\n/**\n * Find custom field definitions applicable to a specific model type instance\n *\n * Returns definitions that either:\n * 1. Have NO mappings (apply to all instances of this model type)\n * 2. Have a mapping to this specific model type instance\n */\nexport const findByModelTypeId = async (\n modelTypeId: string,\n options: Pick<SadotFindOptions, 'withDisabled' | 'transaction'> & { entityIds?: string[]; modelType: string; modelOptions?: ModelOptions; } = { withDisabled: false, modelType: '' },\n): Promise<CustomFieldDefinition[]> => {\n const { withDisabled, entityIds, modelType, modelOptions } = options;\n\n const { include, useEntityIdFromInclude } = modelOptions ?? {};\n\n const baseWhere: WhereOptions = {\n modelType,\n ...(!useEntityIdFromInclude && entityIds && entityIds.length > 0 && { entityId: { [Op.in]: entityIds } }),\n };\n\n const queryModel = withDisabled\n ? (CustomFieldDefinition as any).unscoped()\n : CustomFieldDefinition;\n\n const includeModelTypeMappings = hasModelTypeMappingsAssociation();\n const includeArray: any[] = [];\n\n // Only include the association if it exists\n if (includeModelTypeMappings) {\n includeArray.push({\n association: 'modelTypeMappings',\n required: false,\n attributes: ['id', 'modelTypeId'],\n });\n }\n\n if (useEntityIdFromInclude && include && entityIds) {\n const contextIncludes = include(entityIds);\n includeArray.push(...contextIncludes);\n }\n\n const definitions = await queryModel.scope('userScope').findAll({\n where: baseWhere,\n include: includeArray.length > 0 ? includeArray : undefined,\n raw: false,\n transaction: options.transaction,\n });\n\n // Only filter by modelTypeId if the association exists\n const filteredDefinitions = includeModelTypeMappings ? definitions.filter((def: CustomFieldDefinition & {\n modelTypeMappings?: MappingData[];\n dataValues?: { modelTypeMappings?: MappingData[]; };\n }) => {\n const mappings = def.modelTypeMappings ?? def.dataValues?.modelTypeMappings ?? [];\n\n if (!Array.isArray(mappings) || mappings.length === 0) {\n return true;\n }\n\n const hasMatch = mappings.some((mapping: MappingData) => {\n const mappingTypeId = mapping.modelTypeId ?? mapping.dataValues?.modelTypeId;\n return mappingTypeId === modelTypeId;\n });\n\n return hasMatch;\n }) : definitions;\n\n // Use association data to extract modelTypeIds if the association exists\n if (includeModelTypeMappings) {\n return filteredDefinitions.map(enrichDefinitionWithModelTypeIds);\n }\n\n return filteredDefinitions as (CustomFieldDefinition & { modelTypeIds?: string[]; })[];\n};\n\nexport const getCustomFieldDefinitionsDictionary = async (\n instances: CustomFieldEntries[],\n {\n findAll: _findAll,\n modelOptions = {},\n withDisabled = false,\n ...options\n }: SadotGetDefinitionsByEntityIdsOptions = { withDisabled: false, modelOptions: {} },\n): Promise<Record<string, CustomFieldDefinition>> => {\n const { modelType } = instances[0]?.dataValues ?? {};\n const customFields = new Set<string>();\n const modelIds = [];\n const entityIds = new Set<string>();\n instances.forEach((instance) => {\n const { dataValues: { modelId, entityId, customFields: instanceCustomFields } } = instance;\n modelIds.push(modelId);\n entityIds.add(entityId);\n\n Object.keys(instanceCustomFields ?? {}).forEach((fieldName) => {\n customFields.add(fieldName);\n });\n });\n\n const where: WhereOptions = {\n modelType,\n entityId: { [Op.in]: Array.from(entityIds) },\n name: { [Op.in]: Array.from(customFields) },\n };\n\n const findAllToUse = _findAll ?? findAll;\n\n // @ts-expect-error findAll doesn't expect modelOptions at all.\n const definitions = await findAllToUse(where, { withDisabled, modelOptions, ...options });\n\n const matchedDefinitions = definitions.filter(def => customFields.has(def.name));\n const matchedDefinitionsByName = Object.fromEntries(matchedDefinitions.map(definition => [definition.name, definition]));\n\n if (!definitions?.length || matchedDefinitions.length !== customFields.size) {\n const unmatchedCustomFields = Array.from(customFields).filter(customField => !matchedDefinitionsByName[customField]);\n throw new MissingDefinitionError(unmatchedCustomFields);\n }\n\n return matchedDefinitionsByName;\n};\n"],"mappings":"4NAuBA,MAAM,MACJ,sBAAwBA,EAAsB,cAAgB,EAAE,EAM5D,EAAmC,GACnC,CAAC,GAAY,CAAC,MAAM,QAAQ,EAAS,CAChC,EAAE,CAGJ,EACJ,IAAK,GAAmB,EAAE,aAAe,EAAE,YAAY,YAAY,CACnE,OAAO,GAAM,EAAQ,EAAI,CAOxB,EACJ,GACyD,CACzD,IAAM,EAAkB,EAMxB,MADA,GAAW,aAAe,EADT,EAAgB,mBAAqB,EAAgB,YAAY,kBACf,CAC5D,GAGI,EAAS,KACpB,IACmC,CACnC,GAAM,CAAE,eAAc,GAAG,GAAmB,EAEtC,EAAS,MAAMA,EAAsB,UAAW,YAAY,KAAO,IAA6B,CACpG,IAAM,EAAwB,MAAMA,EAAsB,OAAO,EAAuB,CACtF,cACD,CAAC,CAYF,OAVI,GAAc,QAChB,MAAMC,EAAwB,WAC5B,EAAa,IAAI,IAAgB,CAC/B,wBAAyB,EAAsB,GAC/C,cACD,EAAE,CACH,CAAE,cAAa,CAChB,CAGI,GACP,CAIF,MAFA,GAAO,aAAe,GAAgB,EAAE,CAEjC,GAYI,EAAU,MACrB,EACA,EAA4B,CAAE,aAAc,GAAO,GACd,CACrC,IAAM,EAAa,EAAQ,aACvBD,EAAsB,UAAU,CAChCA,EAGAE,EAAwD,EAAQ,QAGpE,GAAI,EAAQ,wBAA0B,GAAiC,CAAE,CACvE,IAAMC,EAAwC,CAC5C,YAAa,oBACb,SAAU,GACV,WAAY,CAAC,cAAc,CAC5B,CAED,AAME,EANE,EAAQ,QAEK,MAAM,QAAQ,EAAQ,QAAQ,CACzC,CAAC,GAAG,EAAQ,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GAGM,EAAO,CAAE,eAAc,eAAc,GAAG,EAAS,CAAC,CAEnF,EAAqB,EAAY,OAAO,GAAO,EAAa,IAAI,EAAI,KAAK,CAAC,CAC1E,EAA2B,OAAO,YAAY,EAAmB,IAAI,GAAc,CAAC,EAAW,KAAM,EAAW,CAAC,CAAC,CAExH,GAAI,CAAC,GAAa,QAAU,EAAmB,SAAW,EAAa,KAErE,MAAM,IAAI,EADoB,MAAM,KAAK,EAAa,CAAC,OAAO,GAAe,CAAC,EAAyB,GAAa,CAC7D,CAGzD,OAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.2.16-beta-e2ec42dc.0",
3
+ "version": "1.2.16-beta-ce490ac0.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -52,9 +52,9 @@
52
52
  "express": "^4.21.2",
53
53
  "npm-watch": "^0.11.0",
54
54
  "supertest": "^7.0.0",
55
- "@autofleet/errors": "^3.1.41",
56
- "@autofleet/node-common": "^4.2.23",
57
55
  "@autofleet/logger": "^4.2.45",
56
+ "@autofleet/node-common": "^4.2.23",
57
+ "@autofleet/errors": "^3.1.41",
58
58
  "@autofleet/zehut": "^4.7.5"
59
59
  },
60
60
  "peerDependencies": {