@oondemand/oon-core-back 0.3.51 → 0.3.52

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.52",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -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
  }