From 97d87cf982fdff2f5e128d1c37db2954787cb77d Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Wed, 17 Jan 2024 13:45:01 +0100 Subject: [PATCH] feat: create functions group --- functions/src/cms4devfest-gdg.ts | 23 +++ functions/src/cms4devfest.ts | 114 ++++++++++++ functions/src/index.ts | 163 +----------------- .../dashboard/dashboard.component.ts | 7 +- 4 files changed, 145 insertions(+), 162 deletions(-) create mode 100644 functions/src/cms4devfest-gdg.ts create mode 100644 functions/src/cms4devfest.ts diff --git a/functions/src/cms4devfest-gdg.ts b/functions/src/cms4devfest-gdg.ts new file mode 100644 index 0000000..585d563 --- /dev/null +++ b/functions/src/cms4devfest-gdg.ts @@ -0,0 +1,23 @@ +import { saveCompany } from "./conference4hall/save-event"; +import * as admin from "firebase-admin"; +import * as functions from "firebase-functions"; + +import { Storage } from "@google-cloud/storage"; + +import { defineSecret } from "firebase-functions/params"; +const GEOCODE_API_KEY = defineSecret("GEOCODE_API_KEY"); + +const firestore = admin.firestore(); +const storage = new Storage(); + +export const updatePartnerToC4H = functions + .runWith({ secrets: [GEOCODE_API_KEY] }) + .firestore.document("companies-2024/{companyId}") + .onUpdate(async (changes) => { + const newValue = changes.after.data(); + await saveCompany(firestore, storage, changes.after.id, newValue, { + c4hId: "devfest-lille-2024", + year: "2024", + geocodeApiKey: GEOCODE_API_KEY.value(), + }); + }); diff --git a/functions/src/cms4devfest.ts b/functions/src/cms4devfest.ts new file mode 100644 index 0000000..dcb51e3 --- /dev/null +++ b/functions/src/cms4devfest.ts @@ -0,0 +1,114 @@ +import * as functions from "firebase-functions"; +import * as admin from "firebase-admin"; +import { sendEmail, sendEmailToAllContacts } from "./utils/mail"; +import { StatusEnum, onDocumentChange } from "./utils/document-change"; +import { DocumentData, Timestamp } from "@google-cloud/firestore"; +import relanceConventionSignee from "./emails/template/relanceConventionSignee"; + +import WelcomeEmailFactory from "./emails/template/step-1-partnership-demand"; +import relancePaiement from "./emails/template/relancePaiement"; +import relanceInformationsComplementaires from "./emails/template/relanceInformationsComplementaires"; +import { Settings } from "./model"; + +const firestore = admin.firestore(); + +function sendWelcomeEmail(company: DocumentData, id: string, settings: Settings) { + const emailTemplate = WelcomeEmailFactory(company, id, settings); + return sendEmailToAllContacts(company, emailTemplate, settings); +} + +function addCreationDate(id: string) { + return firestore + .doc("companies-2024/" + id) + .update({ + creationDate: Timestamp.fromDate(new Date()), + }) + .catch((err) => console.log(err)); +} + +function updatesStatus(id: string, company: any, status: any) { + return firestore + .doc("companies-2024/" + id) + .update({ + ...company, + status, + }) + .catch((err) => console.log(err)); +} + +const relance = ( + emailFactory: (partner: Record, settings: Settings) => any, + partners: any[], + settings: Settings +) => { + partners.forEach((c: any) => { + const emailTemplate = emailFactory(c, settings); + sendEmailToAllContacts(c, emailTemplate, settings); + }); +}; +export const relancePartnaireConventionASigner = functions.https.onRequest(async (req, res) => { + const data = await firestore.collection("companies-2024").get(); + const partners = data.docs.map((d) => d.data()).filter((p) => p.status.sign === StatusEnum.PENDING); + relance(relanceConventionSignee, partners, functions.config() as Settings); + res.send("ok"); +}); + +export const relancePartnaireFacture = functions.https.onRequest(async (req, res) => { + const data = await firestore.collection("companies-2024").get(); + const partners = data.docs.map((d) => d.data()).filter((p) => p.status.paid === StatusEnum.PENDING); + relance(relancePaiement, partners, functions.config() as Settings); + res.send("ok"); +}); + +export const relanceInformationPourGeneration = functions.https.onRequest(async (req, res) => { + const data = await firestore.collection("companies-2024").get(); + const partners = data.docs.map((d) => d.data()).filter((p) => p.status.generated === StatusEnum.PENDING); + relance(relanceInformationsComplementaires, partners, functions.config() as Settings); + res.send("ok"); +}); + +export const newPartner = functions.firestore.document("companies-2024/{companyId}").onCreate(async (snap) => { + const settings = functions.config() as Settings; + const company = snap.data() || {}; + const id = snap.id; + await addCreationDate(id); + await sendWelcomeEmail(company, snap.id, settings); + await sendEmail( + settings.mail.to, + "🎉 Nouveau Partenaire " + company.name, + ` +La société ${company.name} souhaite devenir partenaire ${company.sponsoring}
+`, + settings + ); + + return updatesStatus(id, company, { + filled: "done", + validated: "pending", + }); +}); + +export const partnershipUpdated = functions.firestore.document("companies-2024/{companyId}").onUpdate((changes) => { + const before = changes.before.data(); + const after = changes.after.data(); + if (!before || !after) { + return; + } + const id = changes.after.id; + + return onDocumentChange(firestore, before, after, id, functions.config() as Settings); +}); + +exports.updateConventionSignedUrlProperty = functions.storage.object().onFinalize(async (object) => { + const name = object.name || ""; + return admin + .storage() + .bucket() + .file(name) + .getSignedUrl({ action: "read", expires: "03-17-2025" }) + .then(([url]) => { + return firestore.doc("companies-2024/" + name.replace("signed/", "")).update({ + conventionSignedUrl: url, + }); + }); +}); diff --git a/functions/src/index.ts b/functions/src/index.ts index e78f471..224f619 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,163 +1,8 @@ -import * as functions from "firebase-functions"; import * as admin from "firebase-admin"; -import { defineSecret } from "firebase-functions/params"; -import { sendEmail, sendEmailToAllContacts } from "./utils/mail"; -import { StatusEnum, onDocumentChange } from "./utils/document-change"; -import { DocumentData, Timestamp } from "@google-cloud/firestore"; -import { Storage } from '@google-cloud/storage'; -import relanceConventionSignee from "./emails/template/relanceConventionSignee"; +import * as cms4devfestFunctions from "./cms4devfest"; +import * as cms4devfestFunctionsGdg from "./cms4devfest-gdg"; -import WelcomeEmailFactory from "./emails/template/step-1-partnership-demand"; -import relancePaiement from "./emails/template/relancePaiement"; -import relanceInformationsComplementaires from "./emails/template/relanceInformationsComplementaires"; -import { Settings } from "./model"; -import { saveCompany } from "./conference4hall/save-event"; admin.initializeApp(); -const GEOCODE_API_KEY = defineSecret("GEOCODE_API_KEY"); -const firestore = admin.firestore(); -const storage = new Storage(); - -function sendWelcomeEmail( - company: DocumentData, - id: string, - settings: Settings, -) { - const emailTemplate = WelcomeEmailFactory(company, id, settings); - return sendEmailToAllContacts(company, emailTemplate, settings); -} - -function addCreationDate(id: string) { - return firestore - .doc("companies-2024/" + id) - .update({ - creationDate: Timestamp.fromDate(new Date()), - }) - .catch((err) => console.log(err)); -} - -function updatesStatus(id: string, company: any, status: any) { - return firestore - .doc("companies-2024/" + id) - .update({ - ...company, - status, - }) - .catch((err) => console.log(err)); -} - -const relance = ( - emailFactory: (partner: Record, settings: Settings) => any, - partners: any[], - settings: Settings, -) => { - partners.forEach((c: any) => { - const emailTemplate = emailFactory(c, settings); - sendEmailToAllContacts(c, emailTemplate, settings); - }); -}; -export const relancePartnaireConventionASigner = functions.https.onRequest( - async (req, res) => { - const data = await firestore.collection("companies-2024").get(); - const partners = data.docs - .map((d) => d.data()) - .filter((p) => p.status.sign === StatusEnum.PENDING); - relance(relanceConventionSignee, partners, functions.config() as Settings); - res.send("ok"); - }, -); - -export const relancePartnaireFacture = functions.https.onRequest( - async (req, res) => { - const data = await firestore.collection("companies-2024").get(); - const partners = data.docs - .map((d) => d.data()) - .filter((p) => p.status.paid === StatusEnum.PENDING); - relance(relancePaiement, partners, functions.config() as Settings); - res.send("ok"); - }, -); - -export const relanceInformationPourGeneration = functions.https.onRequest( - async (req, res) => { - const data = await firestore.collection("companies-2024").get(); - const partners = data.docs - .map((d) => d.data()) - .filter((p) => p.status.generated === StatusEnum.PENDING); - relance( - relanceInformationsComplementaires, - partners, - functions.config() as Settings, - ); - res.send("ok"); - }, -); - -export const newPartner = functions.firestore - .document("companies-2024/{companyId}") - .onCreate(async (snap) => { - const settings = functions.config() as Settings; - const company = snap.data() || {}; - const id = snap.id; - await addCreationDate(id); - await sendWelcomeEmail(company, snap.id, settings); - await sendEmail( - settings.mail.to, - "🎉 Nouveau Partenaire " + company.name, - ` -La société ${company.name} souhaite devenir partenaire ${company.sponsoring}
-`, - settings, - ); - - return updatesStatus(id, company, { - filled: "done", - validated: "pending", - }); - }); - -export const updatePartnerToC4H = functions.runWith({ secrets: [GEOCODE_API_KEY] }).firestore.document("companies-2024/{companyId}").onUpdate(async (changes) => { - const newValue = changes.after.data(); - await saveCompany(firestore, storage, changes.after.id, newValue, { - c4hId: "devfest-lille-2024", - year: "2024", - geocodeApiKey: GEOCODE_API_KEY.value(), - }); -}) - -export const partnershipUpdated = functions.firestore - .document("companies-2024/{companyId}") - .onUpdate((changes) => { - const before = changes.before.data(); - const after = changes.after.data(); - if (!before || !after) { - return; - } - const id = changes.after.id; - - return onDocumentChange( - firestore, - before, - after, - id, - functions.config() as Settings, - ); - }); - -exports.updateConventionSignedUrlProperty = functions.storage - .object() - .onFinalize(async (object) => { - const name = object.name || ""; - return admin - .storage() - .bucket() - .file(name) - .getSignedUrl({ action: "read", expires: "03-17-2025" }) - .then(([url]) => { - return firestore - .doc("companies-2024/" + name.replace("signed/", "")) - .update({ - conventionSignedUrl: url, - }); - }); - }); +export const cms4devfest = cms4devfestFunctions; +export const cms4devfestgdg = cms4devfestFunctionsGdg; diff --git a/public/src/app/private-dashboard/dashboard/dashboard.component.ts b/public/src/app/private-dashboard/dashboard/dashboard.component.ts index bd48606..f2575b6 100644 --- a/public/src/app/private-dashboard/dashboard/dashboard.component.ts +++ b/public/src/app/private-dashboard/dashboard/dashboard.component.ts @@ -125,24 +125,25 @@ export class DashboardComponent implements AfterViewInit { private readonly partnerService: PartnerService = inject(PartnerService); private readonly httpClient: HttpClient = inject(HttpClient); + //TODO use firebase callable functions ? Or move API endpoint somewhere relance() { const status = this.filterByStatusValue(); if (status[0] === 'generated') { return this.httpClient .get( - 'https://us-central1-cms4partners-ce427.cloudfunctions.net/relanceInformationPourGeneration', + 'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relanceInformationPourGeneration', ) .subscribe(); } else if (status[0] === 'sign') { return this.httpClient .get( - 'https://us-central1-cms4partners-ce427.cloudfunctions.net/relancePartnaireConventionASigner', + 'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relancePartnaireConventionASigner', ) .subscribe(); } else if (status[0] === 'paid') { return this.httpClient .get( - 'https://us-central1-cms4partners-ce427.cloudfunctions.net/relancePartnaireFacture', + 'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relancePartnaireFacture', ) .subscribe(); }