From 78660bee868f63b99a6d08074b706175ac3a7c05 Mon Sep 17 00:00:00 2001 From: "thong.nguyen5" Date: Tue, 16 Apr 2024 21:17:44 +0700 Subject: [PATCH] CRYP-47 Add api retrieve webhook deliveries --- app/apps/onebox/src/main.module.ts | 2 + .../modules/delivery/delivery.controller.ts | 36 +++++++++++ .../src/modules/delivery/delivery.module.ts | 21 +++++++ .../src/modules/delivery/delivery.service.ts | 39 ++++++++++++ .../src/modules/delivery/dto/delivery.dto.ts | 60 +++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 app/apps/onebox/src/modules/delivery/delivery.controller.ts create mode 100644 app/apps/onebox/src/modules/delivery/delivery.module.ts create mode 100644 app/apps/onebox/src/modules/delivery/delivery.service.ts create mode 100644 app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts diff --git a/app/apps/onebox/src/main.module.ts b/app/apps/onebox/src/main.module.ts index e7985d3..62e20ba 100644 --- a/app/apps/onebox/src/main.module.ts +++ b/app/apps/onebox/src/main.module.ts @@ -11,6 +11,7 @@ import { MonitorModule } from './modules/monitor/monitor.module'; import { ProjectModule } from './modules/project/project.module'; import { UsersModule } from './modules/users/users.module'; import { PollingBlockService } from './polling.block/polling.block.service'; +import { DeliveryModule } from './modules/delivery/delivery.module'; @Module({ imports: [ @@ -44,6 +45,7 @@ import { PollingBlockService } from './polling.block/polling.block.service'; BlockSyncModule, MonitorModule, MonitorAddressModule, + DeliveryModule, ], providers: [GlobalService, PollingBlockService], }) diff --git a/app/apps/onebox/src/modules/delivery/delivery.controller.ts b/app/apps/onebox/src/modules/delivery/delivery.controller.ts new file mode 100644 index 0000000..c713ae1 --- /dev/null +++ b/app/apps/onebox/src/modules/delivery/delivery.controller.ts @@ -0,0 +1,36 @@ +import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common'; +import { + ApiBearerAuth, + ApiOkResponse, + ApiOperation, + ApiTags, +} from '@nestjs/swagger'; +import { Request } from 'express'; +import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; +import { User } from '../users/schemas/user.schema'; +import { DeliveryService } from './delivery.service'; +import { + GetMonitorDeliveryDto, + MonitorDeliveryResponseDto, +} from './dto/delivery.dto'; + +@ApiTags('Monitor Delivery') +@Controller('/delivery') +export class DeliveryController { + constructor(private readonly deliveryService: DeliveryService) {} + + @ApiOperation({ summary: 'Get Monitor Deliveries' }) + @ApiBearerAuth('JWT') + @UseGuards(JwtAuthGuard) + @Get('') + @ApiOkResponse({ type: [MonitorDeliveryResponseDto] }) + async getMonitorDeliveries( + @Req() req: Request, + @Query() body: GetMonitorDeliveryDto, + ): Promise { + return await this.deliveryService.getMonitorDeliveries( + req.user as User, + body, + ); + } +} diff --git a/app/apps/onebox/src/modules/delivery/delivery.module.ts b/app/apps/onebox/src/modules/delivery/delivery.module.ts new file mode 100644 index 0000000..bedcbe7 --- /dev/null +++ b/app/apps/onebox/src/modules/delivery/delivery.module.ts @@ -0,0 +1,21 @@ +import { MonitorModule as MonitorModelModule } from '@app/shared_modules/monitor/monitor.module'; +import { ProjectModule as ProjectModelModule } from '@app/shared_modules/project/project.module'; +import { WebhookModule } from '@app/shared_modules/webhook/webhook.module'; +import { Module } from '@nestjs/common'; +import { MonitorModule } from '../monitor/monitor.module'; +import { ProjectModule } from '../project/project.module'; +import { DeliveryController } from './delivery.controller'; +import { DeliveryService } from './delivery.service'; +@Module({ + controllers: [DeliveryController], + providers: [DeliveryService], + exports: [DeliveryService], + imports: [ + WebhookModule, + ProjectModelModule, + MonitorModelModule, + ProjectModule, + MonitorModule, + ], +}) +export class DeliveryModule {} diff --git a/app/apps/onebox/src/modules/delivery/delivery.service.ts b/app/apps/onebox/src/modules/delivery/delivery.service.ts new file mode 100644 index 0000000..862bb46 --- /dev/null +++ b/app/apps/onebox/src/modules/delivery/delivery.service.ts @@ -0,0 +1,39 @@ +import { WebhookService } from '@app/shared_modules/webhook/webhook.service'; +import { Injectable } from '@nestjs/common'; +import { MonitorService } from '../monitor/monitor.service'; +import { User } from '../users/schemas/user.schema'; +import { + GetMonitorDeliveryDto, + MonitorDeliveryResponseDto, +} from './dto/delivery.dto'; + +@Injectable() +export class DeliveryService { + constructor( + private readonly webhookService: WebhookService, + private readonly monitorService: MonitorService, + ) {} + + async getMonitorDeliveries( + user: User, + request: GetMonitorDeliveryDto, + ): Promise { + const monitor = await this.monitorService.findAndAuthMonitor( + user, + request.monitorId, + ); + + return this.webhookService + .getDeliveries( + monitor.webhookId, + request.limit, + request.offset, + request.status, + ) + .then((response) => { + return response.deliveries.map((delivery) => + MonitorDeliveryResponseDto.from(delivery), + ); + }); + } +} diff --git a/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts b/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts new file mode 100644 index 0000000..23b8425 --- /dev/null +++ b/app/apps/onebox/src/modules/delivery/dto/delivery.dto.ts @@ -0,0 +1,60 @@ +import { DeliveryDto } from '@app/shared_modules/webhook/webhook.service'; +import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger'; +import { Builder } from 'builder-pattern'; +import { Transform } from 'class-transformer'; +import { IsNotEmpty, Max, Min } from 'class-validator'; + +export class GetMonitorDeliveryDto { + @ApiProperty() + @IsNotEmpty() + monitorId: string; + + @ApiProperty({ required: false }) + status?: 'succeeded' | 'pending' | 'failed'; + + @ApiProperty({ default: 10 }) + @Max(10) + @Min(1) + @Transform(({ value }) => parseInt(value)) + limit: number; + + @ApiProperty({ default: 0 }) + @Min(0) + @Transform(({ value }) => parseInt(value)) + offset: number; +} + +export class MonitorDeliveryResponseDto { + @ApiResponseProperty() + id: string; + + @ApiResponseProperty() + payload: string; + + @ApiResponseProperty() + status: string; + + @ApiResponseProperty() + attempts: number; + + @ApiResponseProperty() + dateScheduled: string; + + @ApiResponseProperty() + dateCreated: string; + + @ApiResponseProperty() + dateUpdated: string; + + static from(dto: DeliveryDto): MonitorDeliveryResponseDto { + return Builder() + .id(dto.id) + .payload(dto.payload) + .status(dto.status) + .attempts(dto.delivery_attempts) + .dateScheduled(dto.scheduled_at) + .dateCreated(dto.created_at) + .dateUpdated(dto.updated_at) + .build(); + } +}