@oondemand/oon-core-back 0.3.66 → 0.3.68

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.66",
3
+ "version": "0.3.68",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oondemand/oon-platform.git",
@@ -13,6 +13,21 @@ function truncate(value, limit) {
13
13
  return `${value.slice(0, limit)}…[truncado:${value.length - limit}]`;
14
14
  }
15
15
 
16
+ function binarySummary(value) {
17
+ const size = Number(value?.byteLength ?? value?.length ?? 0);
18
+ const type = value?.constructor?.name || "Binary";
19
+ return `[${type}:${size} bytes]`;
20
+ }
21
+
22
+ function bsonObjectId(value) {
23
+ if (!value || typeof value !== "object") return null;
24
+ const isObjectId = value._bsontype === "ObjectId" || value.constructor?.name === "ObjectId";
25
+ if (!isObjectId) return null;
26
+ if (typeof value.toHexString === "function") return value.toHexString();
27
+ const text = String(value);
28
+ return /^[a-f\d]{24}$/i.test(text) ? text : null;
29
+ }
30
+
16
31
  function sanitizeIntegrationValue(value, options = {}, state = { depth: 0, seen: new WeakSet() }) {
17
32
  const config = { ...DEFAULTS, ...options };
18
33
  if (value == null || typeof value === "boolean" || typeof value === "number") return value;
@@ -28,6 +43,12 @@ function sanitizeIntegrationValue(value, options = {}, state = { depth: 0, seen:
28
43
  };
29
44
  }
30
45
  if (typeof value !== "object") return truncate(String(value), config.maxStringLength);
46
+
47
+ const objectId = bsonObjectId(value);
48
+ if (objectId) return objectId;
49
+ if (Buffer.isBuffer(value) || ArrayBuffer.isView(value)) return binarySummary(value);
50
+ if (value instanceof ArrayBuffer) return binarySummary(value);
51
+
31
52
  if (state.depth >= config.maxDepth) return "[limite-de-profundidade]";
32
53
  if (state.seen.has(value)) return "[referência-circular]";
33
54
  state.seen.add(value);
@@ -46,6 +46,11 @@ function normalizeList(definition = {}, index = 0) {
46
46
  statusCode: 422,
47
47
  });
48
48
  }
49
+ if (definition.filter !== undefined && typeof definition.filter !== "function") {
50
+ throw new GenericError(`O filtro da lista Omie ${key} deve ser uma função.`, {
51
+ statusCode: 422,
52
+ });
53
+ }
49
54
  if (!definition.call) {
50
55
  throw new GenericError(`A lista Omie ${key} deve referenciar uma chamada declarada.`, {
51
56
  statusCode: 422,
@@ -180,6 +185,7 @@ function describeOmieDefinitions(definitions = collectOmieDefinitions()) {
180
185
  direction: list.direction,
181
186
  targetModel: list.target.model,
182
187
  externalKey: list.target.externalKey,
188
+ filtered: typeof list.filter === "function",
183
189
  policies: list.policies,
184
190
  schedule: list.schedule || null,
185
191
  order: list.order,
@@ -108,6 +108,21 @@ function effectiveListMode(list, payload = {}) {
108
108
  return requested || list.mode;
109
109
  }
110
110
 
111
+ async function evaluateListFilter(item, filter, scope = {}) {
112
+ if (typeof filter !== "function") return { include: true, reason: "" };
113
+ const result = await filter(item, scope);
114
+ if (result && typeof result === "object" && Object.prototype.hasOwnProperty.call(result, "include")) {
115
+ return {
116
+ include: result.include !== false,
117
+ reason: String(result.reason || "filtered"),
118
+ };
119
+ }
120
+ return {
121
+ include: result !== false,
122
+ reason: result === false ? "filtered" : "",
123
+ };
124
+ }
125
+
111
126
  async function synchronizeOmieList(definitions, list, event, context = {}) {
112
127
  const mode = effectiveListMode(list, event.payload || {});
113
128
  if (!["import", "full", "incremental", "individual"].includes(mode)) {
@@ -177,11 +192,23 @@ async function synchronizeOmieList(definitions, list, event, context = {}) {
177
192
  for (const [index, item] of items.entries()) {
178
193
  let externalValue;
179
194
  try {
180
- const mapped = mapRecord(item, list.mapping || {}, {
195
+ const scope = {
181
196
  input,
182
197
  response: callResult.response || callResult.data,
183
198
  instanceId,
184
- });
199
+ index,
200
+ };
201
+ const filterResult = await evaluateListFilter(item, list.filter, scope);
202
+ if (!filterResult.include) {
203
+ summary.skipped += 1;
204
+ context.recordItem?.({
205
+ index,
206
+ status: "skipped",
207
+ reason: filterResult.reason || "filtered",
208
+ });
209
+ continue;
210
+ }
211
+ const mapped = mapRecord(item, list.mapping || {}, scope);
185
212
  const externalField = list.target.externalKey;
186
213
  externalValue = mapped[externalField]
187
214
  ?? getPath(item, list.sourceExternalKey || externalField);
@@ -491,6 +518,7 @@ module.exports = {
491
518
  clientFor,
492
519
  enqueueOmieCall,
493
520
  ensureOmieProviderRegistered,
521
+ evaluateListFilter,
494
522
  executeConfiguredCall,
495
523
  handleOmieWebhook,
496
524
  mapRecord,