Skip to content

Commit

Permalink
✨ Translate notifications (#581)
Browse files Browse the repository at this point in the history
Co-authored-by: Monta <[email protected]>
Co-authored-by: Eric Doughty-Papassideris <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2024
1 parent 8a26511 commit 222a6d1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Notifications manquées dans votre Twake Drive
Notifications non reçues dans votre Twake Drive
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ 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"
: "notification-document-version-updated";
try {
const { html, text, subject } = await globalResolver.platformServices.emailPusher.build(
emailTemplate,
receiver.language || "en",
language,
{
sender,
receiver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 () => {
Expand All @@ -35,6 +38,7 @@ describe("the Drive feature", () => {
"statistics",
"platform-services",
"documents",
"email-pusher"
],
});
currentUser = await UserApi.getInstance(platform);
Expand Down Expand Up @@ -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),
);
});
});
7 changes: 7 additions & 0 deletions tdrive/backend/node/test/e2e/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,9 @@ export type User = {
id: string;
first_name?: string;
isWorkspaceModerator?: boolean;
preferences?: {
language?: string;
};
};

export interface TestPlatform {
Expand All @@ -40,6 +44,7 @@ export interface TestPlatform {
app: FastifyInstance;
database: DatabaseServiceAPI;
storage: StorageAPI;
emailPusher: EmailPusherAPI;
messageQueue: MessageQueueServiceAPI;
authService: AuthServiceAPI;
filesService: FileServiceImpl;
Expand Down Expand Up @@ -83,13 +88,15 @@ 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,
app,
messageQueue,
database,
storage,
emailPusher,
workspace: { company_id: "", workspace_id: "" },
currentUser: { id: "" },
currentSession: uuidv1(),
Expand Down
5 changes: 5 additions & 0 deletions tdrive/backend/node/test/e2e/utils.prepare.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class TestDbService {
cache?: User["cache"];
identity_provider?: string;
type?: UserType;
preferences?: User["preferences"];
} = {},
id: string = uuidv1(),
): Promise<User> {
Expand All @@ -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 = [
Expand Down

0 comments on commit 222a6d1

Please sign in to comment.