mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
82 lines
3.1 KiB
TypeScript
82 lines
3.1 KiB
TypeScript
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
|
|
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
|
import { Roles } from '~/auth/enums';
|
|
import { IJwtPayload } from '~/auth/interfaces';
|
|
import { AllowedRoles, AuthenticatedUser } from '~/common/decorators';
|
|
import { AccessTokenGuard, RolesGuard } from '~/common/guards';
|
|
import { ApiDataResponse } from '~/core/decorators';
|
|
import { ResponseFactory } from '~/core/utils';
|
|
import { CreateMoneyRequestDto, MoneyRequestsFiltersRequestDto, RejectionMoneyRequestDto } from '../dtos/request';
|
|
import { MoneyRequestResponseDto } from '../dtos/response/money-request.response.dto';
|
|
import { MoneyRequestsService } from '../services/money-requests.service';
|
|
|
|
@Controller('money-requests')
|
|
@ApiTags('Money Requests')
|
|
@UseGuards(AccessTokenGuard, RolesGuard)
|
|
@ApiBearerAuth()
|
|
export class MoneyRequestsController {
|
|
constructor(private readonly moneyRequestsService: MoneyRequestsService) {}
|
|
@Post()
|
|
@AllowedRoles(Roles.JUNIOR)
|
|
@ApiDataResponse(MoneyRequestResponseDto)
|
|
async createMoneyRequest(@AuthenticatedUser() { sub }: IJwtPayload, @Body() body: CreateMoneyRequestDto) {
|
|
const moneyRequest = await this.moneyRequestsService.createMoneyRequest(sub, body);
|
|
|
|
return ResponseFactory.data(new MoneyRequestResponseDto(moneyRequest));
|
|
}
|
|
|
|
@Get()
|
|
@AllowedRoles(Roles.JUNIOR, Roles.GUARDIAN)
|
|
@ApiDataResponse(MoneyRequestResponseDto)
|
|
async getMoneyRequests(
|
|
@AuthenticatedUser() { sub, roles }: IJwtPayload,
|
|
@Query() filters: MoneyRequestsFiltersRequestDto,
|
|
) {
|
|
const [moneyRequests, count] = await this.moneyRequestsService.findMoneyRequests(
|
|
sub,
|
|
roles.includes(Roles.GUARDIAN) ? Roles.GUARDIAN : Roles.JUNIOR,
|
|
filters,
|
|
);
|
|
return ResponseFactory.dataPage(
|
|
moneyRequests.map((mr) => new MoneyRequestResponseDto(mr)),
|
|
{
|
|
page: filters.page,
|
|
size: filters.size,
|
|
itemCount: count,
|
|
},
|
|
);
|
|
}
|
|
|
|
@Get(':id')
|
|
@AllowedRoles(Roles.JUNIOR, Roles.GUARDIAN)
|
|
@ApiDataResponse(MoneyRequestResponseDto)
|
|
async getMoneyRequest(@Param('id') id: string, @AuthenticatedUser() { sub, roles }: IJwtPayload) {
|
|
const moneyRequest = await this.moneyRequestsService.findById(
|
|
id,
|
|
sub,
|
|
roles.includes(Roles.GUARDIAN) ? Roles.GUARDIAN : Roles.JUNIOR,
|
|
);
|
|
return ResponseFactory.data(new MoneyRequestResponseDto(moneyRequest));
|
|
}
|
|
|
|
@Patch(':id/approve')
|
|
@AllowedRoles(Roles.GUARDIAN)
|
|
@ApiDataResponse(MoneyRequestResponseDto)
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async approveMoneyRequest(@Param('id') id: string, @AuthenticatedUser() { sub }: IJwtPayload) {
|
|
await this.moneyRequestsService.approveMoneyRequest(id, sub);
|
|
}
|
|
|
|
@Patch(':id/reject')
|
|
@AllowedRoles(Roles.GUARDIAN)
|
|
@ApiDataResponse(MoneyRequestResponseDto)
|
|
@HttpCode(HttpStatus.NO_CONTENT)
|
|
async rejectMoneyRequest(
|
|
@Param('id') id: string,
|
|
@AuthenticatedUser() { sub }: IJwtPayload,
|
|
@Body() rejectionReasondto: RejectionMoneyRequestDto,
|
|
) {
|
|
await this.moneyRequestsService.rejectMoneyRequest(id, sub, rejectionReasondto);
|
|
}
|
|
}
|