Created
December 11, 2021 14:42
-
-
Save tporto/7ec861f1bc9545a268a73bffad4cdc3a to your computer and use it in GitHub Desktop.
Esse é um outro módulo que pega o que retornou da API, valida e salva na tabela (modules -> boleto -> services)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Logger } from '@nestjs/common'; | |
import { dateTimeNow } from '@/common/util'; | |
import { Boleto, ModelProvider } from '@/modules/database/providers/model.provider'; | |
import { FitbankBoletoService } from '@/modules/fitbank/services/fitbank-boleto.service'; | |
import { IBoletoResult } from '../types/boleto.interface'; | |
import { BoletoStatusType } from '../types/boleto.types'; | |
@Injectable() | |
export class BoletoChangeDuedateService { | |
private readonly logger = new Logger(BoletoChangeDuedateService.name); | |
constructor( | |
private readonly models: ModelProvider, | |
private readonly fitbankBoletoService: FitbankBoletoService, | |
) {} | |
/** | |
* @function call | |
* @description Altera a data de vencimento do boleto | |
* @param {number} boletoId: ID do boleto | |
* @param {string} dataVencimento: Nova data de vencimento | |
* @returns {IBoletoResult} | |
*/ | |
async call(boletoId: number, dataVencimento: string): Promise<IBoletoResult> { | |
this.logger.log(`Change DueDate Boleto: ${boletoId}`); | |
// Obtêm o boleto do banco | |
const boleto = await this.fetchBoleto(boletoId); | |
// Validações | |
const validation: IBoletoResult = this.validations(boleto); | |
if (!validation?.success) return validation; | |
// Atualiza a data na FITBANK | |
const response = await this.fitbankBoletoService.changeDueDateBoleto( | |
boleto.numeroDocumento, | |
dataVencimento, | |
); | |
// Atualiza a data no banco | |
if (response.success) { | |
const result = await this.update(boleto, dataVencimento); | |
return { | |
id: boleto.id, | |
success: result, | |
message: result | |
? 'Boleto atualizado com sucesso.' | |
: 'Erro ao salvar boleto no banco de dados', | |
}; | |
} | |
return response; | |
} | |
/** | |
* @private | |
* @function fetchBoleto | |
* @description Busca o boleto no banco de dados | |
* @param {number} boletoId: ID do boleto | |
* @returns {Boleto} | |
*/ | |
private async fetchBoleto(boletoId: number): Promise<Boleto> { | |
return this.models.Boleto.findOne(boletoId); | |
} | |
/** | |
* @private | |
* @function update | |
* @description Atualiza a data de vencimento do boleto no banco de dados | |
* @param {Boleto} boleto: Boleto | |
* @param {string} dataVencimento: Nova data de vencimento | |
* @returns {boolean} | |
*/ | |
private async update(boleto: Boleto, dataVencimento: string): Promise<boolean> { | |
const result = await this.models.Boleto.update(boleto.id, { | |
dataVencimento: dataVencimento, | |
updatedAt: dateTimeNow(), | |
}); | |
return result.affected > 0; | |
} | |
/** | |
* @private | |
* @function validations | |
* @description Valida algumas informações | |
* @param {Boleto} boleto : Boleto | |
* @returns {IBoletoResult[]} | |
*/ | |
private validations(boleto: Boleto): IBoletoResult { | |
if (!boleto) { | |
return { | |
success: false, | |
message: 'Boleto não encontrado.', | |
}; | |
} | |
if (boleto.status !== BoletoStatusType.REGISTRADO) { | |
return { | |
success: false, | |
message: 'Só é permitido alterar o vencimento de boleto registrado.', | |
}; | |
} | |
return { | |
success: true, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment