Skip to content

Commit

Permalink
Use lang parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek committed Feb 15, 2024
1 parent b96f0bb commit 626f748
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 81 deletions.
3 changes: 3 additions & 0 deletions clients/banking/src/components/MembershipDetailEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,12 @@ export const MembershipDetailEditor = ({
setInvitationSending(AsyncData.Loading());
const request = Future.make<Result<undefined, undefined>>(resolve => {
const xhr = new XMLHttpRequest();

// TODO: oauth2
const query = new URLSearchParams();
query.append("inviterAccountMembershipId", currentUserAccountMembershipId);
query.append("lang", locale.language);

xhr.open(
"POST",
match(projectConfiguration)
Expand Down
14 changes: 8 additions & 6 deletions clients/banking/src/components/MembershipsArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Suspense, useCallback, useEffect, useMemo, useRef } from "react";
import { StyleSheet, View } from "react-native";
import { P, match } from "ts-pattern";
import { AccountCountry, AccountMembershipFragment, MembersPageDocument } from "../graphql/partner";
import { t } from "../utils/i18n";
import { locale, t } from "../utils/i18n";
import { projectConfiguration } from "../utils/projectId";
import { Router, membershipsRoutes } from "../utils/routes";
import { ErrorView } from "./ErrorView";
Expand Down Expand Up @@ -209,18 +209,20 @@ export const MembershipsArea = ({
},
({ params: { resourceId } }) => {
const xhr = new XMLHttpRequest();
const query = new URLSearchParams();

query.append("inviterAccountMembershipId", accountMembershipId);
query.append("lang", locale.language);

xhr.open(
"POST",
match(projectConfiguration)
.with(
Option.P.Some({ projectId: P.select(), mode: "MultiProject" }),
projectId =>
`/api/projects/${projectId}/invitation/${resourceId}/send?inviterAccountMembershipId=${accountMembershipId}`,
`/api/projects/${projectId}/invitation/${resourceId}/send?${query.toString()}`,
)
.otherwise(
() =>
`/api/invitation/${resourceId}/send?inviterAccountMembershipId=${accountMembershipId}`,
),
.otherwise(() => `/api/invitation/${resourceId}/send?${query.toString()}`),
true,
);
xhr.addEventListener("load", () => {
Expand Down
3 changes: 3 additions & 0 deletions clients/banking/src/components/NewMembershipWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,12 @@ export const NewMembershipWizard = ({
}) => {
const request = Future.make<Result<undefined, undefined>>(resolve => {
const xhr = new XMLHttpRequest();

// TODO: oauth2
const query = new URLSearchParams();
query.append("inviterAccountMembershipId", currentUserAccountMembership.id);
query.append("lang", locale.language);

xhr.open(
"POST",
match(projectConfiguration)
Expand Down
66 changes: 33 additions & 33 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ const COOKIE_MAX_AGE = 60 * (env.NODE_ENV !== "test" ? 5 : 60); // 5 minutes (ex
const OAUTH_STATE_COOKIE_MAX_AGE = 900; // 15 minutes

export type InvitationConfig = {
acceptLanguage: string | undefined;
accessToken: string;
inviteeAccountMembershipId: string;
inviterAccountMembershipId: string;
language: string;
};

type AppConfig = {
Expand Down Expand Up @@ -441,42 +441,42 @@ export const start = async ({
* Send an account membership invitation
* e.g. /api/invitation/:id/send?inviterAccountMembershipId=1234
*/
app.post<{ Querystring: Record<string, string>; Params: { inviteeAccountMembershipId: string } }>(
"/api/invitation/:inviteeAccountMembershipId/send",
async (request, reply) => {
const accessToken = request.accessToken;
app.post<{
Querystring: Record<string, string>;
Params: { inviteeAccountMembershipId: string; lang: string };
}>("/api/invitation/:inviteeAccountMembershipId/send", async (request, reply) => {
const accessToken = request.accessToken;

if (accessToken == null) {
return reply.status(401).send("Unauthorized");
}
if (sendAccountMembershipInvitation == null) {
return reply.status(400).send("Not implemented");
}

if (accessToken == null) {
return reply.status(401).send("Unauthorized");
}
if (sendAccountMembershipInvitation == null) {
return reply.status(400).send("Not implemented");
}
const inviterAccountMembershipId = request.query.inviterAccountMembershipId;

const inviterAccountMembershipId = request.query.inviterAccountMembershipId;
if (inviterAccountMembershipId == null) {
return reply.status(400).send("Missing inviterAccountMembershipId");
}

if (inviterAccountMembershipId == null) {
return reply.status(400).send("Missing inviterAccountMembershipId");
}
try {
const result = await sendAccountMembershipInvitation({
accessToken,
inviteeAccountMembershipId: request.params.inviteeAccountMembershipId,
inviterAccountMembershipId,
language: request.params.lang,
});
return reply.send({ success: result });
} catch (err) {
request.log.error(err);

try {
const result = await sendAccountMembershipInvitation({
acceptLanguage: request.headers["accept-language"],
accessToken,
inviteeAccountMembershipId: request.params.inviteeAccountMembershipId,
inviterAccountMembershipId,
});
return reply.send({ success: result });
} catch (err) {
request.log.error(err);

return replyWithError(app, request, reply, {
status: 400,
requestId: String(request.id),
});
}
},
);
return replyWithError(app, request, reply, {
status: 400,
requestId: String(request.id),
});
}
});

/**
* Builds a OAuth2 auth link and redirects to it.
Expand Down
15 changes: 7 additions & 8 deletions server/src/index.swan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { InvitationConfig, start } from "./app";
import { env, url as validateUrl } from "./env";
import { replyWithError } from "./error";
import { AccountCountry, GetAccountMembershipInvitationDataQuery } from "./graphql/partner";
import { findBestLanguage } from "./utils/language";

const keysPath = path.join(__dirname, "../keys");

Expand Down Expand Up @@ -59,10 +58,10 @@ const swanColorHex = "#6240B5";

const getMailjetInput = ({
invitationData,
requestLanguage,
language,
}: {
invitationData: GetAccountMembershipInvitationDataQuery;
requestLanguage: string;
language: string;
}) =>
match(invitationData)
.with(
Expand Down Expand Up @@ -103,10 +102,10 @@ const getMailjetInput = ({
Email: inviteeAccountMembership.email,
},
],
TemplateID: match(requestLanguage)
TemplateID: match(language)
.with("fr", () => 2725624)
.otherwise(() => 2850442), // "english"
Subject: match(requestLanguage)
Subject: match(language)
.with("fr", () => `Rejoignez votre espace bancaire sur ${projectInfo.name}`)
.otherwise(() => `Join your banking space on ${projectInfo.name}`),
TemplateLanguage: true,
Expand Down Expand Up @@ -141,7 +140,7 @@ const sendAccountMembershipInvitation = (invitationConfig: InvitationConfig) =>
.mapOkToResult(invitationData =>
getMailjetInput({
invitationData,
requestLanguage: findBestLanguage(invitationConfig.acceptLanguage),
language: invitationConfig.language,
}),
)
.flatMapOk(data => {
Expand Down Expand Up @@ -207,7 +206,7 @@ start({
*/
app.post<{
Querystring: Record<string, string>;
Params: { inviteeAccountMembershipId: string; projectId: string };
Params: { inviteeAccountMembershipId: string; lang: string; projectId: string };
}>(
"/api/projects/:projectId/invitation/:inviteeAccountMembershipId/send",
async (request, reply) => {
Expand All @@ -226,10 +225,10 @@ start({
.flatMapOk(accessToken =>
Future.fromPromise(
sendAccountMembershipInvitation({
acceptLanguage: request.headers["accept-language"],
accessToken,
inviteeAccountMembershipId: request.params.inviteeAccountMembershipId,
inviterAccountMembershipId,
language: request.params.lang,
}),
),
)
Expand Down
34 changes: 0 additions & 34 deletions server/src/utils/language.ts

This file was deleted.

0 comments on commit 626f748

Please sign in to comment.