Skip to content
Merged
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
50 changes: 50 additions & 0 deletions backend/src/notifications/__test__/notification.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ const mockScan = vi.fn().mockReturnThis();
const mockGet = vi.fn().mockReturnThis();
const mockSend = vi.fn().mockReturnThis(); // for SES
const mockPut = vi.fn().mockReturnThis();
const mockDelete = vi.fn().mockReturnThis();
const mockQuery = vi.fn().mockReturnThis();
const mockSendEmail = vi.fn().mockReturnThis();

const mockDocumentClient = {
scan: mockScan,
get: mockGet,
put : mockPut,
delete : mockDelete,
promise: mockPromise,
query : mockQuery
};
Expand Down Expand Up @@ -330,5 +332,53 @@ describe('NotificationController', () => {
});
});

describe('deleteNotification', () => {
it('should successfully delete a notification given a valid id', async () => {
mockPromise.mockResolvedValueOnce({})

const result = await notificationService.deleteNotification('0')

expect(mockDelete).toHaveBeenCalledTimes(1)

expect(mockDelete).toHaveBeenCalledWith({
TableName: 'BCANNotifications',
Key: {
notificationId: '0',
},
ConditionExpression: 'attribute_exists(notificationId)'
})

expect(result).toEqual('Notification with id 0 successfully deleted')
})

it('uses the fallback table when the environment variable is not set', async () => {
delete process.env.DYNAMODB_NOTIFICATION_TABLE_NAME
mockPromise.mockResolvedValueOnce({})

const result = await notificationService.deleteNotification('0')

expect(mockDelete).toHaveBeenCalledWith({
TableName: 'TABLE_FAILURE',
Key: {
notificationId: '0',
},
ConditionExpression: 'attribute_exists(notificationId)'
})
})

it('throws an error when the given notification id does not exist', async () => {
mockPromise.mockRejectedValueOnce({
code: 'ConditionalCheckFailedException',
message: 'The item does not exist'
});

try {
await notificationService.deleteNotification('999');
throw new Error('Expected deleteNotification to throw');
} catch (err: any) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('Notification with id 999 not found');
}
})
})
});
25 changes: 25 additions & 0 deletions backend/src/notifications/notifcation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,29 @@ export class NotificationService {
}
}

/**
* Deletes the notification with the given id from the database and returns a success message if the deletion was successful
* @param notificationId the id of the notification to delete
*/
async deleteNotification(notificationId: string): Promise<string> {
const params = {
TableName: process.env.DYNAMODB_NOTIFICATION_TABLE_NAME || 'TABLE_FAILURE',
Key: {
notificationId,
},
ConditionExpression: 'attribute_exists(notificationId)'
}

try {
await this.dynamoDb.delete(params).promise()
return `Notification with id ${notificationId} successfully deleted`
} catch (error: any) {
if (error.code === "ConditionalCheckFailedException") {
throw new Error(`Notification with id ${notificationId} not found`)
}

console.error(error)
throw new Error(`Failed to delete notification with id ${notificationId}`)
}
}
}
12 changes: 10 additions & 2 deletions backend/src/notifications/notification.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Post, Body, Get, Query, Param } from '@nestjs/common';
import { Controller, Post, Body, Get, Query, Param, Delete } from '@nestjs/common';
import { NotificationService } from './notifcation.service';
import { Notification } from '../../../middle-layer/types/Notification';

Expand Down Expand Up @@ -28,5 +28,13 @@ export class NotificationController {
return await this.notificationService.getNotificationByUserId(userId);
}


/**
* Deletes the notification with the given id from the database, if it exists.
*
* @param notificationId the id of the notification to delete
*/
@Delete(':notificationId')
async deleteNotification(@Param('notificationId') notificationId: string) {
return await this.notificationService.deleteNotification(notificationId);
}
}