@vtex/faststore-plugin-buyer-portal 1.1.107 → 1.1.109

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/package.json +1 -1
  2. package/src/features/custom-fields/layouts/CustomFieldsLayout/CustomFieldsLayout.tsx +1 -0
  3. package/src/features/shared/clients/Auth.ts +25 -0
  4. package/src/features/shared/clients/Client.ts +11 -1
  5. package/src/features/shared/services/index.ts +4 -0
  6. package/src/features/shared/services/validate-access.service.ts +11 -0
  7. package/src/features/shared/types/AuthRouteProps.ts +5 -0
  8. package/src/features/shared/types/index.ts +1 -0
  9. package/src/features/shared/utils/constants.ts +1 -1
  10. package/src/features/shared/utils/index.ts +5 -0
  11. package/src/features/shared/utils/withAuth.tsx +31 -0
  12. package/src/features/shared/utils/withAuthLoader.ts +47 -0
  13. package/src/features/shared/utils/withAuthProvider.tsx +38 -0
  14. package/src/features/shared/utils/withBuyerPortal.tsx +24 -0
  15. package/src/features/shared/utils/withProviders.tsx +35 -0
  16. package/src/pages/address-details.tsx +95 -91
  17. package/src/pages/addresses.tsx +63 -63
  18. package/src/pages/budgets-details.tsx +44 -43
  19. package/src/pages/budgets.tsx +58 -56
  20. package/src/pages/buying-policies.tsx +84 -78
  21. package/src/pages/buying-policy-details.tsx +43 -44
  22. package/src/pages/collections.tsx +59 -60
  23. package/src/pages/cost-centers.tsx +48 -51
  24. package/src/pages/credit-cards.tsx +44 -49
  25. package/src/pages/home.tsx +28 -23
  26. package/src/pages/org-unit-details.tsx +39 -36
  27. package/src/pages/org-units.tsx +86 -90
  28. package/src/pages/payment-methods.tsx +41 -42
  29. package/src/pages/po-numbers.tsx +43 -46
  30. package/src/pages/profile.tsx +54 -52
  31. package/src/pages/releases.tsx +43 -41
  32. package/src/pages/role-details.tsx +57 -60
  33. package/src/pages/roles.tsx +42 -39
  34. package/src/pages/user-details.tsx +58 -58
  35. package/src/pages/users.tsx +83 -80
@@ -1,21 +1,19 @@
1
1
  import { getContractsByOrgUnitIdService } from "../features/contracts/services";
2
2
  import { OrgUnitsDetailsLayout } from "../features/org-units/layouts";
3
3
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
4
- import {
5
- BuyerPortalProvider,
6
- withErrorBoundary,
7
- } from "../features/shared/components";
4
+ import { withErrorBoundary } from "../features/shared/components";
8
5
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
9
6
  import {
10
7
  type ClientContext,
11
- getClientContext,
12
8
  withLoaderErrorBoundary,
9
+ withAuthLoader,
10
+ withProviders,
13
11
  } from "../features/shared/utils";
14
12
  import { getUserByIdService } from "../features/users/services";
15
13
 
16
14
  import type { ContractData } from "../features/contracts/types";
17
15
  import type { OrgUnitBasicData } from "../features/org-units/types";
18
- import type { LoaderData } from "../features/shared/types";
16
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
19
17
  import type { UserData } from "../features/users/types";
20
18
 
21
19
  export type OrgUnitDetailsPageData = {
@@ -24,7 +22,9 @@ export type OrgUnitDetailsPageData = {
24
22
  contracts: ContractData[];
25
23
  user: UserData | null;
26
24
  };
27
- clientContext: ClientContext;
25
+ context: {
26
+ clientContext: ClientContext;
27
+ };
28
28
  hasError?: boolean;
29
29
  error?: {
30
30
  error: {
@@ -45,35 +45,37 @@ type OrgUnitDetailsPageQuery = {
45
45
 
46
46
  const loaderFunction = async (
47
47
  data: LoaderData<OrgUnitDetailsPageQuery>
48
- ): Promise<OrgUnitDetailsPageData> => {
49
- const { orgUnitId } = data.query ?? {};
48
+ ): Promise<AuthRouteProps<OrgUnitDetailsPageData>> => {
49
+ const { orgUnitId } = data.query;
50
50
 
51
51
  if (!orgUnitId) {
52
52
  throw new Error(`Missing required query param: orgUnitId=${orgUnitId}`);
53
53
  }
54
54
 
55
- const { cookie, userId, ...clientContext } = await getClientContext(data);
55
+ return withAuthLoader(data, async ({ cookie, userId, ...clientContext }) => {
56
+ const orgUnit = await getOrgUnitBasicDataService({
57
+ id: orgUnitId,
58
+ cookie,
59
+ });
56
60
 
57
- const orgUnit = await getOrgUnitBasicDataService({
58
- id: orgUnitId,
59
- cookie,
60
- });
61
+ const user = await getUserByIdService({ orgUnitId, userId, cookie });
61
62
 
62
- const user = await getUserByIdService({ orgUnitId, userId, cookie });
63
+ const contracts = await getContractsByOrgUnitIdService({
64
+ orgUnitId,
65
+ cookie,
66
+ });
63
67
 
64
- const contracts = await getContractsByOrgUnitIdService({
65
- orgUnitId,
66
- cookie,
68
+ return {
69
+ data: {
70
+ contracts,
71
+ orgUnit,
72
+ user,
73
+ },
74
+ context: {
75
+ clientContext: { cookie, userId, ...clientContext },
76
+ },
77
+ };
67
78
  });
68
-
69
- return {
70
- data: {
71
- contracts,
72
- orgUnit,
73
- user,
74
- },
75
- clientContext: { cookie, userId, ...clientContext },
76
- };
77
79
  };
78
80
 
79
81
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -83,24 +85,25 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
83
85
 
84
86
  const OrgUnitDetailsPage = ({
85
87
  data,
86
- clientContext,
87
88
  hasError,
88
89
  error,
89
90
  }: OrgUnitDetailsPageData) => {
90
91
  return (
91
- <BuyerPortalProvider clientContext={clientContext}>
92
+ <>
92
93
  {hasError ? (
93
94
  <ErrorTabsLayout error={error} />
94
95
  ) : (
95
96
  <OrgUnitsDetailsLayout data={data} />
96
97
  )}
97
- </BuyerPortalProvider>
98
+ </>
98
99
  );
99
100
  };
100
101
 
101
- export default withErrorBoundary(OrgUnitDetailsPage, {
102
- tags: {
103
- component: "OrgUnitDetailsPage",
104
- errorType: "org_unit_details_error",
105
- },
106
- });
102
+ export default withProviders(
103
+ withErrorBoundary(OrgUnitDetailsPage, {
104
+ tags: {
105
+ component: "OrgUnitDetailsPage",
106
+ errorType: "org_unit_details_error",
107
+ },
108
+ })
109
+ );
@@ -5,16 +5,14 @@ import {
5
5
  getOrgUnitByIdService,
6
6
  searchOrgUnitsByNameService,
7
7
  } from "../features/org-units/services";
8
- import {
9
- BuyerPortalProvider,
10
- withErrorBoundary,
11
- } from "../features/shared/components";
8
+ import { withErrorBoundary } from "../features/shared/components";
12
9
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
13
10
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
14
11
  import {
15
12
  type ClientContext,
16
- getClientContext,
17
13
  withLoaderErrorBoundary,
14
+ withAuthLoader,
15
+ withProviders,
18
16
  } from "../features/shared/utils";
19
17
  import { getUserByIdService } from "../features/users/services";
20
18
 
@@ -22,7 +20,7 @@ import type {
22
20
  OrgUnitBasicData,
23
21
  OrgUnitSearchResponse,
24
22
  } from "../features/org-units/types";
25
- import type { LoaderData } from "../features/shared/types";
23
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
26
24
  import type { UserData } from "../features/users/types";
27
25
 
28
26
  export type OrgUnitsPageData = {
@@ -44,80 +42,82 @@ type OrgUnitsPageQuery = {
44
42
 
45
43
  const loaderFunction = async (
46
44
  data: LoaderData<OrgUnitsPageQuery>
47
- ): Promise<OrgUnitsPageData> => {
48
- const { cookie, customerId, userId, ...restClientContext } =
49
- await getClientContext(data);
50
-
45
+ ): Promise<AuthRouteProps<OrgUnitsPageData>> => {
51
46
  const { search, orgUnitId } = data.query;
52
47
 
53
- const currentOrgUnit = await getOrgUnitBasicDataService({
54
- id: data.query.orgUnitId,
55
- cookie,
56
- });
57
-
58
- const user = await getUserByIdService({ orgUnitId, userId, cookie });
48
+ return withAuthLoader(
49
+ data,
50
+ async ({ cookie, customerId, userId, ...restClientContext }) => {
51
+ const currentOrgUnit = await getOrgUnitBasicDataService({
52
+ id: data.query.orgUnitId,
53
+ cookie,
54
+ });
59
55
 
60
- const clientContext = {
61
- cookie,
62
- customerId,
63
- userId,
64
- ...restClientContext,
65
- };
56
+ const user = await getUserByIdService({ orgUnitId, userId, cookie });
66
57
 
67
- if (search) {
68
- return {
69
- data: await searchOrgUnitsByNameService({
70
- name: search,
71
- contractId: customerId,
58
+ const clientContext = {
72
59
  cookie,
73
- }),
74
- search,
75
- context: {
76
- clientContext,
77
- currentOrgUnit,
78
- currentUser: user,
79
- },
80
- };
81
- }
82
-
83
- const orgUnit = await getOrgUnitByIdService({
84
- id: orgUnitId,
85
- cookie,
86
- });
87
-
88
- if (orgUnit) {
89
- const orgUnitChildren = await getChildrenOrgUnitsService(
90
- orgUnitId,
91
- orgUnit.name,
92
- cookie
93
- );
94
-
95
- return {
96
- data: {
97
- organizationalUnits: orgUnitChildren.nodes ?? [],
98
- total: orgUnitChildren.totalChildrenQuantity ?? 0,
99
- },
100
- search: "",
101
- context: {
102
- clientContext,
103
- currentOrgUnit,
104
- currentUser: user,
105
- },
106
- };
107
- }
108
-
109
- data.res?.writeHead(302, { Location: "/pvt/organization-account" });
110
- data.res?.end();
111
-
112
- return {
113
- data: { organizationalUnits: [], total: 0 },
114
- search: "",
115
- context: {
116
- clientContext,
117
- currentOrgUnit,
118
- currentUser: user,
119
- },
120
- };
60
+ customerId,
61
+ userId,
62
+ ...restClientContext,
63
+ };
64
+
65
+ if (search) {
66
+ return {
67
+ data: await searchOrgUnitsByNameService({
68
+ name: search,
69
+ contractId: customerId,
70
+ cookie,
71
+ }),
72
+ search,
73
+ context: {
74
+ clientContext,
75
+ currentOrgUnit,
76
+ currentUser: user,
77
+ },
78
+ };
79
+ }
80
+
81
+ const orgUnit = await getOrgUnitByIdService({
82
+ id: orgUnitId,
83
+ cookie,
84
+ });
85
+
86
+ if (orgUnit) {
87
+ const orgUnitChildren = await getChildrenOrgUnitsService(
88
+ orgUnitId,
89
+ orgUnit.name,
90
+ cookie
91
+ );
92
+
93
+ return {
94
+ data: {
95
+ organizationalUnits: orgUnitChildren.nodes ?? [],
96
+ total: orgUnitChildren.totalChildrenQuantity ?? 0,
97
+ },
98
+ search: "",
99
+ context: {
100
+ clientContext,
101
+ currentOrgUnit,
102
+ currentUser: user,
103
+ },
104
+ };
105
+ }
106
+
107
+ data.res?.writeHead(302, { Location: "/pvt/organization-account" });
108
+ data.res?.end();
109
+
110
+ return {
111
+ data: { organizationalUnits: [], total: 0 },
112
+ search: "",
113
+ context: {
114
+ clientContext,
115
+ currentOrgUnit,
116
+ currentUser: user,
117
+ },
118
+ };
119
+ }
120
+ );
121
121
  };
122
122
 
123
123
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -125,27 +125,23 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
125
125
  redirectToError: true,
126
126
  });
127
127
 
128
- const OrgUnitsPage = ({
129
- data,
130
- search,
131
- context,
132
- hasError,
133
- error,
134
- }: OrgUnitsPageData) => {
128
+ const OrgUnitsPage = ({ data, search, hasError, error }: OrgUnitsPageData) => {
135
129
  return (
136
- <BuyerPortalProvider {...context}>
130
+ <>
137
131
  {hasError ? (
138
132
  <ErrorTabsLayout error={error} />
139
133
  ) : (
140
134
  <OrgUnitsLayout data={data} search={search} />
141
135
  )}
142
- </BuyerPortalProvider>
136
+ </>
143
137
  );
144
138
  };
145
139
 
146
- export default withErrorBoundary(OrgUnitsPage, {
147
- tags: {
148
- component: "OrgUnitsPage",
149
- errorType: "org_units_error",
150
- },
151
- });
140
+ export default withProviders(
141
+ withErrorBoundary(OrgUnitsPage, {
142
+ tags: {
143
+ component: "OrgUnitsPage",
144
+ errorType: "org_units_error",
145
+ },
146
+ })
147
+ );
@@ -5,23 +5,21 @@ import {
5
5
  getPaymentMethodsByUnitIdService,
6
6
  GetPaymentMethodsByUnitIdServiceProps,
7
7
  } from "../features/payment-methods/services";
8
- import {
9
- BuyerPortalProvider,
10
- withErrorBoundary,
11
- } from "../features/shared/components";
8
+ import { withErrorBoundary } from "../features/shared/components";
12
9
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
13
10
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
14
11
  import {
15
12
  type ClientContext,
16
- getClientContext,
17
13
  withLoaderErrorBoundary,
14
+ withAuthLoader,
15
+ withProviders,
18
16
  } from "../features/shared/utils";
19
17
  import { getUserByIdService } from "../features/users/services";
20
18
 
21
19
  import type { ContractData } from "../features/contracts/types";
22
20
  import type { OrgUnitBasicData } from "../features/org-units/types";
23
21
  import type { PaymentMethodData } from "../features/payment-methods/types";
24
- import type { LoaderData } from "../features/shared/types";
22
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
25
23
  import type { UserData } from "../features/users/types";
26
24
 
27
25
  export type PaymentMethodsPageData = {
@@ -44,40 +42,40 @@ export type PaymentMethodsPageQuery = GetPaymentMethodsByUnitIdServiceProps & {
44
42
 
45
43
  const loaderFunction = async (
46
44
  data: LoaderData<PaymentMethodsPageQuery>
47
- ): Promise<PaymentMethodsPageData> => {
45
+ ): Promise<AuthRouteProps<PaymentMethodsPageData>> => {
48
46
  const { contractId, orgUnitId, search } = data.query;
49
47
 
50
- const { cookie, userId, ...clientContext } = await getClientContext(data);
48
+ return withAuthLoader(data, async ({ cookie, userId, ...clientContext }) => {
49
+ const currentOrgUnit = await getOrgUnitBasicDataService({
50
+ id: orgUnitId,
51
+ cookie,
52
+ });
51
53
 
52
- const currentOrgUnit = await getOrgUnitBasicDataService({
53
- id: orgUnitId,
54
- cookie,
55
- });
54
+ const user = await getUserByIdService({ orgUnitId, userId, cookie });
56
55
 
57
- const user = await getUserByIdService({ orgUnitId, userId, cookie });
56
+ const contract = await getContractDetailsService({
57
+ contractId,
58
+ cookie,
59
+ unitId: orgUnitId,
60
+ });
58
61
 
59
- const contract = await getContractDetailsService({
60
- contractId,
61
- cookie,
62
- unitId: orgUnitId,
63
- });
62
+ const paymentMethods = await getPaymentMethodsByUnitIdService({
63
+ customerId: contractId,
64
+ unitId: currentOrgUnit.id,
65
+ cookie,
66
+ });
64
67
 
65
- const paymentMethods = await getPaymentMethodsByUnitIdService({
66
- customerId: contractId,
67
- unitId: currentOrgUnit.id,
68
- cookie,
68
+ return {
69
+ data: paymentMethods,
70
+ search: search ?? "",
71
+ context: {
72
+ clientContext: { cookie, userId, ...clientContext },
73
+ currentOrgUnit,
74
+ currentUser: user,
75
+ currentContract: contract,
76
+ },
77
+ };
69
78
  });
70
-
71
- return {
72
- data: paymentMethods,
73
- search: search ?? "",
74
- context: {
75
- clientContext: { cookie, userId, ...clientContext },
76
- currentOrgUnit,
77
- currentUser: user,
78
- currentContract: contract,
79
- },
80
- };
81
79
  };
82
80
 
83
81
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -87,22 +85,23 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
87
85
 
88
86
  const PaymentMethodsPage = ({
89
87
  data,
90
- context,
91
88
  hasError,
92
89
  error,
93
90
  }: PaymentMethodsPageData) => (
94
- <BuyerPortalProvider {...context}>
91
+ <>
95
92
  {hasError ? (
96
93
  <ErrorTabsLayout error={error} />
97
94
  ) : (
98
95
  <PaymentMethodsLayout data={data} />
99
96
  )}
100
- </BuyerPortalProvider>
97
+ </>
101
98
  );
102
99
 
103
- export default withErrorBoundary(PaymentMethodsPage, {
104
- tags: {
105
- component: "PaymentMethodsPage",
106
- errorType: "payment_methods_error",
107
- },
108
- });
100
+ export default withProviders(
101
+ withErrorBoundary(PaymentMethodsPage, {
102
+ tags: {
103
+ component: "PaymentMethodsPage",
104
+ errorType: "payment_methods_error",
105
+ },
106
+ })
107
+ );
@@ -1,22 +1,20 @@
1
1
  import { getContractDetailsService } from "../features/contracts/services";
2
2
  import { CustomFieldsLayout } from "../features/custom-fields/layouts";
3
3
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
4
- import {
5
- BuyerPortalProvider,
6
- withErrorBoundary,
7
- } from "../features/shared/components";
4
+ import { withErrorBoundary } from "../features/shared/components";
8
5
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
9
6
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
10
7
  import {
11
8
  ClientContext,
12
- getClientContext,
13
9
  withLoaderErrorBoundary,
10
+ withAuthLoader,
11
+ withProviders,
14
12
  } from "../features/shared/utils";
15
13
  import { getUserByIdService } from "../features/users/services";
16
14
 
17
15
  import type { ContractData } from "../features/contracts/types";
18
16
  import type { OrgUnitBasicData } from "../features/org-units/types";
19
- import type { LoaderData } from "../features/shared/types";
17
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
20
18
  import type { UserData } from "../features/users/types";
21
19
 
22
20
  export type PONumbersPageData = {
@@ -40,35 +38,37 @@ type OrgUnitsPageQuery = {
40
38
 
41
39
  const loaderFunction = async (
42
40
  data: LoaderData<OrgUnitsPageQuery>
43
- ): Promise<PONumbersPageData> => {
44
- const { customerId, cookie, userId, ...clientContext } =
45
- await getClientContext(data);
46
-
41
+ ): Promise<AuthRouteProps<PONumbersPageData>> => {
47
42
  const { contractId, orgUnitId } = data.query;
48
43
 
49
- const [orgUnit, contract, user] = await Promise.all([
50
- getOrgUnitBasicDataService({
51
- id: orgUnitId,
52
- cookie,
53
- }),
54
- getContractDetailsService({
55
- contractId,
56
- unitId: orgUnitId,
57
- cookie,
58
- }),
59
- getUserByIdService({ orgUnitId, userId, cookie }),
60
- ]);
44
+ return withAuthLoader(
45
+ data,
46
+ async ({ customerId, cookie, userId, ...clientContext }) => {
47
+ const [orgUnit, contract, user] = await Promise.all([
48
+ getOrgUnitBasicDataService({
49
+ id: orgUnitId,
50
+ cookie,
51
+ }),
52
+ getContractDetailsService({
53
+ contractId,
54
+ unitId: orgUnitId,
55
+ cookie,
56
+ }),
57
+ getUserByIdService({ orgUnitId, userId, cookie }),
58
+ ]);
61
59
 
62
- return {
63
- data: contract,
64
- context: {
65
- search: "",
66
- clientContext: { customerId, cookie, userId, ...clientContext },
67
- currentOrgUnit: orgUnit,
68
- currentContract: contract,
69
- currentUser: user,
70
- },
71
- };
60
+ return {
61
+ data: contract,
62
+ context: {
63
+ search: "",
64
+ clientContext: { customerId, cookie, userId, ...clientContext },
65
+ currentOrgUnit: orgUnit,
66
+ currentContract: contract,
67
+ currentUser: user,
68
+ },
69
+ };
70
+ }
71
+ );
72
72
  };
73
73
 
74
74
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -76,26 +76,23 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
76
76
  redirectToError: true,
77
77
  });
78
78
 
79
- const PONumbersPage = ({
80
- data,
81
- context,
82
- hasError,
83
- error,
84
- }: PONumbersPageData) => {
79
+ const PONumbersPage = ({ data, hasError, error }: PONumbersPageData) => {
85
80
  return (
86
- <BuyerPortalProvider {...context}>
81
+ <>
87
82
  {hasError ? (
88
83
  <ErrorTabsLayout error={error} />
89
84
  ) : (
90
85
  <CustomFieldsLayout data={data} customFieldsLabel="PO Number" />
91
86
  )}
92
- </BuyerPortalProvider>
87
+ </>
93
88
  );
94
89
  };
95
90
 
96
- export default withErrorBoundary(PONumbersPage, {
97
- tags: {
98
- component: "PONumbersPage",
99
- errorType: "po_numbers_error",
100
- },
101
- });
91
+ export default withProviders(
92
+ withErrorBoundary(PONumbersPage, {
93
+ tags: {
94
+ component: "PONumbersPage",
95
+ errorType: "po_numbers_error",
96
+ },
97
+ })
98
+ );