Translate notifications (#581)

Co-authored-by: Monta <monta@HP-ProBook-445-14-inch-G9-Notebook-PC-505aadfc.localdomain>
Co-authored-by: Eric Doughty-Papassideris <edoughtypapassideris@linagora.com>
This commit is contained in:
Montassar Ghanmy
2024-07-18 19:16:10 +01:00
committed by GitHub
parent 8a2651134b
commit 222a6d1059
8 changed files with 56 additions and 5 deletions
@@ -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,
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
@@ -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,
@@ -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),
);
});
});
@@ -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<AuthServiceAPI>("auth");
const storage: StorageAPI = platform.getProvider<StorageAPI>("storage");
const search: SearchServiceAPI = platform.getProvider<SearchServiceAPI>("search");
const emailPusher: EmailPusherAPI = platform.getProvider<EmailPusherAPI>("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(),
@@ -130,6 +130,7 @@ export class TestDbService {
cache?: User["cache"];
identity_provider?: string;
type?: UserType;
preferences?: User["preferences"];
} = {},
id: string = uuidv1(),
): Promise<User> {
@@ -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 = [