Skip to content

Commit

Permalink
chore: comment offer
Browse files Browse the repository at this point in the history
  • Loading branch information
1tpp committed Aug 10, 2023
1 parent a4a3418 commit de5826d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 70 deletions.
1 change: 0 additions & 1 deletion src/offers/offers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class OffersController {
message: 'success',
data: buyCaron,
};
//return this.offersService.buyCarbon(id,buyCarbonDto, user);
}

@Get()
Expand Down
135 changes: 66 additions & 69 deletions src/offers/offers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -20,7 +18,7 @@ export class OffersService {
@InjectModel(User.name) private userModel: Model<UserDocument>,
@InjectModel(Project.name) private projectModel: Model<ProjectDocument>,
private readonly transactionService: TransactionsService,
) {}
) { }

async create(createOfferDto: CreateOfferDto, user) {
return await this.offerModel.create({
Expand All @@ -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) {
Expand Down

0 comments on commit de5826d

Please sign in to comment.