@edgedev/firebase 1.9.19 → 1.9.20

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/functions.js +60 -53
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.9.19",
3
+ "version": "1.9.20",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 plugin for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {
package/src/functions.js CHANGED
@@ -201,68 +201,75 @@ exports.currentUserRegister = functions.https.onCall(async (data, context) => {
201
201
  }
202
202
  })
203
203
 
204
- exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpdate((change, context) => {
204
+ exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpdate(async (change, context) => {
205
205
  const eventId = context.eventId
206
206
  const eventRef = db.collection('events').doc(eventId)
207
207
  const stagedDocId = context.params.docId
208
208
  let newData = change.after.data()
209
209
  const oldData = change.before.data()
210
- return shouldProcess(eventRef).then((process) => {
211
- if (process) {
212
- // Note: we can trust on newData.uid because we are checking in rules that it matches the auth.uid
213
- if (newData.userId) {
214
- const userRef = db.collection('users').doc(newData.userId)
215
- setUser(userRef, newData, oldData, stagedDocId).then(() => {
216
- return markProcessed(eventRef)
217
- })
210
+
211
+ const shouldProcess = await eventRef.get().then((eventDoc) => {
212
+ return !eventDoc.exists || !eventDoc.data().processed
213
+ })
214
+
215
+ if (!shouldProcess) {
216
+ return null
217
+ }
218
+
219
+ // Note: we can trust on newData.uid because we are checking in rules that it matches the auth.uid
220
+ if (newData.userId) {
221
+ const userRef = db.collection('users').doc(newData.userId)
222
+ await setUser(userRef, newData, oldData, stagedDocId)
223
+ await markProcessed(eventRef)
224
+ }
225
+ else {
226
+ if (newData.templateUserId !== oldData.templateUserId) {
227
+ // Check if templateUserId already exists in the staged-users collection
228
+ const stagedUserRef = db.collection('staged-users').doc(newData.templateUserId)
229
+ const doc = await stagedUserRef.get()
230
+
231
+ // If it exists, skip the creation process
232
+ if (doc.exists) {
233
+ return null
218
234
  }
219
- else {
220
- if (newData.templateUserId !== oldData.templateUserId) {
221
- newData.isTemplate = false
222
- const templateUserId = newData.templateUserId
223
- newData.meta = newData.templateMeta
224
- delete newData.templateMeta
225
- delete newData.templateUserId
226
- if (Object.prototype.hasOwnProperty.call(newData, 'subCreate') && Object.values(newData.subCreate).length > 0) {
227
- const subCreate = newData.subCreate
228
- delete newData.subCreate
229
- db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue }).then((addedDoc) => {
230
- db.collection(subCreate.rootPath).doc(addedDoc.id).update({ docId: addedDoc.id }).then(() => {
231
- delete newData.dynamicDocumentFieldValue
232
- const newRole = { [`${subCreate.rootPath}-${addedDoc.id}`]: { collectionPath: `${subCreate.rootPath}-${addedDoc.id}`, role: subCreate.role } }
233
- if (Object.prototype.hasOwnProperty.call(newData, 'collectionPaths')) {
234
- newData.collectionPaths.push(`${subCreate.rootPath}-${addedDoc.id}`)
235
- }
236
- else {
237
- newData.collectionPaths = [`${subCreate.rootPath}-${addedDoc.id}`]
238
- }
239
- const newRoles = { ...newData.roles, ...newRole }
240
- newData = { ...newData, roles: newRoles }
241
- const stagedUserRef = db.collection('staged-users').doc(templateUserId)
242
- return stagedUserRef.set({ ...newData, userId: templateUserId }).then(() => {
243
- const userRef = db.collection('users').doc(templateUserId)
244
- setUser(userRef, newData, oldData, templateUserId).then(() => {
245
- return markProcessed(eventRef)
246
- })
247
- })
248
- })
249
- })
250
- }
251
- else {
252
- const stagedUserRef = db.collection('staged-users').doc(templateUserId)
253
- return stagedUserRef.set({ ...newData, userId: templateUserId }).then(() => {
254
- const userRef = db.collection('users').doc(templateUserId)
255
- setUser(userRef, newData, oldData, templateUserId).then(() => {
256
- return markProcessed(eventRef)
257
- })
258
- })
259
- }
235
+
236
+ newData.isTemplate = false
237
+ const templateUserId = newData.templateUserId
238
+ newData.meta = newData.templateMeta
239
+ delete newData.templateMeta
240
+ delete newData.templateUserId
241
+ if (Object.prototype.hasOwnProperty.call(newData, 'subCreate') && Object.values(newData.subCreate).length > 0) {
242
+ const subCreate = newData.subCreate
243
+ delete newData.subCreate
244
+ const addedDoc = await db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue })
245
+ await db.collection(subCreate.rootPath).doc(addedDoc.id).update({ docId: addedDoc.id })
246
+ delete newData.dynamicDocumentFieldValue
247
+ const newRole = { [`${subCreate.rootPath}-${addedDoc.id}`]: { collectionPath: `${subCreate.rootPath}-${addedDoc.id}`, role: subCreate.role } }
248
+ if (Object.prototype.hasOwnProperty.call(newData, 'collectionPaths')) {
249
+ newData.collectionPaths.push(`${subCreate.rootPath}-${addedDoc.id}`)
250
+ }
251
+ else {
252
+ newData.collectionPaths = [`${subCreate.rootPath}-${addedDoc.id}`]
260
253
  }
254
+ const newRoles = { ...newData.roles, ...newRole }
255
+ newData = { ...newData, roles: newRoles }
256
+ const stagedUserRef = db.collection('staged-users').doc(templateUserId)
257
+ await stagedUserRef.set({ ...newData, userId: templateUserId })
258
+ const userRef = db.collection('users').doc(templateUserId)
259
+ await setUser(userRef, newData, oldData, templateUserId)
260
+ await markProcessed(eventRef)
261
+ }
262
+ else {
263
+ const stagedUserRef = db.collection('staged-users').doc(templateUserId)
264
+ await stagedUserRef.set({ ...newData, userId: templateUserId })
265
+ const userRef = db.collection('users').doc(templateUserId)
266
+ await setUser(userRef, newData, oldData, templateUserId)
267
+ await markProcessed(eventRef)
261
268
  }
262
- return markProcessed(eventRef)
263
269
  }
264
- })
265
- })
270
+ }
271
+ await markProcessed(eventRef)
272
+ }
266
273
 
267
274
  function setUser(userRef, newData, oldData, stagedDocId) {
268
275
  // IT's OK If "users" doesn't match exactly matched "staged-users" because this is only preventing