Skip to content

Commit

Permalink
fix: super admin is compatible with company subsidiaries (#214)
Browse files Browse the repository at this point in the history
* fix: super admin is compatible with company subsidiaries

* fix: createShoppingList Specifies the type change

* fix: address permission for quoteDraft
  • Loading branch information
BrianJiang2021 authored Jan 21, 2025
1 parent 711d3a1 commit b69412e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
4 changes: 4 additions & 0 deletions apps/storefront/src/components/B3Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Dialog,
DialogActions,
DialogContent,
DialogProps,
DialogTitle,
SxProps,
Theme,
Expand Down Expand Up @@ -40,6 +41,7 @@ export interface B3DialogProps<T> {
dialogContentSx?: SxProps<Theme>;
dialogSx?: SxProps<Theme>;
dialogWidth?: string;
restDialogParams?: Omit<DialogProps, 'open' | 'onClose'>;
}

export default function B3Dialog<T>({
Expand All @@ -64,6 +66,7 @@ export default function B3Dialog<T>({
fullWidth = false,
disabledSaveBtn = false,
dialogWidth = '',
restDialogParams,
}: B3DialogProps<T>) {
const container = useRef<HTMLInputElement | null>(null);

Expand Down Expand Up @@ -113,6 +116,7 @@ export default function B3Dialog<T>({
aria-describedby="alert-dialog-description"
id="b2b-dialog-container"
sx={customStyle}
{...restDialogParams}
>
{title && (
<DialogTitle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,6 @@ function HierarchyDialog({
),
}),
);

if (companyId === +currentCompanyId) {
const { hash } = window.location;
if (hash.includes('/shoppingList/')) {
navigate('/shoppingLists');
}
}

if (companyId !== +currentCompanyId && !isHasCurrentPagePermission) {
const key = Object.keys(pagesSubsidiariesPermission).find((key) => {
return !!pagesSubsidiariesPermission[key as keyof PagesSubsidiariesPermissionProps];
});

const route = PAGES_SUBSIDIARIES_PERMISSION_KEYS.find((item) => item.key === key);

if (route) {
handleClose();
setLoading(false);
navigate(route.path);
}
}
} catch (error) {
b2bLogger.error(error);
} finally {
Expand All @@ -131,6 +110,33 @@ function HierarchyDialog({
loading={loading}
handleLeftClick={handleClose}
handRightClick={handleSwitchCompanyClick}
restDialogParams={{
TransitionProps: {
onExited: () => {
if (!currentRow) return;
const { companyId } = currentRow;
if (companyId === +currentCompanyId) {
const { hash } = window.location;
if (hash.includes('/shoppingList/')) {
navigate('/shoppingLists');
}
}
if (companyId !== +currentCompanyId && !isHasCurrentPagePermission) {
const key = Object.keys(pagesSubsidiariesPermission).find((key) => {
return !!pagesSubsidiariesPermission[key as keyof PagesSubsidiariesPermissionProps];
});

const route = PAGES_SUBSIDIARIES_PERMISSION_KEYS.find((item) => item.key === key);

if (route) {
handleClose();
setLoading(false);
navigate(route.path);
}
}
},
},
}}
dialogSx={{
'& .MuiDialogTitle-root': {
border: 0,
Expand Down
28 changes: 23 additions & 5 deletions apps/storefront/src/pages/QuoteDraft/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { cloneDeep, concat, uniq } from 'lodash-es';
import CustomButton from '@/components/button/CustomButton';
import { getContrastColor } from '@/components/outSideComponents/utils/b3CustomStyles';
import B3Spin from '@/components/spin/B3Spin';
import { useMobile, useSetCountry } from '@/hooks';
import { permissionLevels } from '@/constants';
import { useMobile, useSetCountry, useValidatePermissionWithComparisonType } from '@/hooks';
import { CustomStyleContext } from '@/shared/customStyleButton';
import { GlobalContext } from '@/shared/global';
import {
Expand Down Expand Up @@ -186,6 +187,12 @@ function QuoteDraft({ setOpenPage }: PageProps) {

const quoteSummaryRef = useRef<QuoteSummaryRef | null>(null);

const [isAddressCompanyHierarchy] = useValidatePermissionWithComparisonType({
level: permissionLevels.COMPANY_SUBSIDIARIES,
code: b2bPermissionsMap.getAddressesPermission,
containOrEqual: 'equal',
});

useSetCountry();

const contactInfoRef = useRef<InfoRefProps | null>(null);
Expand Down Expand Up @@ -213,9 +220,20 @@ function QuoteDraft({ setOpenPage }: PageProps) {
try {
if (isB2BUser) {
const companyId = companyB2BId || salesRepCompanyId;
const {
addresses: { edges: addressB2BList = [] },
} = await getB2BCustomerAddresses(+companyId);

let addressB2BList = [];
const fetchAddresses = async (id: number) => {
const {
addresses: { edges },
} = await getB2BCustomerAddresses(id);
return edges;
};

if (!selectCompanyHierarchyId) {
addressB2BList = await fetchAddresses(+companyId);
} else if (selectCompanyHierarchyId && isAddressCompanyHierarchy) {
addressB2BList = await fetchAddresses(+selectCompanyHierarchyId);
}

const shippingDefaultAddress = addressB2BList.find(
(item: B2BAddress) => item?.node?.isDefaultShipping === 1,
Expand Down Expand Up @@ -314,7 +332,7 @@ function QuoteDraft({ setOpenPage }: PageProps) {
init();
// disabling as we only need to run this once and values at starting render are good enough
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [selectCompanyHierarchyId, isAddressCompanyHierarchy]);

const quoteAndExtraFieldsInfo = useMemo(() => {
const contactInfo: CustomFieldItems = quoteInfoOrigin.contactInfo || {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ const updateShoppingList = (
}
}`;

const createShoppingList = (
fn: string,
) => `mutation($shoppingListData: ShoppingListsCreateInputType!){
const createShoppingList = (fn: string) => `mutation($shoppingListData: ShoppingListsInputType!){
${fn}(
shoppingListData: $shoppingListData
) {
Expand Down
5 changes: 5 additions & 0 deletions apps/storefront/src/utils/b3CheckPermissions/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export const verifyLevelPermission = ({
const { permissionLevel } = info;

if (!permissionLevel) return false;

const salesRepCompanyId = store.getState().b2bFeatures.masqueradeCompany.id;

if (salesRepCompanyId) return true;

const { companyInfo, customer } = store.getState().company || {};

return levelComparison({
Expand Down

0 comments on commit b69412e

Please sign in to comment.