@vtex/faststore-plugin-buyer-portal 2.0.38 → 2.0.40
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.
- package/package.json +1 -1
- package/src/features/shared/clients/Client.ts +11 -2
- package/src/features/shared/clients/__tests__/Client.test.ts +84 -0
- package/src/features/shared/utils/constants.ts +1 -1
- package/src/features/users/components/CreateUserDrawerWithUsername/CreateUserDrawerWithUsername.tsx +2 -1
- package/src/features/users/components/UpdateUserDrawerWithUsername/UpdateUserDrawerWithUsername.tsx +2 -1
- package/src/features/users/components/UserFormFields/UserFormFields.tsx +21 -6
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import { toQueryParams } from "../utils/toQueryParams";
|
|
|
8
8
|
|
|
9
9
|
const BUYER_PORTAL_CLIENT_HEADER = "x-buyer-portal-client";
|
|
10
10
|
const BUYER_PORTAL_CLIENT = `organization-account/${CURRENT_VERSION}`;
|
|
11
|
+
const OPERATION_ID_HEADER = "x-operation-id";
|
|
11
12
|
|
|
12
13
|
interface RequestConfig<Input = unknown> {
|
|
13
14
|
url: string;
|
|
@@ -98,9 +99,14 @@ class Client {
|
|
|
98
99
|
}: RequestConfig<Input>): Promise<Return> {
|
|
99
100
|
// fetch joins names that differ only in case into one comma-separated
|
|
100
101
|
// value, which the BFF rejects.
|
|
101
|
-
const
|
|
102
|
-
|
|
102
|
+
const callerHeaderNames = Object.keys(headers).map((name) =>
|
|
103
|
+
name.toLowerCase()
|
|
103
104
|
);
|
|
105
|
+
const callerSetsClientHeader = callerHeaderNames.includes(
|
|
106
|
+
BUYER_PORTAL_CLIENT_HEADER
|
|
107
|
+
);
|
|
108
|
+
const callerSetsOperationId =
|
|
109
|
+
callerHeaderNames.includes(OPERATION_ID_HEADER);
|
|
104
110
|
|
|
105
111
|
const config: RequestInit = {
|
|
106
112
|
method,
|
|
@@ -110,6 +116,9 @@ class Client {
|
|
|
110
116
|
...(this.isBffClient && !callerSetsClientHeader
|
|
111
117
|
? { [BUYER_PORTAL_CLIENT_HEADER]: BUYER_PORTAL_CLIENT }
|
|
112
118
|
: {}),
|
|
119
|
+
...(this.isBffClient && !callerSetsOperationId
|
|
120
|
+
? { [OPERATION_ID_HEADER]: crypto.randomUUID() }
|
|
121
|
+
: {}),
|
|
113
122
|
...(headers.Cookie
|
|
114
123
|
? getCookieWithoutSessionObjects(headers.Cookie ?? "")
|
|
115
124
|
: {}),
|
|
@@ -12,6 +12,7 @@ import { CURRENT_VERSION } from "../../utils/constants";
|
|
|
12
12
|
import { Client, ClientError } from "../Client";
|
|
13
13
|
|
|
14
14
|
const CLIENT_HEADER = "x-buyer-portal-client";
|
|
15
|
+
const OPERATION_ID_HEADER = "x-operation-id";
|
|
15
16
|
|
|
16
17
|
function sentHeaders(fetchMock: ReturnType<typeof makeFetchMock>) {
|
|
17
18
|
const [, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
|
@@ -76,6 +77,89 @@ describe("Client", () => {
|
|
|
76
77
|
});
|
|
77
78
|
});
|
|
78
79
|
|
|
80
|
+
describe("x-operation-id", () => {
|
|
81
|
+
const UUID_V4 =
|
|
82
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
83
|
+
|
|
84
|
+
it("sends a UUID on BFF requests", async () => {
|
|
85
|
+
const fetchMock = makeFetchMock({ ok: true });
|
|
86
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
87
|
+
|
|
88
|
+
await new Client(getApiUrl()).get("units/unit-1/summary");
|
|
89
|
+
|
|
90
|
+
expect(sentHeaders(fetchMock)[OPERATION_ID_HEADER]).toMatch(UUID_V4);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("sends it next to the cookie and auth headers on server-side requests", async () => {
|
|
94
|
+
const fetchMock = makeFetchMock({ ok: true });
|
|
95
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
96
|
+
|
|
97
|
+
await new Client(getApiUrl()).get("granted", {
|
|
98
|
+
headers: {
|
|
99
|
+
Cookie: "VtexIdclientAutCookie_test-store=token-value;other=1",
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const headers = sentHeaders(fetchMock);
|
|
104
|
+
|
|
105
|
+
expect(headers[OPERATION_ID_HEADER]).toMatch(UUID_V4);
|
|
106
|
+
expect(headers).toMatchObject({
|
|
107
|
+
"VtexIdclientAutCookie_test-store": "token-value",
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("uses a different id on each request", async () => {
|
|
112
|
+
const fetchMock = makeFetchMock({ ok: true });
|
|
113
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
114
|
+
|
|
115
|
+
const client = new Client(getApiUrl());
|
|
116
|
+
await client.get("resource");
|
|
117
|
+
await client.get("resource");
|
|
118
|
+
|
|
119
|
+
const ids = fetchMock.mock.calls.map(
|
|
120
|
+
([, init]) =>
|
|
121
|
+
(init as RequestInit & { headers: Record<string, string> }).headers[
|
|
122
|
+
OPERATION_ID_HEADER
|
|
123
|
+
]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
expect(ids[0]).not.toBe(ids[1]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it.each(["", "https://test.vtex.com/api/", "https://api.example.com/"])(
|
|
130
|
+
"is not sent to base URL %j",
|
|
131
|
+
async (baseURL) => {
|
|
132
|
+
const fetchMock = makeFetchMock({ ok: true });
|
|
133
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
134
|
+
|
|
135
|
+
await new Client(baseURL).get("resource");
|
|
136
|
+
|
|
137
|
+
expect(sentHeaders(fetchMock)).not.toHaveProperty(OPERATION_ID_HEADER);
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
it.each([OPERATION_ID_HEADER, "X-Operation-Id"])(
|
|
142
|
+
"lets a header passed by the caller as %s win, sent once",
|
|
143
|
+
async (name) => {
|
|
144
|
+
const fetchMock = makeFetchMock({ ok: true });
|
|
145
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
146
|
+
|
|
147
|
+
await new Client(getApiUrl()).get("resource", {
|
|
148
|
+
headers: { [name]: "caller-id" },
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const headers = sentHeaders(fetchMock);
|
|
152
|
+
|
|
153
|
+
expect(new Headers(headers).get(OPERATION_ID_HEADER)).toBe("caller-id");
|
|
154
|
+
expect(
|
|
155
|
+
Object.keys(headers).filter(
|
|
156
|
+
(key) => key.toLowerCase() === OPERATION_ID_HEADER
|
|
157
|
+
)
|
|
158
|
+
).toHaveLength(1);
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
|
|
79
163
|
describe("x-buyer-portal-client", () => {
|
|
80
164
|
it("identifies the plugin on BFF requests", async () => {
|
|
81
165
|
const fetchMock = makeFetchMock({ ok: true });
|
package/src/features/users/components/CreateUserDrawerWithUsername/CreateUserDrawerWithUsername.tsx
CHANGED
|
@@ -92,6 +92,7 @@ export const CreateUserDrawerWithUsername = ({
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
const isFormValid = () => {
|
|
95
|
+
if (!name.trim()) return false;
|
|
95
96
|
if (!trimmedLogin) return false;
|
|
96
97
|
if (roles.length === 0) return false;
|
|
97
98
|
if (loginType === "email" && !isTransactionalEmailValid()) return false;
|
|
@@ -107,7 +108,7 @@ export const CreateUserDrawerWithUsername = ({
|
|
|
107
108
|
: trimmedLogin;
|
|
108
109
|
const base = {
|
|
109
110
|
login: payloadLogin,
|
|
110
|
-
name: name
|
|
111
|
+
name: name.trim(),
|
|
111
112
|
roles,
|
|
112
113
|
orgUnitId,
|
|
113
114
|
};
|
package/src/features/users/components/UpdateUserDrawerWithUsername/UpdateUserDrawerWithUsername.tsx
CHANGED
|
@@ -135,6 +135,7 @@ export const UpdateUserDrawerWithUsername = ({
|
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
const isFormValid = () => {
|
|
138
|
+
if (!name.trim()) return false;
|
|
138
139
|
if (!trimmedLogin) return false;
|
|
139
140
|
if (roles.length === 0) return false;
|
|
140
141
|
if (loginType === "email" && !isTransactionalEmailValid()) return false;
|
|
@@ -240,7 +241,7 @@ export const UpdateUserDrawerWithUsername = ({
|
|
|
240
241
|
|
|
241
242
|
updateUser({
|
|
242
243
|
userId,
|
|
243
|
-
name,
|
|
244
|
+
name: name.trim(),
|
|
244
245
|
email: emailForPayload,
|
|
245
246
|
phone: phoneForPayload,
|
|
246
247
|
roles: roleNamesForPayload,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
1
3
|
import { Alert, Skeleton } from "@faststore/ui";
|
|
2
4
|
|
|
3
5
|
import { ErrorMessage, Icon, InputText } from "../../../shared/components";
|
|
@@ -64,6 +66,7 @@ export const UserFormFields = ({
|
|
|
64
66
|
loginTestId,
|
|
65
67
|
}: UserFormFieldsProps) => {
|
|
66
68
|
const { t } = useLocalization();
|
|
69
|
+
const [isNameTouched, setIsNameTouched] = useState(false);
|
|
67
70
|
const {
|
|
68
71
|
login,
|
|
69
72
|
name,
|
|
@@ -86,6 +89,7 @@ export const UserFormFields = ({
|
|
|
86
89
|
!isEmpty &&
|
|
87
90
|
(loginType === "phone" || loginType === "username") &&
|
|
88
91
|
!isValidEmail(trimmedEmail);
|
|
92
|
+
const showNameError = (isTouched || isNameTouched) && !name.trim();
|
|
89
93
|
|
|
90
94
|
const rolesAttr =
|
|
91
95
|
mode === "create"
|
|
@@ -100,12 +104,23 @@ export const UserFormFields = ({
|
|
|
100
104
|
style={{ marginBottom: "1rem" }}
|
|
101
105
|
/>
|
|
102
106
|
) : (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
<>
|
|
108
|
+
<InputText
|
|
109
|
+
label={t("users.forms.fullName")}
|
|
110
|
+
value={name}
|
|
111
|
+
wrapperProps={{ style: { marginBottom: 16 } }}
|
|
112
|
+
hasError={showNameError}
|
|
113
|
+
onBlur={() => setIsNameTouched(true)}
|
|
114
|
+
onChange={(event) => {
|
|
115
|
+
setIsNameTouched(true);
|
|
116
|
+
updateField("name", event.target.value);
|
|
117
|
+
}}
|
|
118
|
+
/>
|
|
119
|
+
<ErrorMessage
|
|
120
|
+
show={showNameError}
|
|
121
|
+
message={t("users.validation.fullNameRequired")}
|
|
122
|
+
/>
|
|
123
|
+
</>
|
|
109
124
|
)}
|
|
110
125
|
|
|
111
126
|
{isFieldLoading ? (
|