diff --git a/functions/src/services/database/db.service.ts b/functions/src/services/database/db.service.ts index 37303935..6d4a4bf8 100644 --- a/functions/src/services/database/db.service.ts +++ b/functions/src/services/database/db.service.ts @@ -14,4 +14,17 @@ export class DbService { setDocument(moduleId, id, data, merge = false): Promise { return Promise.resolve([]); } + + addDocument(moduleId, data): Promise { + return Promise.resolve([]); + } + + collectionInstance(name): any { + return Promise.resolve([]); + } + + getDocuments(moduleId, data) { + return Promise.resolve([]); + } + } diff --git a/functions/src/services/database/firebase.service.ts b/functions/src/services/database/firebase.service.ts index 863ba030..ce03d51e 100644 --- a/functions/src/services/database/firebase.service.ts +++ b/functions/src/services/database/firebase.service.ts @@ -17,4 +17,12 @@ export class FirebaseDatabaseService extends DbService { setDocument(moduleId, id, data, merge = false): Promise { return admin.firestore().collection(moduleId).doc(id).set(data, {merge}); } + + addDocument(moduleId, data): Promise { + return admin.firestore().collection(moduleId).add(data); + } + + getDocuments(moduleId, data) { + return admin.firestore().collection(moduleId).where(data.key, data.operator, data.value).get(); + } } diff --git a/functions/src/triggers/document-deleted.ts b/functions/src/triggers/document-deleted.ts index 12e86fa1..22ad86e3 100644 --- a/functions/src/triggers/document-deleted.ts +++ b/functions/src/triggers/document-deleted.ts @@ -2,6 +2,7 @@ import {Storage} from '@google-cloud/storage'; import {parseTemplate} from '@jaspero/utils'; import {ModuleDeleteCollection, MODULES, ModuleSubCollection, SHARED_CONFIG} from 'definitions'; import * as admin from 'firebase-admin'; +import {database} from 'firebase-admin'; import * as functions from 'firebase-functions'; import {dbService} from '../consts/dbService.const'; import {deleteCollection} from '../utils/delete-collection'; @@ -98,16 +99,18 @@ export const documentDeleted = functions } const method = async () => { - let col: any = firestore.collection(name); - + let col; for (const f of filters) { - col = col.where(f.key, f.operator, f.value); + col = await dbService.getDocuments(name, { + key: f.key, + operator: f.operator, + value: f.value + }); } - const {docs} = await col.get(); - + const docs = col.docs; await Promise.all( - docs.map(doc => doc.ref.delete()) + docs.map(doc => dbService.deleteDocument(name, doc.id)) ); } diff --git a/functions/src/triggers/document-write.ts b/functions/src/triggers/document-write.ts index 879ac3db..10aba2b1 100644 --- a/functions/src/triggers/document-write.ts +++ b/functions/src/triggers/document-write.ts @@ -2,6 +2,7 @@ import {random} from '@jaspero/utils'; import {relevantIndex} from 'adv-firestore-functions'; import {Collections, MODULES, SHARED_CONFIG} from 'definitions'; import * as functions from 'firebase-functions'; +import {dbService} from '../consts/dbService.const'; export const documentWrite = functions .region(SHARED_CONFIG.cloudRegion) @@ -44,7 +45,7 @@ export const documentWrite = functions `${module.metadata?.docIdPrefix || module.id.slice(0, 2)}-${random.string(module.metadata?.docIdSize || 12)}` ) - await change.after.ref.collection(Collections.History).doc(historyId).set({ + await dbService.setDocument(Collections.History, historyId, { type: change.before.exists ? (change.after.exists ? 'update' : 'delete') : 'create', createdOn: Date.now(), ...change.before.exists && {before: change.before.data()}, diff --git a/functions/src/triggers/file-created.ts b/functions/src/triggers/file-created.ts index 4a30607d..f09f7b7c 100644 --- a/functions/src/triggers/file-created.ts +++ b/functions/src/triggers/file-created.ts @@ -8,6 +8,7 @@ import {tmpdir} from 'os'; import {basename, dirname, join} from 'path'; import sharp from 'sharp'; import {promisify} from 'util'; +import {dbService} from '../consts/dbService.const'; import {unpackGenerateImageString} from '../utils/unpack-generate-image-string'; export const fileCreated = functions @@ -48,9 +49,9 @@ export const fileCreated = functions }); if (previousStorageDocument) { - await storageColl.doc(previousStorageDocument.id).set(storageDocument, {merge: true}); + await dbService.setDocument('storage', previousStorageDocument.id, storageDocument, true); } else { - await storageColl.add(storageDocument); + await dbService.addDocument('storage', storageDocument); } /** @@ -88,7 +89,7 @@ export const fileCreated = functions .where('path', '==', (folder as any).path).get(); if (previousFolder.empty) { - await storageColl.add(folder); + await dbService.addDocument('storage', folder); } } diff --git a/functions/src/triggers/role-updated.ts b/functions/src/triggers/role-updated.ts index fcd8024a..e04e1740 100644 --- a/functions/src/triggers/role-updated.ts +++ b/functions/src/triggers/role-updated.ts @@ -38,9 +38,11 @@ export const roleUpdated = functions if (diff) { - const {docs} = await fs.collection(Collections.Users) - .where('role', '==', change.after.id) - .get(); + const {docs} = await dbService.getDocuments(Collections.Users, { + key: 'role', + operator: '==', + value: change.after.id + }) await Promise.allSettled( docs.map(() => diff --git a/functions/src/triggers/storage-item-deleted.ts b/functions/src/triggers/storage-item-deleted.ts index b01615bc..c1979f1a 100644 --- a/functions/src/triggers/storage-item-deleted.ts +++ b/functions/src/triggers/storage-item-deleted.ts @@ -2,6 +2,7 @@ import {SHARED_CONFIG} from 'definitions'; import * as admin from 'firebase-admin'; import * as functions from 'firebase-functions'; import {Storage} from '@google-cloud/storage'; +import {dbService} from '../consts/dbService.const'; export const storageItemDeleted = functions .region(SHARED_CONFIG.cloudRegion) @@ -32,7 +33,7 @@ export const storageItemDeleted = functions return storage.file(filePath).delete(); } - return doc.ref.delete() + return dbService.deleteDocument('storage', doc.id); }) ); diff --git a/functions/src/triggers/user-created.ts b/functions/src/triggers/user-created.ts index 1ff138f2..661a1f16 100644 --- a/functions/src/triggers/user-created.ts +++ b/functions/src/triggers/user-created.ts @@ -1,10 +1,9 @@ -import {firestore} from 'firebase-admin'; +import {Collections, EmailTemplates, SHARED_CONFIG} from 'definitions'; import * as functions from 'firebase-functions'; +import {dbService} from '../consts/dbService.const'; import {STATIC_CONFIG} from '../consts/static-config.const'; import {EmailService} from '../services/email/email.service'; import {createJwt} from '../utils/create-jwt'; -import {SHARED_CONFIG, Collections, EmailTemplates} from 'definitions'; -import {dbService} from '../consts/dbService.const'; export const userCreated = functions .region(SHARED_CONFIG.cloudRegion) @@ -17,7 +16,6 @@ export const userCreated = functions } const inviteRef = await dbService.getDocument(Collections.UserInvites, user.email as string); - const userRef = await dbService.getDocument(Collections.Users, user.uid); const role: { role: string; @@ -51,18 +49,17 @@ export const userCreated = functions } } - await userRef - .set({ - createdOn: Date.now(), - email: user.email, - active: true, - ...role ? { - role: role.role, - requireReset: role.requireReset || false, - ...role.createdBy && {invitedBy: role.createdBy} - } : { - role: '', - requireReset: false - } - }); + await dbService.setDocument(Collections.Users, user.uid, { + createdOn: Date.now(), + email: user.email, + active: true, + ...role ? { + role: role.role, + requireReset: role.requireReset || false, + ...role.createdBy && {invitedBy: role.createdBy} + } : { + role: '', + requireReset: false + } + }); }); diff --git a/functions/src/triggers/user-document-updated.ts b/functions/src/triggers/user-document-updated.ts index c86acff4..dede3118 100644 --- a/functions/src/triggers/user-document-updated.ts +++ b/functions/src/triggers/user-document-updated.ts @@ -15,7 +15,6 @@ export const userDocumentUpdated = functions const after: any = change.after.data(); const before: any = change.before.data(); const ah = auth(); - const fs = firestore(); if (after.role !== before.role) { @@ -27,7 +26,6 @@ export const userDocumentUpdated = functions {role: after.role} ), dbService.updateDocument('authorization', 'permissions', roleRef.data()?.permissions || {}), - // change.after.ref.collection('authorization').doc('permissions').set(roleRef.data()?.permissions || {}) ]) }