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 #11

Merged
merged 3 commits into from
Aug 4, 2023
Merged

Dev #11

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
14 changes: 8 additions & 6 deletions src/common/interfaces/member.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { User } from '@/users/schemas/user.schema';
import { User } from "@/users/schemas/user.schema";
export interface Imember {
user: User;
amount: number;
lastbuy: Date;
percentage: number;
}
findOneAndUpdate(arg0: { user: any; }, arg1: { amount: number; }): unknown;
updateMany(arg0: Imember): unknown;
user: User;
amount: number;
lastbuy: Date;
percentage: number;
}
28 changes: 23 additions & 5 deletions src/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ProjectsController {
};
}

@Post()
@Post('/buy')
@UseGuards(JwtGuard, RolesGuard)
@Roles(Role.Provider, Role.Admin)
async Buy(@GetUser() user, @Body() buyProjectDto: BuyProjectDto) {
Expand All @@ -62,8 +62,18 @@ export class ProjectsController {
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.projectsService.findOne(+id);
async findOne(@Param('id') id: string) {
const project = await this.projectsService.findOne(id);
console.log(project);
if(project == undefined){
throw new Error('project not found');
}else{
return {
success: true,
message: 'Projects retrieved successfully',
data: project,
};
}
}

@Patch(':id')
Expand All @@ -72,7 +82,15 @@ export class ProjectsController {
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.projectsService.remove(+id);
@UseGuards(JwtGuard, RolesGuard)
@Roles(Role.Provider, Role.Admin)
async remove(@Param('id') id: string) {
console.log(id);
const projectRemoved = await this.projectsService.remove(id);
return {
success: true,
message: 'Ticket removed successfully',
data: projectRemoved,
};
}
}
57 changes: 41 additions & 16 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Model } from 'mongoose';
import { BuyProjectDto } from './dto/buy-project.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Imember } from '@/common/interfaces/member.interface';
import { HttpException } from '@nestjs/common';
@Injectable()
export class ProjectsService {
constructor(
Expand All @@ -25,47 +26,71 @@ export class ProjectsService {

async buy(buyProjectDto: BuyProjectDto, user) {
const project = await this.projectModel.findById(buyProjectDto.id).exec();

if(!project){
throw new Error('project not found');
}
const maximum = project.maximum;

if (project.amount + buyProjectDto.amount > project.maximum) {
if (project.amount + buyProjectDto.amount > maximum) {
throw new Error('maximum amount exceeded');
} else {
const customer = await project.member.find((member) => {
return member.user == user._id;
if(member.user == user._id){
return member;
}
});

if (customer) {
customer.amount += buyProjectDto.amount;


const update = await customer.findOneAndUpdate(
{ user: user._id },
{ amount: customer.amount + buyProjectDto.amount });
console.log(update);
// customer.amount += buyProjectDto.amount;
// customer.percentage = (customer.amount / maximum)*100;
// customer.lastbuy = Date.now() as unknown as Date;

} else {
let to_add: Imember = {
project.member.push({
user: user._id,
amount: buyProjectDto.amount,
lastbuy: Date.now() as unknown as Date,
percentage: 0,
};
// project.member.push({
} as Imember);

project.amount += buyProjectDto.amount;
//project.save();
//this.projectModel.findByIdAndUpdate(project._id,project).exec();
}

// });
project.amount += buyProjectDto.amount;
project.save();
}

project.member.push(user._id);
await project.save();
}
}

async findMember(id: string) {}
async findMember(id: string) {

findOne(id: number) {
return `This action returns a #${id} project`;
}


async findOne(id: string) {
const projectExits = await this.projectModel
.findById(id)
.exec();
if (projectExits) {
return projectExits
}
}

update(id: number, updateProjectDto: UpdateProjectDto) {
return `This action updates a #${id} project`;
}

remove(id: number) {
return `This action removes a #${id} project`;
async remove(id: string) {

//todo
return await this.projectModel.findByIdAndDelete(id).exec();
}
}