@autofleet/sadot 1.0.0 → 1.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.
@@ -4,36 +4,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const joi_1 = __importDefault(require("joi"));
7
- // Schema for validating JSON Schema objects
8
7
  const jsonSchemaValidation = joi_1.default.object().unknown(true);
8
+ const schemaObject = joi_1.default.object({
9
+ type: joi_1.default.string().valid('object'),
10
+ properties: joi_1.default.object({
11
+ before: jsonSchemaValidation,
12
+ after: jsonSchemaValidation,
13
+ }).required(),
14
+ required: joi_1.default.array().items(joi_1.default.string()), // 👈 כאן הוספנו
15
+ allOf: joi_1.default.array().items(joi_1.default.object()), // 👈 אופציונלי
16
+ anyOf: joi_1.default.array().items(joi_1.default.object()), // 👈 אופציונלי
17
+ oneOf: joi_1.default.array().items(joi_1.default.object()), // 👈 אופציונלי
18
+ additionalProperties: joi_1.default.alternatives().try(joi_1.default.boolean(), joi_1.default.object()),
19
+ $id: joi_1.default.string(),
20
+ $schema: joi_1.default.string(),
21
+ if: joi_1.default.object(),
22
+ then: joi_1.default.object(),
23
+ else: joi_1.default.object(),
24
+ });
9
25
  const validationSchemas = {
10
26
  create: joi_1.default.object({
11
27
  entityId: joi_1.default.string().uuid().required(),
12
28
  entityType: joi_1.default.string().required(),
13
- schema: joi_1.default.object({
14
- type: joi_1.default.string().valid('object').required(),
15
- properties: joi_1.default.object({
16
- before: jsonSchemaValidation,
17
- after: jsonSchemaValidation,
18
- }).required(),
19
- if: joi_1.default.object().optional(),
20
- then: joi_1.default.object().optional(),
21
- else: joi_1.default.object().optional(),
22
- }).required(),
29
+ schema: schemaObject.required(), // schema חייב להיות קיים
23
30
  }),
24
31
  update: joi_1.default.object({
25
32
  entityId: joi_1.default.string().uuid(),
26
33
  entityType: joi_1.default.string(),
27
- schema: joi_1.default.object({
28
- type: joi_1.default.string().valid('object'),
29
- properties: joi_1.default.object({
30
- before: jsonSchemaValidation,
31
- after: jsonSchemaValidation,
32
- }),
33
- if: joi_1.default.object().optional(),
34
- then: joi_1.default.object().optional(),
35
- else: joi_1.default.object().optional(),
36
- }),
34
+ schema: schemaObject,
37
35
  disabled: joi_1.default.boolean(),
38
36
  }).min(1),
39
37
  };
@@ -90,6 +90,19 @@ const getCompleteCustomFields = async (instance, options) => {
90
90
  }
91
91
  return instance.customFields || {};
92
92
  };
93
+ const buildBeforeFromInstance = (instance) => {
94
+ const beforeFull = { ...instance.dataValues };
95
+ const changedKeys = instance.changed?.() || [];
96
+ for (const k of changedKeys) {
97
+ const prevVal = instance.previous?.(k);
98
+ if (prevVal !== undefined)
99
+ beforeFull[k] = prevVal;
100
+ }
101
+ const prevCF = instance.previous?.('customFields');
102
+ if (prevCF !== undefined)
103
+ beforeFull.customFields = prevCF;
104
+ return beforeFull;
105
+ };
93
106
  const formatAjvErrors = (errors) => errors.reduce((acc, err) => {
94
107
  const basePath = (err.instancePath || '')
95
108
  .split('/')
@@ -153,10 +166,7 @@ const validateModel = async (instance, options, scopeAttributes, modelOptions =
153
166
  // For updates, get the previous values
154
167
  let originalValues = null;
155
168
  if (!isCreate) {
156
- // Create originalValues with our helper function
157
- originalValues = manualObjectCopy(instance.previous());
158
- // Add customFields separately
159
- originalValues.customFields = instance.previous('customFields') || {};
169
+ originalValues = buildBeforeFromInstance(instance);
160
170
  }
161
171
  // Get complete custom fields by merging DB values with update values
162
172
  // This is especially important for partial updates to ensure all related fields are available
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
@@ -31,8 +8,8 @@ const logger_1 = __importDefault(require("@autofleet/logger"));
31
8
  const logger = (0, logger_1.default)();
32
9
  async function tryAddingTraceIdMiddleware() {
33
10
  try {
34
- const { outbreak } = await Promise.resolve().then(() => __importStar(require('@autofleet/zehut')));
35
- logger.addContextMiddleware(() => ({ traceId: outbreak.getCurrentContextTraceId() }));
11
+ // const { outbreak } = await import('@autofleet/zehut');
12
+ // logger.addContextMiddleware(() => ({ traceId: outbreak.getCurrentContextTraceId() }));
36
13
  }
37
14
  catch (err) {
38
15
  logger.error('Failed to add traceId middleware', { err });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-beta",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,37 +1,36 @@
1
1
  import Joi from 'joi';
2
2
 
3
- // Schema for validating JSON Schema objects
4
3
  const jsonSchemaValidation = Joi.object().unknown(true);
5
4
 
5
+ const schemaObject = Joi.object({
6
+ type: Joi.string().valid('object'),
7
+ properties: Joi.object({
8
+ before: jsonSchemaValidation,
9
+ after: jsonSchemaValidation,
10
+ }).required(),
11
+ required: Joi.array().items(Joi.string()),
12
+ allOf: Joi.array().items(Joi.object()),
13
+ anyOf: Joi.array().items(Joi.object()),
14
+ oneOf: Joi.array().items(Joi.object()),
15
+ additionalProperties: Joi.alternatives().try(Joi.boolean(), Joi.object()),
16
+ $id: Joi.string(),
17
+ $schema: Joi.string(),
18
+ if: Joi.object(),
19
+ then: Joi.object(),
20
+ else: Joi.object(),
21
+ });
22
+
6
23
  const validationSchemas = {
7
24
  create: Joi.object({
8
25
  entityId: Joi.string().uuid().required(),
9
26
  entityType: Joi.string().required(),
10
- schema: Joi.object({
11
- type: Joi.string().valid('object').required(),
12
- properties: Joi.object({
13
- before: jsonSchemaValidation,
14
- after: jsonSchemaValidation,
15
- }).required(),
16
- if: Joi.object().optional(),
17
- then: Joi.object().optional(),
18
- else: Joi.object().optional(),
19
- }).required(),
27
+ schema: schemaObject.required(),
20
28
  }),
21
29
 
22
30
  update: Joi.object({
23
31
  entityId: Joi.string().uuid(),
24
32
  entityType: Joi.string(),
25
- schema: Joi.object({
26
- type: Joi.string().valid('object'),
27
- properties: Joi.object({
28
- before: jsonSchemaValidation,
29
- after: jsonSchemaValidation,
30
- }),
31
- if: Joi.object().optional(),
32
- then: Joi.object().optional(),
33
- else: Joi.object().optional(),
34
- }),
33
+ schema: schemaObject,
35
34
  disabled: Joi.boolean(),
36
35
  }).min(1),
37
36
  };
@@ -78,6 +78,25 @@ const getCompleteCustomFields = async (instance, options): Promise<Record<string
78
78
  return instance.customFields || {};
79
79
  };
80
80
 
81
+ const buildBeforeFromInstance = (instance: any) => {
82
+ const beforeFull: any = { ...instance.dataValues };
83
+
84
+ const changedKeys: string[] = instance.changed?.() || [];
85
+ changedKeys.forEach((key) => {
86
+ const prevVal = instance.previous?.(key);
87
+ if (prevVal !== undefined) {
88
+ beforeFull[key] = prevVal;
89
+ }
90
+ });
91
+
92
+ const prevCF = instance.previous?.('customFields');
93
+ if (prevCF !== undefined) {
94
+ beforeFull.customFields = prevCF;
95
+ }
96
+
97
+ return beforeFull;
98
+ };
99
+
81
100
  const formatAjvErrors = (
82
101
  errors: {
83
102
  instancePath?: string;
@@ -172,11 +191,7 @@ const validateModel = async (
172
191
  // For updates, get the previous values
173
192
  let originalValues = null;
174
193
  if (!isCreate) {
175
- // Create originalValues with our helper function
176
- originalValues = manualObjectCopy(instance.previous());
177
-
178
- // Add customFields separately
179
- originalValues.customFields = instance.previous('customFields') || {};
194
+ originalValues = buildBeforeFromInstance(instance);
180
195
  }
181
196
 
182
197
  // Get complete custom fields by merging DB values with update values