Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: super admin is compatible with company subsidiaries #214

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/storefront/src/components/B3Dialog.tsx
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import {
Dialog,
DialogActions,
DialogContent,
DialogProps,
DialogTitle,
SxProps,
Theme,
@@ -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>({
@@ -64,6 +66,7 @@ export default function B3Dialog<T>({
fullWidth = false,
disabledSaveBtn = false,
dialogWidth = '',
restDialogParams,
}: B3DialogProps<T>) {
const container = useRef<HTMLInputElement | null>(null);

@@ -113,6 +116,7 @@ export default function B3Dialog<T>({
aria-describedby="alert-dialog-description"
id="b2b-dialog-container"
sx={customStyle}
{...restDialogParams}
>
{title && (
<DialogTitle
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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,
28 changes: 23 additions & 5 deletions apps/storefront/src/pages/QuoteDraft/index.tsx
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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);
@@ -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,
@@ -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 || {};
Original file line number Diff line number Diff line change
@@ -121,9 +121,7 @@ const updateShoppingList = (
}
}`;

const createShoppingList = (
fn: string,
) => `mutation($shoppingListData: ShoppingListsCreateInputType!){
const createShoppingList = (fn: string) => `mutation($shoppingListData: ShoppingListsInputType!){
${fn}(
shoppingListData: $shoppingListData
) {
5 changes: 5 additions & 0 deletions apps/storefront/src/utils/b3CheckPermissions/check.ts
Original file line number Diff line number Diff line change
@@ -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({