From 56ecd32dea50c086abbcf33945afef8563f100ac Mon Sep 17 00:00:00 2001 From: Danielzxccc Date: Wed, 1 May 2024 19:00:02 +0800 Subject: [PATCH] chore: add remove action to existing crops --- src/modules/Auth/AuthInteractor.ts | 4 +- src/modules/Community/CommunityController.ts | 15 +++++++ src/modules/Community/CommunityInteractor.ts | 19 ++++++++ src/modules/Community/CommunityOpenApi.ts | 47 ++++++++++++++++++++ src/modules/Community/CommunityRouter.ts | 6 +++ src/modules/Community/CommunityService.ts | 7 +++ 6 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/modules/Auth/AuthInteractor.ts b/src/modules/Auth/AuthInteractor.ts index 457eab2..6e8fbf9 100644 --- a/src/modules/Auth/AuthInteractor.ts +++ b/src/modules/Auth/AuthInteractor.ts @@ -56,11 +56,11 @@ export async function findForgottenAccount(account: string) { const user = await Service.findByEmailOrUsername(account) if (!user) { - throw new HttpError('Invalid email or contact', 401) + throw new HttpError('Invalid email or contact', 400) } if (user.isbanned) { - throw new HttpError('Your account has been banned.', 401) + throw new HttpError('Your account has been banned.', 400) } const { avatar, firstname, lastname, email, username, contact_number } = user diff --git a/src/modules/Community/CommunityController.ts b/src/modules/Community/CommunityController.ts index 41c9541..35a12e3 100644 --- a/src/modules/Community/CommunityController.ts +++ b/src/modules/Community/CommunityController.ts @@ -505,3 +505,18 @@ export async function eventAction(req: SessionRequest, res: Response) { errorHandler(res, error) } } + +export async function removeExistingCropReport( + req: SessionRequest, + res: Response +) { + try { + const { id } = req.params + const { userid } = req.session + + await Interactor.removeExistingCropReport(id, userid) + res.status(200).json({ mesage: 'Success' }) + } catch (error) { + errorHandler(res, error) + } +} diff --git a/src/modules/Community/CommunityInteractor.ts b/src/modules/Community/CommunityInteractor.ts index 6a17dc9..fd6d012 100644 --- a/src/modules/Community/CommunityInteractor.ts +++ b/src/modules/Community/CommunityInteractor.ts @@ -1053,3 +1053,22 @@ export async function eventAction( // `${user.firstname} ${user.lastname} is ${action} to your event (${event.title}).` // ) } + +export async function removeExistingCropReport(id: string, userid: string) { + const user = await getUserOrThrow(userid) + const cropReport = await findCommunityReportById(id, user.farm_id) + + if (!cropReport) { + throw new HttpError('Report not found', 404) + } + + if (cropReport.farmid !== user.farm_id) { + throw new HttpError('Unauthorized', 404) + } + + if (cropReport.date_harvested) { + throw new HttpError('This crop is already harvested', 400) + } + + await Service.deleteCommunityCropReport(id) +} diff --git a/src/modules/Community/CommunityOpenApi.ts b/src/modules/Community/CommunityOpenApi.ts index 106f6f0..965f74e 100644 --- a/src/modules/Community/CommunityOpenApi.ts +++ b/src/modules/Community/CommunityOpenApi.ts @@ -2070,3 +2070,50 @@ * - going * - interested */ + +/** + * @openapi + * /api/community-farm/remove/existing/report/{id}: + * delete: + * summary: Remove existing report + * tags: + * - Auth + * parameters: + * - in: path + * name: id + * required: true + * description: ID of the report to remove + * schema: + * type: string + * responses: + * "200": + * description: Report removed successfully + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/MessageResponse" + * "401": + * description: Unauthorized + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "400": + * description: Validation Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "404": + * description: Not Found Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ErrorResponse" + * "500": + * description: Server Error + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/ServerError" + */ diff --git a/src/modules/Community/CommunityRouter.ts b/src/modules/Community/CommunityRouter.ts index 22b91f5..8db68f8 100644 --- a/src/modules/Community/CommunityRouter.ts +++ b/src/modules/Community/CommunityRouter.ts @@ -135,3 +135,9 @@ CommunityRouter.delete( CommunityRouter.get('/event/view/:id', CommunityController.viewCommunityEvent) CommunityRouter.post('/event/action/:id', CommunityController.eventAction) + +CommunityRouter.delete( + '/remove/existing/report/:id', + UserGuard(['farm_head']), + CommunityController.removeExistingCropReport +) diff --git a/src/modules/Community/CommunityService.ts b/src/modules/Community/CommunityService.ts index 5780ab7..d9e8b60 100644 --- a/src/modules/Community/CommunityService.ts +++ b/src/modules/Community/CommunityService.ts @@ -891,3 +891,10 @@ export async function eventAction( .returningAll() .executeTakeFirst() } + +export async function deleteCommunityCropReport(id: string) { + return await db + .deleteFrom('community_crop_reports') + .where('id', '=', id) + .execute() +}