diff --git a/src/common/enums/money-type.enum.ts b/src/common/enums/money-type.enum.ts new file mode 100644 index 0000000..5811cca --- /dev/null +++ b/src/common/enums/money-type.enum.ts @@ -0,0 +1,6 @@ +export enum MoneyType { + money = 'money', + retailCC = 'retailCC', + carbonCredit = 'carbonCredit', + } + \ No newline at end of file diff --git a/src/users/dto/add-money.dto.ts b/src/users/dto/add-money.dto.ts new file mode 100644 index 0000000..0d6e5b0 --- /dev/null +++ b/src/users/dto/add-money.dto.ts @@ -0,0 +1,20 @@ +import { IsNotEmpty, IsNumber, IsEnum } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; +import { MoneyType } from '@/common/enums/money-type.enum'; +export class AddMoneyDto { + @ApiProperty({ + description: 'Quantity', + example: 20, + }) + @IsNotEmpty() + @IsNumber() + quantity: number; + + @ApiProperty({ + description: 'Option', + example: MoneyType.carbonCredit, + }) + @IsNotEmpty() + @IsEnum(MoneyType) + option: MoneyType; +} diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 5f6aa27..46fdc3a 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -8,7 +8,8 @@ import { Role } from '@/common/enums/role.enum'; import { Roles } from '@/common/decorators/roles.decorator'; import { UsersService } from './users.service'; - +import { Patch, Param, Body } from '@nestjs/common'; +import { AddMoneyDto } from './dto/add-money.dto'; @ApiTags('Users') @ApiBearerAuth() @UseGuards(JwtGuard) @@ -28,6 +29,22 @@ export class UsersController { }; } + + @Patch(':id/add-money') + @UseGuards(JwtGuard, RolesGuard) + @Roles(Role.User, Role.Provider, Role.Admin) //for dev + buyCarbon( + @Param('id') id: string, + @Body() addMoneyDto: AddMoneyDto, + @GetUser() user) + { + const addMoney = this.usersService.addMoney(id,addMoneyDto, user); + return { + message: 'success', + data: addMoney + } + } + @Get() @UseGuards(RolesGuard) @Roles(Role.Admin) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 164c4bf..04e44fc 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -3,7 +3,7 @@ import { Injectable, HttpException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { User, UserDocument } from '@/users/schemas/user.schema'; - +import { AddMoneyDto } from '@/users/dto/add-money.dto'; @Injectable() export class UsersService { constructor( @@ -68,4 +68,40 @@ export class UsersService { async findAll(): Promise { return await this.userModel.find().select('-__v -password').exec(); } + + async addMoney(id: string, addMoneyDto: AddMoneyDto,user) { + const moneyType = addMoneyDto.option; + const quantity = addMoneyDto.quantity; + if(quantity < 0){ + throw new HttpException( + { + success: false, + message: 'Quantity must be positive', + }, + 400, + ); + } + const userExists = await this.findOneById(id); + if (!userExists) { + throw new HttpException( + { + success: false, + message: 'User not found', + }, + 404, + ); + } + if (moneyType === 'carbonCredit') { + userExists.carbonCredit += quantity; + } + if (moneyType === 'money') { + userExists.money += quantity; + } + if (moneyType === 'retailCC') { + userExists.retailCC += quantity; + } + userExists.save(); + return userExists; + } + }