Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #24

Merged
merged 3 commits into from
Aug 9, 2023
Merged

Dev #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/common/enums/money-type.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum MoneyType {
money = 'money',
retailCC = 'retailCC',
carbonCredit = 'carbonCredit',
}

20 changes: 20 additions & 0 deletions src/users/dto/add-money.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 18 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
38 changes: 37 additions & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -68,4 +68,40 @@ export class UsersService {
async findAll(): Promise<UserDocument[]> {
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;
}

}