-
Notifications
You must be signed in to change notification settings - Fork 0
[SSF-102] Add endpoints to update user info #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { IsString, IsPhoneNumber, IsOptional } from 'class-validator'; | ||
|
|
||
| export class updateUserInfo { | ||
| @IsOptional() | ||
| @IsString() | ||
| firstName?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| lastName?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add |
||
| @IsPhoneNumber('US', { | ||
| message: | ||
| 'phone must be a valid phone number (make sure all the digits are correct)', | ||
| }) | ||
| phone?: string; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { userSchemaDto } from './dtos/userSchema.dto'; | |
|
|
||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { mock } from 'jest-mock-extended'; | ||
| import { updateUserInfo } from './dtos/updateUserInfo.dto'; | ||
|
|
||
| const mockUserService = mock<UsersService>(); | ||
|
|
||
|
|
@@ -116,6 +117,38 @@ describe('UsersController', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('PUT :id/info', () => { | ||
| it('should update user info with valid information', async () => { | ||
| const updatedUser = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we type this? I would suggest moving the |
||
| ...mockUser1, | ||
| firstName: 'UpdatedFirstName', | ||
| lastName: 'UpdatedLastName', | ||
| phone: '777-777-7777', | ||
| }; | ||
| mockUserService.update.mockResolvedValue(updatedUser); | ||
|
|
||
| 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, updateUserSchema); | ||
| }); | ||
|
|
||
| it('should update user info with defaults', async () => { | ||
| mockUserService.update.mockResolvedValue(mockUser1); | ||
|
|
||
| const updateUserSchema: Partial<updateUserInfo> = {}; | ||
| const result = await controller.updateInfo(1, updateUserSchema); | ||
|
|
||
| expect(result).toEqual(mockUser1); | ||
| expect(mockUserService.update).toHaveBeenCalledWith(1, updateUserSchema); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add one more test here where we just update with the first name and last name, to test that an actual |
||
| }); | ||
|
|
||
| describe('POST /api/users', () => { | ||
| it('should create a new user with all required fields', async () => { | ||
| const createUserSchema: userSchemaDto = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import { User } from './user.entity'; | |
| import { Role } from './types'; | ||
| import { VOLUNTEER_ROLES } from './types'; | ||
| import { userSchemaDto } from './dtos/userSchema.dto'; | ||
| import { updateUserInfo } from './dtos/updateUserInfo.dto'; | ||
| //import { CurrentUserInterceptor } from '../interceptors/current-user.interceptor'; | ||
|
|
||
| @Controller('users') | ||
|
|
@@ -51,6 +52,21 @@ export class UsersController { | |
| return this.usersService.update(id, { role: role as Role }); | ||
| } | ||
|
|
||
| @Put(':id/info') | ||
| async updateInfo( | ||
| @Param('id', ParseIntPipe) id: number, | ||
| @Body() updateUserInfo: updateUserInfo, | ||
| ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we give this a |
||
| const { firstName, lastName, phone } = updateUserInfo; | ||
|
|
||
| const updateData: Partial<User> = {}; | ||
| if (firstName !== undefined) updateData.firstName = firstName; | ||
| if (lastName !== undefined) updateData.lastName = lastName; | ||
| if (phone !== undefined) updateData.phone = phone; | ||
|
|
||
| return this.usersService.update(id, updateData); | ||
| } | ||
|
|
||
| @Post('/') | ||
| async createUser(@Body() createUserDto: userSchemaDto): Promise<User> { | ||
| const { email, firstName, lastName, phone, role } = createUserDto; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add
@IsNotEmptyand@MaxLength(255)to this and the last name?