diff --git a/apps/backend/src/foodManufacturers/manufacturers.service.ts b/apps/backend/src/foodManufacturers/manufacturers.service.ts index ee4b49ac..63c43349 100644 --- a/apps/backend/src/foodManufacturers/manufacturers.service.ts +++ b/apps/backend/src/foodManufacturers/manufacturers.service.ts @@ -1,4 +1,8 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { + Injectable, + NotFoundException, + ConflictException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { FoodManufacturer } from './manufacturers.entity'; import { Repository } from 'typeorm'; @@ -28,6 +32,7 @@ export class FoodManufacturersService { const foodManufacturer = await this.repo.findOne({ where: { foodManufacturerId }, + relations: ['foodManufacturerRepresentative'], }); if (!foodManufacturer) { @@ -119,13 +124,17 @@ export class FoodManufacturersService { async approve(id: number) { validateId(id, 'Food Manufacturer'); - const foodManufacturer = await this.repo.findOne({ - where: { foodManufacturerId: id }, - }); + const foodManufacturer = await this.findOne(id); if (!foodManufacturer) { throw new NotFoundException(`Food Manufacturer ${id} not found`); } + if (foodManufacturer.status !== ApplicationStatus.PENDING) { + throw new ConflictException( + `Cannot approve a Food Manufacturer with status: ${foodManufacturer.status}`, + ); + } + const createUserDto: userSchemaDto = { email: foodManufacturer.foodManufacturerRepresentative.email, firstName: foodManufacturer.foodManufacturerRepresentative.firstName, @@ -145,13 +154,17 @@ export class FoodManufacturersService { async deny(id: number) { validateId(id, 'Food Manufacturer'); - const foodManufacturer = await this.repo.findOne({ - where: { foodManufacturerId: id }, - }); + const foodManufacturer = await this.findOne(id); if (!foodManufacturer) { throw new NotFoundException(`Food Manufacturer ${id} not found`); } + if (foodManufacturer.status !== ApplicationStatus.PENDING) { + throw new ConflictException( + `Cannot deny a Food Manufacturer with status: ${foodManufacturer.status}`, + ); + } + await this.repo.update(id, { status: ApplicationStatus.DENIED }); } } diff --git a/apps/backend/src/pantries/pantries.service.ts b/apps/backend/src/pantries/pantries.service.ts index d21cd516..24eec40b 100644 --- a/apps/backend/src/pantries/pantries.service.ts +++ b/apps/backend/src/pantries/pantries.service.ts @@ -3,6 +3,7 @@ import { Inject, Injectable, NotFoundException, + ConflictException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { In, Repository } from 'typeorm'; @@ -117,6 +118,12 @@ export class PantriesService { throw new NotFoundException(`Pantry ${id} not found`); } + if (pantry.status !== ApplicationStatus.PENDING) { + throw new ConflictException( + `Cannot approve a pantry with status: ${pantry.status}`, + ); + } + const createUserDto: userSchemaDto = { ...pantry.pantryUser, role: Role.PANTRY, @@ -138,6 +145,12 @@ export class PantriesService { throw new NotFoundException(`Pantry ${id} not found`); } + if (pantry.status !== ApplicationStatus.PENDING) { + throw new ConflictException( + `Cannot deny a pantry with status: ${pantry.status}`, + ); + } + await this.repo.update(id, { status: ApplicationStatus.DENIED }); }