Skip to content

Commit

Permalink
chore: add remove action to existing crops
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielzxccc committed May 1, 2024
1 parent cd4181f commit 56ecd32
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modules/Auth/AuthInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/modules/Community/CommunityController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
19 changes: 19 additions & 0 deletions src/modules/Community/CommunityInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
47 changes: 47 additions & 0 deletions src/modules/Community/CommunityOpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
*/
6 changes: 6 additions & 0 deletions src/modules/Community/CommunityRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 7 additions & 0 deletions src/modules/Community/CommunityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 56ecd32

Please sign in to comment.