diff --git a/apps/backend/src/foodManufacturers/manufacturers.service.ts b/apps/backend/src/foodManufacturers/manufacturers.service.ts index ee4b49ac..25584fca 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, + BadRequestException, +} 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) { @@ -126,6 +131,12 @@ export class FoodManufacturersService { throw new NotFoundException(`Food Manufacturer ${id} not found`); } + if (foodManufacturer.status !== ApplicationStatus.PENDING) { + throw new BadRequestException( + `Cannot approve a Food Manufacturer with status: ${foodManufacturer.status}`, + ); + } + const createUserDto: userSchemaDto = { email: foodManufacturer.foodManufacturerRepresentative.email, firstName: foodManufacturer.foodManufacturerRepresentative.firstName, @@ -152,6 +163,12 @@ export class FoodManufacturersService { throw new NotFoundException(`Food Manufacturer ${id} not found`); } + if (foodManufacturer.status !== ApplicationStatus.PENDING) { + throw new BadRequestException( + `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..06ecdc26 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, + BadRequestException, } 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 BadRequestException( + `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 BadRequestException( + `Cannot deny a pantry with status: ${pantry.status}`, + ); + } + await this.repo.update(id, { status: ApplicationStatus.DENIED }); }