Skip to content

Commit

Permalink
create api get history of user
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-MinhVu committed Dec 19, 2023
1 parent 01e1b6e commit 5b11788
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/database/repositories/event-log.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ export class EventLogRepository extends BaseRepository<EventLog> {
.where(`${this.alias}.id = :id`, { id })
.execute()
}

public async getHistoriesOfUser(address: string, options) {
const queryBuilder = this.createQb();
queryBuilder
.where(`${this.alias}.sender_address = :address`, { address})
this.queryBuilderAddPagination(queryBuilder, options);
return queryBuilder.getManyAndCount();
}
}
53 changes: 53 additions & 0 deletions src/modules/users/dto/history-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ApiProperty } from '@nestjs/swagger';
import { BasePaginationRequestDto } from '@shared/dtos/base-request.dto';

export class GetHistoryOfUserResponseDto {
@ApiProperty()
id: number;

@ApiProperty()
senderAddress: string;

@ApiProperty()
amountFrom: string;

@ApiProperty()
tokenFromAddress: string;

@ApiProperty()
tokenFromName: string;

@ApiProperty()
networkFrom: string;

@ApiProperty()
receiveAddress: string;

@ApiProperty()
amountReceived: string;

@ApiProperty()
tokenReceivedAddress: string;

@ApiProperty()
networkReceived: string;

@ApiProperty()
tokenReceivedName: string;

@ApiProperty()
protocolFee: string;

@ApiProperty()
status: string;

@ApiProperty()
blockTimeLock: string;

@ApiProperty()
createdAt: Date
}

export class getHistoryOfUserDto extends BasePaginationRequestDto {

}
11 changes: 10 additions & 1 deletion src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Post, Put } from '@nestjs/common';
import { Body, Controller, Get, Post, Put, Param, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';

import { ETableName } from '@constants/entity.constant';
Expand All @@ -7,9 +7,11 @@ import { IJwtPayload } from '@modules/auth/interfaces/auth.interface';

import { AuthUser } from '@shared/decorators/auth-user.decorator';
import { AuthUserGuard } from '@shared/decorators/http.decorator';
import { GuardPublic } from '@guards/guard.decorator';

import { CreateUserDto, UpdateProfileBodyDto } from './dto/user-request.dto';
import { GetProfileResponseDto } from './dto/user-response.dto';
import { GetHistoryOfUserResponseDto, getHistoryOfUserDto } from './dto/history-response.dto';
import { UsersService } from './users.service';

@ApiTags('Users')
Expand All @@ -24,4 +26,11 @@ export class UsersController {
getProfile(@AuthUser() user: IJwtPayload) {
return this.userService.getProfile(user.userId);
}

@Get('history/:address')
@GuardPublic()
@ApiOkResponse({ type: [GetHistoryOfUserResponseDto] })
getHistoriesOfUser(@Param('address') address: string, @Query() query: getHistoryOfUserDto) {
return this.userService.getHistoriesOfUser(address, query);
}
}
3 changes: 2 additions & 1 deletion src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from '@nestjs/common';
import { UserRepository } from 'database/repositories/user.repository';
import { EventLogRepository } from 'database/repositories/event-log.repository';
import { CustomRepositoryModule } from 'nestjs-typeorm-custom-repository';

import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
imports: [CustomRepositoryModule.forFeature([UserRepository])],
imports: [CustomRepositoryModule.forFeature([UserRepository, EventLogRepository])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
Expand Down
14 changes: 11 additions & 3 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import { DataSource, In, Not } from 'typeorm';
import { EError } from '@constants/error.constant';

import { httpBadRequest, httpForbidden, httpNotFound } from '@shared/exceptions/http-exeption';

import { CreateUserDto, UpdateProfileBodyDto } from './dto/user-request.dto';
import { User } from './entities/user.entity';
import { EventLogRepository } from 'database/repositories/event-log.repository';

@Injectable()
export class UsersService {
constructor(
private readonly usersRepository: UserRepository,
private dataSource: DataSource,
private configService: ConfigService,
private readonly eventLogRepository: EventLogRepository,
) {}
async getProfile(userId: number) {
const user = await this.usersRepository.findOne({
Expand All @@ -26,4 +25,13 @@ export class UsersService {

return user;
}

async getHistoriesOfUser(address: string, options ) {
try {
const [data, count] = await this.eventLogRepository.getHistoriesOfUser(address, options)
return data.toPageDto(options, count);
} catch (error) {

}
}
}

0 comments on commit 5b11788

Please sign in to comment.