-
Notifications
You must be signed in to change notification settings - Fork 0
Automated event creation for submitted event requests #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,4 +4,54 @@ | |||||||||
|
|
||||||||||
| import { factories } from '@strapi/strapi' | ||||||||||
|
|
||||||||||
| export default factories.createCoreController('api::event-request.event-request'); | ||||||||||
| export default factories.createCoreController('api::event-request.event-request', ({ strapi }) => ({ | ||||||||||
| async approve(ctx) { | ||||||||||
| const { id } = ctx.params; | ||||||||||
|
|
||||||||||
| const request: any = await strapi.documents('api::event-request.event-request').findOne({ | ||||||||||
| documentId: id, | ||||||||||
| populate: ['event' as any], | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is any needed here? |
||||||||||
| }); | ||||||||||
|
|
||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also verify if the user is authorized for an additional check.
Suggested change
|
||||||||||
| if (!request) { | ||||||||||
| return ctx.notFound('Event request not found'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (!request.event) { | ||||||||||
| return ctx.badRequest('No linked draft event found'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (request.status === 'approved') { | ||||||||||
| return ctx.badRequest('Event request already approved'); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| try { | ||||||||||
| await strapi.documents('api::event.event').publish({ | ||||||||||
| documentId: request.event.documentId, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| await strapi.documents('api::event-request.event-request').update({ | ||||||||||
| documentId: id, | ||||||||||
| data: { status: 'approved' } as any, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| await strapi.plugins['email'].services.email.send({ | ||||||||||
| to: request.initiatorEmail, | ||||||||||
| from: 'hello@42.mk', | ||||||||||
| replyTo: 'hello@42.mk', | ||||||||||
| subject: 'Your event has been approved! - 42.mk', | ||||||||||
| html: ` | ||||||||||
| <p>Great news! Your event request "<strong>${request.eventName}</strong>" has been approved and is now published.</p> | ||||||||||
| <p>You can view it on our platform.</p> | ||||||||||
| <p>Best regards,<br/>42.mk Team</p> | ||||||||||
| `, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| ctx.body = { message: 'Event request approved' }; | ||||||||||
| } catch (error) { | ||||||||||
| strapi.log.error('Error approving event request:', error); | ||||||||||
| ctx.status = 500; | ||||||||||
| ctx.body = { error: { message: 'Failed to approve event request' } }; | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| })); | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,16 @@ | |
| * event-request router | ||
| */ | ||
|
|
||
| import { factories } from '@strapi/strapi'; | ||
|
|
||
| export default factories.createCoreRouter('api::event-request.event-request'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this line removed, you are removing all of the default routes (find, findOne, update,..etc) from strapi for event-request. This means that you are overriding the default routes and only adding one route for the event-request content type. |
||
| export default { | ||
| routes: [ | ||
| { | ||
| method: 'POST', | ||
| path: '/event-requests/:id/approve', | ||
| handler: 'event-request.approve', | ||
| config: { | ||
| policies: [], | ||
| middlewares: [], | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,55 @@ import { eventNotificationMiddleware } from './middlewares/event-notifications'; | |
|
|
||
| const FIREBASE_FCM_CREDENTIALS_PATH = './base42-mobile-app-fcm-firebase-adminsdk.json'; | ||
|
|
||
| const eventRequestApprovalMiddleware = () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Export this middleware to a new file, the same way event-notifications is handled |
||
| return async (context, next) => { | ||
| if (context.uid !== 'api::event.event') { | ||
| return await next(); | ||
| } | ||
|
|
||
| const result = await next(); | ||
|
|
||
| if (context.action === 'publish' && result?.documentId) { | ||
| try { | ||
| const request = await strapi.db.query('api::event-request.event-request').findOne({ | ||
| where: { | ||
| event: { documentId: result.documentId }, | ||
| }, | ||
| }); | ||
|
|
||
| if (request && request.status !== 'approved') { | ||
| await strapi.documents('api::event-request.event-request').update({ | ||
| documentId: request.documentId, | ||
| data: { status: 'approved' } as any, | ||
| }); | ||
|
|
||
| const reqDetails = await strapi.db.query('api::event-request.event-request').findOne({ | ||
| where: { documentId: request.documentId }, | ||
| }); | ||
|
|
||
| if (reqDetails?.initiatorEmail) { | ||
| await strapi.plugins['email'].services.email.send({ | ||
| to: reqDetails.initiatorEmail, | ||
| from: 'hello@42.mk', | ||
| replyTo: 'hello@42.mk', | ||
| subject: 'Your event has been approved! - 42.mk', | ||
| html: ` | ||
| <p>Great news! Your event request "<strong>${reqDetails.eventName}</strong>" has been approved and is now published.</p> | ||
| <p>You can view it on our platform.</p> | ||
| <p>Best regards,<br/>42.mk Team</p> | ||
| `, | ||
| }); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| strapi.log.error('[Event Request Approval] Error syncing status after publish:', error); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
| }; | ||
|
|
||
| export default { | ||
| /** | ||
| * An asynchronous register function that runs before | ||
|
|
@@ -42,6 +91,7 @@ export default { | |
| }); | ||
|
|
||
| strapi.documents.use(eventNotificationMiddleware()); | ||
| strapi.documents.use(eventRequestApprovalMiddleware()); | ||
| }, | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try not to use any a lot, maybe define a type for the request. (& everywhere that can be changed)