@oondemand/oon-core-back 0.3.51 → 0.3.53

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.51",
3
+ "version": "0.3.53",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -1,8 +1,14 @@
1
+ "use strict";
2
+
1
3
  const express = require("express");
4
+ const fs = require("node:fs");
5
+ const path = require("node:path");
2
6
  const registry = require("../core/registry");
3
7
  const ctx = require("../core/context");
8
+ const corePackage = require("../../package.json");
4
9
 
5
10
  const router = express.Router();
11
+ const startedAt = new Date().toISOString();
6
12
 
7
13
  function modelSummary(entry) {
8
14
  return {
@@ -31,10 +37,79 @@ function appSummary() {
31
37
  };
32
38
  }
33
39
 
40
+ function firstValue(...values) {
41
+ return values.find((candidate) => typeof candidate === "string" && candidate.trim())?.trim();
42
+ }
43
+
44
+ function packageVersion(file) {
45
+ try {
46
+ const payload = JSON.parse(fs.readFileSync(file, "utf8"));
47
+ return typeof payload.version === "string" && payload.version.trim()
48
+ ? payload.version.trim()
49
+ : undefined;
50
+ } catch (_error) {
51
+ return undefined;
52
+ }
53
+ }
54
+
55
+ function centralVersion() {
56
+ const manifestRoot = ctx.config.appManifestPath
57
+ ? path.dirname(ctx.config.appManifestPath)
58
+ : undefined;
59
+ const candidates = [
60
+ manifestRoot ? path.join(manifestRoot, "package.json") : undefined,
61
+ path.join(process.cwd(), "package.json"),
62
+ path.join(process.cwd(), "..", "package.json"),
63
+ ].filter(Boolean);
64
+ const detected = candidates.map(packageVersion).find(Boolean);
65
+ const configuredServiceVersion = ctx.config.serviceVersion !== "0.0.0"
66
+ ? ctx.config.serviceVersion
67
+ : undefined;
68
+
69
+ return firstValue(
70
+ process.env.OON_CENTRAL_VERSION,
71
+ process.env.CENTRAL_VERSION,
72
+ detected,
73
+ configuredServiceVersion,
74
+ );
75
+ }
76
+
77
+ function runtimeSummary() {
78
+ return {
79
+ core: {
80
+ backendVersion: corePackage.version,
81
+ },
82
+ central: {
83
+ version: centralVersion(),
84
+ },
85
+ deployment: {
86
+ commit: firstValue(
87
+ process.env.OON_BUILD_COMMIT,
88
+ process.env.CENTRAL_COMMIT_SHA,
89
+ process.env.SOURCE_COMMIT_SHA,
90
+ process.env.GITHUB_SHA,
91
+ ),
92
+ releaseId: firstValue(process.env.OON_RELEASE_ID, process.env.RELEASE_ID),
93
+ builtAt: firstValue(
94
+ process.env.OON_BUILD_TIME,
95
+ process.env.BUILD_TIME,
96
+ process.env.SOURCE_UPDATED_AT,
97
+ ),
98
+ startedAt,
99
+ environment: firstValue(
100
+ process.env.OON_ENVIRONMENT,
101
+ process.env.DEPLOY_ENVIRONMENT,
102
+ process.env.NODE_ENV,
103
+ ),
104
+ },
105
+ };
106
+ }
107
+
34
108
  router.get("/metadata", (_req, res) => {
35
109
  res.json({
36
110
  service: { name: ctx.config.serviceName, version: ctx.config.serviceVersion },
37
111
  app: appSummary(),
112
+ runtime: runtimeSummary(),
38
113
  models: registry.listModels().map(modelSummary),
39
114
  collections: registry.listCollections(),
40
115
  documents: registry.listDocuments(),
@@ -76,3 +151,4 @@ router.get("/menus", (_req, res) => res.json({ results: registry.listMenus() }))
76
151
  router.get("/features", (_req, res) => res.json({ results: registry.listFeatures() }));
77
152
 
78
153
  module.exports = router;
154
+ module.exports.runtimeSummary = runtimeSummary;
@@ -474,7 +474,9 @@ function createFieldDescriptor(field) {
474
474
  ]);
475
475
  }
476
476
 
477
- if (field.readonly || field.computed) descriptor.immutable = true;
477
+ // Campos calculados precisam ser persistidos pelo próprio Core após o recálculo.
478
+ // A proteção contra alteração pelo usuário permanece em applyDomainMutation.
479
+ if (field.readonly && !field.computed) descriptor.immutable = true;
478
480
  if (field.unique) descriptor.unique = true;
479
481
  if (field.index) descriptor.index = true;
480
482
  if (typeof field.min === "number") descriptor.min = field.min;
@@ -352,7 +352,7 @@ async function assertAtomicInvariants(entry, candidate, current, session) {
352
352
  let parentQuery = parentEntry.mongooseModel.findOneAndUpdate(
353
353
  { _id: parentId },
354
354
  { $inc: { __oonInvariantVersion: 1 } },
355
- { new: true, session, projection: { [rule.parentField]: 1 } },
355
+ { new: true, session },
356
356
  );
357
357
  parentQuery = queryWithSession(parentQuery, session);
358
358
  const parent = await parentQuery.lean();
@@ -381,7 +381,16 @@ async function assertAtomicInvariants(entry, candidate, current, session) {
381
381
  ? Number(candidate?.[rule.sourceField] || 0)
382
382
  : 0;
383
383
  const total = existing + candidateValue;
384
- const limit = Number(parent?.[rule.parentField] || 0);
384
+ // Recalcula os campos computados do pai a partir das entradas persistidas.
385
+ // Isso evita validar contra totais derivados antigos ou ausentes em registros legados.
386
+ const parentState = applyDomainMutation({
387
+ rules: parentEntry.definition?.domainRules,
388
+ current: parent,
389
+ changes: {},
390
+ op: "update",
391
+ skipValidations: true,
392
+ }).consolidated;
393
+ const limit = Number(parentState?.[rule.parentField] || 0);
385
394
  if (total > limit + Number(rule.tolerance || 0)) invariantError(rule, total, limit);
386
395
  }
387
396
  }