diff --git a/apps/backend/src/foodManufacturers/manufacturers.service.ts b/apps/backend/src/foodManufacturers/manufacturers.service.ts index ee4b49ac..ecab69bd 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,11 +124,12 @@ export class FoodManufacturersService { async approve(id: number) { validateId(id, 'Food Manufacturer'); - const foodManufacturer = await this.repo.findOne({ - where: { foodManufacturerId: id }, - }); - if (!foodManufacturer) { - throw new NotFoundException(`Food Manufacturer ${id} not found`); + const foodManufacturer = await this.findOne(id); + + if (foodManufacturer.status !== ApplicationStatus.PENDING) { + throw new ConflictException( + `Cannot approve a Food Manufacturer with status: ${foodManufacturer.status}`, + ); } const createUserDto: userSchemaDto = { @@ -145,11 +151,12 @@ export class FoodManufacturersService { async deny(id: number) { validateId(id, 'Food Manufacturer'); - const foodManufacturer = await this.repo.findOne({ - where: { foodManufacturerId: id }, - }); - if (!foodManufacturer) { - throw new NotFoundException(`Food Manufacturer ${id} not found`); + const foodManufacturer = await this.findOne(id); + + 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 8483545f..542cf100 100644 --- a/apps/backend/src/pantries/pantries.service.ts +++ b/apps/backend/src/pantries/pantries.service.ts @@ -4,6 +4,7 @@ import { Inject, Injectable, NotFoundException, + ConflictException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { In, Repository } from 'typeorm'; @@ -325,6 +326,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, @@ -346,6 +353,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 }); }