Skip to content
7 changes: 4 additions & 3 deletions functions/src/callable/exchange-token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {Collections, SHARED_CONFIG} from 'definitions';
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import {verify} from 'jsonwebtoken';
import {dbService} from '../consts/dbService.const';
import {ENV_CONFIG} from '../consts/env-config.const';
import {SHARED_CONFIG, Collections} from 'definitions';


export const exchangeToken = functions
.region(SHARED_CONFIG.cloudRegion)
Expand All @@ -15,13 +17,12 @@ export const exchangeToken = functions
} catch (e) {
throw new functions.https.HttpsError('unauthenticated', 'Token invalid');
}
const firestore = admin.firestore();
const auth = admin.auth();

const [token, user] = await Promise.all([
auth.createCustomToken(decoded.id),
(data.pullUser !== false ?
firestore.collection(Collections.Users).doc(decoded.id).get() :
dbService.getDocument(Collections.Users, decoded.id) :
Promise.resolve()) as any
]);

Expand Down
10 changes: 6 additions & 4 deletions functions/src/callable/update-email.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Collections, SHARED_CONFIG} from 'definitions';
import {auth, firestore} from 'firebase-admin';
import {auth} from 'firebase-admin';
import * as functions from 'firebase-functions';
import {dbService} from '../consts/dbService.const';
import {hasPermission} from '../utils/auth';
import {schemaValidation} from '../utils/schema-validation';

Expand All @@ -27,9 +28,10 @@ export const updateEmail = functions

try {
await auth().updateUser(id, {email});
await firestore().collection(Collections.Users).doc(id).update({
email
});
await dbService.updateDocument(Collections.Users, id, {email});
// await firestore().collection(Collections.Users).doc(id).update({
// email
// });
} catch (e) {
functions.logger.error(e);
throw new functions.https.HttpsError('internal', e.toString());
Expand Down
3 changes: 3 additions & 0 deletions functions/src/consts/dbService.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {FirebaseDatabaseService} from '../services/database/firebase.service';

export const dbService = new FirebaseDatabaseService();
24 changes: 7 additions & 17 deletions functions/src/rest/export-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import XLSX from 'xlsx';
import {CORS} from '../consts/cors-whitelist.const';
import {authenticated} from './middlewares/authenticated';
import {MODULES, SHARED_CONFIG} from 'definitions';
import {dbService} from '../consts/dbService.const';

enum Type {
csv = 'csv',
Expand All @@ -20,7 +21,7 @@ const app = express();
app.use(CORS);

function getModules(url: string) {
return url.split('/').filter((v, index) => index % 2);
return url.split('/').filter((v, index) => index % 2);
}

app.post('/**', authenticated(), (req, res) => {
Expand Down Expand Up @@ -74,22 +75,11 @@ app.post('/**', authenticated(), (req, res) => {
}
}

if (sort) {
col = col.orderBy(
sort.active,
sort.direction
)
}

if (skip) {
col = col.offset(skip);
}

if (limit) {
col = col.limit(limit);
}

let docs = (await col.get()).docs.reduce((acc: any[], doc: any) => {
let docs = (await dbService.getDocuments(collectionRef || req.url, [],
{active: sort.active || undefined, direction: sort.direction || undefined},
skip || undefined,
limit || undefined
)).docs.reduce((acc: any[], doc: any) => {
if (!ids || ids.includes(doc.id)) {
acc.push({
...doc.data(),
Expand Down
11 changes: 4 additions & 7 deletions functions/src/rest/import-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import {constants} from 'http2';
import {CORS} from '../consts/cors-whitelist.const';
import {dbService} from '../consts/dbService.const';
import {safeEval} from '../utils/safe-eval';
import {authenticated} from './middlewares/authenticated';

Expand Down Expand Up @@ -37,7 +38,6 @@ app.post('/', authenticated(), (req, res) => {
async function exec() {
const validator = ajvInstance.compile(JSON.parse(parsedData.schema));
const type = parsedData.type || 'csv';
const afs = admin.firestore();

// @ts-ignore
const {permissions} = req['user'];
Expand Down Expand Up @@ -73,17 +73,14 @@ app.post('/', authenticated(), (req, res) => {
acc.errors.push({index, errors: validator.errors});
} else {
const {id, ...saveData} = cur;
const col = afs
.collection(parsedData.collection);

if (rowFunction) {
acc.created.push(async () => {
const sd = await rowFunction(saveData, afs, req.query);
return id ? col.doc(id).set(sd) : col.add(sd)
const sd = await rowFunction(saveData, req.query);
return id ? dbService.setDocument(parsedData.collection, id, sd) : dbService.addDocument(parsedData.collection, sd);
});
} else {
acc.created.push(() =>
id ? col.doc(id).set(saveData) : col.add(saveData)
id ? dbService.setDocument(parsedData.collection, id, saveData) : dbService.addDocument(parsedData.collection, saveData)
);
}
}
Expand Down
34 changes: 34 additions & 0 deletions functions/src/services/database/db.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class DbService {
static getDocument(moduleId, id): Promise<any> {
return Promise.resolve([]);
}

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

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

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, orderBy?, offset?, limit?): any {
return Promise.resolve([]);
}

deleteCollection(db, collectionPath, batchSize) {
return Promise.resolve([]);
}

}
52 changes: 52 additions & 0 deletions functions/src/services/database/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as admin from 'firebase-admin';
import {deleteCollection} from '../../utils/delete-collection';
import {DbService} from './db.service';
export class FirebaseDatabaseService extends DbService {

getDocument(moduleId, id): Promise<any> {
return admin.firestore().collection(moduleId).doc(id).get();
}

updateDocument(moduleId, id, data): Promise<any> {
return admin.firestore().collection(moduleId).doc(id).update(data);
}

deleteDocument(moduleId, id): Promise<any> {
return admin.firestore().collection(moduleId).doc(id).delete();
}

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, orderBy?, offset?, limit?) {
const coll = admin.firestore().collection(moduleId)

data.forEach((item) => {
coll.where(item.key, item.operator, item.value);
})

if (orderBy && orderBy.active) {
coll.orderBy(orderBy.active, orderBy.direction)
}

if (offset) {
coll.offset(offset);
}

if (limit) {
coll.limit(limit);
}

return coll.get()

}
deleteCollection(db, collectionPath, batchSize): Promise<any> {
return deleteCollection(db, collectionPath, batchSize)
}

}
43 changes: 43 additions & 0 deletions functions/src/services/database/mongo.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {DbService} from './db.service';
import fetch from 'node-fetch';
export class MongoDbService extends DbService {

getDocument(moduleId, id): Promise<any> {
fetch(`/api/document/${moduleId}/${id}`, {
method: 'GET',
})
}

updateDocument(moduleId, id, data): Promise<any> {
fetch(`/api/document/${moduleId}/?field=${id}`, {
method: 'POST',
body: JSON.stringify(data),
})
}

deleteDocument(moduleId, id): Promise<any> {
fetch(`/api/document/${moduleId}/${id}`, {
method: 'DELETE',
})
}

setDocument(moduleId, id, data, merge = false): Promise<any> {
fetch(`/api/document/${moduleId}/`, {
method: 'POST',
body: JSON.stringify(data),
})
}

addDocument(moduleId, data): Promise<any> {
fetch(`/api/document/${moduleId}/`, {
method: 'POST',
body: JSON.stringify(data),
})
}

getDocuments(moduleId, data, orderBy?, offset?, limit?) {
}
deleteCollection(db, collectionPath, batchSize): Promise<any> {
}

}
13 changes: 4 additions & 9 deletions functions/src/services/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Collections, EMAIL_LAYOUT, EMAIL_STYLE} from 'definitions';
import {firestore} from 'firebase-admin';
import * as functions from 'firebase-functions';
import {compile} from 'handlebars';
import {dbService} from '../../consts/dbService.const';
import {ENV_CONFIG} from '../../consts/env-config.const';
import {EmailTemplate} from './email-template.interface';

Expand All @@ -27,11 +28,7 @@ export class EmailService {
addedData?: any
) {

const fs = firestore();

const messageSnap = await fs
.doc(`automatic-emails/${templateId}`)
.get();
const messageSnap = await dbService.getDocument('automatic-emails', templateId)
const message: EmailTemplate = messageSnap.data() as any;

if (!message.active) {
Expand Down Expand Up @@ -78,16 +75,14 @@ export class EmailService {

try {

const emailDoc = firestore().collection(Collections.SentEmails).doc();

await emailDoc.create({
const emailDoc = await dbService.addDocument(Collections.SentEmails, {
createdOn: Date.now(),
to,
html,
subject: message.subject,
templateId,
...res === true ? {status: true} : {status: false, error: res}
});
})

sentEmail = emailDoc.id
} catch (e) {
Expand Down
24 changes: 14 additions & 10 deletions functions/src/triggers/document-deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ 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';

export const documentDeleted = functions
.region(SHARED_CONFIG.cloudRegion)
.firestore
.document('{moduleId}/{documentId}')
.onDelete(async (snap, context) => {

const firestore = admin.firestore();
const {moduleId, documentId} = context.params;
const moduleDoc = MODULES.find(item => item.id === moduleId);
Expand Down Expand Up @@ -66,7 +68,7 @@ export const documentDeleted = functions
subCollections.forEach(
({name, batch}: ModuleSubCollection) => {
toExec.push(
deleteCollection(
dbService.deleteCollection(
firestore,
`${moduleId}/${documentId}/${name}`,
batch || 100
Expand All @@ -82,7 +84,7 @@ export const documentDeleted = functions

if (!filter) {
toExec.push(
firestore.collection(name).doc(documentId).delete()
dbService.deleteDocument(name, documentId)
);
return;
}
Expand All @@ -91,22 +93,24 @@ export const documentDeleted = functions

if (typeof filters === 'string') {
toExec.push(
firestore.collection(name).doc(filters).delete()
dbService.deleteDocument(name, filters)
);
return;
}

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
Loading