Skip to content

Commit

Permalink
fix(congregation): issue when congregation update overwrites cloud data
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao authored Jan 8, 2023
1 parent 6052379 commit 3bb98bd
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 128 deletions.
7 changes: 5 additions & 2 deletions src/classes/Congregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const db = getFirestore(); //get default database
export class Congregation {
constructor(id) {
this.id = id;
this.country_code = '';
this.cong_name = '';
this.cong_number = '';
this.cong_persons = '';
Expand All @@ -27,6 +28,7 @@ Congregation.prototype.loadDetails = async function () {
const congRef = db.collection('congregations').doc(this.id);
const congSnap = await congRef.get();

this.country_code = congSnap.data().country_code;
this.cong_name = congSnap.data().cong_name;
this.cong_number = congSnap.data().cong_number;
this.last_backup = congSnap.data().last_backup;
Expand Down Expand Up @@ -61,7 +63,8 @@ Congregation.prototype.loadDetails = async function () {
};

Congregation.prototype.updateInfo = async function (congInfo) {
await db.collection('congregations').doc(this.id).set(congInfo);
await db.collection('congregations').doc(this.id).set(congInfo, { merge: true });
this.country_code = congInfo.country_code;
this.cong_name = congInfo.cong_name;
this.cong_number = congInfo.cong_number;
};
Expand Down Expand Up @@ -193,7 +196,7 @@ Congregation.prototype.updateUserRole = async function (userId, userRole) {

Congregation.prototype.createPocketUser = async function (pocketName, pocketId) {
const code = randomstring.generate(10).toUpperCase();
const secureCode = encryptData(code);
const secureCode = encryptData(`${this.country_code}${this.cong_number}-${code}`);

const ref = await db.collection('users').add({
about: { name: pocketName, role: 'pocket' },
Expand Down
8 changes: 7 additions & 1 deletion src/classes/Congregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ Congregations.prototype.findCongregationById = function (id) {
};

Congregations.prototype.findByNumber = function (number) {
return this.list.find((cong) => cong.cong_number === number);
const regex = new RegExp('\\d+');

const array = regex.exec(number);
const cong_country = number.substring(0, array.index);
const cong_number = array[0];

return this.list.find((cong) => cong.country_code === cong_country && cong.cong_number === cong_number);
};

Congregations.prototype.create = async function (congInfo) {
Expand Down
20 changes: 11 additions & 9 deletions src/classes/Users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getAuth } from 'firebase-admin/auth';
import { getFirestore } from 'firebase-admin/firestore';
import { decryptData } from '../utils/encryption-utils.js';
import { sendVerificationEmail } from '../utils/sendEmail.js';
import { dbFetchUsers } from '../utils/user-utils.js';
import { congregations } from './Congregations.js';
Expand Down Expand Up @@ -46,14 +45,17 @@ Users.prototype.findUserById = function (id) {

Users.prototype.findUserByOTPCode = function (code) {
let user;
for (let i = 0; i < this.list.length; i++) {
const item = this.list[i];
const otpCode = item.pocket_oCode;
if (otpCode !== '') {
const pocket_oCode = decryptData(otpCode);

if (code === pocket_oCode) {
user = item;

// parse otp code
const cong_number = code.split('-')[0];

// get congregation
const cong = congregations.findByNumber(cong_number);
if (cong) {
for (let i = 0; i < cong.cong_members.length; i++) {
const item = cong.cong_members[i];
if (code === item.pocket_oCode) {
user = this.findUserById(item.id);
break;
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/controllers/congregation-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ export const getCongregations = async (req, res, next) => {

export const createCongregation = async (req, res, next) => {
try {
const { email, cong_name, cong_number, app_requestor } = req.body;
const { email, country_code, cong_name, cong_number, app_requestor } = req.body;

const errors = validationResult(req);

Expand Down Expand Up @@ -1391,7 +1391,7 @@ export const createCongregation = async (req, res, next) => {
}

// find congregation
const cong = congregations.findByNumber(cong_number);
const cong = congregations.findByNumber(`${country_code}${cong_number}`);
if (cong) {
res.locals.type = 'warn';
res.locals.message = 'the congregation requested already exists';
Expand All @@ -1401,8 +1401,7 @@ export const createCongregation = async (req, res, next) => {
}

// create congregation
const congData = { cong_name, cong_number };
const newCong = await congregations.create(congData);
const newCong = await congregations.create({ country_code, cong_name, cong_number });

// add user to congregation
const tmpUser = users.findUserByEmail(email);
Expand All @@ -1420,7 +1419,7 @@ export const updateCongregationInfo = async (req, res, next) => {
try {
const { id } = req.params;
const { email } = req.headers;
const { cong_name, cong_number } = req.body;
const { country_code, cong_name, cong_number } = req.body;

const errors = validationResult(req);

Expand All @@ -1446,7 +1445,7 @@ export const updateCongregationInfo = async (req, res, next) => {
const isValid = cong.isMember(email);

if (isValid) {
const data = { cong_name, cong_number };
const data = { country_code, cong_name, cong_number };
await cong.updateInfo(data);

for await (const user of cong.cong_members) {
Expand Down
Loading

0 comments on commit 3bb98bd

Please sign in to comment.