@autofleet/sadot 0.0.1-beta
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/jest.config.d.ts +12 -0
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/api/index.js +11 -0
- package/dist/src/api/v1/definition/index.d.ts +2 -0
- package/dist/src/api/v1/definition/index.js +118 -0
- package/dist/src/api/v1/definition/validations.d.ts +2 -0
- package/dist/src/api/v1/definition/validations.js +36 -0
- package/dist/src/api/v1/errors.d.ts +2 -0
- package/dist/src/api/v1/errors.js +15 -0
- package/dist/src/api/v1/index.d.ts +2 -0
- package/dist/src/api/v1/index.js +10 -0
- package/dist/src/errors/index.d.ts +16 -0
- package/dist/src/errors/index.js +45 -0
- package/dist/src/events/index.d.ts +4 -0
- package/dist/src/events/index.js +47 -0
- package/dist/src/hooks/create.d.ts +9 -0
- package/dist/src/hooks/create.js +70 -0
- package/dist/src/hooks/enrich.d.ts +5 -0
- package/dist/src/hooks/enrich.js +118 -0
- package/dist/src/hooks/find.d.ts +1 -0
- package/dist/src/hooks/find.js +29 -0
- package/dist/src/hooks/index.d.ts +6 -0
- package/dist/src/hooks/index.js +18 -0
- package/dist/src/hooks/update.d.ts +9 -0
- package/dist/src/hooks/update.js +58 -0
- package/dist/src/hooks/workaround.d.ts +10 -0
- package/dist/src/hooks/workaround.js +37 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +105 -0
- package/dist/src/models/CustomFieldDefinition.d.ts +23 -0
- package/dist/src/models/CustomFieldDefinition.js +165 -0
- package/dist/src/models/CustomFieldValue.d.ts +15 -0
- package/dist/src/models/CustomFieldValue.js +148 -0
- package/dist/src/models/index.d.ts +8 -0
- package/dist/src/models/index.js +87 -0
- package/dist/src/models/tests/AssociatedTestModel.d.ts +12 -0
- package/dist/src/models/tests/AssociatedTestModel.js +71 -0
- package/dist/src/models/tests/TestModel.d.ts +11 -0
- package/dist/src/models/tests/TestModel.js +63 -0
- package/dist/src/repository/definition.d.ts +17 -0
- package/dist/src/repository/definition.js +80 -0
- package/dist/src/repository/value.d.ts +24 -0
- package/dist/src/repository/value.js +107 -0
- package/dist/src/tests/api/test-api.d.ts +2 -0
- package/dist/src/tests/api/test-api.js +56 -0
- package/dist/src/tests/helpers/database-config.d.ts +15 -0
- package/dist/src/tests/helpers/database-config.js +16 -0
- package/dist/src/tests/helpers/index.d.ts +2 -0
- package/dist/src/tests/helpers/index.js +18 -0
- package/dist/src/tests/mocks/definition.mock.d.ts +37 -0
- package/dist/src/tests/mocks/definition.mock.js +64 -0
- package/dist/src/tests/mocks/events.mock.d.ts +3 -0
- package/dist/src/tests/mocks/events.mock.js +19 -0
- package/dist/src/tests/mocks/testModel.d.ts +12 -0
- package/dist/src/tests/mocks/testModel.js +35 -0
- package/dist/src/types/definition/index.d.ts +23 -0
- package/dist/src/types/definition/index.js +2 -0
- package/dist/src/types/index.d.ts +13 -0
- package/dist/src/types/index.js +2 -0
- package/dist/src/types/value/index.d.ts +15 -0
- package/dist/src/types/value/index.js +2 -0
- package/dist/src/utils/constants/index.d.ts +1 -0
- package/dist/src/utils/constants/index.js +5 -0
- package/dist/src/utils/db/index.d.ts +4 -0
- package/dist/src/utils/db/index.js +24 -0
- package/dist/src/utils/logger/index.d.ts +2 -0
- package/dist/src/utils/logger/index.js +6 -0
- package/dist/src/utils/validations/custom-fields.d.ts +2 -0
- package/dist/src/utils/validations/custom-fields.js +10 -0
- package/dist/src/utils/validations/custom.d.ts +15 -0
- package/dist/src/utils/validations/custom.js +42 -0
- package/dist/src/utils/validations/index.d.ts +2 -0
- package/dist/src/utils/validations/index.js +20 -0
- package/dist/src/utils/validations/type.d.ts +18 -0
- package/dist/src/utils/validations/type.js +50 -0
- package/dist/src/utils/validations/validators.d.ts +12 -0
- package/dist/src/utils/validations/validators.js +33 -0
- package/package.json +44 -0
- package/src/api/index.ts +9 -0
- package/src/api/v1/definition/index.ts +110 -0
- package/src/api/v1/definition/validations.ts +35 -0
- package/src/api/v1/errors.ts +16 -0
- package/src/api/v1/index.ts +9 -0
- package/src/errors/index.ts +42 -0
- package/src/events/index.ts +50 -0
- package/src/hooks/create.ts +51 -0
- package/src/hooks/enrich.ts +125 -0
- package/src/hooks/find.ts +27 -0
- package/src/hooks/index.ts +15 -0
- package/src/hooks/update.ts +39 -0
- package/src/hooks/workaround.ts +47 -0
- package/src/index.ts +101 -0
- package/src/models/CustomFieldDefinition.ts +140 -0
- package/src/models/CustomFieldValue.ts +114 -0
- package/src/models/index.ts +101 -0
- package/src/models/tests/AssociatedTestModel.ts +57 -0
- package/src/models/tests/TestModel.ts +49 -0
- package/src/repository/definition.ts +111 -0
- package/src/repository/value.ts +99 -0
- package/src/tests/api/test-api.ts +38 -0
- package/src/tests/helpers/database-config.ts +14 -0
- package/src/tests/helpers/index.ts +15 -0
- package/src/tests/mocks/definition.mock.ts +69 -0
- package/src/tests/mocks/events.mock.ts +18 -0
- package/src/tests/mocks/testModel.ts +37 -0
- package/src/types/definition/index.ts +22 -0
- package/src/types/index.ts +15 -0
- package/src/types/value/index.ts +14 -0
- package/src/utils/constants/index.ts +2 -0
- package/src/utils/db/index.ts +21 -0
- package/src/utils/logger/index.ts +6 -0
- package/src/utils/validations/custom-fields.ts +9 -0
- package/src/utils/validations/custom.ts +39 -0
- package/src/utils/validations/index.ts +19 -0
- package/src/utils/validations/type.ts +45 -0
- package/src/utils/validations/validators.ts +34 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDefinitions = exports.createDefinition = exports.rangeField = exports.enumField = exports.booleanField = exports.coolFieldDefinition3 = exports.coolFieldDefinition2 = exports.coolFieldDefinition = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
exports.coolFieldDefinition = {
|
|
6
|
+
name: 'cool field',
|
|
7
|
+
modelType: 'TestModel',
|
|
8
|
+
fieldType: 'number',
|
|
9
|
+
entityId: (0, uuid_1.v4)(),
|
|
10
|
+
entityType: 'fleetId',
|
|
11
|
+
};
|
|
12
|
+
exports.coolFieldDefinition2 = {
|
|
13
|
+
...exports.coolFieldDefinition,
|
|
14
|
+
name: 'cool field2',
|
|
15
|
+
};
|
|
16
|
+
exports.coolFieldDefinition3 = {
|
|
17
|
+
...exports.coolFieldDefinition,
|
|
18
|
+
name: 'cool field3',
|
|
19
|
+
};
|
|
20
|
+
const booleanField = (modelType) => ({
|
|
21
|
+
name: 'shapeless',
|
|
22
|
+
modelType,
|
|
23
|
+
fieldType: 'boolean',
|
|
24
|
+
entityId: (0, uuid_1.v4)(),
|
|
25
|
+
entityType: 'fleetId',
|
|
26
|
+
});
|
|
27
|
+
exports.booleanField = booleanField;
|
|
28
|
+
const enumField = (modelType, options) => ({
|
|
29
|
+
name: 'choices',
|
|
30
|
+
modelType,
|
|
31
|
+
fieldType: 'select',
|
|
32
|
+
validation: options,
|
|
33
|
+
entityId: (0, uuid_1.v4)(),
|
|
34
|
+
entityType: 'fleetId',
|
|
35
|
+
});
|
|
36
|
+
exports.enumField = enumField;
|
|
37
|
+
const rangeField = (modelType) => ({
|
|
38
|
+
name: 'ranges',
|
|
39
|
+
modelType,
|
|
40
|
+
fieldType: 'number',
|
|
41
|
+
validation: {
|
|
42
|
+
between: [10, 12],
|
|
43
|
+
},
|
|
44
|
+
entityId: (0, uuid_1.v4)(),
|
|
45
|
+
entityType: 'fleetId',
|
|
46
|
+
});
|
|
47
|
+
exports.rangeField = rangeField;
|
|
48
|
+
// eslint-disable-next-line max-len
|
|
49
|
+
const createDefinition = (defaults) => ({
|
|
50
|
+
name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
|
|
51
|
+
modelType: defaults?.modelType || 'TestModel',
|
|
52
|
+
fieldType: defaults?.fieldType || 'boolean',
|
|
53
|
+
entityId: defaults?.entityId || (0, uuid_1.v4)(),
|
|
54
|
+
entityType: defaults?.entityType || 'fleetId',
|
|
55
|
+
});
|
|
56
|
+
exports.createDefinition = createDefinition;
|
|
57
|
+
const createDefinitions = (defaults, length = 1) => (Array(length).fill({}).map((_) => ({
|
|
58
|
+
name: defaults?.name || `def_${(0, uuid_1.v4)()}`,
|
|
59
|
+
modelType: defaults?.modelType || 'TestModel',
|
|
60
|
+
fieldType: defaults?.fieldType || 'boolean',
|
|
61
|
+
entityId: defaults?.entityId || (0, uuid_1.v4)(),
|
|
62
|
+
entityType: defaults?.entityType || 'fleetId',
|
|
63
|
+
})));
|
|
64
|
+
exports.createDefinitions = createDefinitions;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const mockEvent: (events: any, eventName: any, numberOfEvents: any) => any[];
|
|
2
|
+
export declare const mockDimCustomFieldDefinitionEvent: (events: any, numberOfEvents: any) => any[];
|
|
3
|
+
export declare const mockDimCustomFieldValueEvent: (events: any, numberOfEvents: any) => any[];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-param-reassign */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.mockDimCustomFieldValueEvent = exports.mockDimCustomFieldDefinitionEvent = exports.mockEvent = void 0;
|
|
5
|
+
const mockEvent = (events, eventName, numberOfEvents) => {
|
|
6
|
+
events.sendObject = jest.fn();
|
|
7
|
+
return [
|
|
8
|
+
events.sendObject,
|
|
9
|
+
() => {
|
|
10
|
+
const matchingEvents = events.sendObject.mock.calls.filter((call) => call[0] === eventName);
|
|
11
|
+
expect(matchingEvents.length).toEqual(numberOfEvents);
|
|
12
|
+
},
|
|
13
|
+
];
|
|
14
|
+
};
|
|
15
|
+
exports.mockEvent = mockEvent;
|
|
16
|
+
const mockDimCustomFieldDefinitionEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_definition', numberOfEvents);
|
|
17
|
+
exports.mockDimCustomFieldDefinitionEvent = mockDimCustomFieldDefinitionEvent;
|
|
18
|
+
const mockDimCustomFieldValueEvent = (events, numberOfEvents) => (0, exports.mockEvent)(events, 'dim_custom_field_value', numberOfEvents);
|
|
19
|
+
exports.mockDimCustomFieldValueEvent = mockDimCustomFieldValueEvent;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TestModel } from '../../models';
|
|
2
|
+
export declare const createTestModel: (payload?: {}) => Promise<TestModel>;
|
|
3
|
+
export declare const createTestModels: (fleetId: any, total: number) => Promise<TestModel[]>;
|
|
4
|
+
export declare const upsertTestModel: (payload: any) => Promise<[TestModel, boolean]>;
|
|
5
|
+
export declare const destroyTestModels: () => Promise<number>;
|
|
6
|
+
export declare const getTestModel: (id: string, options?: {}) => Promise<TestModel>;
|
|
7
|
+
export declare const getSomeTestModels: (options?: {
|
|
8
|
+
limit: number;
|
|
9
|
+
}) => Promise<TestModel[]>;
|
|
10
|
+
export declare const updateTestModel: (payload: any, query: any) => Promise<[affectedCount: number, affectedRows: TestModel[]]>;
|
|
11
|
+
export declare const createTestModelWithAssociation: () => Promise<TestModel>;
|
|
12
|
+
export declare const getTestModelWithAssociation: (limit?: number) => Promise<TestModel[]>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTestModelWithAssociation = exports.createTestModelWithAssociation = exports.updateTestModel = exports.getSomeTestModels = exports.getTestModel = exports.destroyTestModels = exports.upsertTestModel = exports.createTestModels = exports.createTestModel = void 0;
|
|
4
|
+
const models_1 = require("../../models");
|
|
5
|
+
const createTestModel = (payload = {}) => models_1.TestModel.create(payload);
|
|
6
|
+
exports.createTestModel = createTestModel;
|
|
7
|
+
const createTestModels = (fleetId, total) => {
|
|
8
|
+
const models = [];
|
|
9
|
+
for (let i = 0; i < total; i += 1) {
|
|
10
|
+
models.push((0, exports.createTestModel)({ fleetId }));
|
|
11
|
+
}
|
|
12
|
+
return Promise.all(models);
|
|
13
|
+
};
|
|
14
|
+
exports.createTestModels = createTestModels;
|
|
15
|
+
const upsertTestModel = (payload) => models_1.TestModel.upsert(payload);
|
|
16
|
+
exports.upsertTestModel = upsertTestModel;
|
|
17
|
+
const destroyTestModels = () => models_1.TestModel.destroy({ truncate: true });
|
|
18
|
+
exports.destroyTestModels = destroyTestModels;
|
|
19
|
+
const getTestModel = (id, options = {}) => models_1.TestModel.findByPk(id, options);
|
|
20
|
+
exports.getTestModel = getTestModel;
|
|
21
|
+
const getSomeTestModels = (options = { limit: 1 }) => models_1.TestModel.findAll(options);
|
|
22
|
+
exports.getSomeTestModels = getSomeTestModels;
|
|
23
|
+
const updateTestModel = (payload, query) => models_1.TestModel.update(payload, query);
|
|
24
|
+
exports.updateTestModel = updateTestModel;
|
|
25
|
+
// Associations
|
|
26
|
+
const createTestModelWithAssociation = async () => {
|
|
27
|
+
const model = await models_1.TestModel.create({});
|
|
28
|
+
await models_1.AssociatedTestModel.create({ testModelId: model.id });
|
|
29
|
+
return model;
|
|
30
|
+
};
|
|
31
|
+
exports.createTestModelWithAssociation = createTestModelWithAssociation;
|
|
32
|
+
const getTestModelWithAssociation = (limit = 1) => models_1.TestModel.findAll({
|
|
33
|
+
limit, include: [models_1.AssociatedTestModel],
|
|
34
|
+
});
|
|
35
|
+
exports.getTestModelWithAssociation = getTestModelWithAssociation;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface CustomFieldDefinitionDTO {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
displayName?: string;
|
|
5
|
+
validation?: any;
|
|
6
|
+
fieldType: string;
|
|
7
|
+
entityId: string;
|
|
8
|
+
entityType: string;
|
|
9
|
+
modelType: string;
|
|
10
|
+
required?: boolean;
|
|
11
|
+
description?: string;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
createdAt?: Date;
|
|
14
|
+
updatedAt?: Date;
|
|
15
|
+
deletedAt?: Date;
|
|
16
|
+
}
|
|
17
|
+
export type CreateCustomFieldDefinition = Omit<CustomFieldDefinitionDTO, 'id'>;
|
|
18
|
+
export type UpdateCustomFieldDefinition = Partial<CreateCustomFieldDefinition>;
|
|
19
|
+
export type SerializedCustomFields = {
|
|
20
|
+
[name: string]: CustomFieldDefinitionDTO & {
|
|
21
|
+
value: any;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type ModelFetcher = (name: string) => any;
|
|
2
|
+
export type ModelOptions = {
|
|
3
|
+
name: string;
|
|
4
|
+
scopeAttributes: string[];
|
|
5
|
+
creationWebhookHandler?: (instance: any) => any;
|
|
6
|
+
updateWebhookHandler?: (instance: any) => any;
|
|
7
|
+
deletionWebhookHandler?: (instance: any) => any;
|
|
8
|
+
};
|
|
9
|
+
export type CustomFieldOptions = {
|
|
10
|
+
models: ModelOptions[];
|
|
11
|
+
databaseConfig: any;
|
|
12
|
+
getUser: () => any;
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CustomFieldValueDTO {
|
|
2
|
+
id?: string;
|
|
3
|
+
modelId: string;
|
|
4
|
+
customFieldDefinitionId: string;
|
|
5
|
+
value: any;
|
|
6
|
+
createdAt?: Date;
|
|
7
|
+
updatedAt?: Date;
|
|
8
|
+
deletedAt?: Date;
|
|
9
|
+
}
|
|
10
|
+
export type ValuesToUpdate = {
|
|
11
|
+
[name: string]: any;
|
|
12
|
+
};
|
|
13
|
+
export type CreateCustomFieldValue = Omit<CustomFieldValueDTO, 'id'>;
|
|
14
|
+
export type UpdateCustomFieldValue = ValuesToUpdate;
|
|
15
|
+
export type BulkUpdateCustomFieldValue = Partial<CustomFieldValueDTO>[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const supportedEntities: string[];
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSequelizeMeta = void 0;
|
|
4
|
+
const sequelize_typescript_1 = require("sequelize-typescript");
|
|
5
|
+
const sequelize_1 = require("sequelize");
|
|
6
|
+
exports.default = (databaseConfig) => {
|
|
7
|
+
const ENV_DEV = 'test';
|
|
8
|
+
const env = process.env.NODE_ENV || ENV_DEV;
|
|
9
|
+
const config = databaseConfig[env];
|
|
10
|
+
let sequelize;
|
|
11
|
+
if (config.use_env_variable) {
|
|
12
|
+
sequelize = new sequelize_typescript_1.Sequelize(process.env[config.use_env_variable], config);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
sequelize = new sequelize_typescript_1.Sequelize(config.database, config.username, config.password, config);
|
|
16
|
+
}
|
|
17
|
+
return sequelize;
|
|
18
|
+
};
|
|
19
|
+
const createSequelizeMeta = (sequelize) => sequelize.query(`
|
|
20
|
+
CREATE TABLE IF NOT EXISTS "SequelizeMeta" (
|
|
21
|
+
name character varying(255) PRIMARY KEY
|
|
22
|
+
);
|
|
23
|
+
`, { type: sequelize_1.QueryTypes.SELECT });
|
|
24
|
+
exports.createSequelizeMeta = createSequelizeMeta;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CustomFieldsSchema = void 0;
|
|
7
|
+
/* eslint-disable import/prefer-default-export */
|
|
8
|
+
const joi_1 = __importDefault(require("@hapi/joi"));
|
|
9
|
+
const CustomFieldsSchema = joi_1.default.object().pattern(joi_1.default.string(), joi_1.default.any());
|
|
10
|
+
exports.CustomFieldsSchema = CustomFieldsSchema;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const mustHaveCustomValidation: {
|
|
2
|
+
select: boolean;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* Validates the given validations object against the supported field types and their validators.
|
|
6
|
+
* @return true if the validation is valid, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export declare const validateValidation: (valueType: any, validation: any) => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Validates the given value against the custom validation rules for the specified field type.
|
|
11
|
+
* If no custom validation rules are provided, it falls back to the default validation.
|
|
12
|
+
* @returns true if the value is valid according to the validation rules, false otherwise.
|
|
13
|
+
*/
|
|
14
|
+
declare const customValidation: (value: any, valueType: any, validation: any) => boolean;
|
|
15
|
+
export default customValidation;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateValidation = exports.mustHaveCustomValidation = void 0;
|
|
7
|
+
/* eslint-disable no-shadow */
|
|
8
|
+
const logger_1 = __importDefault(require("../logger"));
|
|
9
|
+
const type_1 = require("./type");
|
|
10
|
+
const validators_1 = __importDefault(require("./validators"));
|
|
11
|
+
exports.mustHaveCustomValidation = {
|
|
12
|
+
[type_1.CustomFieldDefinitionType.SELECT]: true,
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Validates the given validations object against the supported field types and their validators.
|
|
16
|
+
* @return true if the validation is valid, false otherwise.
|
|
17
|
+
*/
|
|
18
|
+
const validateValidation = (valueType, validation) => {
|
|
19
|
+
if (!validation) {
|
|
20
|
+
if (exports.mustHaveCustomValidation[valueType]) {
|
|
21
|
+
logger_1.default.error(`No custom validation for custom field type ${valueType} found`);
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
exports.validateValidation = validateValidation;
|
|
29
|
+
/**
|
|
30
|
+
* Validates the given value against the custom validation rules for the specified field type.
|
|
31
|
+
* If no custom validation rules are provided, it falls back to the default validation.
|
|
32
|
+
* @returns true if the value is valid according to the validation rules, false otherwise.
|
|
33
|
+
*/
|
|
34
|
+
const customValidation = (value, valueType, validation) => {
|
|
35
|
+
const validator = validators_1.default?.[valueType];
|
|
36
|
+
if (!validation || !validator) {
|
|
37
|
+
return (0, exports.validateValidation)(valueType, validation);
|
|
38
|
+
}
|
|
39
|
+
// Always allow null values
|
|
40
|
+
return value === null || validator(value, validation);
|
|
41
|
+
};
|
|
42
|
+
exports.default = customValidation;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const custom_1 = __importDefault(require("./custom"));
|
|
7
|
+
const type_1 = __importDefault(require("./type"));
|
|
8
|
+
const validateValue = (value, valueType, customValidations) => {
|
|
9
|
+
if (!(0, type_1.default)(value, valueType)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (customValidations) {
|
|
13
|
+
return (0, custom_1.default)(value, valueType, customValidations);
|
|
14
|
+
}
|
|
15
|
+
// if (validations.required && !value) {
|
|
16
|
+
// return false;
|
|
17
|
+
// }
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
exports.default = validateValue;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported custom field types
|
|
3
|
+
*/
|
|
4
|
+
export declare enum CustomFieldDefinitionType {
|
|
5
|
+
NUMBER = "number",
|
|
6
|
+
BOOLEAN = "boolean",
|
|
7
|
+
DATE = "date",
|
|
8
|
+
DATETIME = "datetime",
|
|
9
|
+
TEXT = "text",
|
|
10
|
+
IMAGE = "image",
|
|
11
|
+
SELECT = "select"
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Validate that the given value is really of type "valueType"
|
|
15
|
+
* TODO: verify that required field not set to null
|
|
16
|
+
*/
|
|
17
|
+
declare const validateValueType: (value: unknown, valueType: CustomFieldDefinitionType) => boolean;
|
|
18
|
+
export default validateValueType;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CustomFieldDefinitionType = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("@hapi/joi"));
|
|
8
|
+
/**
|
|
9
|
+
* Supported custom field types
|
|
10
|
+
*/
|
|
11
|
+
// eslint-disable-next-line no-shadow
|
|
12
|
+
var CustomFieldDefinitionType;
|
|
13
|
+
(function (CustomFieldDefinitionType) {
|
|
14
|
+
CustomFieldDefinitionType["NUMBER"] = "number";
|
|
15
|
+
CustomFieldDefinitionType["BOOLEAN"] = "boolean";
|
|
16
|
+
CustomFieldDefinitionType["DATE"] = "date";
|
|
17
|
+
CustomFieldDefinitionType["DATETIME"] = "datetime";
|
|
18
|
+
CustomFieldDefinitionType["TEXT"] = "text";
|
|
19
|
+
CustomFieldDefinitionType["IMAGE"] = "image";
|
|
20
|
+
CustomFieldDefinitionType["SELECT"] = "select";
|
|
21
|
+
})(CustomFieldDefinitionType = exports.CustomFieldDefinitionType || (exports.CustomFieldDefinitionType = {}));
|
|
22
|
+
/**
|
|
23
|
+
* Validate that the given value is really of type "valueType"
|
|
24
|
+
* TODO: verify that required field not set to null
|
|
25
|
+
*/
|
|
26
|
+
const validateValueType = (value, valueType) => {
|
|
27
|
+
if (value === null) {
|
|
28
|
+
// Null is always allowed
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
switch (valueType) {
|
|
32
|
+
case CustomFieldDefinitionType.TEXT:
|
|
33
|
+
return typeof value === 'string';
|
|
34
|
+
case CustomFieldDefinitionType.NUMBER:
|
|
35
|
+
return typeof value === 'number';
|
|
36
|
+
case CustomFieldDefinitionType.BOOLEAN:
|
|
37
|
+
return typeof value === 'boolean';
|
|
38
|
+
case CustomFieldDefinitionType.DATE:
|
|
39
|
+
case CustomFieldDefinitionType.DATETIME:
|
|
40
|
+
return !joi_1.default.date().validate(value).error;
|
|
41
|
+
case CustomFieldDefinitionType.SELECT:
|
|
42
|
+
return true; // custom validation
|
|
43
|
+
case CustomFieldDefinitionType.IMAGE:
|
|
44
|
+
return !joi_1.default.array().min(1).unique().items(joi_1.default.string().uri())
|
|
45
|
+
.validate(value).error;
|
|
46
|
+
default:
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
exports.default = validateValueType;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare enum CustomValidations {
|
|
2
|
+
SELECT = "select",
|
|
3
|
+
RANGE = "between"
|
|
4
|
+
}
|
|
5
|
+
type Validator = (value: any, validation: any) => boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Validators for custom fields
|
|
8
|
+
*/
|
|
9
|
+
declare const validators: {
|
|
10
|
+
[key: string]: Validator;
|
|
11
|
+
};
|
|
12
|
+
export default validators;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomValidations = void 0;
|
|
4
|
+
// eslint-disable-next-line no-shadow
|
|
5
|
+
var CustomValidations;
|
|
6
|
+
(function (CustomValidations) {
|
|
7
|
+
CustomValidations["SELECT"] = "select";
|
|
8
|
+
CustomValidations["RANGE"] = "between";
|
|
9
|
+
})(CustomValidations = exports.CustomValidations || (exports.CustomValidations = {}));
|
|
10
|
+
/**
|
|
11
|
+
* Validate {@link CustomValidations.ENUM Enum}
|
|
12
|
+
*/
|
|
13
|
+
const validateEnum = (value, enumValues) => (Array.isArray(enumValues)
|
|
14
|
+
&& enumValues.length > 0
|
|
15
|
+
&& enumValues.includes(value));
|
|
16
|
+
/**
|
|
17
|
+
* Validate {@link CustomValidations.RANGE Range}
|
|
18
|
+
*/
|
|
19
|
+
const validateRange = (value, range) => {
|
|
20
|
+
const [min, max] = range;
|
|
21
|
+
if (min === undefined || max === undefined) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return value >= range.min && value <= range.max;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Validators for custom fields
|
|
28
|
+
*/
|
|
29
|
+
const validators = {
|
|
30
|
+
[CustomValidations.SELECT]: validateEnum,
|
|
31
|
+
[CustomValidations.RANGE]: validateRange,
|
|
32
|
+
};
|
|
33
|
+
exports.default = validators;
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@autofleet/sadot",
|
|
3
|
+
"version": "0.0.1-beta",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "ts-node src/index.ts",
|
|
8
|
+
"build": "rm -rf dist && tsc",
|
|
9
|
+
"linter": "./node_modules/.bin/eslint .",
|
|
10
|
+
"test": "jest --forceExit --runInBand",
|
|
11
|
+
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
|
12
|
+
"dev": "nodemon"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@autofleet/errors": "^1.0.10",
|
|
16
|
+
"@autofleet/events": "^2.0.0",
|
|
17
|
+
"@autofleet/logger": "^2.0.5",
|
|
18
|
+
"@hapi/joi": "^17.1.1",
|
|
19
|
+
"express": "^4.18.2",
|
|
20
|
+
"pg": "^8.10.0",
|
|
21
|
+
"reflect-metadata": "^0.1.13",
|
|
22
|
+
"sequelize": "^6.31.1",
|
|
23
|
+
"sequelize-typescript": "^2.1.5",
|
|
24
|
+
"uuid": "^9.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/express": "^4.17.17",
|
|
28
|
+
"@types/jest": "^27.0.9",
|
|
29
|
+
"@types/node": "^20.5.6",
|
|
30
|
+
"@types/uuid": "^9.0.2",
|
|
31
|
+
"@typescript-eslint/eslint-plugin": "^4.8.1",
|
|
32
|
+
"@typescript-eslint/parser": "^4.33.0",
|
|
33
|
+
"eslint": "^7.13.0",
|
|
34
|
+
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
35
|
+
"eslint-plugin-import": "^2.22.1",
|
|
36
|
+
"jest": "^27.0.5",
|
|
37
|
+
"ts-jest": "^27.0.3",
|
|
38
|
+
"ts-node": "^8.6.2",
|
|
39
|
+
"typescript": "^4.9.5",
|
|
40
|
+
"typescript-eslint": "^0.0.1-alpha.0"
|
|
41
|
+
},
|
|
42
|
+
"author": "Autofleet",
|
|
43
|
+
"license": "ISC"
|
|
44
|
+
}
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ResourceNotFoundError } from '@autofleet/errors';
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
import type { Request, Response } from 'express';
|
|
4
|
+
import handleError from '../errors';
|
|
5
|
+
import * as DefinitionRepo from '../../../repository/definition';
|
|
6
|
+
import { CreateCustomFieldDefinition, UpdateCustomFieldDefinition } from '../../../types/definition';
|
|
7
|
+
import { validateCustomFieldDefinitionCreation, validateCustomFieldDefinitionUpdate } from './validations';
|
|
8
|
+
import logger from '../../../utils/logger';
|
|
9
|
+
|
|
10
|
+
const router = Router({ mergeParams: true });
|
|
11
|
+
const ENTITY = 'CustomFieldDefinition';
|
|
12
|
+
|
|
13
|
+
const toPascalCase = (str: string): string => str.replace(/(^\w|-\w)/g, (subStr) => subStr.replace(/-/, '').toUpperCase());
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create
|
|
17
|
+
*/
|
|
18
|
+
router.post('/', async (req: Request, res: Response) => {
|
|
19
|
+
const { modelName } = req.params as any;
|
|
20
|
+
const modelType = toPascalCase(modelName);
|
|
21
|
+
try {
|
|
22
|
+
const validatedPayload: CreateCustomFieldDefinition = await
|
|
23
|
+
validateCustomFieldDefinitionCreation(req.body);
|
|
24
|
+
|
|
25
|
+
const customFieldDefinition = await DefinitionRepo.create({
|
|
26
|
+
...validatedPayload,
|
|
27
|
+
modelType,
|
|
28
|
+
});
|
|
29
|
+
return res.status(201).json(customFieldDefinition);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
logger.error('Failed to create custom field definition', err);
|
|
32
|
+
return handleError(err, res, { message: `Error in create ${ENTITY} request` });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get by id
|
|
38
|
+
*/
|
|
39
|
+
router.get('/:customFieldDefinitionId', async (req, res) => {
|
|
40
|
+
const { customFieldDefinitionId } = req.params;
|
|
41
|
+
try {
|
|
42
|
+
const customFieldDefinition = await DefinitionRepo.findById(customFieldDefinitionId);
|
|
43
|
+
|
|
44
|
+
if (!customFieldDefinition) {
|
|
45
|
+
throw new ResourceNotFoundError();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return res.json(customFieldDefinition);
|
|
49
|
+
} catch (err) {
|
|
50
|
+
logger.error('Failed to fetch custom field definition', err);
|
|
51
|
+
return handleError(err, res, { message: `Error in get ${ENTITY} request` });
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get all
|
|
57
|
+
*/
|
|
58
|
+
router.get('/', async (req, res) => {
|
|
59
|
+
const { modelName } = req.params as any;
|
|
60
|
+
const { entityIds } = req.query as any;
|
|
61
|
+
|
|
62
|
+
const modelType = toPascalCase(modelName);
|
|
63
|
+
try {
|
|
64
|
+
const where: any = { modelType };
|
|
65
|
+
if (entityIds?.length > 0) {
|
|
66
|
+
where.entityId = entityIds;
|
|
67
|
+
}
|
|
68
|
+
const customFieldDefinitions = await DefinitionRepo.findAll(
|
|
69
|
+
{ ...where },
|
|
70
|
+
{ withDisabled: true },
|
|
71
|
+
);
|
|
72
|
+
return res.json(customFieldDefinitions);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
logger.error('Failed to fetch custom field definitions', err);
|
|
75
|
+
return handleError(err, res, { message: `Error in get all ${ENTITY} request` });
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Update
|
|
81
|
+
*/
|
|
82
|
+
router.patch('/:customFieldDefinitionId', async (req, res) => {
|
|
83
|
+
const { customFieldDefinitionId, modelName } = req.params as any;
|
|
84
|
+
const modelType = toPascalCase(modelName);
|
|
85
|
+
try {
|
|
86
|
+
// eslint-disable-next-line max-len
|
|
87
|
+
const validatedPayload: UpdateCustomFieldDefinition = await validateCustomFieldDefinitionUpdate(req.body);
|
|
88
|
+
|
|
89
|
+
const customFieldDefinition = await DefinitionRepo.findByWhere({
|
|
90
|
+
id: customFieldDefinitionId,
|
|
91
|
+
modelType,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (!customFieldDefinition) {
|
|
95
|
+
throw new ResourceNotFoundError();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const updatedCustomFieldDefinition = await DefinitionRepo.update(
|
|
99
|
+
customFieldDefinitionId,
|
|
100
|
+
{ ...validatedPayload, modelType },
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
return res.status(200).json(updatedCustomFieldDefinition);
|
|
104
|
+
} catch (err) {
|
|
105
|
+
logger.error('Failed to patch custom field definition', err);
|
|
106
|
+
return handleError(err, res, { message: `Error in update ${ENTITY} request` });
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export default router;
|