Skip to content

Commit

Permalink
feat: migrate to callable function
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Jan 17, 2024
1 parent 3126485 commit 4dd7a7a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ jobs:
run: |
npx firebase-tools deploy -P default --only hostings
npx firebase-tools deploy -P default --only firestore:rules
npx firebase-tools deploy -P default --only functions:cms
npx firebase-tools deploy -P default --only functions:cms4devfestgdg
env:
FIREBASE_TOKEN: ${{ secrets.DEVFEST_FIREBASE_TOKEN }}
- name: Deploy to Firebase for Devfest Lille
- name: Deploy to Firebase for cloudnord
run: |
npx firebase-tools deploy -P cloudnord --only hostings
npx firebase-tools deploy -P cloudnord --only firestore:rules
npx firebase-tools deploy -P cloudnord --only functions:cms4
npx firebase-tools deploy -P cloudnord --only functions:cms4devfestcloudnord
env:
FIREBASE_TOKEN: ${{ secrets.CLOUDNORD_FIREBASE_TOKEN }}
9 changes: 3 additions & 6 deletions functions/src/cms4devfest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,22 @@ const relance = (
sendEmailToAllContacts(c, emailTemplate, settings);
});
};
export const relancePartnaireConventionASigner = functions.https.onRequest(async (req, res) => {
export const relancePartnaireConventionASigner = functions.https.onCall(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) => {
export const relancePartnaireFacture = functions.https.onCall(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) => {
export const relanceInformationPourGeneration = functions.https.onCall(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) => {
Expand Down
11 changes: 4 additions & 7 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import * as cms4devfestFunctions from "./cms4devfest";
import * as cms4devfestFunctionsGdg from "./cms4devfest-gdg";

export const cms4devfestgdg = {
...cms4devfestFunctions,
...cms4devfestFunctionsGdg,
};
export const cms = cms4devfestFunctions;
export const cms4devfestgdg = cms4devfestFunctionsGdg;

export const cms4devfestcloudnord = {
...cms4devfestFunctions,
};
/*export const cms4devfestcloudnord = {
};*/
27 changes: 5 additions & 22 deletions functions/src/utils/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,19 @@ export function getFrom(mail: Email) {
};
}

export function sendEmailToAllContacts(
company: DocumentData,
emailFactory: any,
settings: Settings,
) {
export function sendEmailToAllContacts(company: DocumentData, emailFactory: any, settings: Settings) {
if (settings.mail.enabled === "false") {
return Promise.resolve();
}
return Promise.all(
[...company.email, settings.mail.cc].map((email: string) => {
return sendEmail(
email.trim(),
emailFactory.subject,
emailFactory.body,
settings,
);
}),
return sendEmail(email.trim(), emailFactory.subject, emailFactory.body, settings);
})
);
}
export function sendEmail(
to: string,
subject: string,
body: string,
settings: Settings,
) {
export function sendEmail(to: string, subject: string, body: string, settings: Settings) {
const mailjet = settings.mailjet;
const mailjetClient = require("node-mailjet").connect(
mailjet.api,
mailjet.private,
);
const mailjetClient = require("node-mailjet").connect(mailjet.api, mailjet.private);
const request = mailjetClient.post("send", { version: "v3.1" }).request({
Messages: [
{
Expand Down
2 changes: 2 additions & 0 deletions public/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Router } from '@angular/router';
import * as Sentry from '@sentry/angular-ivy';

import { registerLocaleData } from '@angular/common';
import { getFunctions, provideFunctions } from '@angular/fire/functions';
registerLocaleData(localeFr);

export const appConfig: ApplicationConfig = {
Expand Down Expand Up @@ -59,5 +60,6 @@ export const appConfig: ApplicationConfig = {
importProvidersFrom(provideAuth(() => getAuth())),
importProvidersFrom(provideFirestore(() => getFirestore())),
importProvidersFrom(provideStorage(() => getStorage())),
importProvidersFrom(provideFunctions(() => getFunctions())),
],
};
32 changes: 13 additions & 19 deletions public/src/app/private-dashboard/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { MatButtonModule } from '@angular/material/button';
import { FormsModule } from '@angular/forms';
import { MatSort, MatSortModule } from '@angular/material/sort';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
import { Functions, httpsCallable } from '@angular/fire/functions';

type FilterValueType =
| 'sign'
Expand Down Expand Up @@ -123,29 +124,22 @@ export class DashboardComponent implements AfterViewInit {
});

private readonly partnerService: PartnerService = inject(PartnerService);
private readonly httpClient: HttpClient = inject(HttpClient);
private readonly functions: Functions = inject(Functions);

//TODO use firebase callable functions ? Or move API endpoint somewhere
relance() {
async relance() {
const status = this.filterByStatusValue();
if (status[0] === 'generated') {
return this.httpClient
.get(
'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relanceInformationPourGeneration',
)
.subscribe();
await httpsCallable(
this.functions,
'cms-relanceInformationPourGeneration',
)();
} else if (status[0] === 'sign') {
return this.httpClient
.get(
'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relancePartnaireConventionASigner',
)
.subscribe();
await httpsCallable(
this.functions,
'cms-relancePartnaireConventionASigner',
)();
} else if (status[0] === 'paid') {
return this.httpClient
.get(
'https://us-central1-cms4partners-ce427.cloudfunctions.net/cms4devfest-relancePartnaireFacture',
)
.subscribe();
await httpsCallable(this.functions, 'cms-relancePartnaireFacture')();
}

return;
Expand Down

0 comments on commit 4dd7a7a

Please sign in to comment.