@flowerforce/flowerbase 1.4.0 → 1.4.1-beta.1

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.
@@ -22,6 +22,7 @@ type Config = {
22
22
  isAutoTrigger?: boolean;
23
23
  match: Record<string, unknown>;
24
24
  operation_types: string[];
25
+ operation_type?: 'CREATE';
25
26
  project: Record<string, unknown>;
26
27
  service_name: string;
27
28
  skip_catchup_events: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAE5D,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,WAAW,CAAA;IACjB,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB,EAAE;QAChB,QAAQ,EAAE;YACR,MAAM,EAAE;gBACN,aAAa,EAAE,MAAM,CAAA;aACtB,CAAA;SACF,CAAA;KACF,CAAA;CACF;AAED,KAAK,MAAM,GAAG;IACZ,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,aAAa,EAAE,OAAO,CAAA;IACtB,2BAA2B,EAAE,OAAO,CAAA;IACpC,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAChC,YAAY,EAAE,MAAM,CAAA;IACpB,mBAAmB,EAAE,OAAO,CAAA;IAC5B,sBAAs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B,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,cAAc,CAAC,EAAE,QAAQ,CAAA;IACzB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAChC,YAAY,EAAE,MAAM,CAAA;IACpB,mBAAmB,EAAE,OAAO,CAAA;IAC5B,sBAAsB,EAAE,OAAO,CAAA;IAC/B,SAAS,EAAE,OAAO,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,UAAU,GAAG,gBAAgB,CAAA;AACrE,MAAM,MAAM,QAAQ,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,EAAE,CAAA;AAE/D,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,cAAc,EAAE,QAAQ,CAAA;IACxB,GAAG,EAAE,eAAe,CAAA;IACpB,QAAQ,EAAE,QAAQ,CAAA;IAClB,aAAa,EAAE,SAAS,CAAA;CACzB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AAqC9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAkB5E,CAAA;AA0OD,eAAO,MAAM,gBAAgB;0EAtN1B,aAAa;yEAiKb,aAAa;+EA5Ib,aAAa;CAqMf,CAAA"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/features/triggers/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAW,QAAQ,EAAE,MAAM,aAAa,CAAA;AAqC9D;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAU,gBAAuB,KAAG,OAAO,CAAC,QAAQ,CAkB5E,CAAA;AAwQD,eAAO,MAAM,gBAAgB;0EApP1B,aAAa;yEA+Lb,aAAa;+EA9Jb,aAAa;CAuNf,CAAA"}
@@ -117,20 +117,37 @@ const handleCronTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ c
117
117
  }));
118
118
  registerOnClose(app, () => task.stop(), 'Scheduled trigger');
119
119
  });
120
+ const mapOp = {
121
+ insert: 'CREATE',
122
+ update: 'UPDATE',
123
+ replace: 'REPLACE'
124
+ };
125
+ const mapOpInverse = {
126
+ CREATE: ['insert', 'update'],
127
+ // LOGIN
128
+ // DELETE
129
+ };
120
130
  const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, function* ({ config, triggerHandler, functionsList, services, app }) {
121
131
  var _b;
122
- const { database, isAutoTrigger } = config;
132
+ const { database, isAutoTrigger, operation_types, operation_type } = config;
123
133
  const authCollection = (_b = constants_1.AUTH_CONFIG.authCollection) !== null && _b !== void 0 ? _b : 'auth_users';
124
134
  const collection = app.mongo.client.db(database || constants_1.DB_NAME).collection(authCollection);
125
135
  const pipeline = [
126
136
  {
127
137
  $match: {
128
- operationType: { $in: ['insert', 'update', 'replace'] }
138
+ operationType: {
139
+ $in: operation_type
140
+ ? mapOpInverse[operation_type]
141
+ : operation_types
142
+ }
129
143
  }
130
144
  }
131
145
  ];
132
146
  const changeStream = collection.watch(pipeline, {
133
- fullDocument: 'whenAvailable'
147
+ fullDocument: 'whenAvailable',
148
+ fullDocumentBeforeChange: config.full_document_before_change
149
+ ? 'whenAvailable'
150
+ : undefined
134
151
  });
135
152
  changeStream.on('error', (error) => {
136
153
  if (shouldIgnoreStreamError(error))
@@ -143,6 +160,7 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
143
160
  const operationType = change['operationType'];
144
161
  const documentKey = change['documentKey'];
145
162
  const fullDocument = change['fullDocument'];
163
+ const fullDocumentBeforeChange = change['fullDocumentBeforeChange'];
146
164
  if (!(documentKey === null || documentKey === void 0 ? void 0 : documentKey._id)) {
147
165
  return;
148
166
  }
@@ -190,10 +208,16 @@ const handleAuthenticationTrigger = (_a) => __awaiter(void 0, [_a], void 0, func
190
208
  _id: currentUser._id.toString(),
191
209
  email: currentUser.email
192
210
  } });
193
- // TODO change va ripulito
211
+ const op = {
212
+ operationType: mapOp[operationType],
213
+ fullDocument,
214
+ fullDocumentBeforeChange,
215
+ documentKey,
216
+ updateDescription
217
+ };
194
218
  try {
195
219
  yield (0, context_1.GenerateContext)({
196
- args: isAutoTrigger ? [userData] : [{ user: userData /*, ...change */ }],
220
+ args: isAutoTrigger ? [userData] : [Object.assign({ user: userData }, op)],
197
221
  app,
198
222
  rules: state_1.StateManager.select("rules"),
199
223
  user: {}, // TODO from currentUser ??
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowerforce/flowerbase",
3
- "version": "1.4.0",
3
+ "version": "1.4.1-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,6 +24,7 @@ type Config = {
24
24
  isAutoTrigger?: boolean
25
25
  match: Record<string, unknown>
26
26
  operation_types: string[]
27
+ operation_type?: 'CREATE'
27
28
  project: Record<string, unknown>
28
29
  service_name: string
29
30
  skip_catchup_events: boolean
@@ -103,6 +103,18 @@ const handleCronTrigger = async ({
103
103
  registerOnClose(app, () => task.stop(), 'Scheduled trigger')
104
104
  }
105
105
 
106
+ const mapOp = {
107
+ insert: 'CREATE',
108
+ update: 'UPDATE',
109
+ replace: 'REPLACE'
110
+ }
111
+
112
+ const mapOpInverse = {
113
+ CREATE: ['insert', 'update'],
114
+ // LOGIN
115
+ // DELETE
116
+ }
117
+
106
118
  const handleAuthenticationTrigger = async ({
107
119
  config,
108
120
  triggerHandler,
@@ -110,31 +122,41 @@ const handleAuthenticationTrigger = async ({
110
122
  services,
111
123
  app
112
124
  }: HandlerParams) => {
113
- const { database, isAutoTrigger } = config
125
+ const { database, isAutoTrigger, operation_types, operation_type } = config
114
126
  const authCollection = AUTH_CONFIG.authCollection ?? 'auth_users'
115
127
  const collection = app.mongo.client.db(database || DB_NAME).collection(authCollection)
116
128
  const pipeline = [
117
129
  {
118
130
  $match: {
119
- operationType: { $in: ['insert', 'update', 'replace'] }
131
+ operationType: {
132
+ $in: operation_type
133
+ ? mapOpInverse[operation_type]
134
+ : operation_types
135
+ }
120
136
  }
121
137
  }
122
138
  ]
123
139
  const changeStream = collection.watch(pipeline, {
124
- fullDocument: 'whenAvailable'
140
+ fullDocument: 'whenAvailable',
141
+ fullDocumentBeforeChange: config.full_document_before_change
142
+ ? 'whenAvailable'
143
+ : undefined
125
144
  })
126
145
  changeStream.on('error', (error) => {
127
146
  if (shouldIgnoreStreamError(error)) return
128
147
  console.error('Authentication trigger change stream error', error)
129
148
  })
130
149
  changeStream.on('change', async function (change) {
131
- const operationType = change['operationType' as keyof typeof change] as string | undefined
150
+ const operationType = change['operationType' as keyof typeof change] as 'insert' | 'update' | 'replace'
132
151
  const documentKey = change['documentKey' as keyof typeof change] as
133
152
  | { _id?: unknown }
134
153
  | undefined
135
154
  const fullDocument = change['fullDocument' as keyof typeof change] as
136
155
  | Record<string, unknown>
137
156
  | null
157
+ const fullDocumentBeforeChange = change['fullDocumentBeforeChange' as keyof typeof change] as
158
+ | Record<string, unknown>
159
+ | null
138
160
  if (!documentKey?._id) {
139
161
  return
140
162
  }
@@ -200,10 +222,18 @@ const handleAuthenticationTrigger = async ({
200
222
  email: (currentUser as { email?: string }).email
201
223
  }
202
224
  }
203
- // TODO change va ripulito
225
+
226
+ const op = {
227
+ operationType: mapOp[operationType],
228
+ fullDocument,
229
+ fullDocumentBeforeChange,
230
+ documentKey,
231
+ updateDescription
232
+ }
233
+
204
234
  try {
205
235
  await GenerateContext({
206
- args: isAutoTrigger ? [userData] : [{ user: userData /*, ...change */ }],
236
+ args: isAutoTrigger ? [userData] : [{ user: userData, ...op }],
207
237
  app,
208
238
  rules: StateManager.select("rules"),
209
239
  user: {}, // TODO from currentUser ??