@reventlessdev/reventless-core 3.0.0-alpha.320 → 3.0.0-alpha.322

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,7 +4,10 @@ import * as Sury from "sury";
4
4
  import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
5
5
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
6
6
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
7
+ import * as Stdlib_Result from "@rescript/runtime/lib/es6/Stdlib_Result.js";
7
8
  import * as Effect from "effect/Effect";
9
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
10
+ import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js";
8
11
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
9
12
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
10
13
  import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
@@ -82,27 +85,63 @@ function Make(Spec) {
82
85
  return Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `retries exhausted for ` + id + `, and the abandonment command could not be published: ` + errMsg));
83
86
  }
84
87
  };
85
- let phase1 = events => {
86
- events.forEach(param => {
87
- Translation.collect(param[1], param[0]).forEach(param => {
88
- let id = param[0];
89
- let match = todoItems[id];
90
- if (match !== undefined) {
91
- return;
88
+ let personalFields = Pii$Reventless.recordFields(Spec.outboundItemSchema);
89
+ let fingerprintPurpose = `outboundWorkItem:` + Spec.name;
90
+ let warnedUnkeyed = {
91
+ contents: false
92
+ };
93
+ let storedKey = (key, capabilities) => {
94
+ if (Primitive_object.equal(personalFields, [])) {
95
+ return key;
96
+ }
97
+ let fingerprint = capabilities.secrets.fingerprint(fingerprintPurpose, key);
98
+ if (fingerprint.TAG === "Ok") {
99
+ return fingerprint._0;
100
+ }
101
+ let why = fingerprint._0._0;
102
+ if (Pii$Reventless.Provider.isRequired()) {
103
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `a work item was not stored: its row id cannot be fingerprinted (` + why + `)`));
104
+ return;
105
+ } else {
106
+ if (!warnedUnkeyed.contents) {
107
+ warnedUnkeyed.contents = true;
108
+ Effect.runSync(EffectLogger$ReventlessCore.logWarn(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `work items carrying personal data are keyed in the clear: no key to fingerprint them with (` + why + `)`));
109
+ }
110
+ return key;
111
+ }
112
+ };
113
+ let phase1 = async (events, capabilities) => {
114
+ for (let i = 0, i_finish = events.length; i < i_finish; ++i) {
115
+ let match = events[i];
116
+ let collected = Translation.collect(match[1], match[0]);
117
+ for (let j = 0, j_finish = collected.length; j < j_finish; ++j) {
118
+ let match$1 = collected[j];
119
+ let id = storedKey(match$1[0], capabilities);
120
+ if (id !== undefined && !Stdlib_Option.isSome(todoItems[id])) {
121
+ let json = Util_Sury$Reventless.toJson(match$1[1], Spec.outboundItemSchema);
122
+ let stored;
123
+ stored = typeof json === "object" && json !== null && !Array.isArray(json) && Primitive_object.notequal(personalFields, []) ? Stdlib_Result.map(await Pii$Reventless.sealRecord(Spec.outboundItemSchema, json), d => (d)) : ({
124
+ TAG: "Ok",
125
+ _0: json
126
+ });
127
+ if (stored.TAG === "Ok") {
128
+ let row_item = stored._0;
129
+ let row_createdAt = new Date().toISOString();
130
+ let row_maxRetries = Spec.maxRetries;
131
+ let row = {
132
+ item: row_item,
133
+ status: "Pending",
134
+ createdAt: row_createdAt,
135
+ retryCount: 0,
136
+ maxRetries: row_maxRetries
137
+ };
138
+ todoItems[id] = row;
139
+ } else {
140
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `a work item was not stored: ` + Pii$Reventless.errorMessage(stored._0)));
141
+ }
92
142
  }
93
- let row_item = Util_Sury$Reventless.toJson(param[1], Spec.outboundItemSchema);
94
- let row_createdAt = new Date().toISOString();
95
- let row_maxRetries = Spec.maxRetries;
96
- let row = {
97
- item: row_item,
98
- status: "Pending",
99
- createdAt: row_createdAt,
100
- retryCount: 0,
101
- maxRetries: row_maxRetries
102
- };
103
- todoItems[id] = row;
104
- });
105
- });
143
+ }
144
+ }
106
145
  };
107
146
  let phase2 = async (publishJsons, capabilities) => {
108
147
  let newlyAbandoned = [];
@@ -131,22 +170,75 @@ function Make(Spec) {
131
170
  let row = param[1];
132
171
  let id = param[0];
133
172
  await prev;
173
+ let data = row.item;
174
+ let opened;
175
+ if (typeof data === "object" && data !== null && !Array.isArray(data) && Primitive_object.notequal(personalFields, [])) {
176
+ let data$1 = await Pii$Reventless.openRecord(Spec.outboundItemSchema, data);
177
+ if (data$1.TAG === "Ok") {
178
+ let data$2 = data$1._0;
179
+ let field = Pii$Reventless.goneField(Spec.outboundItemSchema, data$2);
180
+ opened = field !== undefined ? ({
181
+ TAG: "Error",
182
+ _0: {
183
+ NAME: "Gone",
184
+ VAL: field
185
+ }
186
+ }) : ({
187
+ TAG: "Ok",
188
+ _0: data$2
189
+ });
190
+ } else {
191
+ opened = {
192
+ TAG: "Error",
193
+ _0: {
194
+ NAME: "Unopened",
195
+ VAL: Pii$Reventless.errorMessage(data$1._0)
196
+ }
197
+ };
198
+ }
199
+ } else {
200
+ opened = {
201
+ TAG: "Ok",
202
+ _0: data
203
+ };
204
+ }
134
205
  let item;
135
- try {
136
- item = Primitive_option.some(Util_Sury$Reventless.fromJson(row.item, Spec.outboundItemSchema));
137
- } catch (raw_exn) {
138
- let exn = Primitive_exceptions.internalToException(raw_exn);
139
- let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
140
- Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `failed to decode outboundItem: ` + errMsg));
141
- item = undefined;
206
+ if (opened.TAG === "Ok") {
207
+ try {
208
+ item = Primitive_option.some(Util_Sury$Reventless.fromJson(opened._0, Spec.outboundItemSchema));
209
+ } catch (raw_exn) {
210
+ let exn = Primitive_exceptions.internalToException(raw_exn);
211
+ let errMsg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
212
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `failed to decode outboundItem: ` + errMsg));
213
+ item = undefined;
214
+ }
215
+ } else {
216
+ let match = opened._0;
217
+ if (match.NAME === "Gone") {
218
+ Effect.runSync(EffectLogger$ReventlessCore.logInfo(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, id + `: its ` + match.VAL + ` was erased; nothing to do`));
219
+ let newrecord = {...row};
220
+ newrecord.completedAt = new Date().toISOString();
221
+ newrecord.status = "Completed";
222
+ todoItems[id] = newrecord;
223
+ item = undefined;
224
+ } else {
225
+ let why = match.VAL;
226
+ Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, id + `: its work item does not open: ` + why));
227
+ let updated = afterFailedAttempt(row, why);
228
+ todoItems[id] = updated;
229
+ if (updated.status === "Abandoned") {
230
+ newlyAbandoned.push(id);
231
+ }
232
+ item = undefined;
233
+ }
142
234
  }
143
235
  if (item === undefined) {
144
236
  return;
145
237
  }
146
- let newrecord = {...row};
147
- newrecord.processedAt = new Date().toISOString();
148
- newrecord.status = "Processing";
149
- todoItems[id] = newrecord;
238
+ let newrecord$1 = {...row};
239
+ newrecord$1.processedAt = new Date().toISOString();
240
+ newrecord$1.status = "Processing";
241
+ todoItems[id] = newrecord$1;
150
242
  let result;
151
243
  try {
152
244
  result = await Translation.translate(id, Primitive_option.valFromOption(item), capabilities);
@@ -159,11 +251,11 @@ function Make(Spec) {
159
251
  };
160
252
  }
161
253
  if (result.TAG === "Ok") {
162
- let match = result._0;
163
- if (match !== undefined) {
254
+ let match$1 = result._0;
255
+ if (match$1 !== undefined) {
164
256
  let commandJson;
165
257
  try {
166
- commandJson = Util_Sury$Reventless.toJson(match[1], Spec.inboundCommandSchema);
258
+ commandJson = Util_Sury$Reventless.toJson(match$1[1], Spec.inboundCommandSchema);
167
259
  } catch (raw_exn$2) {
168
260
  let exn$2 = Primitive_exceptions.internalToException(raw_exn$2);
169
261
  let errMsg$1 = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn$2), Stdlib_JsExn.message), "unknown");
@@ -172,7 +264,7 @@ function Make(Spec) {
172
264
  }
173
265
  if (commandJson !== undefined) {
174
266
  try {
175
- let msg_id = match[0];
267
+ let msg_id = match$1[0];
176
268
  let msg_meta = makeMeta();
177
269
  let msg$1 = {
178
270
  id: msg_id,
@@ -180,18 +272,18 @@ function Make(Spec) {
180
272
  commandJson: commandJson
181
273
  };
182
274
  await publishJsons([msg$1]);
183
- let newrecord$1 = {...row};
184
- newrecord$1.completedAt = new Date().toISOString();
185
- newrecord$1.status = "Completed";
186
- todoItems[id] = newrecord$1;
275
+ let newrecord$2 = {...row};
276
+ newrecord$2.completedAt = new Date().toISOString();
277
+ newrecord$2.status = "Completed";
278
+ todoItems[id] = newrecord$2;
187
279
  return;
188
280
  } catch (raw_exn$3) {
189
281
  let exn$3 = Primitive_exceptions.internalToException(raw_exn$3);
190
282
  let errMsg$2 = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn$3), Stdlib_JsExn.message), "unknown");
191
283
  Effect.runSync(EffectLogger$ReventlessCore.logError(`OutboundTranslationSlice(` + Spec.name + `)`, undefined, `failed to publish command: ` + errMsg$2));
192
- let updated = afterFailedAttempt(row, errMsg$2);
193
- todoItems[id] = updated;
194
- if (updated.status === "Abandoned") {
284
+ let updated$1 = afterFailedAttempt(row, errMsg$2);
285
+ todoItems[id] = updated$1;
286
+ if (updated$1.status === "Abandoned") {
195
287
  newlyAbandoned.push(id);
196
288
  return;
197
289
  } else {
@@ -199,9 +291,9 @@ function Make(Spec) {
199
291
  }
200
292
  }
201
293
  } else {
202
- let updated$1 = afterFailedAttempt(row, undefined);
203
- todoItems[id] = updated$1;
204
- if (updated$1.status === "Abandoned") {
294
+ let updated$2 = afterFailedAttempt(row, undefined);
295
+ todoItems[id] = updated$2;
296
+ if (updated$2.status === "Abandoned") {
205
297
  newlyAbandoned.push(id);
206
298
  return;
207
299
  } else {
@@ -209,16 +301,16 @@ function Make(Spec) {
209
301
  }
210
302
  }
211
303
  } else {
212
- let newrecord$2 = {...row};
213
- newrecord$2.completedAt = new Date().toISOString();
214
- newrecord$2.status = "Completed";
215
- todoItems[id] = newrecord$2;
304
+ let newrecord$3 = {...row};
305
+ newrecord$3.completedAt = new Date().toISOString();
306
+ newrecord$3.status = "Completed";
307
+ todoItems[id] = newrecord$3;
216
308
  return;
217
309
  }
218
310
  } else {
219
- let updated$2 = afterFailedAttempt(row, result._0);
220
- todoItems[id] = updated$2;
221
- if (updated$2.status === "Abandoned") {
311
+ let updated$3 = afterFailedAttempt(row, result._0);
312
+ todoItems[id] = updated$3;
313
+ if (updated$3.status === "Abandoned") {
222
314
  newlyAbandoned.push(id);
223
315
  return;
224
316
  } else {
@@ -21,3 +21,8 @@ type command = Join({@owner memberId: string, @pii name: string, @pii address: R
21
21
  // only ever opened, so it needs no subject.
22
22
  @schema
23
23
  type consumedEvent = CustomerRegistered({@pii email: Reventless.Email.t})
24
+
25
+ // An automation's work item: the type stays, and the mark keeps it out of logs
26
+ // and makes the work list seal it.
27
+ @schema
28
+ type todoItem = {memberId: string, @pii("memberId") name: string}
@@ -35,6 +35,11 @@ let consumedEventSchema = Sury.$schema(s => ({
35
35
  email: s.m(Pii$Reventless.marked("$stream", undefined, Email$Reventless.schema))
36
36
  }));
37
37
 
38
+ let todoItemSchema = Sury.$schema(s => ({
39
+ memberId: s.m(Sury.string),
40
+ name: s.m(Pii$Reventless.mark("memberId", undefined, Sury.string))
41
+ }));
42
+
38
43
  function authorizationOf(param) {
39
44
  return "AllowAuthenticated";
40
45
  }
@@ -59,6 +64,7 @@ export {
59
64
  eventSchema,
60
65
  commandSchema,
61
66
  consumedEventSchema,
67
+ todoItemSchema,
62
68
  moduleUrl,
63
69
  authorizationOf,
64
70
  readConsistency,
@@ -53,6 +53,14 @@ describe("@pii on an event", () => {
53
53
  ])
54
54
  })
55
55
 
56
+ testSync("an automation's work item is marked and keeps its type", () => {
57
+ let item: M.todoItem = {memberId: "m-1", name: "Ada"}
58
+ expect(
59
+ M.todoItemSchema->Reventless.Pii.recordFields->Array.map(((name, m)) => (name, m.subject)),
60
+ )->toEqual([("name", "memberId")])
61
+ expect(item.name)->toBe("Ada")
62
+ })
63
+
56
64
  testSync("a consumed field naming no subject is about the aggregate it came from", () =>
57
65
  expect(
58
66
  M.consumedEventSchema
@@ -110,6 +110,16 @@ globalThis.describe("@pii on an event", () => {
110
110
  "invitationId"
111
111
  ]]);
112
112
  });
113
+ globalThis.test("an automation's work item is marked and keeps its type", () => {
114
+ globalThis.expect(Pii$Reventless.recordFields(PiiMember$ReventlessCore.todoItemSchema).map(param => [
115
+ param[0],
116
+ param[1].subject
117
+ ])).toEqual([[
118
+ "name",
119
+ "memberId"
120
+ ]]);
121
+ globalThis.expect("Ada").toBe("Ada");
122
+ });
113
123
  globalThis.test("a consumed field naming no subject is about the aggregate it came from", () => {
114
124
  globalThis.expect(Pii$Reventless.variantFields(PiiMember$ReventlessCore.consumedEventSchema, "CustomerRegistered").map(param => [
115
125
  param[0],