Ride-The-Lightning-RTL/server/models/database.model.ts

157 lines
5.0 KiB
TypeScript
Raw Normal View History

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