@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.
- package/package.json +1 -1
- package/src/functions.js +60 -53
package/package.json
CHANGED
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|