Files
dmenu-api/src/modules/payments/controllers/payments.controller.ts
T
2025-12-19 20:58:19 +03:30

157 lines
6.2 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { PaymentsService } from '../services/payments.service';
import {
ApiTags,
ApiOperation,
ApiNotFoundResponse,
ApiBody,
ApiParam,
ApiBearerAuth,
ApiHeader,
} from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { PaymentMethodService } from '../services/payment-method.service';
import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto';
import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto';
import { VerifyPaymentDto } from '../dto/verify-payment.dto';
import { RestId, UserId } from 'src/common/decorators';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { API_HEADER_SLUG } from 'src/common/constants';
@ApiTags('payments')
@Controller()
export class PaymentsController {
constructor(
private readonly paymentsService: PaymentsService,
private readonly paymentMethodService: PaymentMethodService,
) {}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/payments/methods/restaurant')
@ApiOperation({ summary: 'Get the restaurant payment methods' })
@ApiHeader(API_HEADER_SLUG)
@ApiNotFoundResponse({ description: 'Restaurant payment methods not found' })
getTheRestaurantPaymentMethods(@RestId() restId: string) {
return this.paymentMethodService.findByRestaurant(restId);
}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/payments')
@ApiOperation({ summary: 'Get all the restaurant payments for user' })
@ApiHeader(API_HEADER_SLUG)
findAllByRestaurant(@UserId() userId: string, @RestId() restId: string) {
return this.paymentsService.findAllByRestaurantId(restId, userId);
}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Get('public/payments/pay-order/:orderId')
@ApiOperation({ summary: 'Pay for an order' })
@ApiParam({ name: 'orderId', type: 'string', description: 'Order ID' })
@ApiHeader(API_HEADER_SLUG)
payAnOrder(@Param('orderId') orderId: string) {
return this.paymentsService.payOrder(orderId);
}
@Post('public/payments/verify')
@ApiOperation({ summary: 'Verify a payment' })
@ApiHeader(API_HEADER_SLUG)
@ApiBody({ type: VerifyPaymentDto })
@ApiNotFoundResponse({ description: 'Payment not found' })
verifyPayment(@Body() verifyPaymentDto: VerifyPaymentDto) {
return this.paymentsService.verifyPayment(verifyPaymentDto.authority, verifyPaymentDto.orderId);
}
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Get('admin/payments/:id')
// @ApiOperation({ summary: 'Get a payment by ID' })
// @ApiParam({ name: 'id', type: 'number', description: 'Payment ID' })
// @ApiOkResponse({ description: 'Payment found' })
// @ApiNotFoundResponse({ description: 'Payment not found' })
// findOnePayment(@Param('id') id: string) {
// return this.paymentsService.findById(+id);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Patch('admin/payments/:id')
// @ApiOperation({ summary: 'Update a payment' })
// @ApiParam({ name: 'id', type: 'number', description: 'Payment ID' })
// @ApiBody({ type: UpdatePaymentDto })
// @ApiOkResponse({ description: 'Payment updated successfully' })
// @ApiNotFoundResponse({ description: 'Payment not found' })
// updatePayment(@Param('id') id: string, @Body() updatePaymentDto: UpdatePaymentDto) {
// return this.paymentsService.update(+id, updatePaymentDto);
// }
// @UseGuards(AdminAuthGuard)
// @ApiBearerAuth()
// @Delete('admin/payments/:id')
// @ApiOperation({ summary: 'Delete a payment' })
// @ApiParam({ name: 'id', type: 'number', description: 'Payment ID' })
// @ApiOkResponse({ description: 'Payment deleted successfully' })
// @ApiNotFoundResponse({ description: 'Payment not found' })
// removePayment(@Param('id') id: string) {
// return this.paymentsService.remove(+id);
// }
/** payment methods */
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/payments/methods')
@ApiOperation({ summary: 'Get restaurant all payment methods' })
findByRestaurant(@RestId() restId: string) {
return this.paymentMethodService.findByRestaurant(restId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Post('admin/payments/methods')
@ApiOperation({ summary: 'Create a new restaurant payment method' })
@ApiBody({ type: CreatePaymentMethodDto })
createRestaurantPaymentMethod(@RestId() restId: string, @Body() createPaymentMethodDto: CreatePaymentMethodDto) {
return this.paymentMethodService.create(restId, createPaymentMethodDto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/payments/methods/:paymentMethodId')
@ApiOperation({ summary: 'Get restaurant payment method by ID' })
@ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' })
findOneRestaurantPaymentMethod(@Param('paymentMethodId') paymentMethodId: string) {
return this.paymentMethodService.findOne(paymentMethodId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/payments/methods/:paymentMethodId')
@ApiOperation({ summary: 'Update a restaurant payment method' })
@ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' })
@ApiBody({ type: UpdatePaymentMethodDto })
updateRestaurantPaymentMethod(
@Param('paymentMethodId') paymentMethodId: string,
@Body() updatePaymentMethodDto: UpdatePaymentMethodDto,
) {
return this.paymentMethodService.update(paymentMethodId, updatePaymentMethodDto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Delete('admin/payments/methods/:paymentMethodId')
@ApiOperation({ summary: 'Delete a restaurant payment method' })
@ApiParam({ name: 'paymentMethodId', type: 'string', description: 'Payment method ID' })
removeRestaurantPaymentMethod(@Param('paymentMethodId') paymentMethodId: string) {
return this.paymentMethodService.remove(paymentMethodId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Post('admin/payments/verify-cash-method/:paymentId')
@ApiOperation({ summary: 'Verify cash payment ' })
@ApiParam({ name: 'paymentId', type: 'string', description: 'Payment ID' })
verifyCashPayment(@Param('paymentId') paymentId: string) {
return this.paymentMethodService.remove(paymentId);
}
}