From bc89f8ec115a29b38bc2e12c798119685ee2d6ff Mon Sep 17 00:00:00 2001 From: "thong.nguyen5" Date: Tue, 16 Apr 2024 21:31:45 +0700 Subject: [PATCH] CRYP-47 Add api retrieve delivery attempts --- .../modules/delivery/delivery.controller.ts | 16 +++++++ .../src/modules/delivery/delivery.service.ts | 14 ++++++ .../src/modules/delivery/dto/delivery.dto.ts | 48 ++++++++++++++++++- .../src/webhook/webhook.service.ts | 4 +- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/app/apps/onebox/src/modules/delivery/delivery.controller.ts b/app/apps/onebox/src/modules/delivery/delivery.controller.ts index c713ae1..ba20fa5 100644 --- a/app/apps/onebox/src/modules/delivery/delivery.controller.ts +++ b/app/apps/onebox/src/modules/delivery/delivery.controller.ts @@ -10,6 +10,7 @@ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { User } from '../users/schemas/user.schema'; import { DeliveryService } from './delivery.service'; import { + DeliveryAttemptResponseDto, GetMonitorDeliveryDto, MonitorDeliveryResponseDto, } from './dto/delivery.dto'; @@ -33,4 +34,19 @@ export class DeliveryController { body, ); } + + @ApiOperation({ summary: 'Get Delivery Attempts' }) + @ApiBearerAuth('JWT') + @UseGuards(JwtAuthGuard) + @Get('/attempts') + @ApiOkResponse({ type: [DeliveryAttemptResponseDto] }) + async getDeliveryAttempts( + @Req() req: Request, + @Query('deliveryId') deliveryId: string, + ): Promise { + return await this.deliveryService.getDeliveryAttempt( + req.user as User, + deliveryId, + ); + } } diff --git a/app/apps/onebox/src/modules/delivery/delivery.service.ts b/app/apps/onebox/src/modules/delivery/delivery.service.ts index 862bb46..b8c7e87 100644 --- a/app/apps/onebox/src/modules/delivery/delivery.service.ts +++ b/app/apps/onebox/src/modules/delivery/delivery.service.ts @@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common'; import { MonitorService } from '../monitor/monitor.service'; import { User } from '../users/schemas/user.schema'; import { + DeliveryAttemptResponseDto, GetMonitorDeliveryDto, MonitorDeliveryResponseDto, } from './dto/delivery.dto'; @@ -36,4 +37,17 @@ export class DeliveryService { ); }); } + + async getDeliveryAttempt( + user: User, + deliveryId: string, + ): Promise { + return this.webhookService + .getDeliveryAttempts(deliveryId) + .then((response) => { + return response.delivery_attempts.map((attempt) => + DeliveryAttemptResponseDto.from(attempt), + ); + }); + } } diff --git a/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts b/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts index 23b8425..ddf0e2a 100644 --- a/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts +++ b/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts @@ -1,4 +1,7 @@ -import { DeliveryDto } from '@app/shared_modules/webhook/webhook.service'; +import { + DeliveryAttemptDto, + DeliveryDto, +} from '@app/shared_modules/webhook/webhook.service'; import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger'; import { Builder } from 'builder-pattern'; import { Transform } from 'class-transformer'; @@ -58,3 +61,46 @@ export class MonitorDeliveryResponseDto { .build(); } } + +export class DeliveryAttemptResponseDto { + @ApiResponseProperty() + id: string; + + @ApiResponseProperty() + deliveryId: string; + + @ApiResponseProperty() + request: string; + + @ApiResponseProperty() + response: string; + + @ApiResponseProperty() + responseStatusCode: number; + + @ApiResponseProperty() + executionDuration: number; + + @ApiResponseProperty() + success: boolean; + + @ApiResponseProperty() + error: string; + + @ApiResponseProperty() + dateCreated: string; + + static from(dto: DeliveryAttemptDto): DeliveryAttemptResponseDto { + return Builder() + .id(dto.id) + .deliveryId(dto.delivery_id) + .request(dto.raw_request) + .response(dto.raw_response) + .responseStatusCode(dto.response_status_code) + .executionDuration(dto.execution_duration) + .success(dto.success) + .error(dto.error) + .dateCreated(dto.created_at) + .build(); + } +} diff --git a/app/libs/shared_modules/src/webhook/webhook.service.ts b/app/libs/shared_modules/src/webhook/webhook.service.ts index 5cdb536..25283c5 100644 --- a/app/libs/shared_modules/src/webhook/webhook.service.ts +++ b/app/libs/shared_modules/src/webhook/webhook.service.ts @@ -351,7 +351,7 @@ class UpdateWebhookRequestDto { active?: boolean; } -interface DeliveryAttemptDto { +export class DeliveryAttemptDto { id: string; // webhook_id: string; delivery_id: string; @@ -364,7 +364,7 @@ interface DeliveryAttemptDto { created_at: string; } -interface DeliveryAttemptsResponseDto { +export class DeliveryAttemptsResponseDto { delivery_attempts: DeliveryAttemptDto[]; limit: number; offset: number;