Skip to content

Commit

Permalink
feat: working on dbService for multiple databases #449
Browse files Browse the repository at this point in the history
  • Loading branch information
svendjanis committed Dec 5, 2022
1 parent 63584a0 commit 50b189c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion functions/src/callable/exchange-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as functions from 'firebase-functions';
import {verify} from 'jsonwebtoken';
import {ENV_CONFIG} from '../consts/env-config.const';
import {SHARED_CONFIG, Collections} from 'definitions';
import {DbService} from '../services/database/db.service';


export const exchangeToken = functions
.region(SHARED_CONFIG.cloudRegion)
Expand All @@ -21,7 +23,7 @@ export const exchangeToken = functions
const [token, user] = await Promise.all([
auth.createCustomToken(decoded.id),
(data.pullUser !== false ?
firestore.collection(Collections.Users).doc(decoded.id).get() :
new DbService().getDocument(Collections.Users, decoded.id) :
Promise.resolve()) as any
]);

Expand Down
8 changes: 5 additions & 3 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 * as functions from 'firebase-functions';
import {DbService} from '../services/database/db.service';
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 new 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
10 changes: 10 additions & 0 deletions functions/src/services/database/db.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class DbService {

This comment has been minimized.

Copy link
@flauc

flauc Dec 5, 2022

Member

This is abstract

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

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

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

This comment has been minimized.

Copy link
@flauc

flauc Dec 5, 2022

Member

This isn't abstract


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

This comment has been minimized.

Copy link
@flauc

flauc Dec 5, 2022

Member

admin.firestore() should be called once in the class

}

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

}

0 comments on commit 50b189c

Please sign in to comment.