UNPKG

@tomei/live-price

Version:

Tomei live-price Package

96 lines (82 loc) 3.36 kB
import { Daemons } from '../../components/daemons/daemons'; import { FeedConfigRepository } from './feed-config.repository'; import { IFeedConfigAttr } from '../../interfaces/price-feed-config-attr.interface'; import { LoginUser } from '@tomei/sso'; import { ActionEnum, Activity } from '@tomei/activity-history'; import { ApplicationLog } from '@tomei/log'; export class FeedConfig { protected static NotificationEmail: string; protected static CutOffReminderEveryXMinutes: number; protected static ManualPriceReminderEveryXMinutes: number; private static _Repo = new FeedConfigRepository(); // Static constructor to load existing configuration from the database static async loadConfig() { const config = await FeedConfig._Repo.getConfig(); if (config) { this.NotificationEmail = config.NotificationEmail || ''; this.CutOffReminderEveryXMinutes = config.CutOffReminderInternal || 0; this.ManualPriceReminderEveryXMinutes = config.ManualPriceReminderInternal || 0; } Daemons.ManualPriceReminder.restart(); Daemons.CutOffReminder.restart(); } static getNotificationEmail() { return FeedConfig.NotificationEmail; } static getCutOffReminderInternal() { return FeedConfig.CutOffReminderEveryXMinutes; } static getManualPriceReminderInternal() { return FeedConfig.ManualPriceReminderEveryXMinutes; } static async updateConfig( config: IFeedConfigAttr, loginUser: LoginUser, dbTransaction?: any, ) { try { const EntityValueBefore = await FeedConfig._Repo.getConfig(dbTransaction); const payload = { NotificationEmail: config.NotificationEmail || this.NotificationEmail, CutOffReminderInternal: config.CutOffReminderInternal || this.CutOffReminderEveryXMinutes, ManualPriceReminderInternal: config.ManualPriceReminderInternal || this.ManualPriceReminderEveryXMinutes, UpdatedById: loginUser.ObjectId, UpdatedAt: new Date(), }; const result = await FeedConfig._Repo.createConfig( payload, dbTransaction, ); //If properties are not provided, keep the existing values this.NotificationEmail = payload.NotificationEmail; this.CutOffReminderEveryXMinutes = payload.CutOffReminderInternal; this.ManualPriceReminderEveryXMinutes = payload.ManualPriceReminderInternal; //Record the activity in the activity log const activity = new Activity(); activity.ActivityId = activity.createId(); activity.Description = 'Update Feed Config'; activity.Action = ActionEnum.UPDATE; activity.EntityType = 'FeedConfig'; activity.EntityId = result.FeedConfigId; activity.EntityValueBefore = JSON.stringify(EntityValueBefore); activity.EntityValueAfter = JSON.stringify(result); await activity.create(loginUser.ObjectId, dbTransaction); Daemons.ManualPriceReminder.restart(); Daemons.CutOffReminder.restart(); } catch (error) { await ApplicationLog.error({ error, source: 'live-price', className: 'FeedConfig', methodName: 'updateConfig', transaction: dbTransaction, }); throw error; } } }