Skip to content

Commit

Permalink
Merge pull request #81 from stellar/sdp-multitenant
Browse files Browse the repository at this point in the history
Multi-tenant Merge to Develop
  • Loading branch information
marwen-abid authored Mar 29, 2024
2 parents 721f729 + b1dfabc commit 2797628
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/api/getDisbursementDetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiDisbursement } from "types";

export const getDisbursementDetails = async (
Expand All @@ -11,6 +12,7 @@ export const getDisbursementDetails = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/api/getDisbursementDrafts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { UI_STATUS_DISBURSEMENT_DRAFT } from "constants/settings";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiDisbursements } from "types";

export const getDisbursementDrafts = async (
Expand All @@ -14,6 +15,7 @@ export const getDisbursementDrafts = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/api/getDisbursementInstructions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { API_URL } from "constants/envVariables";
import { SESSION_EXPIRED } from "constants/settings";
import { getSdpTenantName } from "helpers/getSdpTenantName";

export const getDisbursementInstructions = async (
token: string,
Expand All @@ -11,6 +12,7 @@ export const getDisbursementInstructions = async (
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/api/getDisbursementReceivers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { handleApiResponse } from "api/handleApiResponse";
import { handleSearchParams } from "api/handleSearchParams";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiDisbursementReceivers, PaginationParams } from "types";

export const getDisbursementReceivers = async (
Expand All @@ -17,6 +18,7 @@ export const getDisbursementReceivers = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
},
);
Expand Down
2 changes: 2 additions & 0 deletions src/api/getDisbursements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { handleApiResponse } from "api/handleApiResponse";
import { handleSearchParams } from "api/handleSearchParams";
import { API_URL } from "constants/envVariables";
import { UI_STATUS_DISBURSEMENT } from "constants/settings";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiDisbursements, PaymentsSearchParams } from "types";

export const getDisbursements = async (
Expand All @@ -23,6 +24,7 @@ export const getDisbursements = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/api/getOrgInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiOrgInfo } from "types";

export const getOrgInfo = async (token: string): Promise<ApiOrgInfo> => {
Expand All @@ -8,6 +9,7 @@ export const getOrgInfo = async (token: string): Promise<ApiOrgInfo> => {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

Expand Down
12 changes: 10 additions & 2 deletions src/api/getOrgLogo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";

export async function getOrgLogo(): Promise<string> {
const response = await fetch(`${API_URL}/organization/logo`);
export async function getOrgLogo(token: string): Promise<string> {
const response = await fetch(`${API_URL}/organization/logo`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

const responseBlob = await response.blob();
return URL.createObjectURL(responseBlob);
Expand Down
2 changes: 2 additions & 0 deletions src/api/getProfileInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiProfileInfo } from "types";

export const getProfileInfo = async (
Expand All @@ -10,6 +11,7 @@ export const getProfileInfo = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

Expand Down
2 changes: 2 additions & 0 deletions src/api/patchDisbursementStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { DisbursementStatus } from "types";

export const patchDisbursementStatus = async (
Expand All @@ -14,6 +15,7 @@ export const patchDisbursementStatus = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: JSON.stringify({
status,
Expand Down
2 changes: 2 additions & 0 deletions src/api/patchOrgInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { sanitizeObject } from "helpers/sanitizeObject";
import { OrgUpdateInfo } from "types";

Expand Down Expand Up @@ -45,6 +46,7 @@ export const patchOrgInfo = async (
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: formData,
});
Expand Down
2 changes: 2 additions & 0 deletions src/api/patchProfileInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { sanitizeObject } from "helpers/sanitizeObject";

export const patchProfileInfo = async (
Expand All @@ -24,6 +25,7 @@ export const patchProfileInfo = async (
method: "PATCH",
headers: {
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: JSON.stringify(fieldsToSubmit),
});
Expand Down
2 changes: 2 additions & 0 deletions src/api/patchUserRole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { UserRole } from "types";

export const patchUserRole = async (
Expand All @@ -12,6 +13,7 @@ export const patchUserRole = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: JSON.stringify({
user_id: userId,
Expand Down
5 changes: 4 additions & 1 deletion src/api/postDisbursement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { ApiDisbursement, Disbursement } from "types";

export const postDisbursement = async (
Expand All @@ -11,14 +12,16 @@ export const postDisbursement = async (
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: JSON.stringify({
name: disbursement.name,
wallet_id: disbursement.wallet.id,
asset_id: disbursement.asset.id,
country_code: disbursement.country.code,
verification_field: disbursement.verificationField || "",
sms_registration_message_template: disbursement.smsRegistrationMessageTemplate,
sms_registration_message_template:
disbursement.smsRegistrationMessageTemplate,
}),
});

Expand Down
2 changes: 2 additions & 0 deletions src/api/postDisbursementFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleApiResponse } from "api/handleApiResponse";
import { API_URL } from "constants/envVariables";
import { getSdpTenantName } from "helpers/getSdpTenantName";

export const postDisbursementFile = async (
token: string,
Expand All @@ -15,6 +16,7 @@ export const postDisbursementFile = async (
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
body: formData,
},
Expand Down
2 changes: 2 additions & 0 deletions src/api/refreshToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { differenceInMinutes, fromUnixTime } from "date-fns";
import { API_URL } from "constants/envVariables";
import { SESSION_EXPIRED } from "constants/settings";
import { parseJwt } from "helpers/parseJwt";
import { getSdpTenantName } from "helpers/getSdpTenantName";

export const refreshToken = async (token: string): Promise<string> => {
const jwt = parseJwt(token);
Expand All @@ -16,6 +17,7 @@ export const refreshToken = async (token: string): Promise<string> => {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(),
},
});

Expand Down
23 changes: 19 additions & 4 deletions src/apiQueries/useForgotPasswordLink.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { useMutation } from "@tanstack/react-query";
import { API_URL } from "constants/envVariables";
import { fetchApi } from "helpers/fetchApi";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { AppError } from "types";

type ForgotPasswordLinkProps = {
organizationName: string;
email: string;
recaptchaToken: string;
};

export const useForgotPasswordLink = () => {
const mutation = useMutation({
mutationFn: ({ email, recaptchaToken }: ForgotPasswordLinkProps) => {
mutationFn: ({
organizationName,
email,
recaptchaToken,
}: ForgotPasswordLinkProps) => {
return fetchApi(
`${API_URL}/forgot-password`,
{
Expand All @@ -19,8 +25,13 @@ export const useForgotPasswordLink = () => {
email,
recaptcha_token: recaptchaToken,
}),
headers: {
"SDP-Tenant-Name": getSdpTenantName(organizationName),
},
},
{
withoutAuth: true,
},
{ withoutAuth: true },
);
},
cacheTime: 0,
Expand All @@ -30,9 +41,13 @@ export const useForgotPasswordLink = () => {
...mutation,
error: mutation.error as AppError,
data: mutation.data as { message: string },
mutateAsync: async ({ email, recaptchaToken }: ForgotPasswordLinkProps) => {
mutateAsync: async ({
organizationName,
email,
recaptchaToken,
}: ForgotPasswordLinkProps) => {
try {
await mutation.mutateAsync({ email, recaptchaToken });
await mutation.mutateAsync({ organizationName, email, recaptchaToken });
} catch (e) {
// do nothing
}
Expand Down
19 changes: 16 additions & 3 deletions src/apiQueries/useResetPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import { useMutation } from "@tanstack/react-query";
import { API_URL } from "constants/envVariables";
import { fetchApi } from "helpers/fetchApi";
import { normalizeApiError } from "helpers/normalizeApiError";
import { getSdpTenantName } from "helpers/getSdpTenantName";
import { AppError } from "types";

type ResetPasswordProps = {
organizationName: string;
password: string;
resetToken: string;
};

export const useResetPassword = () => {
const mutation = useMutation({
mutationFn: ({ password, resetToken }: ResetPasswordProps) => {
mutationFn: ({
organizationName,
password,
resetToken,
}: ResetPasswordProps) => {
return fetchApi(
`${API_URL}/reset-password`,
{
Expand All @@ -20,6 +26,9 @@ export const useResetPassword = () => {
password,
reset_token: resetToken,
}),
headers: {
"SDP-Tenant-Name": getSdpTenantName(organizationName),
},
},
{
withoutAuth: true,
Expand All @@ -46,9 +55,13 @@ export const useResetPassword = () => {
...mutation,
error: mutation.error as AppError,
data: mutation.data as boolean,
mutateAsync: async ({ password, resetToken }: ResetPasswordProps) => {
mutateAsync: async ({
organizationName,
password,
resetToken,
}: ResetPasswordProps) => {
try {
await mutation.mutateAsync({ password, resetToken });
await mutation.mutateAsync({ organizationName, password, resetToken });
} catch (e) {
// do nothing
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const LOCAL_STORAGE_SESSION_TOKEN = "sdp_session";
export const LOCAL_STORAGE_DEVICE_ID = "sdp_deviceID";
export const UI_STATUS_DISBURSEMENT = "STARTED,PAUSED,COMPLETED";
export const UI_STATUS_DISBURSEMENT_DRAFT = "DRAFT,READY";
export const ORG_NAME_INFO_TEXT =
"You can find your organization name in the invitation email";

export const CANCELED_PAYMENT_STATUS = "CANCELED";
export const READY_PAYMENT_STATUS = "READY";
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/fetchApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { localStorageSessionToken } from "helpers/localStorageSessionToken";
import { parseJwt } from "helpers/parseJwt";
import { normalizeApiError } from "helpers/normalizeApiError";
import { AnyObject } from "types";
import { getSdpTenantName } from "./getSdpTenantName";

type FetchApiOptions = {
withoutAuth?: boolean;
omitContentType?: boolean;
customCallback?: (request: Response) => void;
organizationName?: string;
};

export const fetchApi = async (
Expand Down Expand Up @@ -42,6 +44,7 @@ export const fetchApi = async (
config.headers = {
...config.headers,
Authorization: `Bearer ${token}`,
"SDP-Tenant-Name": getSdpTenantName(options?.organizationName),
...(!options?.omitContentType
? {
"Content-Type":
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/formatDisbursements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ export const formatDisbursement = (
timestamp: h.timestamp,
userId: h.user_id,
})),
smsRegistrationMessageTemplate: disbursement.sms_registration_message_template,
smsRegistrationMessageTemplate:
disbursement.sms_registration_message_template,
});
2 changes: 2 additions & 0 deletions src/helpers/getSdpTenantName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getSdpTenantName = (organizationName?: string): string =>
organizationName ?? window.location.hostname.split(".")[0];
Loading

0 comments on commit 2797628

Please sign in to comment.