Skip to content
Open
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
11 changes: 10 additions & 1 deletion apps/backend/src/aws/aws-s3.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Injectable } from '@nestjs/common';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';

interface MulterFile {
fieldname: string;
originalname: string;
encoding: string;
mimetype: string;
size: number;
buffer: Buffer;
}

@Injectable()
export class AWSS3Service {
private client: S3Client;
Expand All @@ -22,7 +31,7 @@ export class AWSS3Service {
});
}

async upload(files: Express.Multer.File[]): Promise<string[]> {
async upload(files: MulterFile[]): Promise<string[]> {
const uploadedFileUrls: string[] = [];
try {
for (const file of files) {
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/config/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { RemoveUnusedStatuses1764816885341 } from '../migrations/1764816885341-R
import { UpdatePantryFields1763762628431 } from '../migrations/1763762628431-UpdatePantryFields';
import { PopulateDummyData1768501812134 } from '../migrations/1768501812134-populateDummyData';
import { RemovePantryFromOrders1769316004958 } from '../migrations/1769316004958-RemovePantryFromOrders';
import { MoveRequestFieldsToOrders1770571145350 } from '../migrations/1770571145350-MoveRequestFieldsToOrders';

const schemaMigrations = [
User1725726359198,
Expand Down Expand Up @@ -54,6 +55,7 @@ const schemaMigrations = [
RemoveUnusedStatuses1764816885341,
PopulateDummyData1768501812134,
RemovePantryFromOrders1769316004958,
MoveRequestFieldsToOrders1770571145350,
];

export default schemaMigrations;
16 changes: 7 additions & 9 deletions apps/backend/src/foodRequests/request.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('RequestsController', () => {
mockRequestsService.findOne.mockReset();
mockRequestsService.find.mockReset();
mockRequestsService.create.mockReset();
mockRequestsService.updateDeliveryDetails?.mockReset();
// mockRequestsService.updateDeliveryDetails?.mockReset(); // Removed - method no longer exists

Choose a reason for hiding this comment

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

Let's actually delete all of these isntead of commenting them out.

mockRequestsService.getOrderDetails.mockReset();
mockAWSS3Service.upload.mockReset();
mockOrdersService.updateStatus.mockReset();
Expand Down Expand Up @@ -151,9 +151,9 @@ describe('RequestsController', () => {
requestedSize: RequestSize.MEDIUM,
requestedItems: ['Test item 1', 'Test item 2'],
additionalInformation: 'Test information.',
dateReceived: null,
feedback: null,
photos: null,
// dateReceived: null, // Removed - no longer on FoodRequest
// feedback: null, // Removed - no longer on FoodRequest
// photos: null, // Removed - no longer on FoodRequest
};

const createdRequest: Partial<FoodRequest> = {
Expand All @@ -175,14 +175,12 @@ describe('RequestsController', () => {
createBody.requestedSize,
createBody.requestedItems,
createBody.additionalInformation,
createBody.dateReceived,
createBody.feedback,
createBody.photos,
);
});
});

describe('POST /:requestId/confirm-delivery', () => {
// COMMENTED OUT: This endpoint was moved to orders controller
/* describe('POST /:requestId/confirm-delivery', () => {
it('should upload photos, update the order, then update the request', async () => {
const requestId = 1;

Expand Down Expand Up @@ -379,5 +377,5 @@ describe('RequestsController', () => {
),
).rejects.toThrow('Invalid date format for deliveryDate');
});
});
}); */
});
108 changes: 1 addition & 107 deletions apps/backend/src/foodRequests/request.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,18 @@ import {
ParseIntPipe,
Post,
Body,
UploadedFiles,
UseInterceptors,
BadRequestException,
NotFoundException,
} from '@nestjs/common';
import { ApiBody } from '@nestjs/swagger';
import { RequestsService } from './request.service';
import { FoodRequest } from './request.entity';
import { AWSS3Service } from '../aws/aws-s3.service';
import { FilesInterceptor } from '@nestjs/platform-express';
import * as multer from 'multer';
import { OrdersService } from '../orders/order.service';
import { RequestSize } from './types';
import { OrderStatus } from '../orders/types';
import { OrderDetailsDto } from './dtos/order-details.dto';

@Controller('requests')
// @UseInterceptors()
export class RequestsController {
constructor(
private requestsService: RequestsService,
private awsS3Service: AWSS3Service,

Choose a reason for hiding this comment

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

Let's clean up the request.module.ts file if we are removing these. We should not need the AWSS3Module anymore, as well as the Multer one I believe.

private ordersService: OrdersService,
) {}
constructor(private requestsService: RequestsService) {}

@Get('/:requestId')
async getRequest(
Expand Down Expand Up @@ -73,19 +61,6 @@ export class RequestsController {
nullable: true,
example: 'Urgent request',
},
dateReceived: {
type: 'string',
format: 'date-time',
nullable: true,
example: null,
},
feedback: { type: 'string', nullable: true, example: null },
photos: {
type: 'array',
items: { type: 'string' },
nullable: true,
example: [],
},
},
},
})
Expand All @@ -96,9 +71,6 @@ export class RequestsController {
requestedSize: RequestSize;
requestedItems: string[];
additionalInformation: string;
dateReceived: Date;
feedback: string;
photos: string[];
},
): Promise<FoodRequest> {
if (
Expand All @@ -111,84 +83,6 @@ export class RequestsController {
body.requestedSize,
body.requestedItems,
body.additionalInformation,
body.dateReceived,
body.feedback,
body.photos,
);
}

//TODO: delete endpoint, here temporarily as a logic reference for order status impl.
@Post('/:requestId/confirm-delivery')
@ApiBody({
description: 'Details for a confirmation form',
schema: {
type: 'object',
properties: {
dateReceived: {
type: 'string',
format: 'date-time',
nullable: true,
example: new Date().toISOString(),
},
feedback: {
type: 'string',
nullable: true,
example: 'Wonderful shipment!',
},
photos: {
type: 'array',
items: { type: 'string' },
nullable: true,
example: [],
},
},
},
})
@UseInterceptors(
FilesInterceptor('photos', 10, { storage: multer.memoryStorage() }),
)
async confirmDelivery(
@Param('requestId', ParseIntPipe) requestId: number,
@Body() body: { dateReceived: string; feedback: string },
@UploadedFiles() photos?: Express.Multer.File[],
): Promise<FoodRequest> {
const formattedDate = new Date(body.dateReceived);
if (isNaN(formattedDate.getTime())) {
throw new Error('Invalid date format for deliveryDate');
}

const uploadedPhotoUrls =
photos && photos.length > 0 ? await this.awsS3Service.upload(photos) : [];
console.log(
'Received photo files:',
photos?.map((p) => p.originalname),
'| Count:',
photos?.length,
);

const updatedRequest = await this.requestsService.updateDeliveryDetails(
requestId,
formattedDate,
body.feedback,
uploadedPhotoUrls,
);

if (!updatedRequest) {
throw new NotFoundException('Invalid request ID');
}

if (!updatedRequest.orders || updatedRequest.orders.length == 0) {
throw new NotFoundException(
'No associated orders found for this request',
);
}

await Promise.all(
updatedRequest.orders.map((order) =>
this.ordersService.updateStatus(order.orderId, OrderStatus.DELIVERED),
),
);

return updatedRequest;
}
}
18 changes: 9 additions & 9 deletions apps/backend/src/foodRequests/request.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
JoinColumn,
} from 'typeorm';
import { Order } from '../orders/order.entity';
import { RequestSize } from './types';
import { RequestSize, FoodRequestStatus } from './types';
import { Pantry } from '../pantries/pantries.entity';

@Entity('food_requests')
Expand Down Expand Up @@ -44,14 +44,14 @@ export class FoodRequest {
})
requestedAt: Date;

@Column({ name: 'date_received', type: 'timestamp', nullable: true })
dateReceived: Date;

@Column({ name: 'feedback', type: 'text', nullable: true })
feedback: string;

@Column({ name: 'photos', type: 'text', array: true, nullable: true })
photos: string[];
@Column({
name: 'status',
type: 'enum',
enumName: 'food_requests_status_enum',
enum: FoodRequestStatus,
default: FoodRequestStatus.ACTIVE,
})
status: FoodRequestStatus;

@OneToMany(() => Order, (order) => order.request, { nullable: true })
orders: Order[];
Expand Down
32 changes: 12 additions & 20 deletions apps/backend/src/foodRequests/request.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const mockRequest: Partial<FoodRequest> = {
requestedItems: ['Canned Goods', 'Vegetables'],
additionalInformation: 'No onions, please.',
requestedAt: null,
dateReceived: null,
feedback: null,
photos: null,
// dateReceived: null, // Removed - no longer on FoodRequest

Choose a reason for hiding this comment

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

Same thing here, let's just delete everything instead of commenting it out.

// feedback: null, // Removed - no longer on FoodRequest
// photos: null, // Removed - no longer on FoodRequest
orders: null,
};

Expand Down Expand Up @@ -250,9 +250,6 @@ describe('RequestsService', () => {
mockRequest.requestedSize,
mockRequest.requestedItems,
mockRequest.additionalInformation,
mockRequest.dateReceived,
mockRequest.feedback,
mockRequest.photos,
);

expect(result).toEqual(mockRequest);
Expand All @@ -261,9 +258,6 @@ describe('RequestsService', () => {
requestedSize: mockRequest.requestedSize,
requestedItems: mockRequest.requestedItems,
additionalInformation: mockRequest.additionalInformation,
dateReceived: mockRequest.dateReceived,
feedback: mockRequest.feedback,
photos: mockRequest.photos,
});
expect(mockRequestsRepository.save).toHaveBeenCalledWith(mockRequest);
});
Expand All @@ -277,9 +271,6 @@ describe('RequestsService', () => {
RequestSize.MEDIUM,
['Canned Goods', 'Vegetables'],
'Additional info',
null,
null,
null,
),
).rejects.toThrow(`Pantry ${invalidPantryId} not found`);

Expand All @@ -299,9 +290,9 @@ describe('RequestsService', () => {
requestedItems: ['Rice', 'Beans'],
additionalInformation: 'Gluten-free items only.',
requestedAt: null,
dateReceived: null,
feedback: null,
photos: null,
// dateReceived: null, // Removed
// feedback: null, // Removed
// photos: null, // Removed
orders: null,
},
{
Expand All @@ -311,9 +302,9 @@ describe('RequestsService', () => {
requestedItems: ['Fruits', 'Snacks'],
additionalInformation: 'No nuts, please.',
requestedAt: null,
dateReceived: null,
feedback: null,
photos: null,
// dateReceived: null, // Removed
// feedback: null, // Removed
// photos: null, // Removed
orders: null,
},
];
Expand All @@ -332,7 +323,8 @@ describe('RequestsService', () => {
});
});

describe('updateDeliveryDetails', () => {
// COMMENTED OUT: updateDeliveryDetails method was removed, functionality moved to orders
/* describe('updateDeliveryDetails', () => {
it('should update and return the food request with new delivery details', async () => {
const mockOrder: Partial<Order> = {
orderId: 1,
Expand Down Expand Up @@ -489,5 +481,5 @@ describe('RequestsService', () => {
relations: ['orders'],
});
});
});
}); */
});
Loading