@agrada_digital/pbm 0.0.56 → 0.0.62

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/dist/index.js CHANGED
@@ -35,30 +35,87 @@ __export(index_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(index_exports);
37
37
 
38
+ // src/libs/zustand/usePBM.tsx
39
+ var import_zustand = require("zustand");
40
+ var import_react = require("zustand/react");
41
+ var initialPBMState = {
42
+ securityNumber: "",
43
+ state: "isEmpty",
44
+ availableDiscountSelected: {
45
+ quantity: 0,
46
+ discount: {
47
+ unit: 0,
48
+ total: 0
49
+ },
50
+ totalPrice: 0
51
+ },
52
+ targetProduct: null,
53
+ campaign: "pbm_campaign"
54
+ };
55
+ var createPBMStore = (set) => ({
56
+ ...initialPBMState,
57
+ setSecurityNumber: (securityNumber) => set({ securityNumber }),
58
+ setState: (state) => set({ state }),
59
+ setTargetProduct: (targetProduct) => set({ targetProduct }),
60
+ setAvailableDiscountSelected: (availableDiscount) => set({ availableDiscountSelected: availableDiscount }),
61
+ setUrlAcceptTerms: (urlAcceptTerms) => set({ urlAcceptTerms })
62
+ });
63
+ var pbmStore = (0, import_zustand.createStore)(createPBMStore);
64
+ function usePBMStore(selector) {
65
+ if (selector) {
66
+ return (0, import_react.useStore)(pbmStore, selector);
67
+ }
68
+ return (0, import_react.useStore)(pbmStore, (state) => state);
69
+ }
70
+
38
71
  // src/components/Header/index.tsx
39
72
  var import_jsx_runtime = require("react/jsx-runtime");
40
73
  function Header({ originalProductPrice }) {
74
+ const { targetProduct } = usePBMStore();
75
+ const Price = targetProduct?.listPrice || originalProductPrice;
76
+ const Discount = Price * ((targetProduct?.discountMax || 0) / 100);
41
77
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
42
78
  "header",
43
79
  {
44
- className: "flex items-center justify-between w-full p-0.5 rounded-full bg-[#44c2c0]/30",
80
+ className: "flex items-center justify-between w-full p-0.5 rounded-full bg-[#44c2c0]/30 mt-5",
45
81
  id: "header_pbm",
46
82
  children: [
47
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
83
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
48
84
  "span",
49
85
  {
50
- className: "py-1 px-6 rounded-full bg-[#44c2c0] shrink-0 text-white text-sm font-bold",
86
+ className: "py-1 px-6 rounded-full bg-[#44c2c0] shrink-0 text-white text-xl font-bold flex items-center justify-start gap-2 relative",
51
87
  "data-testid": "test_id_header_price",
52
88
  id: "header_price",
53
- children: Number(originalProductPrice)?.toLocaleString("pt-BR", {
54
- currency: "BRL",
55
- currencyDisplay: "symbol",
56
- currencySign: "standard",
57
- style: "currency"
58
- })
89
+ children: [
90
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "absolute -top-6 left-10 bg-emerald-500 px-4 py-1 rounded-t-2xl text-xs font-medium text-white", children: [
91
+ Discount.toLocaleString("pt-BR", {
92
+ currency: "BRL",
93
+ currencyDisplay: "symbol",
94
+ currencySign: "standard",
95
+ style: "currency"
96
+ }),
97
+ " OFF"
98
+ ] }),
99
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", { width: "32px", height: "32px", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [
100
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("g", { id: "SVGRepo_bgCarrier", strokeWidth: "0" }),
101
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("g", { id: "SVGRepo_tracerCarrier", strokeLinecap: "round", strokeLinejoin: "round" }),
102
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("g", { id: "SVGRepo_iconCarrier", children: [
103
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M10 8.99998C10.5523 8.99998 11 9.44769 11 9.99998C11 10.5523 10.5523 11 10 11C9.44775 11 9.00004 10.5523 9.00004 9.99998C9.00004 9.44769 9.44775 8.99998 10 8.99998Z", fill: "#fff" }),
104
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M13 14C13 14.5523 13.4478 15 14 15C14.5523 15 15 14.5523 15 14C15 13.4477 14.5523 13 14 13C13.4478 13 13 13.4477 13 14Z", fill: "#fff" }),
105
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M10.7071 14.7071L14.7071 10.7071C15.0977 10.3166 15.0977 9.6834 14.7071 9.29287C14.3166 8.90235 13.6835 8.90235 13.2929 9.29287L9.29293 13.2929C8.90241 13.6834 8.90241 14.3166 9.29293 14.7071C9.68346 15.0976 10.3166 15.0976 10.7071 14.7071Z", fill: "#fff" }),
106
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { fillRule: "evenodd", clipRule: "evenodd", d: "M16.3117 4.07145L15.1708 4.34503L14.5575 3.34485C13.3869 1.43575 10.6131 1.43575 9.44254 3.34485L8.82926 4.34503L7.68836 4.07145C5.51069 3.54925 3.54931 5.51063 4.07151 7.6883L4.34509 8.8292L3.34491 9.44248C1.43581 10.6131 1.43581 13.3869 3.34491 14.5575L4.34509 15.1708L4.07151 16.3117C3.54931 18.4893 5.51069 20.4507 7.68836 19.9285L8.82926 19.6549L9.44254 20.6551C10.6131 22.5642 13.3869 22.5642 14.5575 20.6551L15.1708 19.6549L16.3117 19.9285C18.4894 20.4507 20.4508 18.4893 19.9286 16.3117L19.655 15.1708L20.6552 14.5575C22.5643 13.3869 22.5643 10.6131 20.6552 9.44248L19.655 8.8292L19.9286 7.6883C20.4508 5.51063 18.4894 3.54925 16.3117 4.07145ZM11.1475 4.3903C11.5377 3.75393 12.4623 3.75393 12.8525 4.3903L13.8454 6.00951C14.0717 6.37867 14.51 6.56019 14.9311 6.45922L16.7781 6.01631C17.504 5.84225 18.1578 6.49604 17.9837 7.22193L17.5408 9.06894C17.4398 9.49003 17.6213 9.92827 17.9905 10.1546L19.6097 11.1475C20.2461 11.5377 20.2461 12.4623 19.6097 12.8525L17.9905 13.8453C17.6213 14.0717 17.4398 14.5099 17.5408 14.931L17.9837 16.778C18.1578 17.5039 17.504 18.1577 16.7781 17.9836L14.9311 17.5407C14.51 17.4398 14.0717 17.6213 13.8454 17.9904L12.8525 19.6097C12.4623 20.246 11.5377 20.246 11.1475 19.6097L10.1547 17.9904C9.92833 17.6213 9.49009 17.4398 9.069 17.5407L7.22199 17.9836C6.4961 18.1577 5.84231 17.5039 6.01637 16.778L6.45928 14.931C6.56026 14.5099 6.37873 14.0717 6.00957 13.8453L4.39036 12.8525C3.75399 12.4623 3.75399 11.5377 4.39036 11.1475L6.00957 10.1546C6.37873 9.92827 6.56026 9.49003 6.45928 9.06894L6.01637 7.22193C5.84231 6.49604 6.4961 5.84225 7.22199 6.01631L9.069 6.45922C9.49009 6.56019 9.92833 6.37867 10.1547 6.00951L11.1475 4.3903Z", fill: "#fff" })
107
+ ] })
108
+ ] }),
109
+ Number(Price - Discount)?.toLocaleString("pt-BR", {
110
+ currency: "BRL",
111
+ currencyDisplay: "symbol",
112
+ currencySign: "standard",
113
+ style: "currency"
114
+ })
115
+ ]
59
116
  }
60
117
  ),
61
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", { id: "header_title", className: "text-center w-full text-[#339c9b] font-bold text-xs px-4 md:text-sm", children: "Benef\xEDcio de Laborat\xF3rio" })
118
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", { id: "header_title", className: "text-center w-full text-[#339c9b] font-bold text-xs px-4 md:text-sm", children: "Desconto de Laborat\xF3rio" })
62
119
  ]
63
120
  }
64
121
  );
@@ -76,7 +133,7 @@ function Container({
76
133
  "main",
77
134
  {
78
135
  className: (0, import_classnames.default)({
79
- "flex flex-col items-center justify-center min-w-[var(--min-container)] max-w-[var(--max-container)] w-full h-auto rounded-2xl p-4 bg-gray-100 gap-4": variant === "main",
136
+ "border-3 border-[#44c2c0] flex flex-col items-center justify-center min-w-[var(--min-container)] max-w-[var(--max-container)] w-full h-auto rounded-2xl p-4 bg-gray-100 gap-4 relative": variant === "main",
80
137
  "w-full h-auto relative": variant === "simple"
81
138
  }),
82
139
  "data-testid": "test_id_container",
@@ -92,38 +149,6 @@ var Container_default = Container;
92
149
  var import_classnames2 = __toESM(require("classnames"));
93
150
  var import_react2 = require("react");
94
151
 
95
- // src/libs/zustand/usePBM.tsx
96
- var import_zustand = require("zustand");
97
- var import_react = require("zustand/react");
98
- var initialPBMState = {
99
- securityNumber: "",
100
- state: "isEmpty",
101
- availableDiscountSelected: {
102
- quantity: 0,
103
- discount: {
104
- unit: 0,
105
- total: 0
106
- },
107
- totalPrice: 0
108
- },
109
- targetProduct: null,
110
- campaign: "pbm_campaign"
111
- };
112
- var createPBMStore = (set) => ({
113
- ...initialPBMState,
114
- setSecurityNumber: (securityNumber) => set({ securityNumber }),
115
- setState: (state) => set({ state }),
116
- setTargetProduct: (targetProduct) => set({ targetProduct }),
117
- setAvailableDiscountSelected: (availableDiscount) => set({ availableDiscountSelected: availableDiscount })
118
- });
119
- var pbmStore = (0, import_zustand.createStore)(createPBMStore);
120
- function usePBMStore(selector) {
121
- if (selector) {
122
- return (0, import_react.useStore)(pbmStore, selector);
123
- }
124
- return (0, import_react.useStore)(pbmStore, (state) => state);
125
- }
126
-
127
152
  // src/services/get-product-by-ean.ts
128
153
  var import_js_cookie = __toESM(require("js-cookie"));
129
154
  var import_meta = {};
@@ -141,7 +166,7 @@ var GetProductByEAN = async ({ PRODUCT_EAN }) => {
141
166
  }
142
167
  });
143
168
  const dataResponse = await response.json();
144
- if (!dataResponse.sucesso) {
169
+ if (!dataResponse.success) {
145
170
  throw new Error(dataResponse.message || "Failed to fetch authorization");
146
171
  }
147
172
  return dataResponse;
@@ -150,45 +175,56 @@ var GetProductByEAN = async ({ PRODUCT_EAN }) => {
150
175
  // src/components/Footer/index.tsx
151
176
  var import_jsx_runtime3 = require("react/jsx-runtime");
152
177
  function Footer() {
153
- const [industryLogo, setIndustryLogo] = (0, import_react2.useState)(void 0);
154
- const { targetProduct } = usePBMStore();
178
+ const [industryLogo, setIndustryLogo] = (0, import_react2.useState)(null);
179
+ const { targetProduct, setTargetProduct, state } = usePBMStore();
155
180
  (0, import_react2.useEffect)(() => {
156
181
  const fetchProductByEan = async () => {
157
- if (!targetProduct) return;
182
+ if (!targetProduct?.ean) return;
158
183
  try {
159
184
  const response = await GetProductByEAN({ PRODUCT_EAN: targetProduct.ean });
160
- if (response.sucesso && response.dados) {
161
- setIndustryLogo(response.dados.pbm.imageLink);
185
+ if (response.success && response.data) {
186
+ setIndustryLogo(response.data.pbm.imageLink);
187
+ const { pbm, sku, ...targetProductNewData } = response.data;
188
+ setTargetProduct({
189
+ ...targetProduct,
190
+ ...targetProductNewData,
191
+ productId: Number(targetProductNewData.productId),
192
+ informativeMessage: pbm.informativeMessage ?? "",
193
+ discountMax: pbm.discountMax ?? 0
194
+ });
162
195
  }
163
196
  } catch (error) {
164
197
  console.error(error);
165
198
  }
166
199
  };
167
200
  fetchProductByEan();
168
- }, [targetProduct]);
169
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("footer", { className: "w-full h-auto relative", id: "footer_pbm", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: (0, import_classnames2.default)("flex items-center w-full h-auto gap-4", { "justify-center": industryLogo, "justify-start": !industryLogo }), children: [
170
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: "w-4/5 h-auto", children: [
171
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { className: "text-start font-semibold text-sm", children: "Economize com o benef\xEDcio do laborat\xF3rio." }),
172
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-start font-normal text-sm", children: "Este produto tem pre\xE7o exclusivo para clientes cadastrados no programa." })
201
+ }, [targetProduct?.ean]);
202
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("footer", { className: "w-full h-auto relative", id: "footer_pbm", children: [
203
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: (0, import_classnames2.default)("flex items-center w-full h-auto gap-4", { "justify-center": industryLogo, "justify-start": !industryLogo }), children: [
204
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: "w-4/5 h-auto", children: [
205
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { className: "text-start font-semibold text-sm", children: "Economize com o benef\xEDcio do laborat\xF3rio." }),
206
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-start font-normal text-sm", children: "Este produto tem pre\xE7o exclusivo para clientes cadastrados no programa." })
207
+ ] }),
208
+ industryLogo && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
209
+ "img",
210
+ {
211
+ src: industryLogo,
212
+ alt: "parceiro",
213
+ className: "w-1/5 min-w-20 h-auto aspect-auto rounded-xl",
214
+ loading: "eager",
215
+ id: "footer_industry_logo_pbm",
216
+ "data-testid": "footer_industry_logo_pbm"
217
+ }
218
+ )
173
219
  ] }),
174
- industryLogo && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
175
- "img",
176
- {
177
- src: industryLogo,
178
- alt: "parceiro",
179
- className: "w-1/5 min-w-20 h-auto aspect-auto",
180
- loading: "eager",
181
- id: "footer_industry_logo_pbm",
182
- "data-testid": "footer_industry_logo_pbm"
183
- }
184
- )
185
- ] }) });
220
+ state !== "isActivated" && targetProduct?.informativeMessage && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "text-start font-semibold text-sm", children: targetProduct?.informativeMessage })
221
+ ] });
186
222
  }
187
223
  var Footer_default = Footer;
188
224
 
189
225
  // src/schema/validation-schema.ts
190
226
  var import_zod = require("zod");
191
- var validationSchema = import_zod.z.strictObject({
227
+ var validationSchema = import_zod.z.object({
192
228
  securityNumber: import_zod.z.string({
193
229
  required_error: "CPF \xE9 obrigat\xF3rio."
194
230
  }).refine((doc) => {
@@ -198,7 +234,7 @@ var validationSchema = import_zod.z.strictObject({
198
234
  const replacedDoc = doc.replace(/\D/g, "");
199
235
  return !!Number(replacedDoc);
200
236
  }, "CPF deve conter apenas n\xFAmeros."),
201
- coupon: import_zod.z.string().min(0)
237
+ coupon: import_zod.z.string({ required_error: "Cupom / ID do Cart\xE3o obrigat\xF3rio." }).optional()
202
238
  });
203
239
 
204
240
  // src/utils/format.ts
@@ -216,10 +252,10 @@ var import_react_hook_form = require("react-hook-form");
216
252
  var import_lucide_react = require("lucide-react");
217
253
  var import_react3 = require("react");
218
254
 
219
- // src/services/validate-document.ts
255
+ // src/services/benefits-with-document.ts
220
256
  var import_js_cookie2 = __toESM(require("js-cookie"));
221
257
  var import_meta2 = {};
222
- var ValidateDocument = async ({ document, products }) => {
258
+ var BenefitsWithDocument = async ({ document: document2, products }) => {
223
259
  const API_URL = import_meta2.env.VITE_API_URL;
224
260
  if (!API_URL) {
225
261
  throw new Error("API URL is not defined in environment variables");
@@ -228,13 +264,13 @@ var ValidateDocument = async ({ document, products }) => {
228
264
  if (!AUTH_TOKEN) {
229
265
  throw new Error("Token is not defined in cookies or is expired");
230
266
  }
231
- const response = await fetch(`${API_URL}/transactions/validate`, {
267
+ const response = await fetch(`${API_URL}/products/benefitByDocument`, {
232
268
  method: "POST",
233
269
  headers: {
234
270
  Authorization: `Bearer ${AUTH_TOKEN}`,
235
271
  "Content-Type": "application/json"
236
272
  },
237
- body: JSON.stringify({ document, products })
273
+ body: JSON.stringify({ consumer: { document: document2 }, products })
238
274
  });
239
275
  const dataResponse = await response.json();
240
276
  if (!dataResponse.success) {
@@ -252,7 +288,7 @@ function Button(props) {
252
288
  {
253
289
  ...props,
254
290
  className: (0, import_classnames3.default)(
255
- "w-3xs cursor-pointer h-10 rounded-full bg-blue-500 hover:bg-blue-400 text-white text-sm font-semibold transition-colors",
291
+ "w-3xs cursor-pointer h-10 rounded-full bg-emerald-500 hover:bg-emerald-400 text-white text-sm font-semibold transition-colors",
256
292
  props.className
257
293
  ),
258
294
  children: props.children
@@ -264,37 +300,60 @@ var Button_default = Button;
264
300
  // src/components/Form/index.tsx
265
301
  var import_jsx_runtime5 = require("react/jsx-runtime");
266
302
  function Form({ setLoading }) {
267
- const { setSecurityNumber, setState, securityNumber, targetProduct } = usePBMStore();
303
+ const { setSecurityNumber, setState, securityNumber, targetProduct, setUrlAcceptTerms } = usePBMStore();
268
304
  const [showCoupoField, setShowCoupoField] = (0, import_react3.useState)(false);
269
305
  const {
270
306
  handleSubmit,
271
307
  register,
272
308
  setValue,
309
+ clearErrors,
310
+ unregister,
273
311
  formState: { errors }
274
312
  } = (0, import_react_hook_form.useForm)({
275
313
  resolver: (0, import_zod2.zodResolver)(validationSchema),
314
+ mode: "onSubmit",
276
315
  defaultValues: {
277
- securityNumber: securityNumber || ""
316
+ securityNumber: securityNumber || "",
317
+ coupon: ""
278
318
  }
279
319
  });
280
320
  const onSubmitDefault = async (values) => {
321
+ if (!showCoupoField) {
322
+ setValue("coupon", void 0, { shouldValidate: false });
323
+ }
281
324
  setLoading(true);
282
325
  try {
283
326
  if (targetProduct === null) {
284
327
  console.error("PBMLOG: Product is not defined!");
285
328
  return;
286
329
  }
287
- const response = await ValidateDocument({
330
+ if (!targetProduct.productId) {
331
+ console.error("PBMLOG: Product ID is not defined!");
332
+ return;
333
+ }
334
+ if (!targetProduct.listPrice) {
335
+ console.error("PBMLOG: List Price is not defined!");
336
+ return;
337
+ }
338
+ const response = await BenefitsWithDocument({
288
339
  document: values.securityNumber.replace(/\D/g, ""),
289
- products: [{ ean: targetProduct.ean, quantity: targetProduct.quantity }]
340
+ products: [{
341
+ productId: targetProduct.productId,
342
+ ean: targetProduct.ean,
343
+ requestedQuantity: 1,
344
+ listPrice: targetProduct.listPrice,
345
+ netPrice: targetProduct.netPrice ?? targetProduct.listPrice
346
+ }]
290
347
  });
291
348
  if (response.success) {
292
349
  const status = {
293
- "active": "isActivated",
294
- "nonexistent": "isInvalid"
350
+ "acceptance": "isInvalid",
351
+ "industry registration": "isRegistered",
352
+ "active": "isActivated"
295
353
  };
296
354
  setSecurityNumber(values.securityNumber);
297
- setState(status[response.data.process_platform.status]);
355
+ setState(status[response.data.product[0].statusCustumer]);
356
+ setUrlAcceptTerms(response.data.product[0].urlAcceptTerm || void 0);
298
357
  }
299
358
  } catch (error) {
300
359
  console.error("PBMLOG: Error validating document -", error);
@@ -310,7 +369,7 @@ function Form({ setLoading }) {
310
369
  onSubmit: handleSubmit(onSubmitDefault),
311
370
  className: (0, import_classnames4.default)(
312
371
  "w-full h-auto flex items-center justify-center mb-0 transition-all duration-150",
313
- { "mb-4": errors.securityNumber || errors.coupon, "gap-2": showCoupoField }
372
+ { "mb-4": errors.securityNumber || errors.coupon && showCoupoField, "gap-2": showCoupoField }
314
373
  ),
315
374
  id: "form_security_number_pbm",
316
375
  children: [
@@ -326,7 +385,7 @@ function Form({ setLoading }) {
326
385
  {
327
386
  type: "text",
328
387
  className: (0, import_classnames4.default)(
329
- "w-full h-8 bg-[#44c2c0]/20 rounded-s-full text-sm font-semibold focus:outline focus:outline-[#339c9b] focus:bg-[#44c2c0]/30 text-zinc-600 placeholder:text-zinc-600 px-4 placeholder:text-sm placeholder:font-semibold",
388
+ "w-full h-8 bg-white rounded-s-full text-sm font-semibold focus:outline focus:outline-[#339c9b] focus:bg-white text-zinc-600 placeholder:text-zinc-600 px-4 placeholder:text-sm placeholder:font-semibold",
330
389
  { "outline outline-red-600": errors.securityNumber, "rounded-full": showCoupoField }
331
390
  ),
332
391
  placeholder: "Digite seu CPF aqui...",
@@ -364,12 +423,12 @@ function Form({ setLoading }) {
364
423
  { "outline outline-red-600": errors.coupon, "rounded-full": showCoupoField }
365
424
  ),
366
425
  placeholder: "Cupom / ID do Cart\xE3o",
367
- required: true,
368
- maxLength: 14,
369
426
  ...register("coupon", {
427
+ required: showCoupoField ? "Cupom / ID do Cart\xE3o obrigat\xF3rio." : false,
428
+ shouldUnregister: !showCoupoField,
370
429
  onChange: (e) => {
371
430
  setValue("coupon", e.target.value, {
372
- shouldValidate: true
431
+ shouldValidate: showCoupoField
373
432
  });
374
433
  }
375
434
  }),
@@ -385,7 +444,7 @@ function Form({ setLoading }) {
385
444
  {
386
445
  type: "submit",
387
446
  className: (0, import_classnames4.default)(
388
- "bg-gray-400 w-1/5 h-8 flex items-center justify-center rounded-e-full cursor-pointer",
447
+ "bg-emerald-500 w-1/5 h-8 flex items-center justify-center rounded-e-full cursor-pointer",
389
448
  { "rounded-full": showCoupoField }
390
449
  ),
391
450
  id: "button_submit_security_number_pbm",
@@ -399,7 +458,14 @@ function Form({ setLoading }) {
399
458
  Button_default,
400
459
  {
401
460
  className: "bg-transparent p-0 pl-2 w-auto h-auto text-zinc-600 underline cursor-pointer hover:text-zinc-900 hover:bg-transparent flex items-center justify-start gap-1",
402
- onClick: () => setShowCoupoField(!showCoupoField),
461
+ onClick: () => {
462
+ const newValue = !showCoupoField;
463
+ setShowCoupoField(newValue);
464
+ if (!newValue) {
465
+ unregister("coupon");
466
+ clearErrors("coupon");
467
+ }
468
+ },
403
469
  id: "check_benefits_button",
404
470
  children: [
405
471
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: !showCoupoField ? "Possui cupom?" : "N\xE3o possui cupom?" }),
@@ -439,31 +505,6 @@ var Loading_default = Loading;
439
505
  // src/components/BenefitsTable/index.tsx
440
506
  var import_react5 = require("react");
441
507
 
442
- // src/mocks/benefits.ts
443
- var BENEFITS_ITEMS = [
444
- {
445
- id: 1,
446
- ean: "001",
447
- authorizedQuantity: 1,
448
- discountValue: 4800,
449
- discountPercentual: 2400
450
- },
451
- {
452
- id: 2,
453
- ean: "002",
454
- authorizedQuantity: 2,
455
- discountValue: 4800,
456
- discountPercentual: 2400
457
- },
458
- {
459
- id: 3,
460
- ean: "003",
461
- authorizedQuantity: 3,
462
- discountValue: 9400,
463
- discountPercentual: 4700
464
- }
465
- ];
466
-
467
508
  // src/components/UI/Title/index.tsx
468
509
  var import_classnames5 = __toESM(require("classnames"));
469
510
  var import_jsx_runtime7 = require("react/jsx-runtime");
@@ -488,13 +529,13 @@ var Title_default = Title;
488
529
  var import_lucide_react2 = require("lucide-react");
489
530
  var import_react4 = require("react");
490
531
  var import_jsx_runtime8 = require("react/jsx-runtime");
491
- function Item({ data, onChange, checked, originalProductPrice }) {
532
+ function Item({ data, onChange, checked }) {
492
533
  const { setAvailableDiscountSelected, securityNumber } = usePBMStore();
493
534
  const ID_INPUT = "unity_quantity_" + data.authorizedQuantity;
494
- const decimalDiscount = data.discountPercentual / 1e4;
495
- const unitDiscountValue = originalProductPrice * decimalDiscount;
535
+ const decimalDiscount = data.discountPercentual / 100;
536
+ const unitDiscountValue = data.grossPrice * decimalDiscount;
496
537
  const discountValue = unitDiscountValue * data.authorizedQuantity;
497
- const totalPriceProductWithDiscountBenefit = originalProductPrice * data.authorizedQuantity - discountValue;
538
+ const totalPriceProductWithDiscountBenefit = data.grossPrice * data.authorizedQuantity - discountValue;
498
539
  const updateStorageData = (0, import_react4.useCallback)(() => {
499
540
  if (checked) {
500
541
  const roundToTwoDecimals = (value) => Math.round(value * 100) / 100;
@@ -566,13 +607,104 @@ function Item({ data, onChange, checked, originalProductPrice }) {
566
607
  }
567
608
  var Item_default = Item;
568
609
 
610
+ // src/services/benefits-without-document.ts
611
+ var import_js_cookie3 = __toESM(require("js-cookie"));
612
+ var import_meta3 = {};
613
+ var CheckBenefistWithoutDocument = async ({ products }) => {
614
+ const API_URL = import_meta3.env.VITE_API_URL;
615
+ if (!API_URL) {
616
+ throw new Error("API URL is not defined in environment variables");
617
+ }
618
+ const AUTH_TOKEN = import_js_cookie3.default.get("pbm-token");
619
+ if (!AUTH_TOKEN) {
620
+ throw new Error("Token is not defined in cookies or is expired");
621
+ }
622
+ const response = await fetch(`${API_URL}/products/genericBenefit`, {
623
+ method: "POST",
624
+ headers: {
625
+ Authorization: `Bearer ${AUTH_TOKEN}`,
626
+ "Content-Type": "application/json"
627
+ },
628
+ body: JSON.stringify({ products })
629
+ });
630
+ const dataResponse = await response.json();
631
+ if (!dataResponse.success) {
632
+ throw new Error(dataResponse.message || "Failed to fetch benefits without document");
633
+ }
634
+ return dataResponse;
635
+ };
636
+
569
637
  // src/components/BenefitsTable/index.tsx
570
638
  var import_jsx_runtime9 = require("react/jsx-runtime");
571
- function BenefitsTable({
572
- originalProductPrice
573
- }) {
574
- const { securityNumber, setState } = usePBMStore();
639
+ function BenefitsTable() {
640
+ const { securityNumber, setState, targetProduct } = usePBMStore();
575
641
  const [selectedDiscout, setSelectedDiscount] = (0, import_react5.useState)(null);
642
+ const [loading, setLoading] = (0, import_react5.useState)(true);
643
+ const [benefitsItems, setBenefitsItems] = (0, import_react5.useState)();
644
+ (0, import_react5.useEffect)(() => {
645
+ const fetchDicountsWithoutDocument = async () => {
646
+ if (!targetProduct?.productId) {
647
+ console.error("PBMLOG: Product ID is not defined on targetProduct");
648
+ return;
649
+ }
650
+ if (!targetProduct.ean) {
651
+ console.error("PBMLOG: EAN is not defined on targetProduct");
652
+ return;
653
+ }
654
+ if (!targetProduct.listPrice) {
655
+ console.error("PBMLOG: List Price is not defined on targetProduct");
656
+ return;
657
+ }
658
+ if (!targetProduct.price) {
659
+ console.error("PBMLOG: Price is not defined on targetProduct");
660
+ return;
661
+ }
662
+ try {
663
+ const data = {
664
+ productId: Number(targetProduct.productId),
665
+ ean: targetProduct.ean,
666
+ requestedQuantity: 1,
667
+ listPrice: targetProduct.listPrice,
668
+ netPrice: targetProduct.price
669
+ };
670
+ const response = await CheckBenefistWithoutDocument({ products: [data] });
671
+ if (response.success && response.data) {
672
+ setBenefitsItems(response.data);
673
+ } else {
674
+ setBenefitsItems(void 0);
675
+ }
676
+ } catch (error) {
677
+ setBenefitsItems(void 0);
678
+ console.error(error);
679
+ } finally {
680
+ setLoading(false);
681
+ }
682
+ };
683
+ const fetchDiscountWithDocument = async () => {
684
+ console.log("consulta feita com documento");
685
+ };
686
+ securityNumber ? fetchDiscountWithDocument() : fetchDicountsWithoutDocument();
687
+ }, []);
688
+ if (loading) {
689
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("main", { className: "flex items-center justify-center gap-4", id: "loading_pbm", children: [
690
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
691
+ "div",
692
+ {
693
+ "data-testid": "test_id_spin",
694
+ className: "w-8 h-8 border-4 border-t-gray-700 border-gray-300 rounded-full animate-spin",
695
+ id: "loading_spin"
696
+ }
697
+ ),
698
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
699
+ "p",
700
+ {
701
+ className: "text-sm font-semibold text-start text-zinc-900",
702
+ id: "loading_label",
703
+ children: "Buscando beneficios dispon\xEDveis..."
704
+ }
705
+ )
706
+ ] });
707
+ }
576
708
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
577
709
  "section",
578
710
  {
@@ -580,31 +712,34 @@ function BenefitsTable({
580
712
  id: "benefits_table_pbm",
581
713
  children: [
582
714
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(Title_default, { children: "Descontos dispon\xEDveis:" }),
583
- /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
715
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
584
716
  "form",
585
717
  {
586
718
  className: "flex flex-col items-center justify-start w-full gap-3",
587
719
  id: "form_benefits_table_pbm",
588
- children: BENEFITS_ITEMS.map((item, index) => {
589
- const ID_INPUT = "unity_quantity_" + item.authorizedQuantity;
590
- return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
591
- Item_default,
592
- {
593
- data: item,
594
- checked: selectedDiscout === ID_INPUT,
595
- onChange: () => setSelectedDiscount(ID_INPUT),
596
- originalProductPrice
597
- },
598
- index
599
- );
600
- })
720
+ children: [
721
+ !benefitsItems && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { className: "text-sm font-semibold text-start text-zinc-900", id: "benefits_empty_pbm", children: "N\xE3o foi poss\xEDvel encontrar benef\xEDcios para esse produto." }),
722
+ benefitsItems && benefitsItems.map((item, index) => {
723
+ const ID_INPUT = "unity_quantity_" + item.authorizedQuantity;
724
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
725
+ Item_default,
726
+ {
727
+ data: item,
728
+ checked: selectedDiscout === ID_INPUT,
729
+ onChange: () => setSelectedDiscount(ID_INPUT)
730
+ },
731
+ index
732
+ );
733
+ }),
734
+ benefitsItems && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { className: "w-full text-sm font-semibold text-center text-zinc-600", id: "benefits_empty_pbm", children: benefitsItems[0].informativeMessage })
735
+ ]
601
736
  }
602
737
  ),
603
738
  !securityNumber && /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
604
739
  Button_default,
605
740
  {
606
741
  onClick: () => setState("isEmpty"),
607
- className: "bg-transparent p-0 pl-2 w-auto h-auto text-zinc-600 cursor-pointer hover:text-zinc-900 hover:bg-transparent text-start",
742
+ className: "bg-transparent p-0 w-auto h-auto text-zinc-600 cursor-pointer hover:text-zinc-900 hover:bg-transparent text-start",
608
743
  id: "unauthorized_benefits_button",
609
744
  children: [
610
745
  "Aten\xE7\xE3o: n\xE3o \xE9 poss\xEDvel utilizar os benef\xEDcos sem realizar a consulta do cpf, por favor",
@@ -651,8 +786,38 @@ var Text_default = Text;
651
786
  // src/components/Iframe/index.tsx
652
787
  var import_classnames7 = __toESM(require("classnames"));
653
788
  var import_lucide_react3 = require("lucide-react");
789
+ var import_react6 = require("react");
654
790
  var import_jsx_runtime11 = require("react/jsx-runtime");
655
791
  function Iframe({ url, title, openModal, setOpenModal }) {
792
+ const [_, setIframeError] = (0, import_react6.useState)(false);
793
+ const [showFallback, setShowFallback] = (0, import_react6.useState)(false);
794
+ (0, import_react6.useEffect)(() => {
795
+ if (openModal && url) {
796
+ setIframeError(false);
797
+ setShowFallback(false);
798
+ const timeout = setTimeout(() => {
799
+ const iframe = document.querySelector('iframe[src="' + url + '"]');
800
+ if (iframe) {
801
+ try {
802
+ const iframeDoc = iframe.contentDocument || iframe.contentWindow?.document;
803
+ if (!iframeDoc) {
804
+ setIframeError(true);
805
+ setShowFallback(true);
806
+ }
807
+ } catch (e) {
808
+ setIframeError(true);
809
+ setShowFallback(true);
810
+ }
811
+ }
812
+ }, 2e3);
813
+ return () => clearTimeout(timeout);
814
+ }
815
+ }, [openModal, url]);
816
+ const handleOpenInNewWindow = () => {
817
+ if (url) {
818
+ window.open(url, "_blank", "noopener,noreferrer");
819
+ }
820
+ };
656
821
  return /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(
657
822
  "main",
658
823
  {
@@ -689,7 +854,22 @@ function Iframe({ url, title, openModal, setOpenModal }) {
689
854
  children: "Fechar"
690
855
  }
691
856
  ) }),
692
- /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
857
+ showFallback ? /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)("div", { className: "w-4/5 h-[80%] bg-zinc-800 z-10 flex flex-col items-center justify-center p-8 rounded-lg border-2 border-yellow-500", children: [
858
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_lucide_react3.TriangleAlert, { size: 48, className: "text-yellow-500 mb-4" }),
859
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("h3", { className: "text-white text-xl font-bold mb-4 text-center", children: "N\xE3o foi poss\xEDvel carregar o conte\xFAdo" }),
860
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("p", { className: "text-white text-sm mb-6 text-center max-w-md", children: "O servidor bloqueou a exibi\xE7\xE3o deste conte\xFAdo em um iframe devido \xE0s pol\xEDticas de seguran\xE7a. Voc\xEA pode abrir o link em uma nova janela para continuar." }),
861
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)(
862
+ "button",
863
+ {
864
+ onClick: handleOpenInNewWindow,
865
+ className: "flex items-center gap-2 bg-emerald-500 hover:bg-emerald-600 text-white font-semibold px-6 py-3 rounded-lg transition-colors",
866
+ children: [
867
+ /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_lucide_react3.ExternalLink, { size: 20 }),
868
+ "Abrir em nova janela"
869
+ ]
870
+ }
871
+ )
872
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(
693
873
  "iframe",
694
874
  {
695
875
  src: url,
@@ -697,7 +877,11 @@ function Iframe({ url, title, openModal, setOpenModal }) {
697
877
  width: "80%",
698
878
  height: "80%",
699
879
  allowFullScreen: true,
700
- className: "z-10"
880
+ className: "z-10",
881
+ onError: () => {
882
+ setIframeError(true);
883
+ setShowFallback(true);
884
+ }
701
885
  }
702
886
  ),
703
887
  /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)("section", { className: "items-center justify-center flex flex-wrap gap-1 bg-zinc-800 z-10 w-4/5 py-2 px-4 rounded-ee-2xl rounded-es-2xl border-t-2 border-gray-100", children: [
@@ -716,10 +900,11 @@ function Iframe({ url, title, openModal, setOpenModal }) {
716
900
  var Iframe_default = Iframe;
717
901
 
718
902
  // src/components/SecurityNumberInvalid/index.tsx
719
- var import_react6 = require("react");
903
+ var import_react7 = require("react");
720
904
  var import_jsx_runtime12 = require("react/jsx-runtime");
721
905
  function SecurityNumberInvalid({ textColor }) {
722
- const [openModal, setOpenModal] = (0, import_react6.useState)(false);
906
+ const [openModal, setOpenModal] = (0, import_react7.useState)(false);
907
+ const { urlAcceptTerms } = usePBMStore();
723
908
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)(
724
909
  "section",
725
910
  {
@@ -741,7 +926,7 @@ function SecurityNumberInvalid({ textColor }) {
741
926
  /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
742
927
  Iframe_default,
743
928
  {
744
- url: "https://termo.azurewebsites.net/",
929
+ url: urlAcceptTerms || "",
745
930
  title: "Aceitar termos PBM",
746
931
  openModal,
747
932
  setOpenModal
@@ -754,7 +939,7 @@ function SecurityNumberInvalid({ textColor }) {
754
939
  var SecurityNumberInvalid_default = SecurityNumberInvalid;
755
940
 
756
941
  // src/PBM.tsx
757
- var import_react7 = require("react");
942
+ var import_react8 = require("react");
758
943
 
759
944
  // src/components/UI/Link/index.tsx
760
945
  var import_classnames8 = __toESM(require("classnames"));
@@ -815,12 +1000,12 @@ function SecurityNumberRegitered({ textColor }) {
815
1000
  var SecurityNumberRegitered_default = SecurityNumberRegitered;
816
1001
 
817
1002
  // src/services/authorization.ts
818
- var import_js_cookie3 = __toESM(require("js-cookie"));
819
- var import_meta3 = {};
1003
+ var import_js_cookie4 = __toESM(require("js-cookie"));
1004
+ var import_meta4 = {};
820
1005
  var GetAuthorization = async ({ clientID }) => {
821
- const API_URL = import_meta3.env.VITE_API_URL;
822
- const STORE_ID = import_meta3.env.VITE_STORE_ID;
823
- const STORE_NAME = import_meta3.env.VITE_STORE_NAME;
1006
+ const API_URL = import_meta4.env.VITE_API_URL;
1007
+ const STORE_ID = import_meta4.env.VITE_STORE_ID;
1008
+ const STORE_NAME = import_meta4.env.VITE_STORE_NAME;
824
1009
  if (!API_URL) {
825
1010
  throw new Error("API URL is not defined in environment variables");
826
1011
  }
@@ -833,22 +1018,22 @@ var GetAuthorization = async ({ clientID }) => {
833
1018
  "Content-Type": "application/json"
834
1019
  },
835
1020
  body: JSON.stringify({
836
- StoreID: STORE_ID,
837
- StoreName: STORE_NAME,
838
- ClientID: clientID
1021
+ storeId: STORE_ID,
1022
+ storeName: STORE_NAME,
1023
+ clientId: clientID
839
1024
  })
840
1025
  });
841
1026
  const dataResponse = await response.json();
842
1027
  if (!dataResponse.success) {
843
1028
  throw new Error(dataResponse.message || "Failed to fetch authorization");
844
1029
  }
845
- import_js_cookie3.default.set("pbm-token", dataResponse.data.Token, {
846
- expires: dataResponse.data.ExpiresIn / (60 * 60),
1030
+ import_js_cookie4.default.set("pbm-token", dataResponse.data.token, {
1031
+ expires: dataResponse.data.expiresIn / (60 * 60),
847
1032
  secure: true,
848
1033
  sameSite: "Strict"
849
1034
  });
850
- import_js_cookie3.default.set("pbm-token-refresh", dataResponse.data.RefreshToken, {
851
- expires: dataResponse.data.RefreshExpiresIn / (60 * 60),
1035
+ import_js_cookie4.default.set("pbm-token-refresh", dataResponse.data.refreshToken, {
1036
+ expires: dataResponse.data.refreshExpiresIn / (60 * 60),
852
1037
  secure: true,
853
1038
  sameSite: "Strict"
854
1039
  });
@@ -866,14 +1051,14 @@ function PBM({
866
1051
  const formatedOriginalProductPrice = Number(
867
1052
  String(originalProductPrice).replace(",", ".")
868
1053
  );
869
- const [loading, setLoading] = (0, import_react7.useState)(false);
1054
+ const [loading, setLoading] = (0, import_react8.useState)(false);
870
1055
  const { setState, state, setTargetProduct } = usePBMStore();
871
- (0, import_react7.useEffect)(() => {
1056
+ (0, import_react8.useEffect)(() => {
872
1057
  if (eanProduct) {
873
- setTargetProduct({ ean: eanProduct, quantity: 1 });
1058
+ setTargetProduct({ ean: eanProduct });
874
1059
  }
875
1060
  }, [eanProduct, setTargetProduct]);
876
- const handleAuthorizationRequest = (0, import_react7.useCallback)(async () => {
1061
+ const handleAuthorizationRequest = (0, import_react8.useCallback)(async () => {
877
1062
  try {
878
1063
  const response = await GetAuthorization({ clientID });
879
1064
  if (!response.success) {
@@ -883,7 +1068,7 @@ function PBM({
883
1068
  console.error("Error fetching authorization:", error);
884
1069
  }
885
1070
  }, [clientID]);
886
- (0, import_react7.useEffect)(() => {
1071
+ (0, import_react8.useEffect)(() => {
887
1072
  handleAuthorizationRequest();
888
1073
  }, [handleAuthorizationRequest]);
889
1074
  return /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(Container_default, { variant: "main", children: [
@@ -907,7 +1092,7 @@ function PBM({
907
1092
  state === "isEmpty" && loading && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Loading_default, {}),
908
1093
  state === "isInvalid" && !loading && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(SecurityNumberInvalid_default, {}),
909
1094
  state === "isRegistered" && !loading && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(SecurityNumberRegitered_default, {}),
910
- state === "isActivated" && !loading && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(BenefitsTable_default, { originalProductPrice: formatedOriginalProductPrice })
1095
+ state === "isActivated" && !loading && /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(BenefitsTable_default, {})
911
1096
  ] }),
912
1097
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Footer_default, {})
913
1098
  ] });