This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(144): Consome msg falha pagamento
- Loading branch information
1 parent
cc2ba2d
commit b0e8c3b
Showing
9 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
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
78 changes: 78 additions & 0 deletions
78
src/infrastructure/message_handlers/falha_pagamento/falha_pag.handler.spec.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { pedidoDTOMock, pedidoUseCaseMock } from 'src/mocks/pedido.mock'; | ||
import { IPedidoUseCase } from 'src/domain/pedido/interfaces/pedido.use_case.port'; | ||
import { messageIdMock } from 'src/mocks/message.mock'; | ||
import { Logger } from '@nestjs/common'; | ||
import { AtualizaPedidoDTO } from 'src/presentation/rest/v1/presenters/pedido/pedido.dto'; | ||
import { FalhaPagamentoMessageHandler } from './falha_pag.handler'; | ||
import { | ||
StatusPagamento, | ||
StatusPedido, | ||
} from 'src/domain/pedido/enums/pedido.enum'; | ||
|
||
describe('FalhaPagamentoMessageHandler', () => { | ||
let falhaPagamentoMessageHandler: FalhaPagamentoMessageHandler; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
Logger, | ||
FalhaPagamentoMessageHandler, | ||
{ | ||
provide: IPedidoUseCase, | ||
useValue: pedidoUseCaseMock, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
get: jest.fn(), | ||
getOrThrow: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
falhaPagamentoMessageHandler = module.get<FalhaPagamentoMessageHandler>( | ||
FalhaPagamentoMessageHandler, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('deve consumir mensagem de falha no pagamento', async () => { | ||
const HTTPResponse = { | ||
mensagem: 'Pedido atualizado com sucesso', | ||
body: pedidoDTOMock, | ||
}; | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.RECUSADO, | ||
statusPedido: StatusPedido.CANCELADO, | ||
}; | ||
pedidoUseCaseMock.editarPedido.mockReturnValue(HTTPResponse); | ||
|
||
await falhaPagamentoMessageHandler.handleMessage(messageIdMock); | ||
|
||
expect(pedidoUseCaseMock.editarPedido).toHaveBeenCalledWith( | ||
pedidoDTOMock.id, | ||
atualizaPedidoDTO, | ||
); | ||
}); | ||
|
||
it('deve lançar exceção ao consumir mensagem de falha no pagamento', async () => { | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.RECUSADO, | ||
statusPedido: StatusPedido.CANCELADO, | ||
}; | ||
pedidoUseCaseMock.editarPedido.mockRejectedValue(new Error('Erro')); | ||
|
||
await falhaPagamentoMessageHandler.handleMessage(messageIdMock); | ||
|
||
expect(pedidoUseCaseMock.editarPedido).toHaveBeenCalledWith( | ||
pedidoDTOMock.id, | ||
atualizaPedidoDTO, | ||
); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
src/infrastructure/message_handlers/falha_pagamento/falha_pag.handler.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Message } from '@aws-sdk/client-sqs'; | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { SqsConsumerEventHandler, SqsMessageHandler } from '@ssut/nestjs-sqs'; | ||
import { | ||
StatusPagamento, | ||
StatusPedido, | ||
} from 'src/domain/pedido/enums/pedido.enum'; | ||
import { IPedidoUseCase } from 'src/domain/pedido/interfaces/pedido.use_case.port'; | ||
import { AtualizaPedidoDTO } from 'src/presentation/rest/v1/presenters/pedido/pedido.dto'; | ||
|
||
@Injectable() | ||
export class FalhaPagamentoMessageHandler { | ||
private _nomeFilaFalhaPagamento: string; | ||
|
||
constructor( | ||
private readonly logger: Logger, | ||
private configService: ConfigService, | ||
@Inject(IPedidoUseCase) | ||
private readonly pedidoUseCase: IPedidoUseCase, | ||
) { | ||
this._nomeFilaFalhaPagamento = this.configService.getOrThrow<string>( | ||
'NOME_FILA_FALHA_PAGAMENTO', | ||
); | ||
} | ||
|
||
@SqsMessageHandler('falha-pagamento', false) | ||
public async handleMessage(message: Message) { | ||
this.logger.debug( | ||
`Nova mensagem recebida na fila falha-pagamento`, | ||
JSON.stringify(message), | ||
); | ||
try { | ||
const idPedido: string = message.Body; | ||
this.logger.warn(`Cancelando o pedido ${idPedido}...`); | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.RECUSADO, | ||
statusPedido: StatusPedido.CANCELADO, | ||
}; | ||
this.logger.warn( | ||
`O pedido ${idPedido} foi cancelado`, | ||
); | ||
await this.pedidoUseCase.editarPedido(idPedido, atualizaPedidoDTO); | ||
} catch (error) { | ||
this.logger.error( | ||
`Ocorreu um erro ao processar a mensagem com MessageId ${message?.MessageId}`, | ||
error, | ||
JSON.stringify(message), | ||
); | ||
} | ||
} | ||
|
||
@SqsConsumerEventHandler('falha-pagamento', 'processing_error') | ||
public onProcessingError(error: Error, message: Message) { | ||
this.logger.error( | ||
`Ocorreu um erro ao processar a mensagem com MessageId ${message?.MessageId}`, | ||
error, | ||
JSON.stringify(message), | ||
); | ||
} | ||
} |
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