From 7a0bd37d91e115083c40481e172d9b62cb31c6f0 Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Sat, 15 Oct 2022 17:25:46 -0700 Subject: [PATCH] Validate Document First --- backend/controllers/cln/payments.js | 17 +++++--- backend/controllers/shared/pageSettings.js | 20 ++++++--- backend/utils/database.js | 43 +++++++++---------- server/controllers/cln/payments.ts | 17 +++++--- server/controllers/shared/pageSettings.ts | 24 +++++++---- server/utils/database.ts | 41 +++++++++--------- .../page-settings/page-settings.component.ts | 2 +- 7 files changed, 93 insertions(+), 71 deletions(-) diff --git a/backend/controllers/cln/payments.js b/backend/controllers/cln/payments.js index daf3837e..50652c21 100644 --- a/backend/controllers/cln/payments.js +++ b/backend/controllers/cln/payments.js @@ -115,12 +115,17 @@ export const postPayment = (req, res, next) => { if (req.body.description) { offerToUpdate['description'] = req.body.description; } - return databaseService.update(req.session.selectedNode, CollectionsEnum.OFFERS, offerToUpdate, CollectionFieldsEnum.BOLT12, req.body.bolt12).then((updatedOffer) => { - logger.log({ level: 'DEBUG', fileName: 'Payments', msg: 'Offer Updated', data: updatedOffer }); - return res.status(201).json({ paymentResponse: body, saveToDBResponse: updatedOffer }); - }).catch((errDB) => { - logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB update error', error: errDB }); - return res.status(201).json({ paymentResponse: body, saveToDBError: errDB }); + return databaseService.validateDocument(CollectionsEnum.OFFERS, offerToUpdate).then((validated) => { + databaseService.update(req.session.selectedNode, CollectionsEnum.OFFERS, offerToUpdate, CollectionFieldsEnum.BOLT12, req.body.bolt12).then((updatedOffer) => { + logger.log({ level: 'DEBUG', fileName: 'Payments', msg: 'Offer Updated', data: updatedOffer }); + return res.status(201).json({ paymentResponse: body, saveToDBResponse: updatedOffer }); + }).catch((errDB) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB update error', error: errDB }); + return res.status(201).json({ paymentResponse: body, saveToDBError: errDB }); + }); + }).catch((errValidation) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB validation error', error: errValidation }); + return res.status(201).json({ paymentResponse: body, saveToDBError: errValidation }); }); } else { diff --git a/backend/controllers/shared/pageSettings.js b/backend/controllers/shared/pageSettings.js index 09ccd913..94a6efed 100644 --- a/backend/controllers/shared/pageSettings.js +++ b/backend/controllers/shared/pageSettings.js @@ -17,13 +17,19 @@ export const getPageSettings = (req, res, next) => { }; export const savePageSettings = (req, res, next) => { logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Saving Page Settings..' }); - return Promise.all(req.body.map((page) => databaseService.update(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS, page, CollectionFieldsEnum.PAGE_ID, page.pageId))). - then((values) => { - logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'PayRequest', msg: 'Payment List Decoded', data: values }); - res.status(201).json(values); - }). - catch((errRes) => { - const err = common.handleError(errRes, 'Page Settings', 'Page Settings Update Error', req.session.selectedNode); + // eslint-disable-next-line arrow-body-style + return Promise.all(req.body.map((page) => databaseService.validateDocument(CollectionsEnum.PAGE_SETTINGS, page))).then((values) => { + return Promise.all(req.body.map((page) => databaseService.update(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS, page, CollectionFieldsEnum.PAGE_ID, page.pageId))). + then((values) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'PayRequest', msg: 'Payment List Decoded', data: values }); + res.status(201).json(values); + }). + catch((errRes) => { + const err = common.handleError(errRes, 'Page Settings', 'Page Settings Update Error', req.session.selectedNode); + throw new Error(JSON.stringify({ message: err.message, error: err.error })); + }); + }).catch((errRes) => { + const err = common.handleError(errRes, 'Page Settings', 'Page Settings Validation Error', req.session.selectedNode); return res.status(err.statusCode).json({ message: err.message, error: err.error }); }); }; diff --git a/backend/utils/database.js b/backend/utils/database.js index d7fcc57d..088566c7 100644 --- a/backend/utils/database.js +++ b/backend/utils/database.js @@ -27,21 +27,26 @@ export class DatabaseService { this.logger.log({ selectedNode: selectedNode, level: 'ERROR', fileName: 'Database', msg: 'Database Load Error', error: err }); } } + validateDocument(collectionName, newDocument) { + return new Promise((resolve, reject) => { + const validationRes = validateDocument(collectionName, newDocument); + if (!validationRes.isValid) { + reject(validationRes.error); + } + else { + resolve(true); + } + }); + } insert(selectedNode, collectionName, newDocument) { return new Promise((resolve, reject) => { try { if (!selectedNode || !selectedNode.index) { reject(new Error('Selected Node Config Not Found.')); } - const validationRes = validateDocument(collectionName, newDocument); - if (!validationRes.isValid) { - reject(validationRes.error); - } - else { - this.nodeDatabase[selectedNode.index].data[collectionName].push(newDocument); - this.saveDatabase(+selectedNode.index); - resolve(newDocument); - } + this.nodeDatabase[selectedNode.index].data[collectionName].push(newDocument); + this.saveDatabase(+selectedNode.index); + resolve(newDocument); } catch (errRes) { reject(errRes); @@ -68,23 +73,17 @@ export class DatabaseService { } updatedDocument = foundDoc; } - const validationRes = validateDocument(collectionName, updatedDocument); - if (!validationRes.isValid) { - reject(validationRes.error); + if (foundDocIdx > -1) { + this.nodeDatabase[selectedNode.index].data[collectionName].splice(foundDocIdx, 1, updatedDocument); } else { - if (foundDocIdx > -1) { - this.nodeDatabase[selectedNode.index].data[collectionName].splice(foundDocIdx, 1, updatedDocument); + if (!this.nodeDatabase[selectedNode.index].data[collectionName]) { + this.nodeDatabase[selectedNode.index].data[collectionName] = []; } - else { - if (!this.nodeDatabase[selectedNode.index].data[collectionName]) { - this.nodeDatabase[selectedNode.index].data[collectionName] = []; - } - this.nodeDatabase[selectedNode.index].data[collectionName].push(updatedDocument); - } - this.saveDatabase(+selectedNode.index); - resolve(updatedDocument); + this.nodeDatabase[selectedNode.index].data[collectionName].push(updatedDocument); } + this.saveDatabase(+selectedNode.index); + resolve(updatedDocument); } catch (errRes) { reject(errRes); diff --git a/server/controllers/cln/payments.ts b/server/controllers/cln/payments.ts index e141a296..a6e572c7 100644 --- a/server/controllers/cln/payments.ts +++ b/server/controllers/cln/payments.ts @@ -98,12 +98,17 @@ export const postPayment = (req, res, next) => { const offerToUpdate: Offer = { bolt12: req.body.bolt12, amountMSat: (req.body.zeroAmtOffer ? 0 : req.body.amount), title: req.body.title, lastUpdatedAt: new Date(Date.now()).getTime() }; if (req.body.vendor) { offerToUpdate['vendor'] = req.body.vendor; } if (req.body.description) { offerToUpdate['description'] = req.body.description; } - return databaseService.update(req.session.selectedNode, CollectionsEnum.OFFERS, offerToUpdate, CollectionFieldsEnum.BOLT12, req.body.bolt12).then((updatedOffer) => { - logger.log({ level: 'DEBUG', fileName: 'Payments', msg: 'Offer Updated', data: updatedOffer }); - return res.status(201).json({ paymentResponse: body, saveToDBResponse: updatedOffer }); - }).catch((errDB) => { - logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB update error', error: errDB }); - return res.status(201).json({ paymentResponse: body, saveToDBError: errDB }); + return databaseService.validateDocument(CollectionsEnum.OFFERS, offerToUpdate).then((validated) => { + databaseService.update(req.session.selectedNode, CollectionsEnum.OFFERS, offerToUpdate, CollectionFieldsEnum.BOLT12, req.body.bolt12).then((updatedOffer) => { + logger.log({ level: 'DEBUG', fileName: 'Payments', msg: 'Offer Updated', data: updatedOffer }); + return res.status(201).json({ paymentResponse: body, saveToDBResponse: updatedOffer }); + }).catch((errDB) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB update error', error: errDB }); + return res.status(201).json({ paymentResponse: body, saveToDBError: errDB }); + }); + }).catch((errValidation) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'ERROR', fileName: 'Payments', msg: 'Offer DB validation error', error: errValidation }); + return res.status(201).json({ paymentResponse: body, saveToDBError: errValidation }); }); } else { return res.status(201).json({ paymentResponse: body, saveToDBResponse: 'NA' }); diff --git a/server/controllers/shared/pageSettings.ts b/server/controllers/shared/pageSettings.ts index 97c8f613..563db802 100644 --- a/server/controllers/shared/pageSettings.ts +++ b/server/controllers/shared/pageSettings.ts @@ -20,13 +20,19 @@ export const getPageSettings = (req, res, next) => { export const savePageSettings = (req, res, next) => { logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Page Settings', msg: 'Saving Page Settings..' }); - return Promise.all(req.body.map((page) => databaseService.update(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS, page, CollectionFieldsEnum.PAGE_ID, page.pageId))). - then((values) => { - logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'PayRequest', msg: 'Payment List Decoded', data: values }); - res.status(201).json(values); - }). - catch((errRes) => { - const err = common.handleError(errRes, 'Page Settings', 'Page Settings Update Error', req.session.selectedNode); - return res.status(err.statusCode).json({ message: err.message, error: err.error }); - }); + // eslint-disable-next-line arrow-body-style + return Promise.all(req.body.map((page) => databaseService.validateDocument(CollectionsEnum.PAGE_SETTINGS, page))).then((values) => { + return Promise.all(req.body.map((page) => databaseService.update(req.session.selectedNode, CollectionsEnum.PAGE_SETTINGS, page, CollectionFieldsEnum.PAGE_ID, page.pageId))). + then((values) => { + logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'PayRequest', msg: 'Payment List Decoded', data: values }); + res.status(201).json(values); + }). + catch((errRes) => { + const err = common.handleError(errRes, 'Page Settings', 'Page Settings Update Error', req.session.selectedNode); + throw new Error(JSON.stringify({ message: err.message, error: err.error })); + }); + }).catch((errRes) => { + const err = common.handleError(errRes, 'Page Settings', 'Page Settings Validation Error', req.session.selectedNode); + return res.status(err.statusCode).json({ message: err.message, error: err.error }); + }); }; diff --git a/server/utils/database.ts b/server/utils/database.ts index 2c1293d1..331e7499 100644 --- a/server/utils/database.ts +++ b/server/utils/database.ts @@ -30,20 +30,26 @@ export class DatabaseService { } } + validateDocument(collectionName, newDocument) { + return new Promise((resolve, reject) => { + const validationRes = validateDocument(collectionName, newDocument); + if (!validationRes.isValid) { + reject(validationRes.error); + } else { + resolve(true); + } + }); + } + insert(selectedNode: CommonSelectedNode, collectionName: CollectionsEnum, newDocument: any) { return new Promise((resolve, reject) => { try { if (!selectedNode || !selectedNode.index) { reject(new Error('Selected Node Config Not Found.')); } - const validationRes = validateDocument(collectionName, newDocument); - if (!validationRes.isValid) { - reject(validationRes.error); - } else { - this.nodeDatabase[selectedNode.index].data[collectionName].push(newDocument); - this.saveDatabase(+selectedNode.index); - resolve(newDocument); - } + this.nodeDatabase[selectedNode.index].data[collectionName].push(newDocument); + this.saveDatabase(+selectedNode.index); + resolve(newDocument); } catch (errRes) { reject(errRes); } @@ -70,21 +76,16 @@ export class DatabaseService { } updatedDocument = foundDoc; } - const validationRes = validateDocument(collectionName, updatedDocument); - if (!validationRes.isValid) { - reject(validationRes.error); + if (foundDocIdx > -1) { + this.nodeDatabase[selectedNode.index].data[collectionName].splice(foundDocIdx, 1, updatedDocument); } else { - if (foundDocIdx > -1) { - this.nodeDatabase[selectedNode.index].data[collectionName].splice(foundDocIdx, 1, updatedDocument); - } else { - if (!this.nodeDatabase[selectedNode.index].data[collectionName]) { - this.nodeDatabase[selectedNode.index].data[collectionName] = []; - } - this.nodeDatabase[selectedNode.index].data[collectionName].push(updatedDocument); + if (!this.nodeDatabase[selectedNode.index].data[collectionName]) { + this.nodeDatabase[selectedNode.index].data[collectionName] = []; } - this.saveDatabase(+selectedNode.index); - resolve(updatedDocument); + this.nodeDatabase[selectedNode.index].data[collectionName].push(updatedDocument); } + this.saveDatabase(+selectedNode.index); + resolve(updatedDocument); } catch (errRes) { reject(errRes); } diff --git a/src/app/shared/components/node-config/page-settings/page-settings.component.ts b/src/app/shared/components/node-config/page-settings/page-settings.component.ts index 33580022..1f10a31e 100644 --- a/src/app/shared/components/node-config/page-settings/page-settings.component.ts +++ b/src/app/shared/components/node-config/page-settings/page-settings.component.ts @@ -66,7 +66,7 @@ export class PageSettingsComponent implements OnInit, OnDestroy { } onUpdatePageSettings(): boolean | void { - if (this.pageSettings.reduce((pacc, page) => pacc || (page.tables.reduce((acc, table) => !(table.recordsPerPage && table.sortBy && table.sortOrder && table.showColumns && table.showColumns.length >= 3), false)), false)) { + if (this.pageSettings.reduce((pacc, page) => pacc || (page.tables.reduce((acc, table) => !(table.recordsPerPage && table.sortBy && table.sortOrder && table.showColumns && table.showColumns.length >= 2), false)), false)) { return true; } this.errorMessage = '';