Skip to content

Commit

Permalink
bugfix(functions): fix translations in functions runtime (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkue authored Jan 22, 2024
1 parent 18137b5 commit be21135
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
112 changes: 58 additions & 54 deletions functions/src/webhooks/admin/donation-certificates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,65 +35,69 @@ export default onCall<CreateDonationCertificatesFunctionProps, Promise<string>>(
userIds = request.data.userIds;
}

for await (const userId of userIds) {
try {
const writer = await DonationCertificateWriter.getInstance(userId, request.data.year);
if (writer.contributionsByCurrency.size() === 0) {
console.info(`No contributions found for user ${userId}`);
continue;
}

await withFile(async ({ path }) => {
await writer.writeDonationCertificatePDF(path);
await Promise.all(
userIds.map(async (userId) => {
try {
const writer = await DonationCertificateWriter.getInstance(userId, request.data.year);
if (writer.contributionsByCurrency.size() === 0) {
console.info(`No contributions found for user ${userId}`);
return;
}

const { downloadUrl } = await storageAdmin.uploadAndGetDownloadURL({
sourceFilePath: path,
destinationFilePath: `users/${userId}/${writer.year}_${writer.user.language}.pdf`,
});
await withFile(async ({ path }) => {
await writer.writeDonationCertificatePDF(path);

await firestoreAdmin
.collection(`${USER_FIRESTORE_PATH}/${userId}/${DONATION_CERTIFICATE_FIRESTORE_PATH}`)
.doc(`${writer.year}-${writer.user.address?.country}`)
.set({ year: writer.year, country: writer.user.address?.country, url: downloadUrl } as DonationCertificate, {
merge: true,
const { downloadUrl } = await storageAdmin.uploadAndGetDownloadURL({
sourceFilePath: path,
destinationFilePath: `users/${userId}/${writer.year}_${writer.user.language}.pdf`,
});

console.info(`Donation certificate document written for user ${userId}`);
successCount += 1;
await firestoreAdmin
.collection(`${USER_FIRESTORE_PATH}/${userId}/${DONATION_CERTIFICATE_FIRESTORE_PATH}`)
.doc(`${writer.year}-${writer.user.address?.country}`)
.set(
{ year: writer.year, country: writer.user.address?.country, url: downloadUrl } as DonationCertificate,
{
merge: true,
},
);
console.info(`Donation certificate document written for user ${userId}`);
successCount += 1;

// if (request.data.sendEmails) {
// await sendEmail({
// to: user.email,
// subject: translator.t('email-subject'),
// // TODO: Use renderEmailTemplate() instead of renderTemplate()
// content: await renderTemplate({
// language: user.language || 'de',
// translationNamespace: 'donation-certificate',
// hbsTemplatePath: 'email/donation-certificate.hbs',
// context: {
// title: translator.t('title', { context: { year } }),
// signature: translator.t('title', { context: { year } }),
// firstname: user.personal?.name,
// year: year,
// },
// }),
// attachments: [
// {
// filename: translator.t('filename', { context: { year } }),
// path: path,
// },
// ],
// from: NOTIFICATION_EMAIL_USER_KERRIN,
// user: NOTIFICATION_EMAIL_USER_KERRIN,
// password: NOTIFICATION_EMAIL_PASSWORD_KERRIN,
// });
// }
});
} catch (e) {
usersWithFailures.push(userId);
console.error(e);
}
}
// if (request.data.sendEmails) {
// await sendEmail({
// to: user.email,
// subject: translator.t('email-subject'),
// // TODO: Use renderEmailTemplate() instead of renderTemplate()
// content: await renderTemplate({
// language: user.language || 'de',
// translationNamespace: 'donation-certificate',
// hbsTemplatePath: 'email/donation-certificate.hbs',
// context: {
// title: translator.t('title', { context: { year } }),
// signature: translator.t('title', { context: { year } }),
// firstname: user.personal?.name,
// year: year,
// },
// }),
// attachments: [
// {
// filename: translator.t('filename', { context: { year } }),
// path: path,
// },
// ],
// from: NOTIFICATION_EMAIL_USER_KERRIN,
// user: NOTIFICATION_EMAIL_USER_KERRIN,
// password: NOTIFICATION_EMAIL_PASSWORD_KERRIN,
// });
// }
});
} catch (e) {
usersWithFailures.push(userId);
console.error(e);
}
}),
);
return `Successfully created ${successCount} donation certificates for ${request.data.year}
Users with errors (${usersWithFailures.length}): ${usersWithFailures.join(',')}`;
});
13 changes: 10 additions & 3 deletions shared/src/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import i18next, { i18n } from 'i18next';
import resourcesToBackend from 'i18next-resources-to-backend';
import path from 'path';
import { LanguageCode } from '../types/language';

export const FALLBACK_LANGUAGE = 'en';
Expand Down Expand Up @@ -32,9 +33,15 @@ export class Translator {
const translator = new Translator(language, namespaces);
await translator.instance
.use(
resourcesToBackend(
(language: string, namespace: string) => import(`@socialincome/shared/locales/${language}/${namespace}.json`),
),
resourcesToBackend((language: string, namespace: string) => {
try {
// for translations to work in the functions runtime, we need to import the local translation files
const fs = require('fs');
const localPath = path.join(__dirname, `../../locales/${language}/${namespace}.json`);
if (fs.existsSync(localPath)) return import(localPath);
} catch (e) {} // do nothing if module not found
return import(`@socialincome/shared/locales/${language}/${namespace}.json`);
}),
)
.init({
lng: language,
Expand Down

0 comments on commit be21135

Please sign in to comment.