Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cms/src/api/event-request/content-types/event-request/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
},
"physicalPresence": {
"type": "boolean"
},
"status": {
"type": "enumeration",
"enum": ["pending", "approved"],
"default": "pending"
},
"event": {
"type": "relation",
"relation": "oneToOne",
"target": "api::event.event"
}
}
}
52 changes: 51 additions & 1 deletion cms/src/api/event-request/controllers/event-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Copy link
Copy Markdown
Contributor

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)

documentId: id,
populate: ['event' as any],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is any needed here?
I think populate: ['event'] is enough

});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 (!user) {
return ctx.unauthorized();
}

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' } };
}
},
}));
18 changes: 18 additions & 0 deletions cms/src/api/event-request/controllers/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export default {
data: eventRequest,
});

const startDateTime = `${eventRequest.eventDate}T${eventRequest.eventStart}`;

const draftEvent = await strapi.documents("api::event.event").create({
data: {
title: eventRequest.eventName,
description: `${eventRequest.eventPurpose}\n\n${eventRequest.eventTheme}`,
start: startDateTime,
summary: eventRequest.eventAgenda,
tags: eventRequest.eventTheme ? [{ tagName: eventRequest.eventTheme }] : [],
},
status: "draft",
});

await strapi.documents("api::event-request.event-request").update({
documentId: res.documentId,
data: { event: draftEvent.documentId } as any,
});

const requestCopy = `<p><strong>Organizing entity</strong>: ${eventRequest.organizingEntity}</p>
<p><strong>Initiator name</strong>: ${eventRequest.initiatorName}</p>
<p><strong>Initiator email</strong>: ${eventRequest.initiatorEmail}</p>
Expand Down
16 changes: 13 additions & 3 deletions cms/src/api/event-request/routes/event-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
* event-request router
*/

import { factories } from '@strapi/strapi';

export default factories.createCoreRouter('api::event-request.event-request');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
Inside the cms folder, run npm run strapi routes:list and you will see :)

export default {
routes: [
{
method: 'POST',
path: '/event-requests/:id/approve',
handler: 'event-request.approve',
config: {
policies: [],
middlewares: [],
},
},
],
};
50 changes: 50 additions & 0 deletions cms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,55 @@ import { eventNotificationMiddleware } from './middlewares/event-notifications';

const FIREBASE_FCM_CREDENTIALS_PATH = './base42-mobile-app-fcm-firebase-adminsdk.json';

const eventRequestApprovalMiddleware = () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -42,6 +91,7 @@ export default {
});

strapi.documents.use(eventNotificationMiddleware());
strapi.documents.use(eventRequestApprovalMiddleware());
},

/**
Expand Down