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
2 changes: 1 addition & 1 deletion apps/backend/src/emails/awsSes.wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ export class AmazonSESWrapper {
},
});

return await this.client.send(command);
return this.client.send(command);
}
}
15 changes: 15 additions & 0 deletions apps/backend/src/foodManufacturers/manufacturers.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,21 @@ describe('FoodManufacturersService', () => {
});
});

describe('findByUserId', () => {
it('findByUserId success', async () => {
const manufacturer = await service.findOne(1);
const userId = manufacturer.foodManufacturerRepresentative.id;
const result = await service.findByUserId(userId);
expect(result.foodManufacturerId).toBe(1);
});

it('findByUserId with non-existent user throws NotFoundException', async () => {
await expect(service.findByUserId(9999)).rejects.toThrow(
new NotFoundException('Food Manufacturer for User 9999 not found'),
);
});
});

describe('getStats', () => {
it('returns proper stats for manufacturer', async () => {
const manufacturerId = 1;
Expand Down
23 changes: 21 additions & 2 deletions apps/backend/src/foodManufacturers/manufacturers.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
BadRequestException,
forwardRef,
Inject,
Injectable,
NotFoundException,
ConflictException,
Expand Down Expand Up @@ -34,6 +36,7 @@ export class FoodManufacturersService {
@InjectRepository(FoodManufacturer)
private repo: Repository<FoodManufacturer>,

@Inject(forwardRef(() => UsersService))
private usersService: UsersService,
private emailsService: EmailsService,

Expand Down Expand Up @@ -140,7 +143,7 @@ export class FoodManufacturersService {
}

async getPendingManufacturers(): Promise<FoodManufacturer[]> {
return await this.repo.find({
return this.repo.find({
where: { status: ApplicationStatus.PENDING },
relations: ['foodManufacturerRepresentative'],
});
Expand Down Expand Up @@ -255,7 +258,7 @@ export class FoodManufacturersService {

Object.assign(manufacturer, foodManufacturerData);

return await this.repo.save(manufacturer);
return this.repo.save(manufacturer);
}

async approve(id: number) {
Expand Down Expand Up @@ -325,6 +328,22 @@ export class FoodManufacturersService {
await this.repo.update(id, { status: ApplicationStatus.DENIED });
}

async findByUserId(userId: number): Promise<FoodManufacturer> {
validateId(userId, 'User');

const foodManufacturer = await this.repo.findOne({
where: { foodManufacturerRepresentative: { id: userId } },
Comment thread
dburkhart07 marked this conversation as resolved.
relations: ['foodManufacturerRepresentative'],
});

if (!foodManufacturer) {
throw new NotFoundException(
`Food Manufacturer for User ${userId} not found`,
);
}
return foodManufacturer;
}

async getStats(id: number): Promise<ManufacturerStatsDto> {
validateId(id, 'Food Manufacturer');

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/foodRequests/request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class RequestsService {
async find(pantryId: number) {
validateId(pantryId, 'Pantry');

return await this.repo.find({
return this.repo.find({
where: { pantryId },
relations: ['orders', 'pantry'],
});
Expand Down
8 changes: 2 additions & 6 deletions apps/backend/src/orders/order.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DonationStatus } from '../donations/types';
import { User } from '../users/users.entity';
import { AuthService } from '../auth/auth.service';
import { DonationService } from '../donations/donations.service';
import { PantriesService } from '../pantries/pantries.service';
import { CreateOrderDto } from './dtos/create-order.dto';
import { DataSource } from 'typeorm';
import { EmailsService } from '../emails/email.service';
Expand Down Expand Up @@ -53,13 +54,8 @@ describe('OrdersService', () => {
DonationItemsService,
AllocationsService,
UsersService,
PantriesService,
DonationService,
EmailsService,
DonationService,
{
provide: DataSource,
useValue: testDataSource,
},
{
provide: DataSource,
useValue: testDataSource,
Expand Down
7 changes: 7 additions & 0 deletions apps/backend/src/pantries/pantries.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { UsersService } from '../users/users.service';
import { AuthService } from '../auth/auth.service';
import { User } from '../users/users.entity';
import { AllocationsService } from '../allocations/allocations.service';
import { FoodManufacturersService } from '../foodManufacturers/manufacturers.service';
import { FoodManufacturer } from '../foodManufacturers/manufacturers.entity';
import { UpdatePantryApplicationDto } from './dtos/update-pantry-application.dto';
import { EmailsService } from '../emails/email.service';
import { mock } from 'jest-mock-extended';
Expand Down Expand Up @@ -108,6 +110,7 @@ describe('PantriesService', () => {
providers: [
PantriesService,
UsersService,
FoodManufacturersService,
{
provide: AuthService,
useValue: {
Expand Down Expand Up @@ -138,6 +141,10 @@ describe('PantriesService', () => {
provide: getRepositoryToken(Donation),
useValue: testDataSource.getRepository(Donation),
},
{
provide: getRepositoryToken(FoodManufacturer),
useValue: testDataSource.getRepository(FoodManufacturer),
},
],
}).compile();

Expand Down
5 changes: 3 additions & 2 deletions apps/backend/src/pantries/pantries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class PantriesService {
}

async getPendingPantries(): Promise<Pantry[]> {
return await this.repo.find({
return this.repo.find({
where: { status: ApplicationStatus.PENDING },
relations: ['pantryUser'],
});
Expand Down Expand Up @@ -396,7 +396,7 @@ export class PantriesService {

Object.assign(pantry, pantryData);

return await this.repo.save(pantry);
return this.repo.save(pantry);
}

async approve(id: number) {
Expand Down Expand Up @@ -578,6 +578,7 @@ export class PantriesService {

const pantry = await this.repo.findOne({
where: { pantryUser: { id: userId } },
relations: ['pantryUser'],
});

if (!pantry) {
Expand Down
18 changes: 18 additions & 0 deletions apps/backend/src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('UsersController', () => {
mockUserService.remove.mockReset();
mockUserService.update.mockReset();
mockUserService.create.mockReset();
mockUserService.getUserDashboardStats.mockReset();

const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
Expand Down Expand Up @@ -123,6 +124,23 @@ describe('UsersController', () => {
});
});

describe('GET /:id/stats', () => {
it('should call getUserDashboardStats and return the result', async () => {
const mockStats = {
'Food Requests': '0',
Orders: '0',
Donations: '0',
Volunteers: '4',
};
mockUserService.getUserDashboardStats.mockResolvedValue(mockStats);

const result = await controller.getUserDashboardStats(1);

expect(result).toEqual(mockStats);
expect(mockUserService.getUserDashboardStats).toHaveBeenCalledWith(1);
});
});

describe('POST /api/users', () => {
it('should create a new user with all required fields', async () => {
const createUserSchema: userSchemaDto = {
Expand Down
10 changes: 10 additions & 0 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { UpdateUserInfoDto } from './dtos/update-user-info.dto';
import { AuthenticatedRequest } from '../auth/authenticated-request';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { UseGuards } from '@nestjs/common';
import { UserStatsDto } from './dtos/user-stats.dto';
import { PantryStatsDto } from '../pantries/dtos/pantry-stats.dto';
import { ManufacturerStatsDto } from '../foodManufacturers/dtos/manufacturer-stats.dto';

@Controller('users')
export class UsersController {
Expand All @@ -32,6 +35,13 @@ export class UsersController {
return this.usersService.findOne(userId);
}

@Get('/:id/stats')
async getUserDashboardStats(
@Param('id', ParseIntPipe) userId: number,
): Promise<UserStatsDto | PantryStatsDto | ManufacturerStatsDto> {
return this.usersService.getUserDashboardStats(userId);
}

@Delete('/:id')
removeUser(@Param('id', ParseIntPipe) userId: number): Promise<User> {
return this.usersService.remove(userId);
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { EmailsModule } from '../emails/email.module';
import { FoodRequest } from '../foodRequests/request.entity';
import { Order } from '../orders/order.entity';
import { Donation } from '../donations/donations.entity';
import { ManufacturerModule } from '../foodManufacturers/manufacturers.module';

@Module({
imports: [
TypeOrmModule.forFeature([User, FoodRequest, Order, Donation]),
forwardRef(() => PantriesModule),
forwardRef(() => AuthModule),
forwardRef(() => ManufacturerModule),
EmailsModule,
],
controllers: [UsersController],
Expand Down
Loading
Loading