Skip to content

Commit

Permalink
update status module
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratap2018 committed Dec 31, 2024
1 parent 881c83c commit ced6d94
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 14 deletions.
23 changes: 23 additions & 0 deletions src/status/dto/registration-status.response.dto.ts
Original file line number Diff line number Diff line change
@@ -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<RegistrationStatus[]>;
}
22 changes: 16 additions & 6 deletions src/status/repository/status.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,11 +15,20 @@ export class TxnStatusRepository {
registrationStatus: FilterQuery<RegistrationStatusDocument>,
projection?: ProjectionType<RegistrationStatusDocument>,
option?: QueryOptions<RegistrationStatusDocument>,
) {
return this.registatiationStatusModel.find(
registrationStatus,
projection,
option,
);
): Promise<any> {
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)
],
},
},
]);
}
}
29 changes: 26 additions & 3 deletions src/status/status.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand All @@ -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<RegistrationStatusList> {
return this.statusService.findBySsiId(id, pagination);
}

@Get('transaction/:transactionHash')
@ApiResponse({
description: 'List of the txns',
type: RegistrationStatusList,
})
@ApiQuery({
name: 'page',
description: 'Page value',
Expand All @@ -54,10 +76,11 @@ export class StatusController {
name: 'transactionHash',
description: 'Enter transactionHash',
})
@UseInterceptors(RegistrationStatusInterceptor)
getStatusByTransactionHash(
@Param('transactionHash') transactionHash: string,
@Query() pagination: PaginationDto,
) {
): Promise<RegistrationStatusList> {
return this.statusService.findByTxnId(transactionHash, pagination);
}
}
15 changes: 10 additions & 5 deletions src/status/status.service.ts
Original file line number Diff line number Diff line change
@@ -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<RegistrationStatusList> {
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<RegistrationStatusList> {
const skip = (option.page - 1) * option.limit;
option['skip'] = skip;
return this.txnStatusRepository.find(
Expand Down
40 changes: 40 additions & 0 deletions src/status/transformer/staus-response.interceptor.ts
Original file line number Diff line number Diff line change
@@ -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<RegistrationStatus>;
}
@Injectable()
export class RegistrationStatusInterceptor implements NestInterceptor {
intercept(
context: ExecutionContext,
next: CallHandler,
): Observable<Response> {
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;
});
}
}

0 comments on commit ced6d94

Please sign in to comment.