Skip to content

Commit

Permalink
feat: additional work on db connected with service #449
Browse files Browse the repository at this point in the history
  • Loading branch information
svendjanis committed Dec 5, 2022
1 parent 144dbc6 commit 5d623ce
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 34 deletions.
13 changes: 13 additions & 0 deletions functions/src/services/database/db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ export class DbService {
setDocument(moduleId, id, data, merge = false): Promise<any> {
return Promise.resolve([]);
}

addDocument(moduleId, data): Promise<any> {
return Promise.resolve([]);
}

collectionInstance(name): any {
return Promise.resolve([]);
}

getDocuments(moduleId, data) {
return Promise.resolve([]);
}

}
8 changes: 8 additions & 0 deletions functions/src/services/database/firebase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ export class FirebaseDatabaseService extends DbService {
setDocument(moduleId, id, data, merge = false): Promise<any> {
return admin.firestore().collection(moduleId).doc(id).set(data, {merge});
}

addDocument(moduleId, data): Promise<any> {
return admin.firestore().collection(moduleId).add(data);
}

getDocuments(moduleId, data) {
return admin.firestore().collection(moduleId).where(data.key, data.operator, data.value).get();
}
}
15 changes: 9 additions & 6 deletions functions/src/triggers/document-deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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))
);
}

Expand Down
3 changes: 2 additions & 1 deletion functions/src/triggers/document-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()},
Expand Down
7 changes: 4 additions & 3 deletions functions/src/triggers/file-created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down
8 changes: 5 additions & 3 deletions functions/src/triggers/role-updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
3 changes: 2 additions & 1 deletion functions/src/triggers/storage-item-deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -32,7 +33,7 @@ export const storageItemDeleted = functions
return storage.file(filePath).delete();
}

return doc.ref.delete()
return dbService.deleteDocument('storage', doc.id);
})
);

Expand Down
33 changes: 15 additions & 18 deletions functions/src/triggers/user-created.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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
}
});
});
2 changes: 0 additions & 2 deletions functions/src/triggers/user-document-updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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 || {})
])
}

Expand Down

0 comments on commit 5d623ce

Please sign in to comment.