Add delete event and husky lib
This commit is contained in:
103
src/api/event/controllers/delete.ts
Normal file
103
src/api/event/controllers/delete.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import type { Core } from "@strapi/strapi";
|
||||
import type { Context } from "koa";
|
||||
|
||||
/**
|
||||
* Custom delete handler for events
|
||||
*
|
||||
* When deleting an event:
|
||||
* 1. Verify that the current user is the owner of the event
|
||||
* 2. Delete all associated EventRelationship records
|
||||
* 3. Delete the event itself
|
||||
*
|
||||
* DELETE /events/:id
|
||||
*/
|
||||
export default ({ strapi }: { strapi: Core.Strapi }) =>
|
||||
async function deleteEvent(ctx: Context) {
|
||||
const userId = ctx.state.user?.id;
|
||||
const eventId = ctx.params.id;
|
||||
|
||||
// Validation
|
||||
if (!userId) {
|
||||
return ctx.badRequest("User ID is required");
|
||||
}
|
||||
|
||||
if (!eventId || isNaN(parseInt(eventId))) {
|
||||
return ctx.badRequest("Event ID is required and must be a valid number");
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Verify the event exists
|
||||
const event = await strapi.db.query("api::event.event").findOne({
|
||||
where: { id: parseInt(eventId) },
|
||||
});
|
||||
|
||||
if (!event) {
|
||||
return ctx.notFound("Event not found");
|
||||
}
|
||||
|
||||
// 2. Check that the current user is the owner of this event
|
||||
const ownerRelationship = await strapi.db
|
||||
.query("api::event-relationship.event-relationship")
|
||||
.findOne({
|
||||
where: {
|
||||
event: { id: parseInt(eventId) },
|
||||
relation: "owner",
|
||||
},
|
||||
populate: {
|
||||
author: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!ownerRelationship) {
|
||||
return ctx.forbidden("Event has no owner relationship - cannot delete");
|
||||
}
|
||||
|
||||
if (ownerRelationship.author?.id !== userId) {
|
||||
return ctx.forbidden("Only the event owner can delete this event");
|
||||
}
|
||||
|
||||
// 3. Delete all EventRelationship records associated with this event
|
||||
// First, find all relationships for this event
|
||||
const allRelationships = await strapi.db
|
||||
.query("api::event-relationship.event-relationship")
|
||||
.findMany({
|
||||
where: {
|
||||
event: {
|
||||
id: parseInt(eventId),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Delete each relationship individually
|
||||
let deletedCount = 0;
|
||||
for (const relationship of allRelationships) {
|
||||
await strapi.db
|
||||
.query("api::event-relationship.event-relationship")
|
||||
.delete({
|
||||
where: { id: relationship.id },
|
||||
});
|
||||
deletedCount++;
|
||||
}
|
||||
|
||||
strapi.log.info(
|
||||
`Deleted ${deletedCount} EventRelationship records for event ${eventId}`
|
||||
);
|
||||
|
||||
// 4. Delete the event itself
|
||||
await strapi.db.query("api::event.event").delete({
|
||||
where: { id: parseInt(eventId) },
|
||||
});
|
||||
|
||||
strapi.log.info(
|
||||
`Event ${eventId} deleted successfully by user ${userId}`
|
||||
);
|
||||
|
||||
// Return success response
|
||||
ctx.send({ message: "Event deleted successfully" }, 200);
|
||||
} catch (error) {
|
||||
strapi.log.error("Error in deleteEvent controller:", error);
|
||||
return ctx.internalServerError(
|
||||
`Failed to delete event: ${error.message}`
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -14,7 +14,7 @@
|
||||
"name": "Apache 2.0",
|
||||
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
|
||||
},
|
||||
"x-generation-date": "2025-10-20T21:52:11.110Z"
|
||||
"x-generation-date": "2025-10-21T16:36:26.242Z"
|
||||
},
|
||||
"x-strapi-config": {
|
||||
"plugins": [
|
||||
|
||||
Reference in New Issue
Block a user