diff --git a/src/status/dto/registration-status.response.dto.ts b/src/status/dto/registration-status.response.dto.ts new file mode 100644 index 0000000..99b8c2f --- /dev/null +++ b/src/status/dto/registration-status.response.dto.ts @@ -0,0 +1,23 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNumber, IsString, IsArray } from 'class-validator'; + +import { RegistrationStatus } from '../schema/status.schema'; + +export class RegistrationStatusList { + @ApiProperty({ + description: 'totalCount', + example: 12, + }) + @IsNumber() + totalCount: number; + + @ApiProperty({ + description: 'data', + type: RegistrationStatus, + example: [], + isArray: true, + }) + @IsString() + @IsArray() + data: Array; +} diff --git a/src/status/repository/status.repository.ts b/src/status/repository/status.repository.ts index 964f7fa..acc0ec3 100644 --- a/src/status/repository/status.repository.ts +++ b/src/status/repository/status.repository.ts @@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { FilterQuery, Model, ProjectionType, QueryOptions } from 'mongoose'; import { RegistrationStatusDocument } from '../schema/status.schema'; import { skip } from 'rxjs'; +import { RegistrationStatusList } from '../dto/registration-status.response.dto'; @Injectable() export class TxnStatusRepository { @@ -14,11 +15,20 @@ export class TxnStatusRepository { registrationStatus: FilterQuery, projection?: ProjectionType, option?: QueryOptions, - ) { - return this.registatiationStatusModel.find( - registrationStatus, - projection, - option, - ); + ): Promise { + return this.registatiationStatusModel.aggregate([ + { $match: { ...registrationStatus } }, // Apply the query filter + { + $facet: { + totalCount: [{ $count: 'total' }], + + data: [ + { $project: { _id: 0 } }, + { $skip: Number(option.skip) }, // Apply the skip (pagination) + { $limit: Number(option.limit) }, // Apply the limit (pagination) + ], + }, + }, + ]); } } diff --git a/src/status/status.controller.ts b/src/status/status.controller.ts index 03527ec..f0bd299 100644 --- a/src/status/status.controller.ts +++ b/src/status/status.controller.ts @@ -5,13 +5,23 @@ import { UseGuards, Query, UseFilters, + UseInterceptors, } from '@nestjs/common'; import { StatusService } from './status.service'; -import { ApiBearerAuth, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; +import { + ApiBearerAuth, + ApiParam, + ApiQuery, + ApiResponse, + ApiTags, +} from '@nestjs/swagger'; import { AuthGuard } from '@nestjs/passport'; import { PaginationDto } from 'src/utils/pagination.dto'; import { AllExceptionsFilter } from 'src/utils/utils'; +import { RegistrationStatus } from './schema/status.schema'; +import { RegistrationStatusList } from './dto/registration-status.response.dto'; +import { RegistrationStatusInterceptor } from './transformer/staus-response.interceptor'; @UseFilters(AllExceptionsFilter) @ApiTags('Status') @ApiBearerAuth('Authorization') @@ -21,6 +31,10 @@ export class StatusController { constructor(private readonly statusService: StatusService) {} @Get('ssi/:id') + @ApiResponse({ + description: 'List of the txns', + type: RegistrationStatusList, + }) @ApiQuery({ name: 'page', description: 'Page value', @@ -35,11 +49,19 @@ export class StatusController { name: 'id', description: 'Enter didId or vcId or schemaId', }) - getStatus(@Param('id') id: string, @Query() pagination: PaginationDto) { + @UseInterceptors(RegistrationStatusInterceptor) + getStatus( + @Param('id') id: string, + @Query() pagination: PaginationDto, + ): Promise { return this.statusService.findBySsiId(id, pagination); } @Get('transaction/:transactionHash') + @ApiResponse({ + description: 'List of the txns', + type: RegistrationStatusList, + }) @ApiQuery({ name: 'page', description: 'Page value', @@ -54,10 +76,11 @@ export class StatusController { name: 'transactionHash', description: 'Enter transactionHash', }) + @UseInterceptors(RegistrationStatusInterceptor) getStatusByTransactionHash( @Param('transactionHash') transactionHash: string, @Query() pagination: PaginationDto, - ) { + ): Promise { return this.statusService.findByTxnId(transactionHash, pagination); } } diff --git a/src/status/status.service.ts b/src/status/status.service.ts index c84d0e4..2437f5a 100644 --- a/src/status/status.service.ts +++ b/src/status/status.service.ts @@ -1,18 +1,23 @@ import { Injectable } from '@nestjs/common'; import { TxnStatusRepository } from './repository/status.repository'; +import { RegistrationStatusList } from './dto/registration-status.response.dto'; @Injectable() export class StatusService { constructor(private readonly txnStatusRepository: TxnStatusRepository) {} - findBySsiId(id: string, option) { + findBySsiId(id: string, option): Promise { const skip = (option.page - 1) * option.limit; option['skip'] = skip; - return this.txnStatusRepository.find({ - id, - }); + return this.txnStatusRepository.find( + { + id, + }, + {}, + option, + ); } - findByTxnId(id: string, option) { + findByTxnId(id: string, option): Promise { const skip = (option.page - 1) * option.limit; option['skip'] = skip; return this.txnStatusRepository.find( diff --git a/src/status/transformer/staus-response.interceptor.ts b/src/status/transformer/staus-response.interceptor.ts new file mode 100644 index 0000000..fb23105 --- /dev/null +++ b/src/status/transformer/staus-response.interceptor.ts @@ -0,0 +1,40 @@ +import { + Injectable, + NestInterceptor, + ExecutionContext, + CallHandler, +} from '@nestjs/common'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { RegistrationStatus } from '../schema/status.schema'; +export interface Response { + totalCount: number; + data: Array; +} +@Injectable() +export class RegistrationStatusInterceptor implements NestInterceptor { + intercept( + context: ExecutionContext, + next: CallHandler, + ): Observable { + return next.handle().pipe( + map((data) => { + const modifiedResponse = { + totalCount: + data[0]['totalCount'].length > 0 + ? data[0]['totalCount'][0].total + : 0, + data: this.mapData(data[0]['data']), + }; + return modifiedResponse; + }), + ); + } + mapData(data) { + return data.map((data) => { + data.message = JSON.parse(data.message); + + return data; + }); + } +}