mirror of
https://github.com/Ride-The-Lightning/RTL.git
synced 2024-11-19 01:40:29 +01:00
Validate Document First
This commit is contained in:
parent
cfd8f095a3
commit
7a0bd37d91
@ -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 {
|
||||
|
@ -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 });
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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' });
|
||||
|
@ -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 });
|
||||
});
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 = '';
|
||||
|
Loading…
Reference in New Issue
Block a user