|
4 | 4 |
|
5 | 5 | import { factories } from '@strapi/strapi' |
6 | 6 |
|
7 | | -export default factories.createCoreController('api::event-request.event-request'); |
| 7 | +export default factories.createCoreController('api::event-request.event-request', ({ strapi }) => ({ |
| 8 | + async approve(ctx) { |
| 9 | + const user = ctx.state?.user; |
| 10 | + if (!user) { |
| 11 | + return ctx.unauthorized(); |
| 12 | + } |
| 13 | + |
| 14 | + const { id } = ctx.params; |
| 15 | + |
| 16 | + const request = (await strapi.documents('api::event-request.event-request').findOne({ |
| 17 | + documentId: id, |
| 18 | + populate: { event: true } as never, |
| 19 | + })) as { |
| 20 | + documentId: string; |
| 21 | + status?: string; |
| 22 | + initiatorEmail?: string; |
| 23 | + eventName?: string; |
| 24 | + event?: { documentId: string } | null; |
| 25 | + } | null; |
| 26 | + |
| 27 | + if (!request) { |
| 28 | + return ctx.notFound('Event request not found'); |
| 29 | + } |
| 30 | + |
| 31 | + if (!request.event) { |
| 32 | + return ctx.badRequest('No linked draft event found'); |
| 33 | + } |
| 34 | + |
| 35 | + if (request.status === 'approved') { |
| 36 | + return ctx.badRequest('Event request already approved'); |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + await strapi.documents('api::event.event').publish({ |
| 41 | + documentId: request.event.documentId, |
| 42 | + }); |
| 43 | + |
| 44 | + await strapi.documents('api::event-request.event-request').update({ |
| 45 | + documentId: id, |
| 46 | + data: { status: 'approved' } as Record<string, unknown>, |
| 47 | + }); |
| 48 | + |
| 49 | + await strapi.plugins['email'].services.email.send({ |
| 50 | + to: request.initiatorEmail, |
| 51 | + from: 'hello@42.mk', |
| 52 | + replyTo: 'hello@42.mk', |
| 53 | + subject: 'Your event has been approved! - 42.mk', |
| 54 | + html: ` |
| 55 | + <p>Great news! Your event request "<strong>${request.eventName}</strong>" has been approved and is now published.</p> |
| 56 | + <p>You can view it on our platform.</p> |
| 57 | + <p>Best regards,<br/>42.mk Team</p> |
| 58 | + `, |
| 59 | + }); |
| 60 | + |
| 61 | + ctx.body = { message: 'Event request approved' }; |
| 62 | + } catch (error) { |
| 63 | + strapi.log.error('Error approving event request:', error); |
| 64 | + ctx.status = 500; |
| 65 | + ctx.body = { error: { message: 'Failed to approve event request' } }; |
| 66 | + } |
| 67 | + }, |
| 68 | +})); |
0 commit comments