@oondemand/oon-core-back 0.3.29 → 0.3.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oondemand/oon-core-back",
3
- "version": "0.3.29",
3
+ "version": "0.3.30",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -1,14 +1,43 @@
1
1
  const Helpers = require("../utils/helpers");
2
2
  const registry = require("../registry");
3
3
 
4
+ function toPlainRecord(record) {
5
+ if (!record || typeof record !== "object") return {};
6
+ if (typeof record.toObject === "function") return record.toObject();
7
+ return { ...record };
8
+ }
9
+
4
10
  /**
5
11
  * Controllers HTTP de CRUD ligados a um service. Aplica validação
6
12
  * declarada via `defineValidation` antes de create/update.
13
+ *
14
+ * Atualizações podem chegar parcialmente pelo formulário em abas ou pelo
15
+ * datagrid. Nesses casos, a validação recebe o registro consolidado, enquanto
16
+ * o service persiste somente os campos enviados pelo cliente.
7
17
  */
8
18
  function createCrudController(entry, service) {
9
- const runValidation = async (data, op) => {
19
+ const runValidation = async (data, context) => {
20
+ const validate = registry.getValidation(entry.name);
21
+ if (validate) await validate(data, context);
22
+ };
23
+
24
+ const runUpdateValidation = async (id, changes, method) => {
10
25
  const validate = registry.getValidation(entry.name);
11
- if (validate) await validate(data, { op });
26
+ if (!validate) return;
27
+
28
+ const currentRecord = await service.getById(id);
29
+ const current = toPlainRecord(currentRecord);
30
+ const normalizedChanges = changes && typeof changes === "object" ? { ...changes } : {};
31
+ const consolidated = { ...current, ...normalizedChanges };
32
+
33
+ await validate(consolidated, {
34
+ op: "update",
35
+ method,
36
+ id,
37
+ current,
38
+ changes: normalizedChanges,
39
+ consolidated,
40
+ });
12
41
  };
13
42
 
14
43
  return {
@@ -30,19 +59,29 @@ function createCrudController(entry, service) {
30
59
  },
31
60
 
32
61
  async create(req, res) {
33
- await runValidation(req.body, "create");
34
- const registro = await service.create(req.body);
62
+ const data = req.body ?? {};
63
+ await runValidation(data, {
64
+ op: "create",
65
+ method: "post",
66
+ current: null,
67
+ changes: data,
68
+ consolidated: data,
69
+ });
70
+ const registro = await service.create(data);
35
71
  Helpers.sendResponse({ res, statusCode: 201, [entry.singular]: registro });
36
72
  },
37
73
 
38
74
  async update(req, res) {
39
- await runValidation(req.body, "update");
40
- const registro = await service.update(req.params.id, req.body);
75
+ const changes = req.body ?? {};
76
+ await runUpdateValidation(req.params.id, changes, "put");
77
+ const registro = await service.update(req.params.id, changes);
41
78
  Helpers.sendResponse({ res, statusCode: 200, [entry.singular]: registro });
42
79
  },
43
80
 
44
81
  async patch(req, res) {
45
- const registro = await service.update(req.params.id, req.body);
82
+ const changes = req.body ?? {};
83
+ await runUpdateValidation(req.params.id, changes, "patch");
84
+ const registro = await service.update(req.params.id, changes);
46
85
  Helpers.sendResponse({ res, statusCode: 200, [entry.singular]: registro });
47
86
  },
48
87