Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RequestsModule } from './foodRequests/request.module';
import { PantriesModule } from './pantries/pantries.module';
import { AssignmentsModule } from './volunteerAssignments/volunteerAssignments.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
Expand Down Expand Up @@ -34,7 +33,6 @@ import { AllocationModule } from './allocations/allocations.module';
AuthModule,
PantriesModule,
RequestsModule,
AssignmentsModule,
DonationModule,
DonationItemsModule,
OrdersModule,
Expand Down
11 changes: 3 additions & 8 deletions apps/backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';

import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { UsersService } from '../users/users.service';
import { User } from '../users/user.entity';
import { JwtStrategy } from './jwt.strategy';
import { UsersModule } from '../users/users.module';

@Module({
imports: [
TypeOrmModule.forFeature([User]),
PassportModule.register({ defaultStrategy: 'jwt' }),
],
imports: [UsersModule, PassportModule.register({ defaultStrategy: 'jwt' })],
controllers: [AuthController],
providers: [AuthService, UsersService, JwtStrategy],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}
2 changes: 2 additions & 0 deletions apps/backend/src/config/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AddingEnumValues1760538239997 } from '../migrations/1760538239997-Addin
import { UpdateColsToUseEnumType1760886499863 } from '../migrations/1760886499863-UpdateColsToUseEnumType';
import { UpdatePantriesTable1742739750279 } from '../migrations/1742739750279-updatePantriesTable';
import { RemoveOrdersDonationId1761500262238 } from '../migrations/1761500262238-RemoveOrdersDonationId';
import { AddVolunteerPantryUniqueConstraint1760033134668 } from '../migrations/1760033134668-AddVolunteerPantryUniqueConstraint';
import { AllergyFriendlyToBoolType1763963056712 } from '../migrations/1763963056712-AllergyFriendlyToBoolType';
import { UpdatePantryUserFieldsFixed1764350314832 } from '../migrations/1764350314832-UpdatePantryUserFieldsFixed';

Expand Down Expand Up @@ -55,6 +56,7 @@ const config = {
UpdateColsToUseEnumType1760886499863,
UpdatePantriesTable1742739750279,
RemoveOrdersDonationId1761500262238,
AddVolunteerPantryUniqueConstraint1760033134668,
AllergyFriendlyToBoolType1763963056712,
UpdatePantryUserFieldsFixed1764350314832,
],
Expand Down
9 changes: 6 additions & 3 deletions apps/backend/src/donationItems/donationItems.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class DonationItemsController {
status: { type: 'string', example: 'available' },
ozPerItem: { type: 'integer', example: 5 },
estimatedValue: { type: 'integer', example: 100 },
foodType: {
type: 'string',
foodType: {
type: 'string',
enum: Object.values(FoodType),
example: FoodType.DAIRY_FREE_ALTERNATIVES,
},
Expand All @@ -59,7 +59,10 @@ export class DonationItemsController {
foodType: FoodType;
},
): Promise<DonationItem> {
if (body.foodType && !Object.values(FoodType).includes(body.foodType as FoodType)) {
if (
body.foodType &&
!Object.values(FoodType).includes(body.foodType as FoodType)
) {
throw new BadRequestException('Invalid foodtype');
}
return this.donationItemsService.create(
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class DonationsController {
type: 'string',
format: 'date-time',
},
status: {
type: 'string',
status: {
type: 'string',
enum: Object.values(DonationStatus),
example: DonationStatus.AVAILABLE,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/donations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export enum DonationStatus {
AVAILABLE = 'available',
FULFILLED = 'fulfilled',
MATCHING = 'matching',
}
}
9 changes: 6 additions & 3 deletions apps/backend/src/foodRequests/request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export class FoodRequestsController {
type: 'object',
properties: {
pantryId: { type: 'integer', example: 1 },
requestedSize: {
type: 'string',
requestedSize: {
type: 'string',
enum: Object.values(RequestSize),
example: RequestSize.LARGE,
},
Expand Down Expand Up @@ -166,7 +166,10 @@ export class FoodRequestsController {
);

const request = await this.requestsService.findOne(requestId);
await this.ordersService.updateStatus(request.order.orderId, OrderStatus.DELIVERED);
await this.ordersService.updateStatus(
request.order.orderId,
OrderStatus.DELIVERED,
);

return this.requestsService.updateDeliveryDetails(
requestId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddVolunteerPantryUniqueConstraint1760033134668
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE volunteer_assignments DROP COLUMN assignment_id;

ALTER TABLE volunteer_assignments
ADD PRIMARY KEY (volunteer_id, pantry_id);

ALTER TABLE volunteer_assignments DROP CONSTRAINT IF EXISTS fk_volunteer_id;

ALTER TABLE volunteer_assignments DROP CONSTRAINT IF EXISTS fk_pantry_id;

ALTER TABLE volunteer_assignments
ADD CONSTRAINT fk_volunteer_id FOREIGN KEY (volunteer_id) REFERENCES users(user_id) ON DELETE CASCADE,
ADD CONSTRAINT fk_pantry_id FOREIGN KEY (pantry_id) REFERENCES pantries(pantry_id) ON DELETE CASCADE;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE volunteer_assignments DROP CONSTRAINT fk_volunteer_id;

ALTER TABLE volunteer_assignments DROP CONSTRAINT fk_pantry_id;

ALTER TABLE volunteer_assignments DROP CONSTRAINT volunteer_assignments_pkey;

ALTER TABLE volunteer_assignments ADD COLUMN assignment_id SERIAL PRIMARY KEY;

ALTER TABLE volunteer_assignments
ADD CONSTRAINT fk_volunteer_id FOREIGN KEY(volunteer_id) REFERENCES users(user_id),
ADD CONSTRAINT fk_pantry_id FOREIGN KEY(pantry_id) REFERENCES pantries(pantry_id);
`);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AllergyFriendlyToBoolType1763963056712 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
export class AllergyFriendlyToBoolType1763963056712
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE pantries
ALTER COLUMN dedicated_allergy_friendly TYPE BOOLEAN USING (FALSE),
ALTER COLUMN dedicated_allergy_friendly SET NOT NULL;
`);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE pantries
ALTER COLUMN dedicated_allergy_friendly TYPE VARCHAR(255),
ALTER COLUMN dedicated_allergy_friendly DROP NOT NULL;
`);
}

}
}
9 changes: 8 additions & 1 deletion apps/backend/src/orders/order.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { OrdersService } from './order.service';
import { mock } from 'jest-mock-extended';
import { Pantry } from '../pantries/pantries.entity';
import { User } from '../users/user.entity';
import { AllergensConfidence, ClientVisitFrequency, PantryStatus, RefrigeratedDonation, ServeAllergicChildren } from '../pantries/types';
import {
AllergensConfidence,
ClientVisitFrequency,
PantryStatus,
RefrigeratedDonation,
ServeAllergicChildren,
} from '../pantries/types';
import { OrderStatus } from './types';

const mockOrdersRepository = mock<Repository<Order>>();
Expand Down Expand Up @@ -37,6 +43,7 @@ const mockPantry: Pantry = {
activitiesComments: '',
itemsInStock: '',
needMoreOptions: '',
volunteers: [],
};

describe('OrdersService', () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/pantries/pantries.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PrimaryGeneratedColumn,
OneToOne,
JoinColumn,
ManyToMany,
} from 'typeorm';
import { User } from '../users/user.entity';
import {
Expand Down Expand Up @@ -158,4 +159,7 @@ export class Pantry {

@Column({ name: 'need_more_options', type: 'text' })
needMoreOptions: string;

@ManyToMany(() => User, (user) => user.pantries)
volunteers?: User[];
}
8 changes: 3 additions & 5 deletions apps/backend/src/pantries/pantries.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from '../users/user.entity';
import { PantriesService } from './pantries.service';
import { PantriesController } from './pantries.controller';
import { JwtStrategy } from '../auth/jwt.strategy';
import { AuthService } from '../auth/auth.service';
import { Pantry } from './pantries.entity';
import { OrdersModule } from '../orders/order.module';

@Module({
imports: [TypeOrmModule.forFeature([Pantry, User]), OrdersModule],
imports: [TypeOrmModule.forFeature([Pantry]), OrdersModule],
controllers: [PantriesController],
providers: [PantriesService, AuthService, JwtStrategy],
providers: [PantriesService],
exports: [PantriesService],
})
export class PantriesModule {}
18 changes: 17 additions & 1 deletion apps/backend/src/pantries/pantries.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { Pantry } from './pantries.entity';
import { User } from '../users/user.entity';
import { validateId } from '../utils/validation.utils';
Expand Down Expand Up @@ -90,4 +90,20 @@ export class PantriesService {

await this.repo.update(id, { status: PantryStatus.DENIED });
}

async findByIds(pantryIds: number[]): Promise<Pantry[]> {
pantryIds.forEach((id) => validateId(id, 'Pantry'));

const pantries = await this.repo.findBy({ pantryId: In(pantryIds) });

if (pantries.length !== pantryIds.length) {
const foundIds = pantries.map((p) => p.pantryId);
const missingIds = pantryIds.filter((id) => !foundIds.includes(id));
throw new NotFoundException(
`Pantries not found: ${missingIds.join(', ')}`,
);
}

return pantries;
}
}
2 changes: 1 addition & 1 deletion apps/backend/src/pantries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export enum PantryStatus {
export enum Activity {
CREATE_LABELED_SHELF = 'Create labeled shelf',
PROVIDE_EDUCATIONAL_PAMPHLETS = 'Provide educational pamphlets',
TRACK_DIETARY_NEEDS ='Spreadsheet to track dietary needs',
TRACK_DIETARY_NEEDS = 'Spreadsheet to track dietary needs',
POST_RESOURCE_FLYERS = 'Post allergen-free resource flyers',
SURVEY_CLIENTS = 'Survey clients to determine medical dietary needs',
COLLECT_FEEDBACK = 'Collect feedback from allergen-avoidant clients',
Expand Down
1 change: 0 additions & 1 deletion apps/backend/src/users/dtos/userSchema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
IsEnum,
IsNotEmpty,
IsString,
IsOptional,
IsPhoneNumber,
} from 'class-validator';
import { Role } from '../types';
Expand Down
23 changes: 22 additions & 1 deletion apps/backend/src/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
import {
Entity,
Column,
PrimaryGeneratedColumn,
ManyToMany,
JoinTable,
} from 'typeorm';

import { Role } from './types';
import { Pantry } from '../pantries/pantries.entity';

@Entity()
export class User {
Expand Down Expand Up @@ -30,4 +37,18 @@ export class User {
length: 20,
})
phone: string;

@ManyToMany(() => Pantry, (pantry) => pantry.volunteers)
@JoinTable({
name: 'volunteer_assignments',
joinColumn: {
name: 'volunteer_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'pantry_id',
referencedColumnName: 'pantryId',
},
})
pantries?: Pantry[];
}
Loading
Loading