Skip to content

Commit

Permalink
feat(tenant): changes account to tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
suhasdeshpande committed Aug 9, 2023
1 parent 35b1e69 commit cdc5f8b
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 85 deletions.
4 changes: 2 additions & 2 deletions packages/client-graphql/docs/inbox/1.methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Inbox } from "@trycourier/client-graphql";
const inboxApi = Inbox(authorization: Authorization);

const getInboxCount = async (params?: {
accountId?: string,
tenantId?: string,
status?: "read" | "unread",
tags?: string[],
}) => {
Expand All @@ -23,7 +23,7 @@ import { Inbox } from "@trycourier/client-graphql";
const inboxApi = Inbox(authorization: Authorization);

const getInboxMessages = async (params?: {
accountId?: string,
tenantId?: string,
status?: "read" | "unread",
tags?: string[],
}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-graphql/src/inbox/__tests__/count.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("getInboxCount", () => {
Array [
"https://fxw3r7gdm9.execute-api.us-east-1.amazonaws.com/production/q",
Object {
"body": "{\\"query\\":\\"query GetInboxCount($params: FilterParamsInput) {\\\\n count(params: $params)\\\\n}\\\\n\\",\\"operationName\\":\\"GetInboxCount\\",\\"variables\\":{}}",
"body": "{\\"query\\":\\"query GetInboxCount($params: FilterParamsInput) {\\\\n count(params: $params)\\\\n}\\\\n\\",\\"operationName\\":\\"GetInboxCount\\",\\"variables\\":{\\"params\\":{}}}",
"headers": Object {
"content-type": "application/json",
"x-courier-client-key": "CLIENT_KEY",
Expand Down
10 changes: 8 additions & 2 deletions packages/client-graphql/src/inbox/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const GET_INBOX_COUNT = `
`;

export interface IInboxCountParams {
accountId?: string;
tenantId?: string;
status: "read" | "unread";
tags?: string[];
from?: string | number;
Expand All @@ -23,9 +23,15 @@ export const getInboxCount =
return Promise.resolve();
}

const { tenantId, ...rest } = params || {};

const results = await client
.query(GET_INBOX_COUNT, {
params,
params: {
...rest,
// [HACK] map tenantId to accountId in order to keep this backwards compatible
accountId: tenantId,
},
})
.toPromise();
return results?.data;
Expand Down
6 changes: 4 additions & 2 deletions packages/client-graphql/src/inbox/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client } from "urql";
import { IActionElemental } from "./message";

export interface IGetInboxMessagesParams {
accountId?: string;
tenantId?: string;
archived?: boolean;
from?: string | number;
limit?: number;
Expand Down Expand Up @@ -100,13 +100,15 @@ export const getInboxMessages =
return Promise.resolve(undefined);
}

const { limit, ...restParams } = params ?? {};
const { limit, tenantId, ...restParams } = params ?? {};
const results = await client
.query(createGetInboxMessagesQuery(!after), {
after,
limit,
params: {
...restParams,
// [HACK] map tenantId to accountId in order to keep this backwards compatible
accountId: tenantId,
pinned: false,
},
pinnedParams: {
Expand Down
16 changes: 12 additions & 4 deletions packages/client-graphql/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const GET_MESSAGE_COUNT = `
`;

export interface IMessageCountParams {
accountId?: string;
tenantId?: string;
tags?: string[];
from?: number;
isRead?: boolean;
Expand All @@ -31,7 +31,7 @@ export const getMessageCount =
};

export interface IGetMessagesParams {
accountId?: string;
tenantId?: string;
from?: number;
isRead?: boolean;
limit?: number;
Expand Down Expand Up @@ -136,9 +136,17 @@ export const getMessages =
return Promise.resolve(undefined);
}

const { limit, ...restParams } = params ?? {};
const { limit, tenantId, ...restParams } = params ?? {};
const results = await client
.query(QUERY_MESSAGES, { after, limit, params: restParams })
.query(QUERY_MESSAGES, {
after,
limit,
params: {
...restParams,
// [HACK] map tenantId to accountId in order to keep this backwards compatible
accountId: tenantId,
},
})
.toPromise();

const messages = results?.data?.messages?.nodes;
Expand Down
17 changes: 9 additions & 8 deletions packages/client-graphql/src/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,31 @@ const DRAFT_PREFERENCE_PAGE = `
}
`;

type GetRecipientPreferences = (accountId?: string) => Promise<any>;
type GetRecipientPreferences = (tenantId?: string) => Promise<any>;
export const getRecipientPreferences =
(client: Client | undefined): GetRecipientPreferences =>
async (accountId?: string) => {
async (tenantId?: string) => {
if (!client) {
return;
}

const results = await client
.query(RECIPIENT_PREFERENCES, { accountId })
.query(RECIPIENT_PREFERENCES, { accountId: tenantId })
.toPromise();
return results.data?.recipientPreferences.nodes;
};

type GetPreferencePage = (accountId?: string) => Promise<any>;
type GetPreferencePage = (tenantId?: string) => Promise<any>;
export const getPreferencePage =
(client: Client | undefined): GetPreferencePage =>
async (accountId?: string) => {
async (tenantId?: string) => {
if (!client) {
return;
}
const results = await client
.query(PREFERENCE_PAGE, {
accountId: accountId,
// [HACK] map tenantId to accountId in order to keep this backwards compatible
accountId: tenantId,
})
.toPromise();
return results.data?.preferencePage;
Expand Down Expand Up @@ -140,7 +141,7 @@ type UpdateRecipientPreferences = (payload: {
hasCustomRouting: boolean;
routingPreferences: Array<string>;
digestSchedule: string;
accountId?: string;
tenantId?: string;
}) => Promise<any>;
export const updateRecipientPreferences =
(client: Client | undefined): UpdateRecipientPreferences =>
Expand All @@ -158,7 +159,7 @@ export const updateRecipientPreferences =
routingPreferences: payload.routingPreferences,
digestSchedule: payload.digestSchedule,
},
accountId: payload.accountId,
accountId: payload.tenantId,
})
.toPromise();

Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/components/PreferencePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ const FooterContainer = styled.div`
`;

const PreferencePage: React.FunctionComponent<{
accountId?: string;
tenantId?: string;
draft?: boolean;
}> = ({ accountId, draft = false }) => {
}> = ({ tenantId, draft = false }) => {
return (
<PreferencePageContainer>
<HeaderContainer>
<Header />
</HeaderContainer>
<PreferencesV4Container>
<PreferencesV4 accountId={accountId} draft={draft} />
<PreferencesV4 tenantId={tenantId} draft={draft} />
</PreferencesV4Container>
<FooterContainer>
<Footer />
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const querySelector = (element: HTMLElement, selector: string) => {
export const CourierComponents: React.FunctionComponent = () => {
const preferencePageDraftMode =
window.courierConfig?.preferencePageDraftMode ?? false;
const accountId = window.courierConfig?.accountId;
const tenantId = window.courierConfig?.tenantId;
const unsubscribePageConfig = window.courierConfig?.unsubscribePage;

const componentConfigs = window.courierConfig?.components;
Expand Down Expand Up @@ -205,7 +205,7 @@ export const CourierComponents: React.FunctionComponent = () => {
createPortal(
<Suspense fallback={<div />}>
<PreferencePage
accountId={accountId}
tenantId={tenantId}
draft={preferencePageDraftMode}
/>
</Suspense>,
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ declare global {
}
}
interface ICourierConfig {
accountId?: string;
tenantId?: string;
apiUrl?: string;
authorization?: string;
brandId?: string;
Expand Down Expand Up @@ -68,7 +68,7 @@ let hasInit = false;

const initCourier = async (courierConfig?: ICourierConfig) => {
const {
accountId,
tenantId,
apiUrl,
authorization,
brandId,
Expand All @@ -94,7 +94,7 @@ const initCourier = async (courierConfig?: ICourierConfig) => {

render(
<CourierProvider
accountId={accountId}
tenantId={tenantId}
apiUrl={apiUrl}
authorization={authorization}
brandId={brandId}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/src/inbox/elemental-inbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { Brand } from "@trycourier/react-provider";

export interface IElementalInbox {
accountId?: string;
tenantId?: string;
brand?: Brand;
from?: number;
isLoading?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface IInboxActions {

const useElementalInboxActions = (): IInboxActions => {
const {
tenantId,
apiUrl,
authorization,
clientSourceId,
Expand All @@ -80,6 +81,7 @@ const useElementalInboxActions = (): IInboxActions => {
clientSourceId,
userId,
userSignature,
tenantId,
});

const inboxClient = Inbox({ client: courierClient });
Expand All @@ -101,6 +103,7 @@ const useElementalInboxActions = (): IInboxActions => {
type: "inbox/FETCH_UNREAD_MESSAGE_COUNT",
payload: () =>
inboxClient.getInboxCount({
tenantId: inbox?.tenantId,
status: "unread",
}),
});
Expand All @@ -127,7 +130,7 @@ const useElementalInboxActions = (): IInboxActions => {
type: "inbox/FETCH_UNREAD_MESSAGE_COUNT",
payload: () =>
inboxClient.getInboxCount({
accountId: inbox?.accountId,
tenantId: inbox?.tenantId,
status: "unread",
}),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/src/inbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
OnEvent,
} from "@trycourier/react-provider";
export interface IInbox<T = IInboxMessagePreview> {
accountId?: string;
tenantId?: string;
brand?: Brand;
from?: number;
isLoading?: boolean;
Expand Down
6 changes: 4 additions & 2 deletions packages/react-hooks/src/inbox/use-inbox-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface IInboxActions {

const useInboxActions = (): IInboxActions => {
const {
tenantId,
apiUrl,
authorization,
clientKey,
Expand All @@ -66,8 +67,9 @@ const useInboxActions = (): IInboxActions => {
const clientParams = {
apiUrl,
authorization,
clientSourceId,
clientKey,
clientSourceId,
tenantId,
userId,
userSignature,
};
Expand Down Expand Up @@ -96,9 +98,9 @@ const useInboxActions = (): IInboxActions => {
type: "inbox/FETCH_UNREAD_MESSAGE_COUNT",
payload: () =>
inboxClient.getInboxCount({
accountId: params?.accountId || inbox?.accountId,
from: params?.from || inbox?.from,
status: "unread",
tenantId: tenantId || params?.tenantId || inbox?.tenantId,
}),
});
};
Expand Down
17 changes: 11 additions & 6 deletions packages/react-hooks/src/inbox/use-inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ import useInboxActions, { IInboxActions } from "./use-inbox-actions";
import { IInbox } from "./types";

const useInbox = (): IInbox<IInboxMessagePreview> & IInboxActions => {
const { dispatch, inbox, transport, brand, accountId } =
useCourier<{
inbox: IInbox;
}>();
const {
dispatch,
inbox,
transport,
brand,
tenantId: tenantId,
} = useCourier<{
inbox: IInbox;
}>();

const actions = useInboxActions();

if (inbox && (brand || inbox.brand)) {
inbox.brand = deepExtend({}, brand ?? {}, inbox.brand ?? {});
}

if (inbox && accountId) {
inbox.accountId = accountId;
if (inbox && tenantId) {
inbox.tenantId = tenantId;
}

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const usePreferencesActions = () => {
const preferences = Preferences({ client: courierClient });

return {
fetchRecipientPreferences: (accountId?: string) => {
fetchRecipientPreferences: (tenantId?: string) => {
dispatch({
type: "preferences/FETCH_RECIPIENT_PREFERENCES",
payload: () => preferences.getRecipientPreferences(accountId),
payload: () => preferences.getRecipientPreferences(tenantId),
});
},
fetchPreferencePage: (accountId?: string, draft = false) => {
fetchPreferencePage: (tenantId?: string, draft = false) => {
if (draft) {
dispatch({
type: "preferences/FETCH_DRAFT_PREFERENCE_PAGE",
Expand All @@ -31,7 +31,7 @@ const usePreferencesActions = () => {
} else {
dispatch({
type: "preferences/FETCH_PREFERENCE_PAGE",
payload: () => preferences.getPreferencePage(accountId),
payload: () => preferences.getPreferencePage(tenantId),
});
}
},
Expand Down
Loading

0 comments on commit cdc5f8b

Please sign in to comment.