@edgedev/create-edge-app 1.0.18 → 1.0.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.
@@ -1,226 +1,7 @@
1
- const functions = require('firebase-functions')
2
- const admin = require('firebase-admin')
3
- admin.initializeApp()
4
- const db = admin.firestore()
5
- // START @edge/firebase functions
6
-
7
- exports.initFirestore = functions.https.onCall(async (data, context) => {
8
- // checks to see of the collections 'collection-data' and 'staged-users' exist if not will seed them with data
9
- const collectionData = await db.collection('collection-data').get()
10
- const stagedUsers = await db.collection('staged-users').get()
11
- if (collectionData.empty) {
12
- // create a document with the id of '-' and one called '-default-':
13
- const admin = { assign: true, delete: true, read: true, write: true }
14
- const editor = { assign: false, delete: true, read: true, write: true }
15
- const writer = { assign: false, delete: false, read: true, write: true }
16
- const user = { assign: false, delete: false, read: true, write: false }
17
- await db.collection('collection-data').doc('-').set({ admin, editor, writer, user })
18
- await db.collection('collection-data').doc('-default-').set({ admin, editor, writer, user })
19
- }
20
- if (stagedUsers.empty) {
21
- const templateUser = {
22
- docId: 'organization-registration-template',
23
- isTemplate: true,
24
- meta: {
25
- name: 'Organization Registration Template',
26
- },
27
- subCreate: {
28
- documentStructure: {
29
- name: '',
30
- },
31
- dynamicDocumentField: 'name',
32
- role: 'admin',
33
- rootPath: 'organizations',
34
- },
35
- userId: '',
36
- }
37
- await db.collection('staged-users').doc('organization-registration-template').set(templateUser)
38
- }
39
- })
40
-
41
- exports.removeNonRegisteredUser = functions.https.onCall(async (data, context) => {
42
- if (data.uid === context.auth.uid) {
43
- const stagedUser = await db.collection('staged-users').doc(data.docId).get()
44
- if (stagedUser.exists) {
45
- const stagedUserData = stagedUser.data()
46
-
47
- const rolesExist = stagedUserData.roles && Object.keys(stagedUserData.roles).length !== 0
48
- const specialPermissionsExist = stagedUserData.specialPermissions && Object.keys(stagedUserData.specialPermissions).length !== 0
49
- const userIdExistsAndNotBlank = stagedUserData.userId && stagedUserData.userId !== ''
50
-
51
- if (!rolesExist && !specialPermissionsExist && !userIdExistsAndNotBlank) {
52
- await db.collection('staged-users').doc(data.docId).delete()
53
- return { success: true, message: '' }
54
- }
55
- else {
56
- let message = ''
57
- if (rolesExist && specialPermissionsExist) {
58
- message = 'Cannot delete because the non-registered user still has roles and special permissions assigned.'
59
- }
60
- else if (rolesExist) {
61
- message = 'Cannot delete because the non-registered user still has roles assigned.'
62
- }
63
- else if (specialPermissionsExist) {
64
- message = 'Cannot delete because the non-registered user still has special permissions assigned.'
65
- }
66
- else if (userIdExistsAndNotBlank) {
67
- message = 'Cannot delete because the user is registered.'
68
- }
69
- return { success: false, message }
70
- }
71
- }
72
- }
73
- return { success: false, message: 'Non-registered user not found.' }
74
- })
75
-
76
- exports.currentUserRegister = functions.https.onCall(async (data, context) => {
77
- if (data.uid === context.auth.uid) {
78
- const stagedUser = await db.collection('staged-users').doc(data.registrationCode).get()
79
- if (!stagedUser.exists) {
80
- return { success: false, message: 'Registration code not found.' }
81
- }
82
- else {
83
- const stagedUserData = await stagedUser.data()
84
- let process = false
85
- if (stagedUserData.isTemplate) {
86
- process = true
87
- }
88
- if (!stagedUserData.isTemplate && stagedUserData.userId === '') {
89
- process = true
90
- }
91
- if (!process) {
92
- return { success: false, message: 'Registration code not valid.' }
93
- }
94
- const newRoles = stagedUserData.roles || {}
95
- const currentUser = await db.collection('users').doc(data.uid).get()
96
- const currentUserData = await currentUser.data()
97
- const currentRoles = currentUserData.roles || {}
98
- const currentUserCollectionPaths = currentUserData.collectionPaths || []
99
- let newRole = {}
100
- if (stagedUserData.subCreate && Object.keys(stagedUserData.subCreate).length !== 0 && stagedUserData.isTemplate) {
101
- if (!data.dynamicDocumentFieldValue) {
102
- return { success: false, message: 'Dynamic document field value is required.' }
103
- }
104
- const rootPath = stagedUserData.subCreate.rootPath
105
- const newDoc = stagedUserData.subCreate.documentStructure
106
- newDoc[stagedUserData.subCreate.dynamicDocumentField] = data.dynamicDocumentFieldValue
107
- const addedDoc = await db.collection(rootPath).add(newDoc)
108
- await db.collection(rootPath).doc(addedDoc.id).update({ docId: addedDoc.id })
109
- newRole = { [`${rootPath}-${addedDoc.id}`]: { collectionPath: `${rootPath}-${addedDoc.id}`, role: stagedUserData.subCreate.role } }
110
- }
111
- const combinedRoles = { ...currentRoles, ...newRoles, ...newRole }
112
- Object.values(combinedRoles).forEach((role) => {
113
- if (!currentUserCollectionPaths.includes(role.collectionPath)) {
114
- currentUserCollectionPaths.push(role.collectionPath)
115
- }
116
- })
117
- await db.collection('staged-users').doc(currentUserData.stagedDocId).update({ roles: combinedRoles, collectionPaths: currentUserCollectionPaths })
118
- if (!stagedUserData.isTemplate) {
119
- await db.collection('staged-users').doc(data.registrationCode).delete()
120
- }
121
- return { success: true, message: '' }
122
- }
123
- }
124
- })
1
+ require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? '.env.prod' : '.env.dev' })
125
2
 
126
- exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpdate((change, context) => {
127
- const eventId = context.eventId
128
- const eventRef = db.collection('events').doc(eventId)
129
- const stagedDocId = context.params.docId
130
- let newData = change.after.data()
131
- const oldData = change.before.data()
132
- return shouldProcess(eventRef).then((process) => {
133
- if (process) {
134
- // Note: we can trust on newData.uid because we are checking in rules that it matches the auth.uid
135
- if (newData.userId) {
136
- const userRef = db.collection('users').doc(newData.userId)
137
- setUser(userRef, newData, oldData, stagedDocId).then(() => {
138
- return markProcessed(eventRef)
139
- })
140
- }
141
- else {
142
- if (newData.templateUserId !== oldData.templateUserId) {
143
- newData.isTemplate = false
144
- const templateUserId = newData.templateUserId
145
- newData.meta = newData.templateMeta
146
- delete newData.templateMeta
147
- delete newData.templateUserId
148
- if (Object.prototype.hasOwnProperty.call(newData, 'subCreate') && Object.values(newData.subCreate).length > 0) {
149
- const subCreate = newData.subCreate
150
- delete newData.subCreate
151
- db.collection(subCreate.rootPath).add({ [subCreate.dynamicDocumentField]: newData.dynamicDocumentFieldValue }).then((addedDoc) => {
152
- db.collection(subCreate.rootPath).doc(addedDoc.id).update({ docId: addedDoc.id }).then(() => {
153
- delete newData.dynamicDocumentFieldValue
154
- const newRole = { [`${subCreate.rootPath}-${addedDoc.id}`]: { collectionPath: `${subCreate.rootPath}-${addedDoc.id}`, role: subCreate.role } }
155
- if (Object.prototype.hasOwnProperty.call(newData, 'collectionPaths')) {
156
- newData.collectionPaths.push(`${subCreate.rootPath}-${addedDoc.id}`)
157
- }
158
- else {
159
- newData.collectionPaths = [`${subCreate.rootPath}-${addedDoc.id}`]
160
- }
161
- const newRoles = { ...newData.roles, ...newRole }
162
- newData = { ...newData, roles: newRoles }
163
- const stagedUserRef = db.collection('staged-users').doc(templateUserId)
164
- return stagedUserRef.set({ ...newData, userId: templateUserId }).then(() => {
165
- const userRef = db.collection('users').doc(templateUserId)
166
- setUser(userRef, newData, oldData, templateUserId).then(() => {
167
- return markProcessed(eventRef)
168
- })
169
- })
170
- })
171
- })
172
- }
173
- else {
174
- const stagedUserRef = db.collection('staged-users').doc(templateUserId)
175
- return stagedUserRef.set({ ...newData, userId: templateUserId }).then(() => {
176
- const userRef = db.collection('users').doc(templateUserId)
177
- setUser(userRef, newData, oldData, templateUserId).then(() => {
178
- return markProcessed(eventRef)
179
- })
180
- })
181
- }
182
- }
183
- }
184
- return markProcessed(eventRef)
185
- }
186
- })
187
- })
3
+ exports.stripe = require('./stripe')
188
4
 
189
- function setUser(userRef, newData, oldData, stagedDocId) {
190
- // IT's OK If "users" doesn't match exactly matched "staged-users" because this is only preventing
191
- // writing from outside the @edgdev/firebase functions, so discrepancies will be rare since
192
- // the package will prevent before it gets this far.
193
- return userRef.get().then((user) => {
194
- let userUpdate = { meta: newData.meta, stagedDocId }
195
-
196
- if (Object.prototype.hasOwnProperty.call(newData, 'roles')) {
197
- userUpdate = { ...userUpdate, roles: newData.roles }
198
- }
199
- if (Object.prototype.hasOwnProperty.call(newData, 'specialPermissions')) {
200
- userUpdate = { ...userUpdate, specialPermissions: newData.specialPermissions }
201
- }
202
-
203
- if (!oldData.userId) {
204
- userUpdate = { ...userUpdate, userId: newData.uid }
205
- }
206
- if (!user.exists) {
207
- return userRef.set(userUpdate)
208
- }
209
- else {
210
- return userRef.update(userUpdate)
211
- }
212
- })
213
- }
214
-
215
- function shouldProcess(eventRef) {
216
- return eventRef.get().then((eventDoc) => {
217
- return !eventDoc.exists || !eventDoc.data().processed
218
- })
219
- }
220
-
221
- function markProcessed(eventRef) {
222
- return eventRef.set({ processed: true }).then(() => {
223
- return null
224
- })
225
- }
5
+ // START @edge/firebase functions
6
+ exports.edgeFirebase = require('./edgeFirebase')
226
7
  // END @edge/firebase functions