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.
Merge pull request #31 from Grupo-G03-4SOAT-FIAP/feature/143/pagament…
…o-confirmado Feature/143/pagamento confirmado
- Loading branch information
Showing
11 changed files
with
602 additions
and
6 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
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#!/bin/bash | ||
awslocal sqs create-queue --queue-name nova-cobranca | ||
awslocal sqs create-queue --queue-name cobranca-gerada | ||
awslocal sqs create-queue --queue-name falha-cobranca | ||
awslocal sqs create-queue --queue-name falha-cobranca | ||
awslocal sqs create-queue --queue-name pagamento-confirmado | ||
awslocal sqs create-queue --queue-name falha-pagamento |
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