2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-15 18:13:00 +00:00
RTL/server/models/database.model.ts

157 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-12-29 23:08:41 +00:00
export enum OfferFieldsEnum {
BOLT12 = 'bolt12',
2022-10-14 20:53:36 +00:00
AMOUNTMSAT = 'amountMSat',
2021-12-29 23:08:41 +00:00
TITLE = 'title',
VENDOR = 'vendor',
DESCRIPTION = 'description'
}
export class Offer {
constructor(
public bolt12: string,
2022-10-14 20:53:36 +00:00
public amountMSat: number,
2021-12-29 23:08:41 +00:00
public title: string,
public vendor?: string,
public description?: string,
public lastUpdatedAt?: number
) { }
}
export const validateDocument = (collectionName: CollectionsEnum, documentToValidate: any): any => {
switch (collectionName) {
case CollectionsEnum.OFFERS:
return validateOffer(documentToValidate);
case CollectionsEnum.PAGE_SETTINGS:
return validatePageSettings(documentToValidate);
default:
return ({ isValid: false, error: 'Collection does not exist' });
}
};
2021-12-29 23:08:41 +00:00
export const validateOffer = (documentToValidate): any => {
if (!documentToValidate.hasOwnProperty(CollectionFieldsEnum.BOLT12)) {
2022-10-15 23:35:39 +00:00
return ({ isValid: false, error: 'Bolt12 is mandatory.' });
2021-12-29 23:08:41 +00:00
}
if (!documentToValidate.hasOwnProperty(CollectionFieldsEnum.AMOUNTMSAT)) {
2022-10-15 23:35:39 +00:00
return ({ isValid: false, error: 'Amount mSat is mandatory.' });
2021-12-29 23:08:41 +00:00
}
if (!documentToValidate.hasOwnProperty(CollectionFieldsEnum.TITLE)) {
2022-10-15 23:35:39 +00:00
return ({ isValid: false, error: 'Title is mandatory.' });
2021-12-29 23:08:41 +00:00
}
if ((typeof documentToValidate[CollectionFieldsEnum.AMOUNTMSAT] !== 'number')) {
2022-10-15 23:35:39 +00:00
return ({ isValid: false, error: 'Amount mSat should be a number.' });
2021-12-29 23:08:41 +00:00
}
return ({ isValid: true });
};
2022-10-14 20:53:36 +00:00
export enum SortOrderEnum {
2022-10-17 15:37:58 +00:00
ASCENDING = 'asc',
DESCENDING = 'desc'
2022-10-14 20:53:36 +00:00
}
export enum PageSettingsFieldsEnum {
PAGE_ID = 'pageId',
2022-10-14 20:53:36 +00:00
TABLES = 'tables'
}
export enum TableSettingsFieldsEnum {
TABLE_ID = 'tableId',
RECORDS_PER_PAGE = 'recordsPerPage',
SORT_BY = 'sortBy',
SORT_ORDER = 'sortOrder',
COLUMN_SELECTION = 'columnSelection',
COLUMN_SELECTION_SM = 'columnSelectionSM'
2022-10-14 20:53:36 +00:00
}
export class TableSetting {
constructor(
public tableId: string,
public recordsPerPage?: number,
public sortBy?: string,
public sortOrder?: SortOrderEnum,
public columnSelection?: any[]
2022-10-14 20:53:36 +00:00
) { }
}
export class PageSettings {
constructor(
public pageId: string,
public tables: TableSetting[]
2022-10-14 20:53:36 +00:00
) { }
}
export const validatePageSettings = (documentToValidate): any => {
2022-10-15 01:59:47 +00:00
let errorMessages = '';
if (!documentToValidate.hasOwnProperty(CollectionFieldsEnum.PAGE_ID)) {
2022-10-15 23:35:39 +00:00
errorMessages = errorMessages + 'Page ID is mandatory.';
2022-10-15 01:59:47 +00:00
}
if (!documentToValidate.hasOwnProperty(CollectionFieldsEnum.TABLES)) {
2022-10-15 23:35:39 +00:00
errorMessages = errorMessages + 'Tables is mandatory.';
2022-10-15 01:59:47 +00:00
}
const tablesMessages = documentToValidate.tables.reduce((tableAcc, table: TableSetting, tableIdx) => {
let errMsg = '';
if (!table.hasOwnProperty(CollectionFieldsEnum.TABLE_ID)) {
2022-10-15 23:35:39 +00:00
errMsg = errMsg + 'Table ID is mandatory.';
2022-10-15 01:59:47 +00:00
}
if (!table.hasOwnProperty(CollectionFieldsEnum.SORT_BY)) {
2022-10-15 23:35:39 +00:00
errMsg = errMsg + 'Sort By is mandatory.';
2022-10-15 01:59:47 +00:00
}
if (!table.hasOwnProperty(CollectionFieldsEnum.SORT_ORDER)) {
2022-10-15 23:35:39 +00:00
errMsg = errMsg + 'Sort Order is mandatory.';
2022-10-15 01:59:47 +00:00
}
if (!table.hasOwnProperty(CollectionFieldsEnum.COLUMN_SELECTION_SM)) {
2022-11-21 21:44:43 +00:00
errMsg = errMsg + 'Column Selection (Mobile Resolution) is mandatory.';
}
if (table[CollectionFieldsEnum.COLUMN_SELECTION_SM].length < 1) {
2022-11-21 21:44:43 +00:00
errMsg = errMsg + 'Column Selection (Mobile Resolution) should have at least 1 field.';
}
2022-10-18 21:51:20 +00:00
if (table[CollectionFieldsEnum.COLUMN_SELECTION_SM].length > 3) {
2022-11-21 21:44:43 +00:00
errMsg = errMsg + 'Column Selection (Mobile Resolution) should have maximum 3 fields.';
}
if (!table.hasOwnProperty(CollectionFieldsEnum.COLUMN_SELECTION)) {
2022-11-21 21:44:43 +00:00
errMsg = errMsg + 'Column Selection (Desktop Resolution) is mandatory.';
}
if (table[CollectionFieldsEnum.COLUMN_SELECTION].length < 2) {
2022-11-21 21:44:43 +00:00
errMsg = errMsg + 'Column Selection (Desktop Resolution) should have at least 2 fields.';
}
2022-10-15 01:59:47 +00:00
if (errMsg.trim() !== '') {
tableAcc.push({ table: (table.hasOwnProperty(CollectionFieldsEnum.TABLE_ID) ? table[CollectionFieldsEnum.TABLE_ID] : (tableIdx + 1)), message: errMsg });
}
return tableAcc;
}, []);
2022-10-15 23:35:39 +00:00
if (errorMessages.trim() === '' && tablesMessages.length === 0) {
2022-10-15 01:59:47 +00:00
return ({ isValid: true });
} else {
const errObj = { page: (documentToValidate.hasOwnProperty(CollectionFieldsEnum.PAGE_ID) ? documentToValidate[CollectionFieldsEnum.PAGE_ID] : 'Unknown') };
if (errorMessages.trim() !== '') {
errObj['message'] = errorMessages;
}
if (tablesMessages.length && tablesMessages.length > 0) {
errObj['tables'] = tablesMessages;
}
return ({ isValid: false, error: JSON.stringify(errObj) });
2022-10-14 20:53:36 +00:00
}
};
export enum CollectionsEnum {
OFFERS = 'Offers',
PAGE_SETTINGS = 'PageSettings'
}
export type Collections = {
Offers: Offer[];
PageSettings: PageSettings[];
2022-10-14 20:53:36 +00:00
}
export const CollectionFieldsEnum = { ...OfferFieldsEnum, ...PageSettingsFieldsEnum, ...TableSettingsFieldsEnum };
2022-10-28 16:41:38 +00:00
export const LNDCollection = [CollectionsEnum.PAGE_SETTINGS];
export const ECLCollection = [CollectionsEnum.PAGE_SETTINGS];
export const CLNCollection = [CollectionsEnum.PAGE_SETTINGS, CollectionsEnum.OFFERS];