@autofleet/sadot 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +0 -1
- package/dist/api/v1/definition/index.d.ts +2 -0
- package/dist/api/v1/definition/index.js +19 -26
- package/dist/api/v1/definition/validations.d.ts +2 -0
- package/dist/api/v1/definition/validations.js +0 -1
- package/dist/api/v1/errors.d.ts +2 -0
- package/dist/api/v1/errors.js +0 -1
- package/dist/api/v1/index.d.ts +2 -0
- package/dist/api/v1/index.js +0 -1
- package/dist/errors/index.d.ts +16 -0
- package/dist/errors/index.js +0 -1
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.js +1 -2
- package/dist/hooks/create.d.ts +9 -0
- package/dist/hooks/create.js +6 -16
- package/dist/hooks/enrich.d.ts +5 -0
- package/dist/hooks/enrich.js +23 -27
- package/dist/hooks/find.d.ts +1 -0
- package/dist/hooks/find.js +3 -5
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +0 -1
- package/dist/hooks/update.d.ts +9 -0
- package/dist/hooks/update.js +3 -13
- package/dist/hooks/workaround.d.ts +10 -0
- package/dist/hooks/workaround.js +3 -13
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -18
- package/dist/models/CustomFieldDefinition.d.ts +23 -0
- package/dist/models/CustomFieldDefinition.js +1 -2
- package/dist/models/CustomFieldValue.d.ts +15 -0
- package/dist/models/CustomFieldValue.js +20 -34
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +13 -23
- package/dist/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/models/tests/AssociatedTestModel.js +0 -1
- package/dist/models/tests/TestModel.d.ts +11 -0
- package/dist/models/tests/TestModel.js +0 -1
- package/dist/repository/definition.d.ts +17 -0
- package/dist/repository/definition.js +19 -33
- package/dist/repository/value.d.ts +24 -0
- package/dist/repository/value.js +18 -28
- package/dist/tests/api/test-api.d.ts +2 -0
- package/dist/tests/api/test-api.js +12 -22
- package/dist/tests/helpers/database-config.d.ts +15 -0
- package/dist/tests/helpers/database-config.js +0 -1
- package/dist/tests/helpers/index.d.ts +2 -0
- package/dist/tests/helpers/index.js +4 -14
- package/dist/tests/mocks/definition.mock.d.ts +37 -0
- package/dist/tests/mocks/definition.mock.js +18 -13
- package/dist/tests/mocks/events.mock.d.ts +3 -0
- package/dist/tests/mocks/events.mock.js +0 -1
- package/dist/tests/mocks/testModel.d.ts +12 -0
- package/dist/tests/mocks/testModel.js +4 -14
- package/dist/types/definition/index.d.ts +23 -0
- package/dist/types/definition/index.js +0 -1
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.js +0 -1
- package/dist/types/value/index.d.ts +15 -0
- package/dist/types/value/index.js +0 -1
- package/dist/utils/constants/index.d.ts +1 -0
- package/dist/utils/constants/index.js +0 -1
- package/dist/utils/db/index.d.ts +4 -0
- package/dist/utils/db/index.js +0 -1
- package/dist/utils/logger/index.d.ts +2 -0
- package/dist/utils/logger/index.js +0 -1
- package/dist/utils/validations/custom-fields.d.ts +2 -0
- package/dist/utils/validations/custom-fields.js +0 -1
- package/dist/utils/validations/custom.d.ts +15 -0
- package/dist/utils/validations/custom.js +1 -2
- package/dist/utils/validations/index.d.ts +2 -0
- package/dist/utils/validations/index.js +0 -1
- package/dist/utils/validations/type.d.ts +18 -0
- package/dist/utils/validations/type.js +0 -1
- package/dist/utils/validations/validators.d.ts +12 -0
- package/dist/utils/validations/validators.js +0 -1
- package/package.json +1 -1
- package/tsconfig.json +9 -25
- package/dist/api/index.js.map +0 -1
- package/dist/api/v1/definition/index.js.map +0 -1
- package/dist/api/v1/definition/validations.js.map +0 -1
- package/dist/api/v1/errors.js.map +0 -1
- package/dist/api/v1/index.js.map +0 -1
- package/dist/errors/index.js.map +0 -1
- package/dist/events/index.js.map +0 -1
- package/dist/hooks/create.js.map +0 -1
- package/dist/hooks/enrich.js.map +0 -1
- package/dist/hooks/find.js.map +0 -1
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/update.js.map +0 -1
- package/dist/hooks/workaround.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/models/CustomFieldDefinition.js.map +0 -1
- package/dist/models/CustomFieldValue.js.map +0 -1
- package/dist/models/index.js.map +0 -1
- package/dist/models/tests/AssociatedTestModel.js.map +0 -1
- package/dist/models/tests/TestModel.js.map +0 -1
- package/dist/repository/definition.js.map +0 -1
- package/dist/repository/value.js.map +0 -1
- package/dist/tests/api/test-api.js.map +0 -1
- package/dist/tests/helpers/database-config.js.map +0 -1
- package/dist/tests/helpers/index.js.map +0 -1
- package/dist/tests/mocks/definition.mock.js.map +0 -1
- package/dist/tests/mocks/events.mock.js.map +0 -1
- package/dist/tests/mocks/testModel.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/definition/index.js.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/types/value/index.js.map +0 -1
- package/dist/utils/constants/index.js.map +0 -1
- package/dist/utils/db/index.js.map +0 -1
- package/dist/utils/logger/index.js.map +0 -1
- package/dist/utils/validations/custom-fields.js.map +0 -1
- package/dist/utils/validations/custom.js.map +0 -1
- package/dist/utils/validations/index.js.map +0 -1
- package/dist/utils/validations/type.js.map +0 -1
- package/dist/utils/validations/validators.js.map +0 -1
|
@@ -19,7 +19,7 @@ const events_1 = require("../events");
|
|
|
19
19
|
const errors_1 = require("../errors");
|
|
20
20
|
let CustomFieldDefinition = class CustomFieldDefinition extends sequelize_typescript_1.Model {
|
|
21
21
|
static displayNameDefaultValue(instance) {
|
|
22
|
-
if (!
|
|
22
|
+
if (!instance?.displayName) {
|
|
23
23
|
// eslint-disable-next-line no-param-reassign
|
|
24
24
|
instance.displayName = instance.name;
|
|
25
25
|
}
|
|
@@ -163,4 +163,3 @@ CustomFieldDefinition = __decorate([
|
|
|
163
163
|
})
|
|
164
164
|
], CustomFieldDefinition);
|
|
165
165
|
exports.default = CustomFieldDefinition;
|
|
166
|
-
//# sourceMappingURL=CustomFieldDefinition.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
import { CustomFieldDefinition } from '.';
|
|
3
|
+
declare class CustomFieldValue extends Model {
|
|
4
|
+
modelId: string;
|
|
5
|
+
customFieldDefinitionId: string;
|
|
6
|
+
value: any;
|
|
7
|
+
createdAt?: Date;
|
|
8
|
+
updatedAt?: Date;
|
|
9
|
+
deletedAt?: Date;
|
|
10
|
+
customFieldDefinition: CustomFieldDefinition;
|
|
11
|
+
static validateValues(instances: CustomFieldValue[]): Promise<void>;
|
|
12
|
+
static validateValue(instance: CustomFieldValue): Promise<void>;
|
|
13
|
+
static afterSaveHandler(instance: CustomFieldValue, options: any): void;
|
|
14
|
+
}
|
|
15
|
+
export default CustomFieldValue;
|
|
@@ -31,15 +31,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
31
31
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
32
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
33
|
};
|
|
34
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
35
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
36
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
37
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
38
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
39
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
40
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
34
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
44
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
45
36
|
};
|
|
@@ -53,36 +44,32 @@ const validations_1 = __importDefault(require("../utils/validations"));
|
|
|
53
44
|
const CustomFieldDefinitionRepo = __importStar(require("../repository/definition"));
|
|
54
45
|
const errors_1 = require("../errors");
|
|
55
46
|
let CustomFieldValue = class CustomFieldValue extends sequelize_typescript_1.Model {
|
|
56
|
-
static validateValues(instances) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
.find((definition) => definition.id === instance.customFieldDefinitionId);
|
|
67
|
-
const isValid = (0, validations_1.default)(instance.value, fieldType, validation);
|
|
68
|
-
if (!isValid) {
|
|
69
|
-
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
static validateValue(instance) {
|
|
75
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
-
const { customFieldDefinitionId } = instance;
|
|
77
|
-
// eslint-disable-next-line max-len
|
|
78
|
-
const cfd = yield CustomFieldDefinitionRepo.findById(customFieldDefinitionId, { withDisabled: true });
|
|
79
|
-
const { validation, fieldType } = cfd;
|
|
47
|
+
static async validateValues(instances) {
|
|
48
|
+
const ids = instances.map((instance) => instance.customFieldDefinitionId);
|
|
49
|
+
const uniqueIds = [...new Set(ids)];
|
|
50
|
+
const definitions = await CustomFieldDefinitionRepo.findByIds(uniqueIds, { withDisabled: true });
|
|
51
|
+
if (!definitions || definitions.length !== uniqueIds.length) {
|
|
52
|
+
throw new Error('Definitions not found');
|
|
53
|
+
}
|
|
54
|
+
instances.forEach((instance) => {
|
|
55
|
+
const { validation, fieldType, } = definitions
|
|
56
|
+
.find((definition) => definition.id === instance.customFieldDefinitionId);
|
|
80
57
|
const isValid = (0, validations_1.default)(instance.value, fieldType, validation);
|
|
81
58
|
if (!isValid) {
|
|
82
59
|
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
83
60
|
}
|
|
84
61
|
});
|
|
85
62
|
}
|
|
63
|
+
static async validateValue(instance) {
|
|
64
|
+
const { customFieldDefinitionId } = instance;
|
|
65
|
+
// eslint-disable-next-line max-len
|
|
66
|
+
const cfd = await CustomFieldDefinitionRepo.findById(customFieldDefinitionId, { withDisabled: true });
|
|
67
|
+
const { validation, fieldType } = cfd;
|
|
68
|
+
const isValid = (0, validations_1.default)(instance.value, fieldType, validation);
|
|
69
|
+
if (!isValid) {
|
|
70
|
+
throw new errors_1.InvalidValueError(instance.value, fieldType);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
86
73
|
static afterSaveHandler(instance, options) {
|
|
87
74
|
if (options.transaction) {
|
|
88
75
|
options.transaction.afterCommit(() => (0, events_1.sendDimEvent)(instance[0]));
|
|
@@ -159,4 +146,3 @@ CustomFieldValue = __decorate([
|
|
|
159
146
|
})
|
|
160
147
|
], CustomFieldValue);
|
|
161
148
|
exports.default = CustomFieldValue;
|
|
162
|
-
//# sourceMappingURL=CustomFieldValue.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Sequelize } from 'sequelize-typescript';
|
|
2
|
+
import CustomFieldDefinition from './CustomFieldDefinition';
|
|
3
|
+
import CustomFieldValue from './CustomFieldValue';
|
|
4
|
+
import TestModel from './tests/TestModel';
|
|
5
|
+
import AssociatedTestModel from './tests/AssociatedTestModel';
|
|
6
|
+
declare const initTables: (sequelize: Sequelize, getUser: any) => Promise<void>;
|
|
7
|
+
declare const initTestModels: (sequelize: Sequelize) => Promise<void>;
|
|
8
|
+
export { CustomFieldValue, CustomFieldDefinition, TestModel, AssociatedTestModel, initTables, initTestModels, };
|
package/dist/models/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -29,7 +20,7 @@ const testModels = [TestModel_1.default, AssociatedTestModel_1.default];
|
|
|
29
20
|
const SADOT_MIGRATION_PREFIX = 'sadot-migration';
|
|
30
21
|
const SCHEMA_VERSION = 1;
|
|
31
22
|
const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
|
|
32
|
-
const initTables = (sequelize, getUser) =>
|
|
23
|
+
const initTables = async (sequelize, getUser) => {
|
|
33
24
|
logger_1.default.info('custom-fields: initialize custom-fields tables');
|
|
34
25
|
// Detect models and import them to the orm
|
|
35
26
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -39,7 +30,7 @@ const initTables = (sequelize, getUser) => __awaiter(void 0, void 0, void 0, fun
|
|
|
39
30
|
sequelize.addModels(productionModels);
|
|
40
31
|
CustomFieldDefinition_1.default.addScope('userScope', () => {
|
|
41
32
|
const user = getUser();
|
|
42
|
-
if (user
|
|
33
|
+
if (user?.permissions) {
|
|
43
34
|
return {
|
|
44
35
|
where: {
|
|
45
36
|
entityId: [
|
|
@@ -66,19 +57,19 @@ const initTables = (sequelize, getUser) => __awaiter(void 0, void 0, void 0, fun
|
|
|
66
57
|
timestamps: false,
|
|
67
58
|
schema: 'public',
|
|
68
59
|
});
|
|
69
|
-
const migrations =
|
|
60
|
+
const migrations = await SequelizeMeta.findAll({ raw: true });
|
|
70
61
|
const currentSadotSchemaVersion = migrations
|
|
71
62
|
.reverse().find((m) => m.name.includes(SADOT_MIGRATION_PREFIX));
|
|
72
63
|
if (!currentSadotSchemaVersion
|
|
73
64
|
|| currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
65
|
+
await CustomFieldDefinition_1.default.sync({ alter: true });
|
|
66
|
+
await CustomFieldValue_1.default.sync({ alter: true });
|
|
67
|
+
await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
|
|
77
68
|
logger_1.default.info('custom-fields: models synced');
|
|
78
69
|
}
|
|
79
|
-
}
|
|
70
|
+
};
|
|
80
71
|
exports.initTables = initTables;
|
|
81
|
-
const initTestModels = (sequelize) =>
|
|
72
|
+
const initTestModels = async (sequelize) => {
|
|
82
73
|
logger_1.default.info('custom-fields: initialize custom-fields test models');
|
|
83
74
|
// Detect models and import them to the orm
|
|
84
75
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
@@ -86,12 +77,11 @@ const initTestModels = (sequelize) => __awaiter(void 0, void 0, void 0, function
|
|
|
86
77
|
throw new Error('sequelize instance must have addModels function');
|
|
87
78
|
}
|
|
88
79
|
sequelize.addModels(testModels);
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
await sequelize.dropSchema('custom-fields', { logging: false });
|
|
81
|
+
await sequelize.createSchema('custom-fields', { logging: false });
|
|
91
82
|
logger_1.default.info('custom-fields: test models added');
|
|
92
|
-
|
|
93
|
-
|
|
83
|
+
await TestModel_1.default.sync({ alter: true });
|
|
84
|
+
await AssociatedTestModel_1.default.sync({ alter: true });
|
|
94
85
|
logger_1.default.info('custom-fields: test models synced');
|
|
95
|
-
}
|
|
86
|
+
};
|
|
96
87
|
exports.initTestModels = initTestModels;
|
|
97
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
import TestModel from './TestModel';
|
|
3
|
+
declare class AssociatedTestModel extends Model {
|
|
4
|
+
id: string;
|
|
5
|
+
testModelId: string;
|
|
6
|
+
fleetId: string;
|
|
7
|
+
businessModelId: string;
|
|
8
|
+
demandSourceId: string;
|
|
9
|
+
anotherAttribute?: boolean;
|
|
10
|
+
testModel: TestModel;
|
|
11
|
+
}
|
|
12
|
+
export default AssociatedTestModel;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Model } from 'sequelize-typescript';
|
|
2
|
+
import AssociatedTestModel from './AssociatedTestModel';
|
|
3
|
+
declare class TestModel extends Model {
|
|
4
|
+
id: string;
|
|
5
|
+
fleetId: string;
|
|
6
|
+
businessModelId: string;
|
|
7
|
+
demandSourceId: string;
|
|
8
|
+
coolAttribute?: boolean;
|
|
9
|
+
associatedModels: AssociatedTestModel[];
|
|
10
|
+
}
|
|
11
|
+
export default TestModel;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CustomFieldDefinition } from '../models';
|
|
2
|
+
import type { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../types/definition';
|
|
3
|
+
export declare const create: (data: CreateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
4
|
+
export declare const findAll: (where: any, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
5
|
+
export declare const findByIds: (ids: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
6
|
+
export declare const findById: (id: string, options?: any) => Promise<CustomFieldDefinition | null>;
|
|
7
|
+
export declare const findByEntityId: (entityId: string, options?: any) => Promise<CustomFieldDefinition[]>;
|
|
8
|
+
export declare const findByEntityIds: (modelType: string, entityIds: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
9
|
+
export declare const findByWhere: (where: any) => Promise<CustomFieldDefinition | null>;
|
|
10
|
+
export declare const findDefinitionsByModels: (modelTypes: string[], options?: any) => Promise<CustomFieldDefinition[]>;
|
|
11
|
+
export declare const update: (id: string, data: UpdateCustomFieldDefinition) => Promise<CustomFieldDefinition>;
|
|
12
|
+
export declare const disable: (id: string) => Promise<any>;
|
|
13
|
+
export declare const destroy: (id: string) => Promise<any>;
|
|
14
|
+
/**
|
|
15
|
+
* Return the names of the required fields for a given model
|
|
16
|
+
*/
|
|
17
|
+
export declare const getRequiredFields: (modelType: string, modelId: string | string[], entityId: string | string[]) => Promise<string[]>;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.getRequiredFields = exports.destroy = exports.disable = exports.update = exports.findDefinitionsByModels = exports.findByWhere = exports.findByEntityIds = exports.findByEntityId = exports.findById = exports.findByIds = exports.findAll = exports.create = void 0;
|
|
13
4
|
const sequelize_1 = require("sequelize");
|
|
@@ -35,44 +26,40 @@ const findById = (id, options = { withDisabled: false }) => {
|
|
|
35
26
|
return models_1.CustomFieldDefinition.scope('userScope').findByPk(id);
|
|
36
27
|
};
|
|
37
28
|
exports.findById = findById;
|
|
38
|
-
const findByEntityId = (entityId, options = {}) =>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
transaction: options.transaction,
|
|
42
|
-
});
|
|
29
|
+
const findByEntityId = async (entityId, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
30
|
+
where: { entityId },
|
|
31
|
+
transaction: options.transaction,
|
|
43
32
|
});
|
|
44
33
|
exports.findByEntityId = findByEntityId;
|
|
45
|
-
const findByEntityIds = (modelType, entityIds, options = {}) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
raw: true,
|
|
53
|
-
});
|
|
34
|
+
const findByEntityIds = async (modelType, entityIds, options = {}) => models_1.CustomFieldDefinition.findAll({
|
|
35
|
+
where: {
|
|
36
|
+
modelType,
|
|
37
|
+
entityId: entityIds,
|
|
38
|
+
},
|
|
39
|
+
transaction: options.transaction,
|
|
40
|
+
raw: true,
|
|
54
41
|
});
|
|
55
42
|
exports.findByEntityIds = findByEntityIds;
|
|
56
43
|
const findByWhere = (where) => models_1.CustomFieldDefinition.scope('userScope').findOne({
|
|
57
44
|
where,
|
|
58
45
|
});
|
|
59
46
|
exports.findByWhere = findByWhere;
|
|
60
|
-
const findDefinitionsByModels = (modelTypes, options) =>
|
|
47
|
+
const findDefinitionsByModels = async (modelTypes, options) => {
|
|
61
48
|
const query = { modelType: { [sequelize_1.Op.in]: modelTypes } };
|
|
62
49
|
return models_1.CustomFieldDefinition.findAll({
|
|
63
50
|
where: query,
|
|
64
|
-
transaction: options
|
|
51
|
+
transaction: options?.transaction,
|
|
65
52
|
});
|
|
66
|
-
}
|
|
53
|
+
};
|
|
67
54
|
exports.findDefinitionsByModels = findDefinitionsByModels;
|
|
68
|
-
const update = (id, data) =>
|
|
69
|
-
const updatedDefinition = (
|
|
55
|
+
const update = async (id, data) => {
|
|
56
|
+
const updatedDefinition = (await models_1.CustomFieldDefinition.scope('userScope').update(data, {
|
|
70
57
|
where: { id },
|
|
71
58
|
returning: true,
|
|
72
59
|
individualHooks: true,
|
|
73
60
|
}))[1][0];
|
|
74
61
|
return updatedDefinition;
|
|
75
|
-
}
|
|
62
|
+
};
|
|
76
63
|
exports.update = update;
|
|
77
64
|
const disable = (id) => models_1.CustomFieldDefinition.update({ disabled: true }, { where: { id } });
|
|
78
65
|
exports.disable = disable;
|
|
@@ -81,14 +68,13 @@ exports.destroy = destroy;
|
|
|
81
68
|
/**
|
|
82
69
|
* Return the names of the required fields for a given model
|
|
83
70
|
*/
|
|
84
|
-
const getRequiredFields = (modelType, modelId, entityId) =>
|
|
71
|
+
const getRequiredFields = async (modelType, modelId, entityId) => {
|
|
85
72
|
const entityIds = Array.isArray(entityId) ? entityId : [entityId];
|
|
86
|
-
const requiredFields =
|
|
73
|
+
const requiredFields = await models_1.CustomFieldDefinition.findAll({
|
|
87
74
|
where: { required: true, modelType, entityId: { [sequelize_1.Op.in]: entityIds } },
|
|
88
75
|
logging: true,
|
|
89
76
|
});
|
|
90
77
|
const requiredFieldsNames = requiredFields.map((definition) => definition.name);
|
|
91
78
|
return [...new Set(requiredFieldsNames)];
|
|
92
|
-
}
|
|
79
|
+
};
|
|
93
80
|
exports.getRequiredFields = getRequiredFields;
|
|
94
|
-
//# sourceMappingURL=definition.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CustomFieldValue } from '../models';
|
|
2
|
+
import { CreateCustomFieldValue, ValuesToUpdate } from '../types/value';
|
|
3
|
+
export declare const findByModelIdAndDefinition: (modelId: string, customFieldDefinitionId: string) => Promise<CustomFieldValue[]>;
|
|
4
|
+
export declare const create: (data: CreateCustomFieldValue, withAssociations?: boolean) => Promise<CustomFieldValue>;
|
|
5
|
+
export declare const findAllValues: () => Promise<CustomFieldValue[]>;
|
|
6
|
+
/**
|
|
7
|
+
* Get all values for model instance id (with their definitions)
|
|
8
|
+
* @param modelId
|
|
9
|
+
* @returns CustomFieldValue[]
|
|
10
|
+
*/
|
|
11
|
+
export declare const findValuesByModelId: (modelId: string) => Promise<CustomFieldValue[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves custom field values for given model IDs
|
|
14
|
+
* @param modelIds - An array of model IDs to query custom field values for.
|
|
15
|
+
* @param options - Optional configuration object.
|
|
16
|
+
*/
|
|
17
|
+
export declare const findValuesByModelIds: (modelIds: string[], options?: any) => Promise<CustomFieldValue[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Try to update custom field values for a model instance.
|
|
20
|
+
* Create new value record if not exists, but fails if value's definition not exist.
|
|
21
|
+
* Return the updated values
|
|
22
|
+
*/
|
|
23
|
+
export declare const updateValues: (modelType: string, modelId: string, identifiers: string[], valuesToUpdate: ValuesToUpdate, options?: any) => Promise<CustomFieldValue[]>;
|
|
24
|
+
export declare const deleteValue: (id: string, options?: any) => Promise<any>;
|
package/dist/repository/value.js
CHANGED
|
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
27
|
};
|
|
@@ -41,32 +32,32 @@ const models_1 = require("../models");
|
|
|
41
32
|
const DefinitionRepo = __importStar(require("./definition"));
|
|
42
33
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
43
34
|
const errors_1 = require("../errors");
|
|
44
|
-
const findByModelIdAndDefinition = (modelId, customFieldDefinitionId) =>
|
|
35
|
+
const findByModelIdAndDefinition = async (modelId, customFieldDefinitionId) => models_1.CustomFieldValue.findAll({ where: { modelId, customFieldDefinitionId }, include: [models_1.CustomFieldDefinition] });
|
|
45
36
|
exports.findByModelIdAndDefinition = findByModelIdAndDefinition;
|
|
46
|
-
const create = (data, withAssociations = false) =>
|
|
47
|
-
const created =
|
|
37
|
+
const create = async (data, withAssociations = false) => {
|
|
38
|
+
const created = await models_1.CustomFieldValue.create(data);
|
|
48
39
|
if (withAssociations) {
|
|
49
|
-
const createdWithAssociations =
|
|
50
|
-
return createdWithAssociations
|
|
40
|
+
const createdWithAssociations = await (0, exports.findByModelIdAndDefinition)(created.modelId, created.customFieldDefinitionId);
|
|
41
|
+
return createdWithAssociations?.[0];
|
|
51
42
|
}
|
|
52
43
|
return created;
|
|
53
|
-
}
|
|
44
|
+
};
|
|
54
45
|
exports.create = create;
|
|
55
|
-
const findAllValues = () =>
|
|
46
|
+
const findAllValues = async () => models_1.CustomFieldValue.findAll({ include: [models_1.CustomFieldDefinition] });
|
|
56
47
|
exports.findAllValues = findAllValues;
|
|
57
48
|
/**
|
|
58
49
|
* Get all values for model instance id (with their definitions)
|
|
59
50
|
* @param modelId
|
|
60
51
|
* @returns CustomFieldValue[]
|
|
61
52
|
*/
|
|
62
|
-
const findValuesByModelId = (modelId) =>
|
|
53
|
+
const findValuesByModelId = async (modelId) => models_1.CustomFieldValue.findAll({ where: { modelId }, include: [models_1.CustomFieldDefinition] });
|
|
63
54
|
exports.findValuesByModelId = findValuesByModelId;
|
|
64
55
|
/**
|
|
65
56
|
* Retrieves custom field values for given model IDs
|
|
66
57
|
* @param modelIds - An array of model IDs to query custom field values for.
|
|
67
58
|
* @param options - Optional configuration object.
|
|
68
59
|
*/
|
|
69
|
-
const findValuesByModelIds = (modelIds, options) =>
|
|
60
|
+
const findValuesByModelIds = async (modelIds, options) => {
|
|
70
61
|
const { transaction } = options;
|
|
71
62
|
return models_1.CustomFieldValue.findAll({
|
|
72
63
|
where: { modelId: modelIds },
|
|
@@ -74,25 +65,25 @@ const findValuesByModelIds = (modelIds, options) => __awaiter(void 0, void 0, vo
|
|
|
74
65
|
raw: true,
|
|
75
66
|
nest: true,
|
|
76
67
|
});
|
|
77
|
-
}
|
|
68
|
+
};
|
|
78
69
|
exports.findValuesByModelIds = findValuesByModelIds;
|
|
79
70
|
/**
|
|
80
71
|
* Try to update custom field values for a model instance.
|
|
81
72
|
* Create new value record if not exists, but fails if value's definition not exist.
|
|
82
73
|
* Return the updated values
|
|
83
74
|
*/
|
|
84
|
-
const updateValues = (modelType, modelId, identifiers, valuesToUpdate, options = {}) =>
|
|
75
|
+
const updateValues = async (modelType, modelId, identifiers, valuesToUpdate, options = {}) => {
|
|
85
76
|
logger_1.default.info(`custom-fields: updating values for ${modelType} ${modelId}`, { valuesToUpdate });
|
|
86
77
|
const names = Object.keys(valuesToUpdate);
|
|
87
|
-
const fieldDefinitions =
|
|
78
|
+
const fieldDefinitions = await DefinitionRepo.findAll({ modelType, name: names, entityId: identifiers }, { withDisabled: true, transaction: options.transaction }) || [];
|
|
88
79
|
const disabledDefinitions = fieldDefinitions.filter((def) => def.disabled);
|
|
89
80
|
if (fieldDefinitions.length !== names.length) {
|
|
90
81
|
const missingDefinitions = names.filter((name) => !fieldDefinitions.some((def) => def.name === name));
|
|
91
82
|
throw new errors_1.MissingDefinitionError(missingDefinitions);
|
|
92
83
|
}
|
|
93
|
-
const disabledNames =
|
|
84
|
+
const disabledNames = disabledDefinitions?.map((def) => def.name) || [];
|
|
94
85
|
const valuesWithDisabledDefinitions = names.filter((name) => disabledNames.includes(name));
|
|
95
|
-
if (
|
|
86
|
+
if (valuesWithDisabledDefinitions?.length > 0) {
|
|
96
87
|
logger_1.default.warn(`custom-fields: trying to update disabled values: ${valuesWithDisabledDefinitions.join(', ')}`);
|
|
97
88
|
}
|
|
98
89
|
const values = names.map((name) => ({
|
|
@@ -101,17 +92,16 @@ const updateValues = (modelType, modelId, identifiers, valuesToUpdate, options =
|
|
|
101
92
|
updatedAt: new Date(),
|
|
102
93
|
customFieldDefinitionId: fieldDefinitions.find((def) => def.name === name).id,
|
|
103
94
|
}));
|
|
104
|
-
return Promise.all(values.map((value) =>
|
|
105
|
-
const [cfv] =
|
|
95
|
+
return Promise.all(values.map(async (value) => {
|
|
96
|
+
const [cfv] = await models_1.CustomFieldValue.upsert(value, {
|
|
106
97
|
transaction: options.transaction,
|
|
107
98
|
});
|
|
108
99
|
return cfv;
|
|
109
|
-
}))
|
|
110
|
-
}
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
111
102
|
exports.updateValues = updateValues;
|
|
112
103
|
const deleteValue = (id, options = {}) => models_1.CustomFieldValue.update({ deletedAt: new Date() }, {
|
|
113
104
|
where: { id },
|
|
114
105
|
transaction: options.transaction,
|
|
115
106
|
});
|
|
116
107
|
exports.deleteValue = deleteValue;
|
|
117
|
-
//# sourceMappingURL=value.js.map
|
|
@@ -22,45 +22,35 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
26
|
const express_1 = __importStar(require("express"));
|
|
36
27
|
const models_1 = require("../../models");
|
|
37
28
|
const app = (0, express_1.default)();
|
|
38
29
|
app.use(express_1.default.json());
|
|
39
30
|
const api = (0, express_1.Router)();
|
|
40
|
-
api.get('/v1/test-models', (req, res) =>
|
|
41
|
-
const testModels =
|
|
31
|
+
api.get('/v1/test-models', async (req, res) => {
|
|
32
|
+
const testModels = await models_1.TestModel.findAll();
|
|
42
33
|
return res.json(testModels);
|
|
43
|
-
})
|
|
44
|
-
api.get('/v1/test-models/:testModelId', (req, res) =>
|
|
34
|
+
});
|
|
35
|
+
api.get('/v1/test-models/:testModelId', async (req, res) => {
|
|
45
36
|
const { params: { testModelId } } = req;
|
|
46
|
-
const testModel =
|
|
37
|
+
const testModel = await models_1.TestModel.findByPk(testModelId);
|
|
47
38
|
return res.json(testModel);
|
|
48
|
-
})
|
|
49
|
-
api.post('/v1/test-models', (req, res) =>
|
|
39
|
+
});
|
|
40
|
+
api.post('/v1/test-models', async (req, res) => {
|
|
50
41
|
const { body } = req;
|
|
51
|
-
const testModel =
|
|
42
|
+
const testModel = await models_1.TestModel.create(body);
|
|
52
43
|
return res.json(testModel);
|
|
53
|
-
})
|
|
54
|
-
api.patch('/v1/test-models/:testModelId', (req, res) =>
|
|
44
|
+
});
|
|
45
|
+
api.patch('/v1/test-models/:testModelId', async (req, res) => {
|
|
55
46
|
const { body, params: { testModelId } } = req;
|
|
56
|
-
const testModel =
|
|
47
|
+
const testModel = await models_1.TestModel.update(body, {
|
|
57
48
|
where: {
|
|
58
49
|
id: testModelId,
|
|
59
50
|
},
|
|
60
51
|
returning: true,
|
|
61
52
|
});
|
|
62
53
|
return res.json(testModel[1][0]);
|
|
63
|
-
})
|
|
54
|
+
});
|
|
64
55
|
app.use('/api', api);
|
|
65
56
|
exports.default = app;
|
|
66
|
-
//# sourceMappingURL=test-api.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
test: {
|
|
3
|
+
username: string;
|
|
4
|
+
password: string | null;
|
|
5
|
+
database: string;
|
|
6
|
+
host: string;
|
|
7
|
+
dialect: string;
|
|
8
|
+
define: {
|
|
9
|
+
underscored: boolean;
|
|
10
|
+
underscoredAll: boolean;
|
|
11
|
+
};
|
|
12
|
+
logging: boolean;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
|
@@ -1,23 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.getModel = exports.cleanup = void 0;
|
|
13
4
|
const models_1 = require("../../models");
|
|
14
5
|
// eslint-disable-next-line import/prefer-default-export
|
|
15
|
-
const cleanup = () =>
|
|
6
|
+
const cleanup = async () => {
|
|
16
7
|
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
await models_1.CustomFieldDefinition.unscoped().destroy({ where: {} });
|
|
9
|
+
await models_1.TestModel.destroy({ where: {} });
|
|
19
10
|
}
|
|
20
|
-
}
|
|
11
|
+
};
|
|
21
12
|
exports.cleanup = cleanup;
|
|
22
13
|
const getModel = (name) => {
|
|
23
14
|
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
@@ -25,4 +16,3 @@ const getModel = (name) => {
|
|
|
25
16
|
return models[name];
|
|
26
17
|
};
|
|
27
18
|
exports.getModel = getModel;
|
|
28
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CreateCustomFieldDefinition, CustomFieldDefinitionDTO } from '../../types/definition';
|
|
2
|
+
export declare const coolFieldDefinition: CreateCustomFieldDefinition;
|
|
3
|
+
export declare const coolFieldDefinition2: {
|
|
4
|
+
name: string;
|
|
5
|
+
required?: boolean | undefined;
|
|
6
|
+
disabled?: boolean | undefined;
|
|
7
|
+
description?: string | undefined;
|
|
8
|
+
createdAt?: Date | undefined;
|
|
9
|
+
updatedAt?: Date | undefined;
|
|
10
|
+
deletedAt?: Date | undefined;
|
|
11
|
+
displayName?: string | undefined;
|
|
12
|
+
validation?: any;
|
|
13
|
+
fieldType: string;
|
|
14
|
+
entityId: string;
|
|
15
|
+
entityType: string;
|
|
16
|
+
modelType: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const coolFieldDefinition3: {
|
|
19
|
+
name: string;
|
|
20
|
+
required?: boolean | undefined;
|
|
21
|
+
disabled?: boolean | undefined;
|
|
22
|
+
description?: string | undefined;
|
|
23
|
+
createdAt?: Date | undefined;
|
|
24
|
+
updatedAt?: Date | undefined;
|
|
25
|
+
deletedAt?: Date | undefined;
|
|
26
|
+
displayName?: string | undefined;
|
|
27
|
+
validation?: any;
|
|
28
|
+
fieldType: string;
|
|
29
|
+
entityId: string;
|
|
30
|
+
entityType: string;
|
|
31
|
+
modelType: string;
|
|
32
|
+
};
|
|
33
|
+
export declare const booleanField: (modelType: string) => CreateCustomFieldDefinition;
|
|
34
|
+
export declare const enumField: (modelType: string, options: any) => CreateCustomFieldDefinition;
|
|
35
|
+
export declare const rangeField: (modelType: string) => CreateCustomFieldDefinition;
|
|
36
|
+
export declare const createDefinition: (defaults: Partial<CustomFieldDefinitionDTO>) => CreateCustomFieldDefinition;
|
|
37
|
+
export declare const createDefinitions: (defaults: Partial<CustomFieldDefinitionDTO>, length?: number) => CreateCustomFieldDefinition[];
|