import DataCache from '../utilities/dataCache' import FeatureModel, { IFeature, IFeatureQuery, TFeatureType } from '../dataSource/models/featureModel' import DataRequest, { IListOutput, IPgeInfo } from '../utilities/dataQuery' // import Config from '../utilities/config' // const env = Config.getEnv() class FeatureController { public cachedData:DataCache public request:DataRequest constructor() { this.cachedData = new DataCache(FeatureModel, { stdTTL: 30, checkperiod: 15 }) this.request = new DataRequest(FeatureModel) } public async getFeaturesMap():Promise<{[key:string]:IFeature}> { const allFeatures = await this.getAllFeatures() return !allFeatures? {}: allFeatures.reduce((acc:{[key:string]:IFeature}, item:IFeature) => { if (item && item._id) acc[item._id] = item return acc }, {}) } public async getFeature(query:IFeatureQuery):Promise { if (!query._id) return null return await this.cachedData.getItem(query._id) } public async getAllFeatures():Promise { const result = await this.cachedData.getAllItems() return result } public async getFeaturesByPage(query:IFeatureQuery = {}, pageInfo: IPgeInfo):Promise> { const result = await this.request.getItemsByPage(query, {}, {}, pageInfo) return result } public async saveFeature(type: TFeatureType, value:string, name:string, tags:string, description:string):Promise { const data:IFeature = {type, value, name, tags: [], description} if (tags && tags.length) data.tags = tags.split(',') const result = await this.cachedData.createItem(data) return result } public async updateFeature(id:string, type: TFeatureType, value:string, name:string, tags:string, description:string):Promise { const doc:IFeature = {value, name, description} if (type) doc.type = type if (tags && tags.length) doc.tags = tags.split(',') const result = await this.cachedData.updateItem(id, doc) return result } public async deleteFeature(id:string):Promise { const result = await this.cachedData.deleteItem(id) return result } } export default new FeatureController()