Skip to content

Commit

Permalink
feat: create functions group
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Jan 17, 2024
1 parent a52306e commit 97d87cf
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 162 deletions.
23 changes: 23 additions & 0 deletions functions/src/cms4devfest-gdg.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
});
114 changes: 114 additions & 0 deletions functions/src/cms4devfest.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>, 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}<br>
`,
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,
});
});
});
163 changes: 4 additions & 159 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>, 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}<br>
`,
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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 97d87cf

Please sign in to comment.