From 222a6d1059d082a629520cc77693ea71142503fe Mon Sep 17 00:00:00 2001 From: Montassar Ghanmy Date: Thu, 18 Jul 2024 19:16:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Translate=20notifications=20(#581)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Monta Co-authored-by: Eric Doughty-Papassideris --- .../platform/services/email-pusher/index.ts | 1 - .../fr/notification-digest.subject.eta | 2 +- .../notification-document-shared.subject.eta | 2 +- ...ation-document-version-updated.subject.eta | 2 +- .../documents/services/engine/index.ts | 3 +- .../documents/documents-notifications.spec.ts | 39 +++++++++++++++++++ tdrive/backend/node/test/e2e/setup/index.ts | 7 ++++ .../backend/node/test/e2e/utils.prepare.db.ts | 5 +++ 8 files changed, 56 insertions(+), 5 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts b/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts index 2cffec83..a47c6256 100644 --- a/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts +++ b/tdrive/backend/node/src/core/platform/services/email-pusher/index.ts @@ -95,7 +95,6 @@ export default class EmailPusherClass if (!existsSync(subjectPath)) { throw Error(`subject template not found: ${subjectPath}`); } - console.log("šŸš€ FILE LINK: ", encodedUrl); const html = await Eta.renderFile(templatePath, { ...data, encodedUrl, diff --git a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-digest.subject.eta b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-digest.subject.eta index e1a85d3e..38f5f1ee 100644 --- a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-digest.subject.eta +++ b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-digest.subject.eta @@ -1 +1 @@ -Notifications manquĆ©es dans votre Twake Drive +Notifications non reƧues dans votre Twake Drive \ No newline at end of file diff --git a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-shared.subject.eta b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-shared.subject.eta index e1a85d3e..38f5f1ee 100644 --- a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-shared.subject.eta +++ b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-shared.subject.eta @@ -1 +1 @@ -Notifications manquĆ©es dans votre Twake Drive +Notifications non reƧues dans votre Twake Drive \ No newline at end of file diff --git a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-version-updated.subject.eta b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-version-updated.subject.eta index e1a85d3e..38f5f1ee 100644 --- a/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-version-updated.subject.eta +++ b/tdrive/backend/node/src/core/platform/services/email-pusher/templates/fr/notification-document-version-updated.subject.eta @@ -1 +1 @@ -Notifications manquĆ©es dans votre Twake Drive +Notifications non reƧues dans votre Twake Drive \ No newline at end of file diff --git a/tdrive/backend/node/src/services/documents/services/engine/index.ts b/tdrive/backend/node/src/services/documents/services/engine/index.ts index 6257c813..930643a1 100644 --- a/tdrive/backend/node/src/services/documents/services/engine/index.ts +++ b/tdrive/backend/node/src/services/documents/services/engine/index.ts @@ -16,6 +16,7 @@ export class DocumentsEngine implements Initializable { const company = await globalResolver.services.companies.getCompany({ id: e.context.company.id, }); + const language = receiver.preferences?.language || "en"; const emailTemplate = event === DocumentEvents.DOCUMENT_SAHRED ? "notification-document-shared" @@ -23,7 +24,7 @@ export class DocumentsEngine implements Initializable { try { const { html, text, subject } = await globalResolver.platformServices.emailPusher.build( emailTemplate, - receiver.language || "en", + language, { sender, receiver, diff --git a/tdrive/backend/node/test/e2e/documents/documents-notifications.spec.ts b/tdrive/backend/node/test/e2e/documents/documents-notifications.spec.ts index b02b48cf..a5993f92 100644 --- a/tdrive/backend/node/test/e2e/documents/documents-notifications.spec.ts +++ b/tdrive/backend/node/test/e2e/documents/documents-notifications.spec.ts @@ -2,6 +2,7 @@ import { describe, beforeEach, it, expect, afterAll, jest } from "@jest/globals" import { init, TestPlatform } from "../setup"; import UserApi from "../common/user-api"; import { DocumentsEngine } from "../../../src/services/documents/services/engine"; +import EmailPusherClass from "../../../src/core/platform/services/email-pusher"; import { deserialize } from "class-transformer"; import { File } from "../../../src/services/files/entities/file"; import { ResourceUpdateResponse } from "../../../src/utils/types"; @@ -14,6 +15,8 @@ describe("the Drive feature", () => { DocumentsEngine.prototype, "notifyDocumentVersionUpdated", ); + const DispatchDocumentEvent = jest.spyOn(DocumentsEngine.prototype, "DispatchDocumentEvent"); + const buildEmailSpy = jest.spyOn(EmailPusherClass.prototype, "build"); let currentUser: UserApi; beforeEach(async () => { @@ -35,6 +38,7 @@ describe("the Drive feature", () => { "statistics", "platform-services", "documents", + "email-pusher" ], }); currentUser = await UserApi.getInstance(platform); @@ -126,4 +130,39 @@ describe("the Drive feature", () => { }), ); }); + + // Test the email language based on the user's language and the email subject + it("Did notify the user after sharing a file in the user's language.", async () => { + const oneUser = await UserApi.getInstance(platform, true, { companyRole: "admin", preferences: { language: "en" } }); + const anotherUser = await UserApi.getInstance(platform, true, { companyRole: "admin", preferences: { language: "fr" }}); + //upload files + const doc = await oneUser.uploadRandomFileAndCreateDocument(); + const doc2 = await anotherUser.uploadRandomFileAndCreateDocument(); + + // shared the file + await oneUser.shareWithPermissions(doc, anotherUser.user.id, "read"); + + // expect the email to be sent in the receiver's language "fr" + expect(buildEmailSpy).toHaveBeenCalledWith( + // ignore the template name + expect.any(String), + // expect the language to be the receiver's language + anotherUser.user.preferences?.language || "fr", + // ignore the email context + expect.any(Object), + ); + + // do the same for the other user + await anotherUser.shareWithPermissions(doc2, oneUser.user.id, "read"); + + // expect the email to be sent in the receiver's language "en" + expect(buildEmailSpy).toHaveBeenCalledWith( + // ignore the template name + expect.any(String), + // expect the language to be the receiver's language + anotherUser.user.preferences?.language || "en", + // ignore the email context + expect.any(Object), + ); + }); }); diff --git a/tdrive/backend/node/test/e2e/setup/index.ts b/tdrive/backend/node/test/e2e/setup/index.ts index 8aefc703..be53770b 100644 --- a/tdrive/backend/node/test/e2e/setup/index.ts +++ b/tdrive/backend/node/test/e2e/setup/index.ts @@ -15,6 +15,7 @@ import {FileServiceImpl} from "../../../src/services/files/services"; import StorageAPI from "../../../src/core/platform/services/storage/provider"; import {SearchServiceAPI} from "../../../src/core/platform/services/search/api"; import Session from "../../../src/services/console/entities/session"; +import EmailPusherAPI from "../../../src/core/platform/services/email-pusher/provider"; type TokenPayload = { sub: string; @@ -30,6 +31,9 @@ export type User = { id: string; first_name?: string; isWorkspaceModerator?: boolean; + preferences?: { + language?: string; + }; }; export interface TestPlatform { @@ -40,6 +44,7 @@ export interface TestPlatform { app: FastifyInstance; database: DatabaseServiceAPI; storage: StorageAPI; + emailPusher: EmailPusherAPI; messageQueue: MessageQueueServiceAPI; authService: AuthServiceAPI; filesService: FileServiceImpl; @@ -83,6 +88,7 @@ export async function init( const auth = platform.getProvider("auth"); const storage: StorageAPI = platform.getProvider("storage"); const search: SearchServiceAPI = platform.getProvider("search"); + const emailPusher: EmailPusherAPI = platform.getProvider("email-pusher"); testPlatform = { platform, @@ -90,6 +96,7 @@ export async function init( messageQueue, database, storage, + emailPusher, workspace: { company_id: "", workspace_id: "" }, currentUser: { id: "" }, currentSession: uuidv1(), diff --git a/tdrive/backend/node/test/e2e/utils.prepare.db.ts b/tdrive/backend/node/test/e2e/utils.prepare.db.ts index dd27cdcb..83da2b4b 100644 --- a/tdrive/backend/node/test/e2e/utils.prepare.db.ts +++ b/tdrive/backend/node/test/e2e/utils.prepare.db.ts @@ -130,6 +130,7 @@ export class TestDbService { cache?: User["cache"]; identity_provider?: string; type?: UserType; + preferences?: User["preferences"]; } = {}, id: string = uuidv1(), ): Promise { @@ -144,6 +145,10 @@ export class TestDbService { user.cache = options.cache || user.cache || { companies: [] }; user.identity_provider = options.identity_provider || "console"; user.type = options.type || "regular"; + user.preferences = options.preferences || { + locale: "en", + timezone: 0, + }; //Fixme this is cheating, we should correctly set the cache in internal mode in the code user.cache.companies = [