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 6, 2022
1 parent c176a3e commit 360b37a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
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
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
1 change: 0 additions & 1 deletion functions/src/triggers/file-created.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const fileCreated = functions
})
.storage.object()
.onFinalize(async ({bucket, name, contentType, metadata, timeCreated, size}: ObjectMetadata) => {
const storageColl = firestore().collection('storage');
const fileName = basename(name);
const dirName = dirname(name);
const folders = {};
Expand Down
1 change: 0 additions & 1 deletion functions/src/triggers/role-updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const roleUpdated = functions
.onUpdate(async change => {
const after: any = change.after.data();
const before: any = change.before.data();
const fs = firestore();
const keys = ['get', 'list', 'create', 'update', 'delete'];
const permissionsKeys = new Set<string>();

Expand Down

0 comments on commit 360b37a

Please sign in to comment.