forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
Sa/mspca 2 get volunteer by id and update volunteer by #2
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
Open
shreeyaadhikari
wants to merge
15
commits into
main
Choose a base branch
from
sa/mspca-2-get-volunteer-by-id-and-update-volunteer-by-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2bed4ed
Delete .github/dependabot.yml
dburkhart07 91a46b2
created update volunteer dto
shreeyaadhikari abb1c10
add service logic for get/update volunteer
shreeyaadhikari c8a565b
added not found exception for coordinator not found
shreeyaadhikari 833a402
added controller logic
shreeyaadhikari 0a2009f
service tests
shreeyaadhikari c548f81
service and controller tests
shreeyaadhikari 9f773f2
..
shreeyaadhikari e229d8b
reverted changes meant for another branch
shreeyaadhikari e93101f
Merge branch 'main' of https://github.com/Code-4-Community/mspca into…
shreeyaadhikari 26a1740
fixed prettier checks
shreeyaadhikari 58956c5
Merge branch 'main' of https://github.com/Code-4-Community/mspca into…
shreeyaadhikari ba4b369
resolved dto comments
shreeyaadhikari 1a7b55d
service.ts resolved
shreeyaadhikari fc1fcb1
controller + specs
shreeyaadhikari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| approvedGitRepositories: | ||
| - "**" | ||
|
|
||
| enableScripts: true | ||
|
|
||
| nodeLinker: node-modules | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { Homebase } from '../../types'; | ||
| import { FosterType } from '../volunteers.types'; | ||
| import { | ||
| IsOptional, | ||
| IsString, | ||
| IsNumber, | ||
| IsEnum, | ||
| IsPositive, | ||
| IsNotEmpty, | ||
| MaxLength, | ||
| IsPhoneNumber, | ||
| IsEmail, | ||
| } from 'class-validator'; | ||
|
|
||
| export class UpdateVolunteerDto { | ||
| @IsNumber() | ||
| @IsPositive() | ||
| volunteerId?: number; | ||
|
shreeyaadhikari marked this conversation as resolved.
shreeyaadhikari marked this conversation as resolved.
|
||
|
|
||
| @IsOptional() | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| firstName?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| lastName?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(20) | ||
| @IsPhoneNumber('US') | ||
| phone?: string; | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(20) | ||
| @IsPhoneNumber('US') | ||
| secondaryPhone?: string; | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| @IsEmail() | ||
| email?: string; | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| address?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(255) | ||
| city?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| @MaxLength(10) | ||
| zipcode?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsEnum(Homebase) | ||
| homebase?: Homebase; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| residentAnimals?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsString() | ||
| @IsNotEmpty() | ||
| notes?: string; | ||
|
|
||
| @IsOptional() | ||
| @IsEnum(FosterType) | ||
| fosterType?: FosterType; | ||
| } | ||
100 changes: 96 additions & 4 deletions
100
apps/backend/src/volunteers/volunteers.controller.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,117 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { NotFoundException, BadRequestException } from '@nestjs/common'; | ||
| import { VolunteersController } from './volunteers.controller'; | ||
| import { VolunteersService } from './volunteers.service'; | ||
| import { FosterVolunteer } from './volunteers.entity'; | ||
|
|
||
| describe('VolunteersController', () => { | ||
| let controller: VolunteersController; | ||
|
|
||
| const mockVolunteer = { | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
| volunteerId: 1, | ||
| firstName: 'Jane', | ||
| lastName: 'Doe', | ||
| notes: 'likes cats', | ||
| } as FosterVolunteer; | ||
|
|
||
| const mockVolunteersService = { | ||
| getVolunteerById: jest.fn(), | ||
| updateVolunteerById: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [VolunteersController], | ||
| providers: [ | ||
| { | ||
| provide: VolunteersService, | ||
| useValue: {}, | ||
| }, | ||
| { provide: VolunteersService, useValue: mockVolunteersService }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<VolunteersController>(VolunteersController); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('getVolunteerById', () => { | ||
| it('should return a volunteer when found', async () => { | ||
| mockVolunteersService.getVolunteerById.mockResolvedValue(mockVolunteer); | ||
|
|
||
| const result = await controller.getVolunteerById(1); | ||
|
|
||
| expect(result).toEqual(mockVolunteer); | ||
| expect(mockVolunteersService.getVolunteerById).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('should throw NotFoundException when volunteer does not exist', async () => { | ||
| mockVolunteersService.getVolunteerById.mockRejectedValue( | ||
| new NotFoundException('Volunteer with id 999 not found'), | ||
| ); | ||
|
|
||
| await expect(controller.getVolunteerById(999)).rejects.toThrow( | ||
| NotFoundException, | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException when id is invalid', async () => { | ||
| await expect(controller.getVolunteerById(0)).rejects.toThrow( | ||
| BadRequestException, | ||
| ); | ||
| expect(mockVolunteersService.getVolunteerById).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('updateVolunteerById', () => { | ||
| it('should update and return the volunteer when found', async () => { | ||
| const dto = { volunteerId: 1, notes: 'updated notes' }; | ||
| const updatedVolunteer = { ...mockVolunteer, ...dto } as FosterVolunteer; | ||
| mockVolunteersService.updateVolunteerById.mockResolvedValue( | ||
| updatedVolunteer, | ||
| ); | ||
|
|
||
| const result = await controller.updateVolunteerById(1, dto); | ||
|
|
||
| expect(result).toEqual(updatedVolunteer); | ||
| expect(mockVolunteersService.updateVolunteerById).toHaveBeenCalledWith( | ||
| 1, | ||
| dto, | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw NotFoundException when volunteer does not exist', async () => { | ||
| mockVolunteersService.updateVolunteerById.mockRejectedValue( | ||
| new NotFoundException('Volunteer with id 999 not found'), | ||
| ); | ||
|
|
||
| await expect( | ||
| controller.updateVolunteerById(999, { volunteerId: 999, notes: 'x' }), | ||
| ).rejects.toThrow(NotFoundException); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException when id is invalid', async () => { | ||
| await expect( | ||
| controller.updateVolunteerById(0, { volunteerId: 0, notes: 'x' }), | ||
| ).rejects.toThrow(BadRequestException); | ||
| expect(mockVolunteersService.updateVolunteerById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException when path and body ids do not match', async () => { | ||
| await expect( | ||
| controller.updateVolunteerById(1, { volunteerId: 2, notes: 'x' }), | ||
| ).rejects.toThrow(BadRequestException); | ||
| expect(mockVolunteersService.updateVolunteerById).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException when no fields besides volunteerId are provided', async () => { | ||
| await expect( | ||
| controller.updateVolunteerById(1, { volunteerId: 1 }), | ||
| ).rejects.toThrow(BadRequestException); | ||
| expect(mockVolunteersService.updateVolunteerById).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,67 @@ | ||
| import { Controller } from '@nestjs/common'; | ||
| import { | ||
| Controller, | ||
| Patch, | ||
| Get, | ||
| Param, | ||
| Body, | ||
| ParseIntPipe, | ||
| BadRequestException, | ||
| } from '@nestjs/common'; | ||
| import { ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger'; | ||
| import { VolunteersService } from './volunteers.service'; | ||
| import { validateId } from '../utils/validation.utils'; | ||
| import { UpdateVolunteerDto } from './dto/update-volunteer.dto'; | ||
| import { FosterVolunteer } from './volunteers.entity'; | ||
|
|
||
| // @ApiTags('Volunteers') | ||
| // @ApiBearerAuth() | ||
| @Controller('volunteers') | ||
| export class VolunteersController { | ||
| constructor(private volunteersService: VolunteersService) {} | ||
|
|
||
| // Example endpoint | ||
| // @Get('/:userId') | ||
| // async getUser(@Param('userId', ParseIntPipe) userId: number): Promise<User> { | ||
| // return this.usersService.findOne(userId); | ||
| // } | ||
| @Get('/:volunteerId') | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
| @ApiOperation({ summary: 'Get a volunteer by ID' }) | ||
| @ApiParam({ | ||
| name: 'volunteerId', | ||
| type: Number, | ||
| description: 'ID of the volunteer', | ||
| }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'The volunteer was found', | ||
| type: FosterVolunteer, | ||
| }) | ||
| async getVolunteerById( | ||
| @Param('volunteerId', ParseIntPipe) volunteerId: number, | ||
| ): Promise<FosterVolunteer> { | ||
| validateId(volunteerId, 'FosterVolunteer'); | ||
| return this.volunteersService.getVolunteerById(volunteerId); | ||
| } | ||
|
|
||
| @Patch('/:volunteerId') | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
| @ApiOperation({ summary: 'Update a volunteer by ID' }) | ||
| @ApiParam({ | ||
| name: 'volunteerId', | ||
| type: Number, | ||
| description: 'ID of the volunteer', | ||
| }) | ||
| @ApiResponse({ | ||
| status: 200, | ||
| description: 'The volunteer was updated', | ||
| type: FosterVolunteer, | ||
| }) | ||
| async updateVolunteerById( | ||
| @Param('volunteerId', ParseIntPipe) volunteerId: number, | ||
| @Body() dto: UpdateVolunteerDto, | ||
|
shreeyaadhikari marked this conversation as resolved.
|
||
| ): Promise<FosterVolunteer> { | ||
| validateId(volunteerId, 'FosterVolunteer'); | ||
| if (dto.volunteerId !== volunteerId) { | ||
| throw new BadRequestException('volunteerId in path and body must match'); | ||
| } | ||
| const fields = Object.keys(dto).filter((key) => key !== 'volunteerId'); | ||
| if (fields.length === 0) { | ||
| throw new BadRequestException('At least one field must be provided'); | ||
| } | ||
| return this.volunteersService.updateVolunteerById(volunteerId, dto); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think we need this file or the install-state.gz. Can you try deleting these and then running
yarn installto see if it reinstalls them? If not, let's keep them removed.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.
I'm not quite sure how to delete these from the PR.