Skip to content

Commit

Permalink
chore: style
Browse files Browse the repository at this point in the history
  • Loading branch information
1tpp committed Aug 4, 2023
1 parent bf75562 commit 0dd368b
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/offers/dto/create-offer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class CreateOfferDto {
@IsPositive()
price_per_kg: number;


@ApiProperty({
description: 'Offer image',
example: 'img_path',
Expand Down
2 changes: 1 addition & 1 deletion src/offers/offers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class OffersController {
@UseGuards(JwtGuard, RolesGuard)
@Roles(Role.Provider, Role.Admin)
create(@Body() createOfferDto: CreateOfferDto, @GetUser() user) {
return this.offersService.create(createOfferDto,user);
return this.offersService.create(createOfferDto, user);
}

@Get()
Expand Down
2 changes: 1 addition & 1 deletion src/offers/offers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { OffersService } from './offers.service';
import { OffersController } from './offers.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { Offer,OfferSchema } from './schemas/offer.schema';
import { Offer, OfferSchema } from './schemas/offer.schema';
import { ProjectsModule } from '@/projects/projects.module';
@Module({
imports: [
Expand Down
14 changes: 4 additions & 10 deletions src/offers/offers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common';
import { CreateOfferDto } from './dto/create-offer.dto';
import { UpdateOfferDto } from './dto/update-offer.dto';
import { OfferDocument } from './schemas/offer.schema';
import {InjectModel} from '@nestjs/mongoose';
import {Model} from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Offer } from './entities/offer.entity';
import { BuyCarbonDto } from './dto/buy-carbon.dto';
import { Project, ProjectDocument } from '@/projects/schemas/project.schema';
Expand All @@ -15,12 +15,9 @@ export class OffersService {
private readonly projectsService: ProjectsService;

constructor(
@InjectModel(Offer.name) private offerModel: Model<OfferDocument>,
//@InjectModel(Project.name) private projectModel: Model<ProjectDocument>,

@InjectModel(Offer.name) private offerModel: Model<OfferDocument>, //@InjectModel(Project.name) private projectModel: Model<ProjectDocument>,
) {}


async create(createOfferDto: CreateOfferDto, user) {
return await this.offerModel.create({
...createOfferDto,
Expand All @@ -31,9 +28,8 @@ export class OffersService {
async findAll() {
return await this.offerModel.find({});
}


async buyCarbon(buyCarbonDto:BuyCarbonDto, user) {
async buyCarbon(buyCarbonDto: BuyCarbonDto, user) {
const offer = await this.offerModel.findById(buyCarbonDto.id);
if (offer.available < buyCarbonDto.amount) {
throw new Error('Not enough carbon available');
Expand All @@ -46,8 +42,6 @@ export class OffersService {
// ...buyCarbonDto,
// owner: user._id,
// });


}

async findOne(id: string) {
Expand Down
1 change: 0 additions & 1 deletion src/offers/schemas/offer.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class Offer extends Document {

@Prop({ default: Date.now })
updatedAt: Date;

}

export const OfferSchema = SchemaFactory.createForClass(Offer);
5 changes: 0 additions & 5 deletions src/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ export class ProjectsController {
}
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateProjectDto: UpdateProjectDto) {
return this.projectsService.update(+id, updateProjectDto);
}

@Delete(':id')
@UseGuards(JwtGuard, RolesGuard)
@Roles(Role.Provider, Role.Admin)
Expand Down
34 changes: 1 addition & 33 deletions src/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,15 @@ export class ProjectsService {

async buy(buyProjectDto: BuyProjectDto, user) {
const project = await this.projectModel.findById(buyProjectDto.id).exec();
if (!project) {
if (!project) {
throw new Error('project not found');
}
const maximum: number = project.maximum;
const maximum: number = project.maximum;

if (project.amount + buyProjectDto.amount > maximum) {
throw new Error('maximum amount exceeded');
}

const isIDinMember = await this.projectModel
.findOne({ _id: project.id, 'member.user': user._id })
.exec();
if (isIDinMember == null) {
const isIDinMember = await this.projectModel
.findOne({ _id: project.id, 'member.user': user._id })
.exec();
Expand All @@ -59,32 +53,12 @@ export class ProjectsService {
},
},
);
} else {
const updateMember = await this.projectModel.findOneAndUpdate(
{ _id: project.id },
{
$push: {
member: {
user: user._id,
amount: buyProjectDto.amount,
lastbuy: now.toLocaleDateString(),
percentage: 0,
},
},
},
);
} else {
const now = new Date();
const currentAmount = project.member.find((m) =>
m.user.equals(user._id),
).amount;
const currentAmount = project.member.find((m) =>
m.user.equals(user._id),
).amount;

const updatedAmount = currentAmount + buyProjectDto.amount;
const updatedLastBuy = now.toLocaleDateString(); // Set the new value for lastbuy here
const updatedPercentage = (updatedAmount / maximum) * 100; // Set the new value for percentage here
const updatedAmount = currentAmount + buyProjectDto.amount;
const updatedLastBuy = now.toLocaleDateString(); // Set the new value for lastbuy here
const updatedPercentage = (updatedAmount / maximum) * 100; // Set the new value for percentage here
Expand All @@ -100,23 +74,17 @@ export class ProjectsService {
},
{ new: true },
);
console.log(updateMember);
}
}


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

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

async remove(id: string) {
//todo
return await this.projectModel.findByIdAndDelete(id).exec();
Expand Down

0 comments on commit 0dd368b

Please sign in to comment.