-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a52306e
commit 97d87cf
Showing
4 changed files
with
145 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters