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
30 changes: 30 additions & 0 deletions apps/backend/src/users/dtos/update-user-info.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
IsString,
IsPhoneNumber,
IsOptional,
IsNotEmpty,
MaxLength,
} from 'class-validator';

export class updateUserInfo {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make this more consistent with our other DTOs by naming the file update-user-info.dto.ts and the class UpdateUserInfoDto?

@IsOptional()
@IsString()
@IsNotEmpty()
@MaxLength(255)
firstName?: string;

@IsOptional()
@IsString()
@IsNotEmpty()
@MaxLength(255)
lastName?: string;

@IsOptional()
@IsString()
@IsNotEmpty()
@IsPhoneNumber('US', {
message:
'phone must be a valid phone number (make sure all the digits are correct)',
})
phone?: string;
}
18 changes: 15 additions & 3 deletions apps/backend/src/users/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ export class User {
})
role!: Role;

@Column()
@Column({
type: 'varchar',
name: 'first_name',
length: 255,
})
firstName!: string;

@Column()
@Column({
type: 'varchar',
name: 'last_name',
length: 255,
})
lastName!: string;

@Column()
@Column({
type: 'varchar',
name: 'email',
length: 255,
})
email!: string;

@Column({
Expand Down
45 changes: 32 additions & 13 deletions apps/backend/src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BadRequestException } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
import { User } from './user.entity';
import { Role } from './types';
import { userSchemaDto } from './dtos/userSchema.dto';

import { Test, TestingModule } from '@nestjs/testing';
import { mock } from 'jest-mock-extended';
import { updateUserInfo } from './dtos/update-user-info.dto';
import { Pantry } from '../pantries/pantries.entity';
import { BadRequestException } from '@nestjs/common';

const mockUserService = mock<UsersService>();

Expand Down Expand Up @@ -68,24 +69,42 @@ describe('UsersController', () => {
});
});

describe('PUT /:id/role', () => {
it('should update user role with valid role', async () => {
const updatedUser = { ...mockUser1, role: Role.ADMIN };
describe('PATCH :id/info', () => {
it('should update user info with valid information', async () => {
const updatedUser = {
...mockUser1,
firstName: 'UpdatedFirstName',
lastName: 'UpdatedLastName',
phone: '777-777-7777',
};
mockUserService.update.mockResolvedValue(updatedUser as User);

const result = await controller.updateRole(1, Role.ADMIN);
const updateUserSchema: updateUserInfo = {
firstName: 'UpdatedFirstName',
lastName: 'UpdatedLastName',
phone: '777-777-7777',
};
const result = await controller.updateInfo(1, updateUserSchema);

expect(result).toEqual(updatedUser);
expect(mockUserService.update).toHaveBeenCalledWith(1, {
role: Role.ADMIN,
});
expect(mockUserService.update).toHaveBeenCalledWith(1, updateUserSchema);
});

it('should throw BadRequestException for invalid role', async () => {
await expect(controller.updateRole(1, 'invalid_role')).rejects.toThrow(
BadRequestException,
it('should throw BadRequestException when DTO is empty', async () => {
mockUserService.update.mockRejectedValue(
new BadRequestException(
'At least one field must be provided to update',
),
);

const updateUserSchema: updateUserInfo = {};

await expect(controller.updateInfo(1, updateUserSchema)).rejects.toThrow(
new BadRequestException(
'At least one field must be provided to update',
),
);
expect(mockUserService.update).not.toHaveBeenCalled();
expect(mockUserService.update).toHaveBeenCalledWith(1, updateUserSchema);
});
});

Expand Down
16 changes: 6 additions & 10 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
Get,
Param,
ParseIntPipe,
Put,
Post,
BadRequestException,
Body,
Patch,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { User } from './user.entity';
import { Role } from './types';
import { userSchemaDto } from './dtos/userSchema.dto';
import { updateUserInfo } from './dtos/update-user-info.dto';

@Controller('users')
export class UsersController {
Expand All @@ -28,15 +27,12 @@ export class UsersController {
return this.usersService.remove(userId);
}

@Put('/:id/role')
async updateRole(
@Patch('/:id')
async updateInfo(
@Param('id', ParseIntPipe) id: number,
@Body('role') role: string,
@Body() dto: updateUserInfo,
): Promise<User> {
if (!Object.values(Role).includes(role as Role)) {
throw new BadRequestException('Invalid role');
}
return this.usersService.update(id, { role: role as Role });
return this.usersService.update(id, dto);
}

@Post('/')
Expand Down
Loading