Skip to content

Commit

Permalink
Remove function will just return HTTP 200 or an error
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpetro committed Mar 2, 2024
1 parent 1072c78 commit d9ffeba
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ describe('IrrigationProgramsController', () => {

it('should call remove', async () => {
mockIrrigationProgramsService.remove.mockResolvedValue(mockDto)
const result = await controller.remove(mockDto.id)
await controller.remove(mockDto.id)
expect(mockIrrigationProgramsService.remove).toHaveBeenCalledWith(mockDto.id)
expect(result).toEqual(mockDto)
})
})
2 changes: 1 addition & 1 deletion src/irrigation-programs/irrigation-programs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ export class IrrigationProgramsController {

@Delete(':id')
async remove(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
return this.irrigationProgramsService.remove(id)
this.irrigationProgramsService.remove(id)
}
}
10 changes: 6 additions & 4 deletions src/irrigation-programs/irrigation-programs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,9 @@ describe('IrrigationProgramsService', () => {
const rev = '1-234'
mockHead.mockResolvedValue({ etag: rev })
mockDestroy.mockResolvedValue({ ok: true, id, rev })
const result = await service.remove(id)
await service.remove(id)
expect(mockHead).toHaveBeenCalledWith(id)
expect(mockDestroy).toHaveBeenCalledWith(id, rev)
expect(result).toBeTruthy()
})

it('should not remove an irrigation program because the revision cannot be found', async () => {
Expand Down Expand Up @@ -386,10 +385,13 @@ describe('IrrigationProgramsService', () => {
const rev = '1-234'
mockHead.mockResolvedValue({ etag: rev })
mockDestroy.mockResolvedValue({ ok: false })
const result = await service.remove(id)
try {
await service.remove(id)
} catch (error) {
expect(error.status).toBe(HttpStatus.INTERNAL_SERVER_ERROR)
}
expect(mockHead).toHaveBeenCalledWith(id)
expect(mockDestroy).toHaveBeenCalledWith(id, rev)
expect(result).toBeFalsy()
})
})
})
11 changes: 9 additions & 2 deletions src/irrigation-programs/irrigation-programs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ export class IrrigationProgramsService implements OnModuleInit {
async remove(id: string) {
try {
const { etag: revision } = await this.db.head(id)
const result = await this.db.destroy(id, revision)
return result.ok
// The revision is wrapped in double quotes, so we need to remove them
const trimmedRevision = revision.replace(/^"|"$/g, '')
const result = await this.db.destroy(id, trimmedRevision)
if (!result.ok) {
throw new HttpException(`Failed to delete irrigation program with ID ${id}`, HttpStatus.INTERNAL_SERVER_ERROR)
}
} catch (error) {
if (error instanceof HttpException) {
throw error
}
if (error.statusCode === 404) {
throw new HttpException(`Irrigation program with ID ${id} not found`, HttpStatus.NOT_FOUND)
} else {
Expand Down

0 comments on commit d9ffeba

Please sign in to comment.