Repository navigation
MSPCA-8/create & withdraw match endpoints #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2a91446
Add create and withdraw match endpoints
Yurika-Kan c42eb05
Restrict withdraw to pending matches
Yurika-Kan 42d026f
Add unit tests for AmazonSESWrapper
Yurika-Kan 23340bb
Remove PluralNamingStrategy
Yurika-Kan d36c8aa
Merge branch 'main' into yk/mspca-8-create-withdraw-match
Yurika-Kan 16a2c16
Restore PluralNamingStrategy with tableName only
Yurika-Kan 265772d
Assert SES client send in every wrapper test
Yurika-Kan 736bf48
Cover match email body and DTO validation
Yurika-Kan c323e4e
use IsPositive for match DTO IDs
Yurika-Kan 1a9643e
Document match endpoints for Swagger
Yurika-Kan 07dd558
Address match service spec review notes
Yurika-Kan 88f4536
Add docstring for notifyCoordinator
Yurika-Kan 296ca8f
Merge remote-tracking branch 'origin/main' into yk/mspca-8-create-wit…
Yurika-Kan 9c642fc
address review comments on match creation
Yurika-Kan 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 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
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,170 @@ | ||
| import { Test } from '@nestjs/testing'; | ||
| import { | ||
| SendEmailCommand, | ||
| SendEmailCommandOutput, | ||
| } from '@aws-sdk/client-sesv2'; | ||
| import { AmazonSESWrapper } from './awsSes.wrapper'; | ||
| import { AMAZON_SES_CLIENT } from './awsSesClient.factory'; | ||
| import { SendEmailDTO } from './sendEmail.dto'; | ||
|
|
||
| describe('AmazonSESWrapper', () => { | ||
| let wrapper: AmazonSESWrapper; | ||
| let mockClient: { send: jest.Mock }; | ||
|
|
||
| const originalSenderEmail = process.env.AWS_SES_SENDER_EMAIL; | ||
|
|
||
| const successOutput: SendEmailCommandOutput = { | ||
| MessageId: 'msg-1', | ||
| $metadata: { httpStatusCode: 200 }, | ||
| } as SendEmailCommandOutput; | ||
|
|
||
| const validDto: SendEmailDTO = { | ||
| toEmail: 'recipient@example.com', | ||
| subject: 'Hello', | ||
| bodyHtml: '<p>Hi there</p>', | ||
| }; | ||
|
|
||
| /** The SendEmailCommand the wrapper handed to the client on its only send. */ | ||
| const sentCommand = (): SendEmailCommand => | ||
| mockClient.send.mock.calls[0][0] as SendEmailCommand; | ||
|
|
||
| /** The composed MIME message, decoded so headers can be inspected. */ | ||
| const sentRawMessage = (): string => { | ||
| const data = sentCommand().input.Content?.Raw?.Data; | ||
| if (!data) { | ||
| throw new Error('Expected the sent command to carry a raw MIME message'); | ||
| } | ||
| return Buffer.from(data).toString('utf8'); | ||
| }; | ||
|
|
||
| beforeEach(async () => { | ||
| mockClient = { send: jest.fn().mockResolvedValue(successOutput) }; | ||
| process.env.AWS_SES_SENDER_EMAIL = 'sender@example.com'; | ||
|
|
||
| const moduleRef = await Test.createTestingModule({ | ||
| providers: [ | ||
| AmazonSESWrapper, | ||
| { provide: AMAZON_SES_CLIENT, useValue: mockClient }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| wrapper = moduleRef.get<AmazonSESWrapper>(AmazonSESWrapper); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalSenderEmail === undefined) { | ||
| delete process.env.AWS_SES_SENDER_EMAIL; | ||
| } else { | ||
| process.env.AWS_SES_SENDER_EMAIL = originalSenderEmail; | ||
| } | ||
| }); | ||
|
|
||
| describe('sendEmail', () => { | ||
| it('returns the client output and sends exactly one command', async () => { | ||
| const result = await wrapper.sendEmail(validDto); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentCommand()).toBeInstanceOf(SendEmailCommand); | ||
| expect(result).toBe(successOutput); | ||
| }); | ||
|
|
||
| it('addresses the recipient in Destination and in the MIME headers', async () => { | ||
| await wrapper.sendEmail(validDto); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentCommand().input.Destination).toEqual({ | ||
| ToAddresses: ['recipient@example.com'], | ||
| }); | ||
|
|
||
| const raw = sentRawMessage(); | ||
| expect(raw).toContain('To: recipient@example.com'); | ||
| expect(raw).toContain('Subject: Hello'); | ||
| expect(raw).toContain('<p>Hi there</p>'); | ||
| }); | ||
|
|
||
| it('uses AWS_SES_SENDER_EMAIL as the From address', async () => { | ||
| process.env.AWS_SES_SENDER_EMAIL = 'noreply@mspca.org'; | ||
|
|
||
| await wrapper.sendEmail(validDto); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentRawMessage()).toContain('From: noreply@mspca.org'); | ||
| }); | ||
|
|
||
| it('puts cc addresses in both Destination and the MIME headers', async () => { | ||
| await wrapper.sendEmail({ | ||
| ...validDto, | ||
| ccEmails: ['cc1@example.com', 'cc2@example.com'], | ||
| }); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentCommand().input.Destination?.CcAddresses).toEqual([ | ||
| 'cc1@example.com', | ||
| 'cc2@example.com', | ||
| ]); | ||
| expect(sentRawMessage()).toContain( | ||
| 'Cc: cc1@example.com, cc2@example.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('puts bcc addresses in Destination but never in the MIME headers', async () => { | ||
| await wrapper.sendEmail({ | ||
| ...validDto, | ||
| bccEmails: ['bcc@example.com'], | ||
| }); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentCommand().input.Destination?.BccAddresses).toEqual([ | ||
| 'bcc@example.com', | ||
| ]); | ||
|
|
||
| // A `Bcc:` header would leak the hidden recipient list to everyone else | ||
| // on the message, so it must not appear anywhere in the raw MIME. | ||
| // Note: MailComposer also strips Bcc unless `keepBcc: true` is passed, | ||
| // so this locks in the end behaviour rather than the wrapper's choice | ||
| // to leave `mailOptions.bcc` unset. | ||
| const raw = sentRawMessage(); | ||
| expect(raw).not.toMatch(/^Bcc:/im); | ||
| expect(raw).not.toContain('bcc@example.com'); | ||
| }); | ||
|
|
||
| it('omits CcAddresses and BccAddresses when the lists are absent or empty', async () => { | ||
| await wrapper.sendEmail({ ...validDto, ccEmails: [], bccEmails: [] }); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| expect(sentCommand().input.Destination).toEqual({ | ||
| ToAddresses: ['recipient@example.com'], | ||
| }); | ||
| }); | ||
|
|
||
| it('encodes attachments into the MIME message', async () => { | ||
| await wrapper.sendEmail({ | ||
| ...validDto, | ||
| attachments: [ | ||
| { filename: 'notes.txt', content: Buffer.from('hello attachment') }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| const raw = sentRawMessage(); | ||
| expect(raw).toContain('filename=notes.txt'); | ||
| expect(raw).toContain(Buffer.from('hello attachment').toString('base64')); | ||
| }); | ||
|
|
||
| it('propagates errors thrown by the SES client', async () => { | ||
| mockClient.send.mockRejectedValue(new Error('SES rejected: throttled')); | ||
|
|
||
| await expect(wrapper.sendEmail(validDto)).rejects.toThrow( | ||
| 'SES rejected: throttled', | ||
| ); | ||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('wraps non-Error rejections in an Error', async () => { | ||
| mockClient.send.mockRejectedValue('throttled'); | ||
|
|
||
| await expect(wrapper.sendEmail(validDto)).rejects.toThrow('throttled'); | ||
| expect(mockClient.send).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); | ||
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,11 @@ | ||
| import { IsInt, IsPositive } from 'class-validator'; | ||
|
|
||
| export class CreateMatchDto { | ||
| @IsInt() | ||
| @IsPositive() | ||
| volunteerId!: number; | ||
|
|
||
| @IsInt() | ||
| @IsPositive() | ||
| chameleonAnimalId!: number; | ||
| } |
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
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,7 +1,78 @@ | ||
| import { Controller } from '@nestjs/common'; | ||
| import { | ||
| Body, | ||
| Controller, | ||
| HttpStatus, | ||
| Param, | ||
| ParseIntPipe, | ||
| Patch, | ||
| Post, | ||
| } from '@nestjs/common'; | ||
| import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
| import { MatchesService } from './matches.service'; | ||
| import { Match } from './matches.entity'; | ||
| import { CreateMatchDto } from './dtos/create-match.dto'; | ||
| import { validateId } from '../utils/validation.utils'; | ||
|
|
||
| @ApiTags('Matches') | ||
| @Controller('matches') | ||
| export class MatchesController { | ||
| constructor(private matchesService: MatchesService) {} | ||
|
|
||
| @Post() | ||
|
Yurika-Kan marked this conversation as resolved.
|
||
| @ApiOperation({ | ||
| summary: 'Create a match', | ||
| description: | ||
| "Creates a PENDING match between a volunteer and a Chameleon animal and emails the volunteer's assigned foster coordinator.", | ||
| }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.CREATED, | ||
| description: 'The created match, with a PENDING status.', | ||
| type: Match, | ||
| }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.BAD_REQUEST, | ||
| description: | ||
| 'The request body is invalid or the volunteer is not active, so a match cannot be made.', | ||
| }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.NOT_FOUND, | ||
| description: 'No volunteer exists with the given ID.', | ||
| }) | ||
| async createMatch(@Body() body: CreateMatchDto): Promise<Match> { | ||
| return this.matchesService.create(body); | ||
| } | ||
|
|
||
| @Patch(':matchId/withdraw') | ||
| @ApiOperation({ | ||
| summary: 'Withdraw a match', | ||
| description: | ||
| 'Sets a PENDING match to WITHDRAWN. The record is kept so volunteers can still see their withdrawn applications.', | ||
| }) | ||
| @ApiParam({ | ||
| name: 'matchId', | ||
| type: Number, | ||
| description: 'The ID of the match to withdraw.', | ||
| example: 10, | ||
| }) | ||
| @ApiResponse({ | ||
|
Yurika-Kan marked this conversation as resolved.
|
||
| status: HttpStatus.OK, | ||
| description: 'The updated match, with a WITHDRAWN status.', | ||
| type: Match, | ||
| }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.BAD_REQUEST, | ||
| description: | ||
| 'The match ID is not a positive integer, or the match is not PENDING and so cannot be withdrawn.', | ||
| }) | ||
| @ApiResponse({ | ||
| status: HttpStatus.NOT_FOUND, | ||
| description: 'No match exists with the given ID.', | ||
| }) | ||
| async withdrawMatch( | ||
| @Param('matchId', ParseIntPipe) matchId: number, | ||
| ): Promise<Match> { | ||
| validateId(matchId, 'Match'); | ||
|
|
||
| return this.matchesService.withdraw(matchId); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.