Skip to content

Commit

Permalink
Merge pull request #23 from TeetouchQQ/main
Browse files Browse the repository at this point in the history
feat:add money
  • Loading branch information
TeetouchQQ authored Aug 9, 2023
2 parents 804262f + 0e12ff7 commit b4ac10d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
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',
}

6 changes: 5 additions & 1 deletion src/transactions/transactions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export class TransactionsService {
const parsedPage = Math.max(Number(page), 1);
const parsedLimit = Number(limit);

const count = await this.transactionModel.countDocuments().exec();
const count = await this.transactionModel
.countDocuments({
user: user._id,
})
.exec();
const transactions = await this.transactionModel
.find({
user: user._id,
Expand Down
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;
}

}

0 comments on commit b4ac10d

Please sign in to comment.