From de5826dcdc3f5ebd65ca7ddea9ea2c8e4f3a5392 Mon Sep 17 00:00:00 2001 From: 1tpp Date: Thu, 10 Aug 2023 08:13:36 +0700 Subject: [PATCH] chore: comment offer --- src/offers/offers.controller.ts | 1 - src/offers/offers.service.ts | 135 ++++++++++++++++---------------- 2 files changed, 66 insertions(+), 70 deletions(-) diff --git a/src/offers/offers.controller.ts b/src/offers/offers.controller.ts index 8c2ad62..b9e3540 100644 --- a/src/offers/offers.controller.ts +++ b/src/offers/offers.controller.ts @@ -44,7 +44,6 @@ export class OffersController { message: 'success', data: buyCaron, }; - //return this.offersService.buyCarbon(id,buyCarbonDto, user); } @Get() diff --git a/src/offers/offers.service.ts b/src/offers/offers.service.ts index 2cf342a..320fe0a 100644 --- a/src/offers/offers.service.ts +++ b/src/offers/offers.service.ts @@ -8,8 +8,6 @@ import { Model } from 'mongoose'; import { Offer } from './schemas/offer.schema'; import { BuyCarbonDto } from './dto/buy-carbon.dto'; import { Project, ProjectDocument } from '@/projects/schemas/project.schema'; -import { ProjectsService } from '@/projects/projects.service'; -import { Inject } from '@nestjs/common'; import { TransactionsService } from '@/transactions/transactions.service'; import { User, UserDocument } from '@/users/schemas/user.schema'; import { HttpException } from '@nestjs/common'; @@ -20,7 +18,7 @@ export class OffersService { @InjectModel(User.name) private userModel: Model, @InjectModel(Project.name) private projectModel: Model, private readonly transactionService: TransactionsService, - ) {} + ) { } async create(createOfferDto: CreateOfferDto, user) { return await this.offerModel.create({ @@ -34,75 +32,74 @@ export class OffersService { } async buyCarbon(id: string, buyCarbonDto: BuyCarbonDto, user) { - const offer = await this.offerModel.findById(id); - if (offer.available < buyCarbonDto.amount) { - throw new Error('Not enough carbon available'); - } - const currentUser = await this.userModel.findById(user._id).exec(); - if (currentUser.money < buyCarbonDto.amount * offer.price_per_kg) { - throw new Error('Not enough money'); - } - if (buyCarbonDto.amount > offer.available) { - throw new Error('Not enough carbon credit'); - } + // const offer = await this.offerModel.findById(id); + // if (offer.available < buyCarbonDto.amount) { + // throw new Error('Not enough carbon available'); + // } + // const currentUser = await this.userModel.findById(user._id).exec(); + // if (currentUser.money < buyCarbonDto.amount * offer.price_per_kg) { + // throw new Error('Not enough money'); + // } + // if (buyCarbonDto.amount > offer.available) { + // throw new Error('Not enough carbon credit'); + // } - // share money - console.log(offer.project_id); - const project = await this.projectModel.findById(offer.project_id).exec(); + // const project = await this.projectModel.findById(offer.project_id).exec(); - const owner = await this.userModel.findById(project.owner).exec(); - if (project.shares_holders.length == 0) { - //No share Holder give all to owener - owner.money += buyCarbonDto.amount * offer.price_per_kg; - } else { - let all_money: number = offer.price_per_kg * buyCarbonDto.amount; - for (const member of project.shares_holders) { - const user = await this.userModel.findById(member.user).exec(); - let share_amount: number = - buyCarbonDto.amount * - ((offer.price_per_kg * member.percentage) / 100); - user.money += share_amount; - all_money -= share_amount; - //create per member transaction - await this.transactionService.create({ - type: 'share', - user: user._id, - ticket: id, - quantity: buyCarbonDto.amount, - description: `Get share from project ${share_amount} Baht`, - status: 'success', - total_price: share_amount, - }); - user.save(); - } - owner.money += all_money; - await this.transactionService.create({ - type: 'share', - user: owner._id, - ticket: id, - quantity: buyCarbonDto.amount, - description: `Get share from project ${all_money} Baht`, - status: 'success', - total_price: all_money, - }); - owner.save(); - } + // const owner = await this.userModel.findById(project.owner).exec(); + // if (project.shares_holders.length == 0) { + // //No share Holder give all to owener + // owner.money += buyCarbonDto.amount * offer.price_per_kg; + // } else { + // let all_money: number = offer.price_per_kg * buyCarbonDto.amount; + // for (const member of project.shares_holders) { + // const user = await this.userModel.findById(member.user).exec(); + // let share_amount: number = + // buyCarbonDto.amount * + // ((offer.price_per_kg * member.percentage) / 100); + // user.money += share_amount; + // all_money -= share_amount; + // //create per member transaction + // // await this.transactionService.create({ + // // type: 'share', + // // user: user._id, + // // ticket: id, + // // quantity: buyCarbonDto.amount, + // // description: `Get share from project ${share_amount} Baht`, + // // status: 'success', + // // total_price: share_amount, + // // }); + // user.save(); + // } + // owner.money += all_money; + // // await this.transactionService.create({ + // // type: 'share', + // // user: owner._id, + // // ticket: id, + // // quantity: buyCarbonDto.amount, + // // description: `Get share from project ${all_money} Baht`, + // // status: 'success', + // // total_price: all_money, + // // }); + // owner.save(); + // } - currentUser.carbonCredit += buyCarbonDto.amount; - currentUser.money -= buyCarbonDto.amount * offer.price_per_kg; - offer.available -= buyCarbonDto.amount; - await this.transactionService.create({ - type: 'carbon', - user: currentUser._id, - ticket: id, - quantity: buyCarbonDto.amount, - description: `Buy CaronCredit ${buyCarbonDto.amount} ton(s)`, - status: 'success', - total_price: buyCarbonDto.amount * offer.price_per_kg, - }); - offer.save(); - currentUser.save(); - return offer; + // currentUser.carbonCredit += buyCarbonDto.amount; + // currentUser.money -= buyCarbonDto.amount * offer.price_per_kg; + // offer.available -= buyCarbonDto.amount; + // // await this.transactionService.create({ + // // type: 'carbon', + // // user: currentUser._id, + // // ticket: id, + // // quantity: buyCarbonDto.amount, + // // description: `Buy CaronCredit ${buyCarbonDto.amount} ton(s)`, + // // status: 'success', + // // total_price: buyCarbonDto.amount * offer.price_per_kg, + // // }); + // offer.save(); + // currentUser.save(); + // return offer; + return null; } async findOne(id: string) {