import { Injectable } from '@nestjs/common' import { ConfigRepository } from '../../../repositories' import { Omie } from '../../../typings/global' @Injectable() export class OmieConfigService { private prefix = 'omie' private config: Omie constructor(private readonly configRepository: ConfigRepository) {} private convertType(value: string, type: string): any { switch (type) { case 'boolean': return value === 'true' case 'number': return +value default: return value } } private async loadOmieConfig(): Promise { const omieConfig: Omie = { enable: false, enable_cancel: false, endpoint: 'https://api.omie.com.br/api/v1/', app_key: '', app_secret: '', checking_account_code: 0, category_code: '', parc_checking_account_code: 0, parc_category_code: '', } const config = await this.configRepository.getByPrefix(this.prefix) config.forEach( element => (omieConfig[element.key.replace(`${this.prefix}_`, '')] = this.convertType( element.value, typeof omieConfig[element.key.replace(`${this.prefix}_`, '')], )), ) this.config = omieConfig } async getConfig(): Promise { if (!this.config) { await this.loadOmieConfig() } return this.config } }