diff --git a/functions/src/application/portal.ts b/functions/src/application/portal.ts index 5b0d3e1..80e47e6 100644 --- a/functions/src/application/portal.ts +++ b/functions/src/application/portal.ts @@ -1,4 +1,4 @@ -import { Response, Request } from "express"; +import { Response, Request, response } from "express"; import * as Sentry from "@sentry/node"; import { firestore } from "../admin/admin"; import { send_dynamic_template, upsert_contact } from "../mail/sendgrid"; @@ -7,6 +7,7 @@ import logger from "../services/logging"; import { environment } from "../environment"; import { build_vanity_link_v2, VanityReqBody } from "../custom/vanity"; import { BadRequestError } from "../utilities/errors/BadRequestError"; +import { ACMEvent, create_event_v2 } from "../custom/event"; const profile_collection = environment.FIRESTORE_PROFILE_COLLECTION as string; const event_collection = environment.FIRESTORE_EVENT_COLLECTION as string; @@ -328,3 +329,15 @@ export const create_vanity_link = async (req: Request<{}, {}, VanityReqBody>, re return res.status(400).json(errObj.serialize()); } }; + +export const create_event_checkin_link = async (req: Request<{}, {}, ACMEvent, {}>, res: Response) => { + try { + const response_data = await create_event_v2(req.body); + return res.json(response_data); + } catch (error) { + return res.status(500).json({ + msg: "Server Error", + error, + }); + } +}; diff --git a/functions/src/custom/event.ts b/functions/src/custom/event.ts index 1aebc4b..a2ea56b 100644 --- a/functions/src/custom/event.ts +++ b/functions/src/custom/event.ts @@ -12,6 +12,12 @@ export interface EventDoc { public: boolean; } +export interface ACMEvent extends EventDoc { + first_name: string; + last_name: string; + email: string; +} + const event_collection = environment.FIRESTORE_EVENT_COLLECTION as string; export const create_event = async (document: FirebaseFirestore.DocumentData): Promise => { @@ -96,3 +102,77 @@ const create_map = async (document: EventDoc): Promise => { message: "Successfully created an event check-in", }); }; + +export const create_event_v2 = async ({ + first_name, + last_name, + email, + name, + path_name, + date, + public: public_event, +}: ACMEvent) => { + try { + const email_options: sendgrid_email = { + from: "development@acmutd.co", + from_name: "ACM Development", + template_id: `${environment.SENDGRID_EVENT_TEMPLATE_ID}`, + to: email, + dynamicSubstitutions: { + first_name, + last_name, + name, + checkin_link: `${environment.URL_PROD}/checkin/${path_name}`, + date, + preheader: "Successful Event Check-in Creation Connection", + subject: "Event Creation Confirmation", + public_event, + }, + }; + + const data: EventDoc = { + name, + path_name, + date, + public: public_event, + }; + + const message: slack_message = { + form_name: "Event Check-in Generator", + name: `${first_name} ${last_name}`, + email, + url: `${environment.URL_PROD}/checkin/${path_name}`, + }; + + await saveToDB(data); + await send_dynamic_template(email_options); + await log_to_slack(message); + + return { + msg: "Successful Event Check-in Creation Connection", + checkin_link: `${environment.URL_PROD}/checkin/${path_name}`, + }; + } catch (err) { + logger.log(err); + Sentry.captureException(err); + return err; + } +}; + +const saveToDB = async (data: EventDoc) => { + // TODO: Add logic to save stuff into MongoDB +}; + +const create_map = async (document: EventDoc): Promise => { + await firestore + .collection(event_collection) + .doc(document.path_name) + .create({ + ...document, + path_name: `/checkin/${document.path_name}`, + }); + logger.log({ + ...document, + message: "Successfully created an event check-in", + }); +}; diff --git a/functions/src/routes/express_portal.ts b/functions/src/routes/express_portal.ts index 00eb07f..8a041a9 100644 --- a/functions/src/routes/express_portal.ts +++ b/functions/src/routes/express_portal.ts @@ -36,6 +36,7 @@ app_portal.post("/auth0/verify-discord", verify_in_acm_server); // Create Vanity Link app_portal.post("/gsuite/vanity/create", portalFunctions.create_vanity_link); +app_portal.post("/gsuite/event/checkin/create", portalFunctions.create_event_checkin_link); // http server endpoints export default app_portal;