@edgedev/firebase 2.0.22 → 2.0.23

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/edgeFirebase.js +62 -45
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "2.0.22",
3
+ "version": "2.0.23",
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": {
@@ -1,4 +1,4 @@
1
- const { onCall, HttpsError, logger, getFirestore, functions, admin, twilio, db, onSchedule, pubsub } = require('./config.js')
1
+ const { onCall, HttpsError, logger, getFirestore, functions, admin, twilio, db, onSchedule, onDocumentUpdated, pubsub } = require('./config.js')
2
2
 
3
3
  const authToken = process.env.TWILIO_AUTH_TOKEN
4
4
  const accountSid = process.env.TWILIO_SID
@@ -57,7 +57,8 @@ exports.topicQueue = onSchedule({ schedule: 'every 1 minutes', timeoutSeconds: 1
57
57
  }
58
58
  })
59
59
 
60
- exports.sendVerificationCode = functions.https.onCall(async (data, context) => {
60
+ exports.sendVerificationCode = onCall(async (request) => {
61
+ const data = request.data
61
62
  const code = (Math.floor(Math.random() * 1000000) + 1000000).toString().substring(1)
62
63
  const phone = formatPhoneNumber(data.phone)
63
64
 
@@ -87,7 +88,8 @@ exports.sendVerificationCode = functions.https.onCall(async (data, context) => {
87
88
  }
88
89
  })
89
90
 
90
- exports.verifyPhoneNumber = functions.https.onCall(async (data, context) => {
91
+ exports.verifyPhoneNumber = onCall(async (request) => {
92
+ const data = request.data
91
93
  const phone = data.phone
92
94
  const code = data.code
93
95
 
@@ -120,7 +122,7 @@ exports.verifyPhoneNumber = functions.https.onCall(async (data, context) => {
120
122
  }
121
123
  })
122
124
 
123
- exports.initFirestore = functions.https.onCall(async (data, context) => {
125
+ exports.initFirestore = onCall(async (request) => {
124
126
  // checks to see of the collections 'collection-data' and 'staged-users' exist if not will seed them with data
125
127
  const collectionData = await db.collection('collection-data').get()
126
128
  const stagedUsers = await db.collection('staged-users').get()
@@ -154,8 +156,10 @@ exports.initFirestore = functions.https.onCall(async (data, context) => {
154
156
  }
155
157
  })
156
158
 
157
- exports.removeNonRegisteredUser = functions.https.onCall(async (data, context) => {
158
- if (data.uid === context.auth.uid) {
159
+ exports.removeNonRegisteredUser = onCall(async (request) => {
160
+ const data = request.data
161
+ const auth = request.auth
162
+ if (data.uid === auth.uid) {
159
163
  const stagedUser = await db.collection('staged-users').doc(data.docId).get()
160
164
  if (stagedUser.exists) {
161
165
  const stagedUserData = stagedUser.data()
@@ -189,8 +193,10 @@ exports.removeNonRegisteredUser = functions.https.onCall(async (data, context) =
189
193
  return { success: false, message: 'Non-registered user not found.' }
190
194
  })
191
195
 
192
- exports.currentUserRegister = functions.https.onCall(async (data, context) => {
193
- if (data.uid === context.auth.uid) {
196
+ exports.currentUserRegister = onCall(async (request) => {
197
+ const data = request.data
198
+ const auth = request.auth
199
+ if (data.uid === auth.uid) {
194
200
  const stagedUser = await db.collection('staged-users').doc(data.registrationCode).get()
195
201
  if (!stagedUser.exists) {
196
202
  return { success: false, message: 'Registration code not found.' }
@@ -246,10 +252,11 @@ exports.checkOrgIdExists = onCall(async (request) => {
246
252
  return { exists: orgDoc.exists }
247
253
  })
248
254
 
249
- exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpdate(async (change, context) => {
250
- const eventId = context.eventId
255
+ exports.updateUser = onDocumentUpdated({ document: 'staged-users/{docId}', timeoutSeconds: 180 }, async (event) => {
256
+ const change = event.data
257
+ const eventId = event.id
251
258
  const eventRef = db.collection('events').doc(eventId)
252
- const stagedDocId = context.params.docId
259
+ const stagedDocId = event.params.docId
253
260
  let newData = change.after.data()
254
261
  const oldData = change.before.data()
255
262
 
@@ -286,7 +293,26 @@ exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpda
286
293
  if (Object.prototype.hasOwnProperty.call(newData, 'subCreate') && Object.values(newData.subCreate).length > 0) {
287
294
  const subCreate = newData.subCreate
288
295
  delete newData.subCreate
289
- const addedDoc = await db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue })
296
+ let newDocId = ''
297
+ if (Object.prototype.hasOwnProperty.call(newData, 'requestedOrgId')) {
298
+ newDocId = newData.requestedOrgId.toLowerCase()
299
+ delete newData.requestedOrgId
300
+ }
301
+ let addedDoc
302
+ if (newDocId) {
303
+ const docRef = db.collection(subCreate.rootPath).doc(newDocId)
304
+ const doc = await docRef.get()
305
+ if (!doc.exists) {
306
+ await docRef.set({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue })
307
+ addedDoc = docRef
308
+ }
309
+ else {
310
+ addedDoc = await db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue })
311
+ }
312
+ }
313
+ else {
314
+ addedDoc = await db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue })
315
+ }
290
316
  await db.collection(subCreate.rootPath).doc(addedDoc.id).update({ docId: addedDoc.id })
291
317
  delete newData.dynamicDocumentFieldValue
292
318
  const newRole = { [`${subCreate.rootPath}-${addedDoc.id}`]: { collectionPath: `${subCreate.rootPath}-${addedDoc.id}`, role: subCreate.role } }
@@ -316,42 +342,33 @@ exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpda
316
342
  await markProcessed(eventRef)
317
343
  })
318
344
 
319
- function setUser(userRef, newData, oldData, stagedDocId) {
320
- // IT's OK If "users" doesn't match exactly matched "staged-users" because this is only preventing
321
- // writing from outside the @edgdev/firebase functions, so discrepancies will be rare since
322
- // the package will prevent before it gets this far.
323
- return userRef.get().then((user) => {
324
- let userUpdate = { meta: newData.meta, stagedDocId }
325
-
326
- if (newData.meta && newData.meta.name) {
327
- const publicUserRef = db.collection('public-users').doc(newData.userId)
328
- const publicMeta = { name: newData.meta.name }
329
- publicUserRef.set({ uid: newData.uid, meta: publicMeta, collectionPaths: newData.collectionPaths, userId: newData.userId })
330
- }
345
+ async function setUser(userRef, newData, oldData, stagedDocId) {
331
346
 
332
- if (Object.prototype.hasOwnProperty.call(newData, 'roles')) {
333
- userUpdate = { ...userUpdate, roles: newData.roles }
334
- }
335
- if (Object.prototype.hasOwnProperty.call(newData, 'specialPermissions')) {
336
- userUpdate = { ...userUpdate, specialPermissions: newData.specialPermissions }
337
- }
347
+ const user = await userRef.get()
348
+ let userUpdate = { meta: newData.meta, stagedDocId }
338
349
 
339
- if (!oldData.userId) {
340
- userUpdate = { ...userUpdate, userId: newData.uid }
341
- }
342
- if (!user.exists) {
343
- return userRef.set(userUpdate)
344
- }
345
- else {
346
- return userRef.update(userUpdate)
347
- }
348
- })
349
- }
350
+ if (newData.meta && newData.meta.name) {
351
+ const publicUserRef = db.collection('public-users').doc(stagedDocId)
352
+ const publicMeta = { name: newData.meta.name }
353
+ publicUserRef.set({ uid: newData.uid, meta: publicMeta, collectionPaths: newData.collectionPaths, userId: stagedDocId })
354
+ }
350
355
 
351
- function shouldProcess(eventRef) {
352
- return eventRef.get().then((eventDoc) => {
353
- return !eventDoc.exists || !eventDoc.data().processed
354
- })
356
+ if (Object.prototype.hasOwnProperty.call(newData, 'roles')) {
357
+ userUpdate = { ...userUpdate, roles: newData.roles }
358
+ }
359
+ if (Object.prototype.hasOwnProperty.call(newData, 'specialPermissions')) {
360
+ userUpdate = { ...userUpdate, specialPermissions: newData.specialPermissions }
361
+ }
362
+
363
+ if (!oldData.userId) {
364
+ userUpdate = { ...userUpdate, userId: newData.uid }
365
+ }
366
+ if (!user.exists) {
367
+ return userRef.set(userUpdate)
368
+ }
369
+ else {
370
+ return userRef.update(userUpdate)
371
+ }
355
372
  }
356
373
 
357
374
  function markProcessed(eventRef) {