Implement foreign key violation handling in EmployeesRepository delete methods
- Enhanced the `delete` and `bulkDelete` methods in `EmployeesRepository` to handle foreign key violations by throwing a `ConflictException` with a descriptive message. - Added unit tests to verify that foreign key violations are correctly mapped to `ConflictException` and that unknown errors are rethrown as expected. - Refactored error handling in the `delete` methods to improve clarity and maintainability.
This commit is contained in:
@@ -220,6 +220,26 @@ describe('EmployeesRepository', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('delete maps foreign-key violations to ConflictException', async () => {
|
||||
returning.mockRejectedValueOnce({ code: '23503' });
|
||||
await expect(repository.delete('emp-1')).rejects.toMatchObject({
|
||||
constructor: ConflictException,
|
||||
message: 'Employee is referenced by other records',
|
||||
});
|
||||
|
||||
returning.mockRejectedValueOnce({
|
||||
cause: { code: '23503' },
|
||||
});
|
||||
await expect(repository.delete('emp-1')).rejects.toBeInstanceOf(
|
||||
ConflictException,
|
||||
);
|
||||
});
|
||||
|
||||
it('delete rethrows unknown errors', async () => {
|
||||
returning.mockRejectedValueOnce(new Error('db down'));
|
||||
await expect(repository.delete('emp-1')).rejects.toThrow('db down');
|
||||
});
|
||||
|
||||
it('bulkUpdateStatus and bulkDelete return 0 for empty ids', async () => {
|
||||
await expect(
|
||||
repository.bulkUpdateStatus([], Status.create('active'), 'user-1'),
|
||||
@@ -240,6 +260,16 @@ describe('EmployeesRepository', () => {
|
||||
await expect(repository.bulkDelete(['emp-1'])).resolves.toBe(1);
|
||||
});
|
||||
|
||||
it('bulkDelete maps foreign-key violations to ConflictException', async () => {
|
||||
returning.mockRejectedValueOnce({
|
||||
cause: { code: '23503' },
|
||||
});
|
||||
await expect(repository.bulkDelete(['emp-1'])).rejects.toMatchObject({
|
||||
constructor: ConflictException,
|
||||
message: 'Employee is referenced by other records',
|
||||
});
|
||||
});
|
||||
|
||||
it('extendListQuery is a passthrough hook', () => {
|
||||
const qb = { join: true };
|
||||
expect(repository.extendListQuery(qb, { limit: 10, offset: 0 })).toBe(qb);
|
||||
|
||||
Reference in New Issue
Block a user