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(143): Consome msg pagamento confirmado
- Loading branch information
1 parent
2984b47
commit a1ca2a7
Showing
8 changed files
with
203 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
src/infrastructure/message_handlers/pagamento_confirmado/pag_confirmado.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 { PagamentoMessageHandler } from './pag_confirmado.handler'; | ||
import { | ||
StatusPagamento, | ||
StatusPedido, | ||
} from 'src/domain/pedido/enums/pedido.enum'; | ||
|
||
describe('PagamentoMessageHandler', () => { | ||
let pagamentoMessageHandler: PagamentoMessageHandler; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
Logger, | ||
PagamentoMessageHandler, | ||
{ | ||
provide: IPedidoUseCase, | ||
useValue: pedidoUseCaseMock, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
get: jest.fn(), | ||
getOrThrow: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
pagamentoMessageHandler = module.get<PagamentoMessageHandler>( | ||
PagamentoMessageHandler, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('deve consumir mensagem de pagamento confirmado', async () => { | ||
const HTTPResponse = { | ||
mensagem: 'Pedido atualizado com sucesso', | ||
body: pedidoDTOMock, | ||
}; | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.PAGO, | ||
statusPedido: StatusPedido.EM_PREPARACAO, | ||
}; | ||
pedidoUseCaseMock.editarPedido.mockReturnValue(HTTPResponse); | ||
|
||
await pagamentoMessageHandler.handleMessage(messageIdMock); | ||
|
||
expect(pedidoUseCaseMock.editarPedido).toHaveBeenCalledWith( | ||
pedidoDTOMock.id, | ||
atualizaPedidoDTO, | ||
); | ||
}); | ||
|
||
it('deve lançar exceção ao consumir mensagem de pagamento confirmado', async () => { | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.PAGO, | ||
statusPedido: StatusPedido.EM_PREPARACAO, | ||
}; | ||
pedidoUseCaseMock.editarPedido.mockRejectedValue(new Error('Erro')); | ||
|
||
await pagamentoMessageHandler.handleMessage(messageIdMock); | ||
|
||
expect(pedidoUseCaseMock.editarPedido).toHaveBeenCalledWith( | ||
pedidoDTOMock.id, | ||
atualizaPedidoDTO, | ||
); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
src/infrastructure/message_handlers/pagamento_confirmado/pag_confirmado.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 PagamentoMessageHandler { | ||
private _nomeFilaPagamentoConfirmado: string; | ||
|
||
constructor( | ||
private readonly logger: Logger, | ||
private configService: ConfigService, | ||
@Inject(IPedidoUseCase) | ||
private readonly pedidoUseCase: IPedidoUseCase, | ||
) { | ||
this._nomeFilaPagamentoConfirmado = this.configService.getOrThrow<string>( | ||
'NOME_FILA_PAGAMENTO_CONFIRMADO', | ||
); | ||
} | ||
|
||
@SqsMessageHandler('pagamento-confirmado', false) | ||
public async handleMessage(message: Message) { | ||
this.logger.debug( | ||
`Nova mensagem recebida na fila pagamento-confirmado`, | ||
JSON.stringify(message), | ||
); | ||
try { | ||
const idPedido: string = message.Body; | ||
this.logger.debug(`Confirmando pagamento do pedido ${idPedido}...`); | ||
const atualizaPedidoDTO: AtualizaPedidoDTO = { | ||
statusPagamento: StatusPagamento.PAGO, | ||
statusPedido: StatusPedido.EM_PREPARACAO, | ||
}; | ||
this.logger.log( | ||
`O pedido ${idPedido} ${StatusPagamento.PAGO} foi enviado para ${StatusPedido.EM_PREPARACAO}`, | ||
); | ||
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('pagamento-confirmado', '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
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