Skip to content

Commit

Permalink
fix: tsc issues
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Feb 18, 2024
1 parent 72dd4af commit 3357a86
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 78 deletions.
16 changes: 10 additions & 6 deletions functions/src/cms4devfest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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 { 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";
import { Company, Settings } from "./model";

admin.initializeApp();
const firestore = admin.firestore();
function sendWelcomeEmail(company: DocumentData, id: string, settings: Settings) {
function sendWelcomeEmail(company: Company, id: string, settings: Settings) {
const emailTemplate = WelcomeEmailFactory(company, id, settings);
return sendEmailToAllContacts(company, emailTemplate, settings);
}
Expand Down Expand Up @@ -78,8 +78,12 @@ export const relanceInformationPourGeneration = functions.https.onCall(async (re

export const newPartner = functions.firestore.document("companies-2024/{companyId}").onCreate(async (snap) => {
const settings = functions.config() as Settings;
const company = snap.data() || {};
const company: Company = snap.data() as Company;
const id = snap.id;

if (!company.name) {
return;
}
await addCreationDate(id);
await sendWelcomeEmail(company, snap.id, settings);
await sendEmail(
Expand All @@ -98,8 +102,8 @@ La société ${company.name} souhaite devenir partenaire ${company.sponsoring}<b
});

export const partnershipUpdated = functions.firestore.document("companies-2024/{companyId}").onUpdate((changes) => {
const before = changes.before.data();
const after = changes.after.data();
const before = changes.before.data() as Company;
const after = changes.after.data() as Company;
if (!before || !after) {
return;
}
Expand Down
23 changes: 6 additions & 17 deletions functions/src/emails/template/step-5-billet-web-url.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { DocumentData } from "@google-cloud/firestore";
import { Settings } from "../../model";
import { Company, Settings } from "../../model";

const generateEmailForSponsorWithoutStand = (
billetWebUrl: string,
settings: Settings,
) => {
const generateEmailForSponsorWithoutStand = (billetWebUrl: string, settings: Settings) => {
return {
subject: `${settings.gdg.event} ${settings.convention.edition} : Lien pour récupérer vos billets`,
body: `
Expand All @@ -20,10 +16,7 @@ const generateEmailForSponsorWithoutStand = (
`,
};
};
const generateEmailForSponsorWithStand = (
billetWebUrl: string,
settings: Settings,
) => ({
const generateEmailForSponsorWithStand = (billetWebUrl: string, settings: Settings) => ({
subject: `${settings.gdg.event} ${settings.convention.edition} : Lien pour récupérer vos billets`,
body: `
Bonjour
Expand All @@ -40,13 +33,9 @@ ${settings.mail.signature} ${settings.convention.edition}
`,
});

export default (company: DocumentData, settings: Settings) => {
const billetWebUrl = company.billetWebUrl;
if (
company.sponsoring === "Platinium" ||
company.sponsoring === "Gold" ||
company.sponsoring === "Silver"
) {
export default (company: Company, settings: Settings) => {
const billetWebUrl = company.billetWebUrl!;
if (company.sponsoring === "Platinium" || company.sponsoring === "Gold" || company.sponsoring === "Silver") {
return generateEmailForSponsorWithStand(billetWebUrl, settings);
}
return generateEmailForSponsorWithoutStand(billetWebUrl, settings);
Expand Down
9 changes: 2 additions & 7 deletions functions/src/emails/template/step-partnership-generated.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { addDays } from "date-fns";
import { Timestamp } from "@google-cloud/firestore";
import { DocumentData } from "firebase-admin/firestore";
import { Settings } from "../../model";
import { Company, Settings } from "../../model";

export default (
{ sponsoring, creationDate }: DocumentData,
id: string,
settings: Settings,
) => {
export default ({ sponsoring, creationDate }: Company, id: string, settings: Settings) => {
const options = {
weekday: "long",
year: "numeric",
Expand Down
1 change: 1 addition & 0 deletions functions/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface Company {
devisUrl: string;
conventionUrl: string;
invoiceUrl: string;
invoiceNumber?: string;
type?: PartnerType;
twitter?: string;
twitterAccount?: string;
Expand Down
26 changes: 13 additions & 13 deletions functions/src/utils/document-change.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DocumentData } from "@google-cloud/firestore";
import partnershipValidated from "./steps/partnershipValidated";
import partnershipGenerated from "./steps/partnershipGenerated";
import { sendEmailToAllContacts } from "./mail";
Expand All @@ -10,7 +9,7 @@ import CommunicationScheduledFactory from "../emails/template/step-4-communcatio
import BilletWebUrlFactory from "../emails/template/step-5-billet-web-url";
import decreasePacks from "./steps/decreasePacks";
import sendKoEmails from "./steps/sendKoEmails";
import { Settings } from "../model";
import { Company, Settings } from "../model";

export enum StatusEnum {
PENDING = "pending",
Expand All @@ -21,14 +20,15 @@ export enum StatusEnum {

export async function onDocumentChange(
firestore: FirebaseFirestore.Firestore,
before: DocumentData,
after: DocumentData,
before: Company,
after: Company,
id: string,
settings: Settings
) {
const document = firestore.doc("companies-2024/" + id);
console.log(`onDocumentChange ${id}: ${JSON.stringify(before.status)} -> ${JSON.stringify(after.status)}`);
const status = after.status;
const status = after.status!;
const beforeStatus = before.status!;
if (status.generated === StatusEnum.PENDING) {
if (!!after.address && !!after.zipCode && !!after.city && !!after.siret && !!after.representant && !!after.role) {
return document.update({
Expand All @@ -39,8 +39,8 @@ export async function onDocumentChange(
});
}
} else if (
before.status.generated !== status.generated &&
((status.generated === StatusEnum.DONE && before.status.generated !== StatusEnum.RETRY) ||
beforeStatus.generated !== status.generated &&
((status.generated === StatusEnum.DONE && beforeStatus.generated !== StatusEnum.RETRY) ||
status.generated === StatusEnum.RETRY)
) {
const configurationFromFirestore = await firestore
Expand All @@ -56,16 +56,16 @@ export async function onDocumentChange(
return document.update({
...partnershipGenerated(after, id, settings, status.generated === StatusEnum.DONE),
});
} else if (before.status.validated !== status.validated && status.validated === StatusEnum.DONE) {
} else if (beforeStatus.validated !== status.validated && status.validated === StatusEnum.DONE) {
const sponsoringType = after.sponsoring.toLowerCase();
await decreasePacks(firestore, sponsoringType);

return document.update({
...partnershipValidated(after, id, settings, status.validated === StatusEnum.DONE),
});
} else if (before.status.validated !== status.validated && status.validated === StatusEnum.REFUSED) {
} else if (beforeStatus.validated !== status.validated && status.validated === StatusEnum.REFUSED) {
await sendKoEmails(after, settings);
} else if (before.status.sign !== status.sign && status.sign === StatusEnum.DONE) {
} else if (beforeStatus.sign !== status.sign && status.sign === StatusEnum.DONE) {
const emailTemplate = ConventionSignedFactory(id, settings);
sendEmailToAllContacts(after, emailTemplate, settings);

Expand All @@ -75,7 +75,7 @@ export async function onDocumentChange(
paid: StatusEnum.PENDING,
},
});
} else if (before.status.paid !== status.paid && status.paid === StatusEnum.DONE) {
} else if (beforeStatus.paid !== status.paid && status.paid === StatusEnum.DONE) {
const emailTemplate = PaymentReceivedFactory(after, id, settings);
sendEmailToAllContacts(after, emailTemplate, settings);
return document.update({
Expand All @@ -94,10 +94,10 @@ export async function onDocumentChange(
},
});
} else if (
(before.status.communicated !== status.communicated && status.communicated === StatusEnum.DONE) ||
(beforeStatus.communicated !== status.communicated && status.communicated === StatusEnum.DONE) ||
(status.communicated === StatusEnum.DONE && before.publicationDate !== after.publicationDate)
) {
if (!!after.publicationDate && after.publicationDate !== "") {
if (!!after.publicationDate && (after.publicationDate as any) !== "") {
const emailTemplate = CommunicationScheduledFactory(
Intl.DateTimeFormat("fr").format(new Date(after.publicationDate)),
settings
Expand Down
7 changes: 3 additions & 4 deletions functions/src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
} from "../generator/lib/generator";
import * as os from "os";
import * as admin from "firebase-admin";
import { DocumentData } from "@google-cloud/firestore";
import { Configuration, Settings } from "../model";
import { Company, Configuration, Settings } from "../model";

export async function storeFile(cloudStorageDest: string, tempPath: string) {
await admin
Expand All @@ -25,7 +24,7 @@ export async function storeFile(cloudStorageDest: string, tempPath: string) {
.getSignedUrl({ action: "read", expires: "03-17-2025" });
}
export async function generateAndStoreProformaInvoiceAndConvention(
company: DocumentData,
company: Company,
id: string,
settings: Settings,
configurationFromFirestore: Configuration
Expand All @@ -48,7 +47,7 @@ export async function generateAndStoreProformaInvoiceAndConvention(
}
export async function generateAndStoreInvoice(
firestore: FirebaseFirestore.Firestore,
company: DocumentData,
company: Company,
id: string,
settings: any,
configurationFromFirestore: Configuration
Expand Down
5 changes: 2 additions & 3 deletions functions/src/utils/mail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DocumentData } from "@google-cloud/firestore";
import { Email, Settings } from "../model";
import { Company, Email, Settings } from "../model";

export function getFrom(mail: Email) {
return {
Expand All @@ -10,7 +9,7 @@ export function getFrom(mail: Email) {
};
}

export function sendEmailToAllContacts(company: DocumentData, emailFactory: any, settings: Settings) {
export function sendEmailToAllContacts(company: Company, emailFactory: any, settings: Settings) {
let emails = [settings.mail.cc];
if (settings.mail.enabled === "true") {
emails = [...emails, ...company.email];
Expand Down
16 changes: 3 additions & 13 deletions functions/src/utils/steps/partnershipGenerated.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { DocumentData } from "@google-cloud/firestore";
import PartnerhipGeneratedFactory from "../../emails/template/step-partnership-generated";
import { sendEmailToAllContacts } from "../mail";

import { StatusEnum } from "../document-change";
import { Settings } from "../../model";
export default (
company: DocumentData,
id: string,
settings: Settings,
shouldSendEmail: boolean,
) => {
import { Company, Settings } from "../../model";
export default (company: Company, id: string, settings: Settings, shouldSendEmail: boolean) => {
if (shouldSendEmail) {
sendEmailToAllContacts(
company,
PartnerhipGeneratedFactory(company, id, settings),
settings,
);
sendEmailToAllContacts(company, PartnerhipGeneratedFactory(company, id, settings), settings);
}

return {
Expand Down
16 changes: 3 additions & 13 deletions functions/src/utils/steps/partnershipValidated.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { DocumentData } from "@google-cloud/firestore";
import PartnerhipValidatedFactory from "../../emails/template/step-2-partnership-validation";
import { sendEmailToAllContacts } from "../mail";

import { StatusEnum } from "../document-change";
import { Settings } from "../../model";
import { Company, Settings } from "../../model";

export default (
company: DocumentData,
id: string,
settings: Settings,
shouldSendEmail: boolean,
) => {
export default (company: Company, id: string, settings: Settings, shouldSendEmail: boolean) => {
if (shouldSendEmail) {
sendEmailToAllContacts(
company,
PartnerhipValidatedFactory(company, id, settings),
settings,
);
sendEmailToAllContacts(company, PartnerhipValidatedFactory(company, id, settings), settings);
}

return {
Expand Down
4 changes: 2 additions & 2 deletions functions/src/utils/steps/sendKoEmails.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DocumentData } from "@google-cloud/firestore";
import { sendEmailToAllContacts } from "../mail";
import PartnerhipKoFactory from "../../emails/template/step-2-partnership-ko";
import { Company } from "../../model";

export default (company: DocumentData, settings: any) => {
export default (company: Company, settings: any) => {
sendEmailToAllContacts(company, PartnerhipKoFactory(settings), settings);
};

0 comments on commit 3357a86

Please sign in to comment.