import DataCache from '../utilities/dataCache' import RoleModel, { IRole, IRoleQuery } from '../dataSource/models/roleModel' import DataRequest, { IListOutput, IPgeInfo } from '../utilities/dataQuery' // import Config from '../utilities/config' // const env = Config.getEnv() class RoleController { public cachedData:DataCache public request:DataRequest constructor() { this.cachedData = new DataCache(RoleModel, { stdTTL: 30, checkperiod: 15 }) this.request = new DataRequest(RoleModel) } public async getMappedRole(id:string):Promise { const rolesMap = await this.getRolesMap() if (rolesMap[id]) return rolesMap[id] return null } public async getRolesMap():Promise<{[key:string]:IRole}> { const allRoles = await this.getAllRoles() return !allRoles? {}: allRoles.reduce((acc:{[key:string]:IRole}, item:IRole) => { if (item && item._id) acc[item._id] = item return acc }, {}) } public async getLeastRole():Promise { let result:IRole|null = null const roles = await this.getAllRoles() if (roles) { if (roles.length > 1) { result = roles .sort((a, b) => { return b.level - a.level })[0] } else { result = roles[0] } } return result } public async getRole(query:IRoleQuery):Promise { if (!query._id) return null return await this.cachedData.getItem(query._id) } public async getAllRoles():Promise { const result = await this.cachedData.getAllItems() return result } public async getRolesByPage(query:IRoleQuery = {}, pageInfo: IPgeInfo):Promise> { const result = await this.request.getItemsByPage(query, {}, {}, pageInfo) return result } public async saveRole(name:string, level:number, reqLimitPerSec:number, description:string):Promise { const doc:IRole = {name, level, reqLimitPerSec, description} const result = await this.cachedData.createItem(doc) return result } public async updateRole(id:string, name:string, level:number, reqLimitPerSec:number, description:string):Promise { const doc:{name?:string, level?:number, reqLimitPerSec?:number, description?:string} = {} if (name) doc.name = name if (level) doc.level = level if (reqLimitPerSec) doc.reqLimitPerSec = reqLimitPerSec if (description) doc.description = description const result = await this.cachedData.updateItem(id, doc) return result } public async deleteRole(id:string):Promise { const result = await this.cachedData.deleteItem(id) return result } } export default new RoleController()