Skip to content
27 changes: 20 additions & 7 deletions apps/backend/src/foodManufacturers/manufacturers.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,6 +32,7 @@ export class FoodManufacturersService {

const foodManufacturer = await this.repo.findOne({
where: { foodManufacturerId },
relations: ['foodManufacturerRepresentative'],
});

if (!foodManufacturer) {
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're using the service's findOne function, that function will throw if the manufacturer is not found, so we don't need this existence check here or in the manufacturer deny function

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,
Expand All @@ -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 });
}
}
13 changes: 13 additions & 0 deletions apps/backend/src/pantries/pantries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Inject,
Injectable,
NotFoundException,
ConflictException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
Expand Down Expand Up @@ -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,
Expand All @@ -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 });
}

Expand Down
Loading