@faststore/api 3.41.8 → 3.45.0

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 (130) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/__generated__/schema.d.ts +756 -0
  3. package/dist/cjs/src/__generated__/schema.js +1 -0
  4. package/dist/cjs/src/__generated__/schema.js.map +1 -1
  5. package/dist/cjs/src/index.d.ts +6 -1
  6. package/dist/cjs/src/platforms/errors.d.ts +5 -1
  7. package/dist/cjs/src/platforms/errors.js +9 -1
  8. package/dist/cjs/src/platforms/errors.js.map +1 -1
  9. package/dist/cjs/src/platforms/vtex/clients/commerce/Contract.d.ts +7 -0
  10. package/dist/cjs/src/platforms/vtex/clients/commerce/Contract.js +3 -0
  11. package/dist/cjs/src/platforms/vtex/clients/commerce/Contract.js.map +1 -0
  12. package/dist/cjs/src/platforms/vtex/clients/commerce/index.d.ts +39 -1
  13. package/dist/cjs/src/platforms/vtex/clients/commerce/index.js +120 -6
  14. package/dist/cjs/src/platforms/vtex/clients/commerce/index.js.map +1 -1
  15. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Address.d.ts +1 -1
  16. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Session.d.ts +1 -0
  17. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Unit.d.ts +18 -0
  18. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Unit.js +3 -0
  19. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Unit.js.map +1 -0
  20. package/dist/cjs/src/platforms/vtex/clients/commerce/types/VtexId.d.ts +11 -0
  21. package/dist/cjs/src/platforms/vtex/clients/commerce/types/VtexId.js +3 -0
  22. package/dist/cjs/src/platforms/vtex/clients/commerce/types/VtexId.js.map +1 -0
  23. package/dist/cjs/src/platforms/vtex/clients/index.d.ts +36 -0
  24. package/dist/cjs/src/platforms/vtex/clients/search/index.d.ts +2 -0
  25. package/dist/cjs/src/platforms/vtex/clients/search/index.js +8 -0
  26. package/dist/cjs/src/platforms/vtex/clients/search/index.js.map +1 -1
  27. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.d.ts +6 -0
  28. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.js +3 -0
  29. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.js.map +1 -0
  30. package/dist/cjs/src/platforms/vtex/index.d.ts +7 -1
  31. package/dist/cjs/src/platforms/vtex/index.js +3 -0
  32. package/dist/cjs/src/platforms/vtex/index.js.map +1 -1
  33. package/dist/cjs/src/platforms/vtex/resolvers/cancelOrder.d.ts +3 -0
  34. package/dist/cjs/src/platforms/vtex/resolvers/cancelOrder.js +13 -0
  35. package/dist/cjs/src/platforms/vtex/resolvers/cancelOrder.js.map +1 -0
  36. package/dist/cjs/src/platforms/vtex/resolvers/mutation.d.ts +2 -1
  37. package/dist/cjs/src/platforms/vtex/resolvers/mutation.js +2 -0
  38. package/dist/cjs/src/platforms/vtex/resolvers/mutation.js.map +1 -1
  39. package/dist/cjs/src/platforms/vtex/resolvers/query.d.ts +4 -1
  40. package/dist/cjs/src/platforms/vtex/resolvers/query.js +35 -0
  41. package/dist/cjs/src/platforms/vtex/resolvers/query.js.map +1 -1
  42. package/dist/cjs/src/platforms/vtex/resolvers/userOrder.d.ts +7 -0
  43. package/dist/cjs/src/platforms/vtex/resolvers/userOrder.js +161 -0
  44. package/dist/cjs/src/platforms/vtex/resolvers/userOrder.js.map +1 -0
  45. package/dist/cjs/src/platforms/vtex/resolvers/validateSession.d.ts +1 -1
  46. package/dist/cjs/src/platforms/vtex/resolvers/validateSession.js +92 -17
  47. package/dist/cjs/src/platforms/vtex/resolvers/validateSession.js.map +1 -1
  48. package/dist/cjs/src/platforms/vtex/utils/cookies.d.ts +2 -0
  49. package/dist/cjs/src/platforms/vtex/utils/cookies.js +23 -1
  50. package/dist/cjs/src/platforms/vtex/utils/cookies.js.map +1 -1
  51. package/dist/cjs/src/typeDefs/mutation.graphql +4 -0
  52. package/dist/cjs/src/typeDefs/query.graphql +64 -0
  53. package/dist/cjs/src/typeDefs/session.graphql +24 -0
  54. package/dist/cjs/src/typeDefs/userOrder.graphql +714 -0
  55. package/dist/esm/package.json +1 -1
  56. package/dist/esm/src/__generated__/schema.d.ts +756 -0
  57. package/dist/esm/src/__generated__/schema.js +1 -0
  58. package/dist/esm/src/__generated__/schema.js.map +1 -1
  59. package/dist/esm/src/index.d.ts +6 -1
  60. package/dist/esm/src/platforms/errors.d.ts +5 -1
  61. package/dist/esm/src/platforms/errors.js +6 -0
  62. package/dist/esm/src/platforms/errors.js.map +1 -1
  63. package/dist/esm/src/platforms/vtex/clients/commerce/Contract.d.ts +7 -0
  64. package/dist/esm/src/platforms/vtex/clients/commerce/Contract.js +2 -0
  65. package/dist/esm/src/platforms/vtex/clients/commerce/Contract.js.map +1 -0
  66. package/dist/esm/src/platforms/vtex/clients/commerce/index.d.ts +39 -1
  67. package/dist/esm/src/platforms/vtex/clients/commerce/index.js +121 -7
  68. package/dist/esm/src/platforms/vtex/clients/commerce/index.js.map +1 -1
  69. package/dist/esm/src/platforms/vtex/clients/commerce/types/Address.d.ts +1 -1
  70. package/dist/esm/src/platforms/vtex/clients/commerce/types/Session.d.ts +1 -0
  71. package/dist/esm/src/platforms/vtex/clients/commerce/types/Unit.d.ts +18 -0
  72. package/dist/esm/src/platforms/vtex/clients/commerce/types/Unit.js +2 -0
  73. package/dist/esm/src/platforms/vtex/clients/commerce/types/Unit.js.map +1 -0
  74. package/dist/esm/src/platforms/vtex/clients/commerce/types/VtexId.d.ts +11 -0
  75. package/dist/esm/src/platforms/vtex/clients/commerce/types/VtexId.js +2 -0
  76. package/dist/esm/src/platforms/vtex/clients/commerce/types/VtexId.js.map +1 -0
  77. package/dist/esm/src/platforms/vtex/clients/index.d.ts +36 -0
  78. package/dist/esm/src/platforms/vtex/clients/search/index.d.ts +2 -0
  79. package/dist/esm/src/platforms/vtex/clients/search/index.js +8 -0
  80. package/dist/esm/src/platforms/vtex/clients/search/index.js.map +1 -1
  81. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.d.ts +6 -0
  82. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.js +2 -0
  83. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.js.map +1 -0
  84. package/dist/esm/src/platforms/vtex/index.d.ts +7 -1
  85. package/dist/esm/src/platforms/vtex/index.js +3 -0
  86. package/dist/esm/src/platforms/vtex/index.js.map +1 -1
  87. package/dist/esm/src/platforms/vtex/resolvers/cancelOrder.d.ts +3 -0
  88. package/dist/esm/src/platforms/vtex/resolvers/cancelOrder.js +9 -0
  89. package/dist/esm/src/platforms/vtex/resolvers/cancelOrder.js.map +1 -0
  90. package/dist/esm/src/platforms/vtex/resolvers/mutation.d.ts +2 -1
  91. package/dist/esm/src/platforms/vtex/resolvers/mutation.js +2 -0
  92. package/dist/esm/src/platforms/vtex/resolvers/mutation.js.map +1 -1
  93. package/dist/esm/src/platforms/vtex/resolvers/query.d.ts +4 -1
  94. package/dist/esm/src/platforms/vtex/resolvers/query.js +36 -1
  95. package/dist/esm/src/platforms/vtex/resolvers/query.js.map +1 -1
  96. package/dist/esm/src/platforms/vtex/resolvers/userOrder.d.ts +7 -0
  97. package/dist/esm/src/platforms/vtex/resolvers/userOrder.js +158 -0
  98. package/dist/esm/src/platforms/vtex/resolvers/userOrder.js.map +1 -0
  99. package/dist/esm/src/platforms/vtex/resolvers/validateSession.d.ts +1 -1
  100. package/dist/esm/src/platforms/vtex/resolvers/validateSession.js +92 -17
  101. package/dist/esm/src/platforms/vtex/resolvers/validateSession.js.map +1 -1
  102. package/dist/esm/src/platforms/vtex/utils/cookies.d.ts +2 -0
  103. package/dist/esm/src/platforms/vtex/utils/cookies.js +20 -0
  104. package/dist/esm/src/platforms/vtex/utils/cookies.js.map +1 -1
  105. package/dist/esm/src/typeDefs/mutation.graphql +4 -0
  106. package/dist/esm/src/typeDefs/query.graphql +64 -0
  107. package/dist/esm/src/typeDefs/session.graphql +24 -0
  108. package/dist/esm/src/typeDefs/userOrder.graphql +714 -0
  109. package/package.json +2 -2
  110. package/src/__generated__/schema.ts +833 -0
  111. package/src/platforms/errors.ts +14 -1
  112. package/src/platforms/vtex/clients/commerce/Contract.ts +7 -0
  113. package/src/platforms/vtex/clients/commerce/index.ts +222 -10
  114. package/src/platforms/vtex/clients/commerce/types/Address.ts +1 -1
  115. package/src/platforms/vtex/clients/commerce/types/Session.ts +1 -0
  116. package/src/platforms/vtex/clients/commerce/types/Unit.ts +19 -0
  117. package/src/platforms/vtex/clients/commerce/types/VtexId.ts +11 -0
  118. package/src/platforms/vtex/clients/search/index.ts +17 -0
  119. package/src/platforms/vtex/clients/search/types/ProductCountResult.ts +6 -0
  120. package/src/platforms/vtex/index.ts +4 -0
  121. package/src/platforms/vtex/resolvers/cancelOrder.ts +20 -0
  122. package/src/platforms/vtex/resolvers/mutation.ts +2 -0
  123. package/src/platforms/vtex/resolvers/query.ts +72 -1
  124. package/src/platforms/vtex/resolvers/userOrder.ts +205 -0
  125. package/src/platforms/vtex/resolvers/validateSession.ts +123 -20
  126. package/src/platforms/vtex/utils/cookies.ts +25 -0
  127. package/src/typeDefs/mutation.graphql +4 -0
  128. package/src/typeDefs/query.graphql +64 -0
  129. package/src/typeDefs/session.graphql +24 -0
  130. package/src/typeDefs/userOrder.graphql +714 -0
@@ -0,0 +1,205 @@
1
+ import type { Resolver } from '..'
2
+ import type {
3
+ Maybe,
4
+ UserOrderCustomField,
5
+ UserOrderDeliveryOption,
6
+ } from '../../..'
7
+ import type { PromiseType } from '../../../typings'
8
+ import type { Query } from './query'
9
+ import { getLocalizedEstimates } from './shippingSLA'
10
+
11
+ type QueryUserOrder = PromiseType<ReturnType<typeof Query.userOrder>>
12
+
13
+ export type Root = QueryUserOrder
14
+
15
+ const deliveryChannelsMapping = {
16
+ ['pickup-in-point']: 'Pickup',
17
+ delivery: 'Delivery',
18
+ pickup: 'Pickup',
19
+ ['take-away']: 'Take away',
20
+ ['']: '',
21
+ } as const
22
+
23
+ export const UserOrder: Record<string, Resolver<Root>> = {
24
+ orderId: ({ orderId }) => orderId,
25
+ totals: ({ totals }) => totals,
26
+ items: ({ items }) => items,
27
+ shippingData: ({ shippingData }) => shippingData,
28
+ paymentData: ({ paymentData }) => paymentData,
29
+ deliveryOptionsData: (root) => {
30
+ const { shippingData, items } = root
31
+ const logisticsInfo = shippingData?.logisticsInfo || []
32
+ const addresses = shippingData?.selectedAddresses ||
33
+ shippingData?.availableAddresses || [shippingData?.address]
34
+
35
+ const groupedDeliveryOptions = logisticsInfo.reduce(
36
+ (acc, logisticsItem) => {
37
+ const selectedSla = logisticsItem?.selectedSla || '' // Normal / Express
38
+ const deliveryChannel =
39
+ logisticsItem?.selectedDeliveryChannel ||
40
+ logisticsItem?.deliveryChannel ||
41
+ ''
42
+ const deliveryCompany = logisticsItem?.deliveryCompany || '' // Correios / Fedex
43
+ const deliveryWindow = logisticsItem?.deliveryWindow
44
+ const pickupStoreInfo = logisticsItem?.pickupStoreInfo
45
+ const shippingEstimate = logisticsItem?.shippingEstimate || '' // 1bd
46
+ const friendlyShippingEstimate = logisticsItem?.shippingEstimate
47
+ ? getLocalizedEstimates(logisticsItem?.shippingEstimate) // e.g. transform 3bd into Up to 3 business days
48
+ : ''
49
+
50
+ const shippingEstimateDate = logisticsItem?.shippingEstimateDate || '' // 2023-10-02T00:00:00Z
51
+ const addressId = logisticsItem?.addressId || '' // 5230338247839
52
+
53
+ const itemIndex = logisticsItem?.itemIndex ?? 0
54
+ const item = items?.[itemIndex]
55
+ const seller = item?.seller || ''
56
+
57
+ const groupKey = `${selectedSla}|${deliveryChannel}|${deliveryCompany}|${seller}|${shippingEstimate}|${shippingEstimateDate}|${addressId}|${deliveryWindow?.startDateUtc}|${deliveryWindow?.endDateUtc}`
58
+
59
+ const address = addresses?.find(
60
+ (address) => address?.addressId === addressId
61
+ )
62
+
63
+ const friendlyDeliveryOptionName =
64
+ `${deliveryChannelsMapping[deliveryChannel as keyof typeof deliveryChannelsMapping] || ''} ${friendlyShippingEstimate} ${address?.neighborhood ? `to ${address?.neighborhood}` : ''}`.trim()
65
+
66
+ // TODO check other totals like bundleItems etc
67
+ const itemTotal =
68
+ (item?.sellingPrice ?? 0) * (item?.quantity ?? 0) + (item?.tax ?? 0)
69
+
70
+ if (!acc[groupKey]) {
71
+ acc[groupKey] = {
72
+ // static fields
73
+ selectedSla,
74
+ deliveryChannel,
75
+ deliveryCompany,
76
+ deliveryWindow,
77
+ shippingEstimate,
78
+ shippingEstimateDate,
79
+ seller,
80
+ friendlyShippingEstimate,
81
+ friendlyDeliveryOptionName,
82
+ address,
83
+ pickupStoreInfo,
84
+ // dynamic fields
85
+ quantityOfDifferentItems: 0,
86
+ total: 0,
87
+ items: [],
88
+ }
89
+ }
90
+
91
+ if (acc[groupKey]) {
92
+ acc[groupKey].items?.push({
93
+ id: item?.id || '',
94
+ name: item?.name || '',
95
+ quantity: item?.quantity || 0,
96
+ price: item?.price || 0,
97
+ imageUrl: item?.imageUrl || '',
98
+ tax: item?.tax || 0,
99
+ total: itemTotal,
100
+ })
101
+
102
+ acc[groupKey].quantityOfDifferentItems =
103
+ (acc[groupKey].quantityOfDifferentItems ?? 0) + 1
104
+ acc[groupKey].total = (acc[groupKey].total ?? 0) + itemTotal
105
+ }
106
+
107
+ return acc
108
+ },
109
+ {} as Record<string, UserOrderDeliveryOption>
110
+ )
111
+
112
+ const deliveryOptions = Object.values(groupedDeliveryOptions)
113
+
114
+ const contactInformation = shippingData?.contactInformation?.[0]
115
+ const contact = {
116
+ name:
117
+ `${contactInformation?.firstName ?? ''} ${contactInformation?.lastName ?? ''}`.trim() ||
118
+ deliveryOptions?.[0]?.address?.receiverName ||
119
+ '',
120
+ email: contactInformation?.email || '',
121
+ phone: contactInformation?.phone || '',
122
+ }
123
+
124
+ return {
125
+ deliveryOptions,
126
+ contact,
127
+ }
128
+ },
129
+ customFields: (root) => {
130
+ const customFields = root?.customData?.customFields || []
131
+ return Object.values(
132
+ customFields.reduce(
133
+ (
134
+ acc: Record<
135
+ string,
136
+ { type: string; id: string; fields: UserOrderCustomField['fields'] }
137
+ >,
138
+ entry: Maybe<UserOrderCustomField>
139
+ ) => {
140
+ const type = entry?.linkedEntity?.type || ''
141
+ const id = entry?.linkedEntity?.id || ''
142
+ const key = `${type}|${id || ''}`
143
+ if (!acc[key]) {
144
+ acc[key] = { type, id, fields: [] }
145
+ }
146
+ acc[key].fields.push(...(entry?.fields || []))
147
+ return acc
148
+ },
149
+ {}
150
+ )
151
+ )
152
+ // Example of custom fields
153
+ // return [
154
+ // {
155
+ // type: 'item',
156
+ // id: '9009169',
157
+ // fields: [
158
+ // {
159
+ // name: 'costCenter',
160
+ // value: 'CC1',
161
+ // refId: 'externalId',
162
+ // },
163
+ // ],
164
+ // },
165
+ // {
166
+ // type: 'item',
167
+ // id: 'ECEAC03EBA2B4BBFA7E899CD4CA5121B',
168
+ // fields: [
169
+ // {
170
+ // name: 'costCenter',
171
+ // value: 'CC2',
172
+ // refId: 'externalId',
173
+ // },
174
+ // ],
175
+ // },
176
+ // {
177
+ // type: 'address',
178
+ // id: 'work-A1',
179
+ // fields: [
180
+ // {
181
+ // name: 'desktop',
182
+ // value: 'A1',
183
+ // refId: 'externalId',
184
+ // },
185
+ // ],
186
+ // },
187
+ // {
188
+ // type: 'order',
189
+ // id: '',
190
+ // fields: [
191
+ // {
192
+ // name: 'poNumber',
193
+ // value: '111222333',
194
+ // refId: 'externalId',
195
+ // },
196
+ // {
197
+ // name: 'release',
198
+ // value: '123',
199
+ // refId: 'externalId',
200
+ // },
201
+ // ],
202
+ // },
203
+ // ]
204
+ },
205
+ }
@@ -1,29 +1,83 @@
1
1
  import deepEquals from 'fast-deep-equal'
2
2
 
3
- import ChannelMarshal from '../utils/channel'
3
+ import { parse } from 'cookie'
4
4
  import type { Context } from '..'
5
5
  import type {
6
6
  MutationValidateSessionArgs,
7
7
  StoreMarketingData,
8
8
  StoreSession,
9
9
  } from '../../../__generated__/schema'
10
+ import ChannelMarshal from '../utils/channel'
11
+ import { parseJwt } from '../utils/cookies'
12
+
13
+ async function getPreciseLocationData(
14
+ clients: Context['clients'],
15
+ country: string,
16
+ postalCode: string
17
+ ) {
18
+ try {
19
+ const address = await clients.commerce.checkout.address({
20
+ postalCode,
21
+ country,
22
+ })
23
+
24
+ const [longitude, latitude] = address.geoCoordinates
25
+ return { city: address.city, geoCoordinates: { latitude, longitude } }
26
+ } catch (err) {
27
+ console.error(
28
+ `Error while getting geo coordinates for the current postal code (${postalCode}) and country (${country}).\n`
29
+ )
30
+
31
+ throw err
32
+ }
33
+ }
10
34
 
11
35
  export const validateSession = async (
12
36
  _: any,
13
37
  { session: oldSession, search }: MutationValidateSessionArgs,
14
- { clients }: Context
38
+ { clients, headers, account }: Context
15
39
  ): Promise<StoreSession | null> => {
16
40
  const channel = ChannelMarshal.parse(oldSession.channel ?? '')
17
41
  const postalCode = String(oldSession.postalCode ?? '')
18
- const geoCoordinates = oldSession.geoCoordinates ?? null
19
-
20
42
  const country = oldSession.country ?? ''
43
+ let city = oldSession.city ?? null
44
+ let geoCoordinates = oldSession.geoCoordinates ?? null
21
45
 
46
+ // Update location data if postal code and country are provided
47
+ const shouldGetPreciseLocation = !city || !geoCoordinates
48
+ if (shouldGetPreciseLocation && postalCode !== '' && country !== '') {
49
+ const preciseLocation = await getPreciseLocationData(
50
+ clients,
51
+ country,
52
+ postalCode
53
+ )
54
+ city = preciseLocation.city
55
+ geoCoordinates = preciseLocation.geoCoordinates
56
+ }
57
+
58
+ /**
59
+ * The Session Manager API (https://developers.vtex.com/docs/api-reference/session-manager-api#patch-/api/sessions) adds the query params to the session public namespace.
60
+ * This is used by Checkout (checkout-session) and Intelligent Search (search-session)
61
+ */
22
62
  const params = new URLSearchParams(search)
23
63
  const salesChannel = params.get('sc') ?? channel.salesChannel
24
-
25
64
  params.set('sc', salesChannel)
26
65
 
66
+ if (!!postalCode) {
67
+ params.set('postalCode', postalCode)
68
+ }
69
+
70
+ if (!!country) {
71
+ params.set('country', country)
72
+ }
73
+
74
+ if (!!geoCoordinates) {
75
+ params.set(
76
+ 'geoCoordinates',
77
+ `${geoCoordinates.latitude},${geoCoordinates.longitude}`
78
+ )
79
+ }
80
+
27
81
  const { marketingData: oldMarketingData } = oldSession
28
82
 
29
83
  const marketingData: StoreMarketingData = {
@@ -36,24 +90,56 @@ export const validateSession = async (
36
90
  utmiPart: params.get('utmi_pc') ?? oldMarketingData?.utmiPart ?? '',
37
91
  }
38
92
 
39
- const [regionData, sessionData] = await Promise.all([
40
- postalCode || geoCoordinates
41
- ? clients.commerce.checkout.region({
42
- postalCode,
43
- geoCoordinates,
44
- country,
45
- salesChannel,
46
- })
47
- : Promise.resolve(null),
93
+ const authCookie = parse(headers?.cookie ?? '')?.[
94
+ 'VtexIdclientAutCookie_' + account
95
+ ]
96
+ const jwt = parseJwt(authCookie)
97
+
98
+ const isRepresentative = jwt?.isRepresentative
99
+ const userId = jwt?.userId
100
+ const customerId = jwt?.customerId
101
+
102
+ const [sessionData, vtexid, unit, contract, user] = await Promise.all([
48
103
  clients.commerce.session(params.toString()).catch(() => null),
104
+
105
+ isRepresentative
106
+ ? clients.commerce.vtexid.validate().catch(() => null)
107
+ : Promise.resolve(null),
108
+
109
+ isRepresentative
110
+ ? clients.commerce.units.getUnitByUserId({ userId }).catch(() => null)
111
+ : Promise.resolve(null),
112
+
113
+ isRepresentative
114
+ ? clients.commerce.masterData
115
+ .getContractById({ contractId: customerId })
116
+ .catch(() => null)
117
+ : Promise.resolve(null),
118
+
119
+ isRepresentative
120
+ ? clients.commerce.licenseManager
121
+ .getUserById({ userId })
122
+ .catch(() => null)
123
+ : Promise.resolve(null),
49
124
  ])
50
125
 
51
126
  const profile = sessionData?.namespaces.profile ?? null
52
127
  const store = sessionData?.namespaces.store ?? null
53
128
  const authentication = sessionData?.namespaces.authentication ?? null
54
- const region = regionData?.[0]
129
+ const checkout = sessionData?.namespaces.checkout ?? null
130
+
55
131
  // Set seller only if it's inside a region
56
- const seller = region?.sellers.find((seller) => channel.seller === seller.id)
132
+ let seller
133
+ if (!!channel.seller && (postalCode || geoCoordinates)) {
134
+ const regionData = await clients.commerce.checkout.region({
135
+ postalCode,
136
+ geoCoordinates,
137
+ country,
138
+ salesChannel,
139
+ })
140
+ const region = regionData?.[0]
141
+ seller = region?.sellers.find((seller) => channel.seller === seller.id)
142
+ }
57
143
 
58
144
  const newSession = {
59
145
  ...oldSession,
@@ -64,13 +150,28 @@ export const validateSession = async (
64
150
  country: store?.countryCode?.value ?? oldSession.country,
65
151
  channel: ChannelMarshal.stringify({
66
152
  salesChannel: store?.channel?.value ?? channel.salesChannel,
67
- regionId: region?.id ?? channel.regionId,
153
+ regionId: checkout?.regionId?.value ?? channel.regionId,
68
154
  seller: seller?.id,
69
155
  hasOnlyDefaultSalesChannel: !store?.channel?.value,
70
156
  }),
71
- b2b: {
72
- customerId: authentication?.customerId?.value ?? '',
73
- },
157
+ b2b: isRepresentative
158
+ ? {
159
+ customerId:
160
+ authentication?.customerId?.value ??
161
+ vtexid?.customerId ??
162
+ customerId ??
163
+ '',
164
+ isRepresentative:
165
+ vtexid?.isRepresentative ?? isRepresentative ?? false,
166
+ unitName: unit?.name ?? vtexid?.unitName ?? '',
167
+ unitId: unit?.id ?? '',
168
+ isCorporate: contract?.isCorporate ?? false,
169
+ corporateName: contract?.corporateName ?? '',
170
+ firstName: contract?.firstName ?? '',
171
+ lastName: contract?.lastName ?? '',
172
+ userName: user?.name ?? '',
173
+ }
174
+ : null,
74
175
  marketingData,
75
176
  person: profile?.id
76
177
  ? {
@@ -80,6 +181,8 @@ export const validateSession = async (
80
181
  familyName: profile.lastName?.value ?? '',
81
182
  }
82
183
  : null,
184
+ geoCoordinates,
185
+ city,
83
186
  }
84
187
 
85
188
  if (deepEquals(oldSession, newSession)) {
@@ -1,3 +1,4 @@
1
+ import { parse } from 'cookie'
1
2
  import type { Context } from '../index'
2
3
 
3
4
  export interface ContextForCookies {
@@ -96,6 +97,23 @@ export const getWithCookie = (ctx: ContextForCookies) =>
96
97
  }
97
98
  }
98
99
 
100
+ export const getWithAutCookie = (ctx: ContextForCookies) => {
101
+ const withCookie = getWithCookie(ctx)
102
+
103
+ return function withAutCookie(forwardedHost: string, account: string) {
104
+ const headers: HeadersInit = withCookie({
105
+ 'content-type': 'application/json',
106
+ 'X-FORWARDED-HOST': forwardedHost,
107
+ })
108
+
109
+ const cookies = parse(ctx?.headers?.cookie ?? '')
110
+ const VtexIdclientAutCookie = cookies[`VtexIdclientAutCookie_${account}`]
111
+ headers['VtexIdclientAutCookie'] = VtexIdclientAutCookie
112
+
113
+ return headers
114
+ }
115
+ }
116
+
99
117
  /**
100
118
  * This function updates the cookie value based on its key
101
119
  *
@@ -124,3 +142,10 @@ export const updatesCookieValueByKey = (
124
142
  // add new storage cookie to the original list of cookies
125
143
  return `${existingCookies};${storageCookieKey}=${storageCookieValue}`
126
144
  }
145
+
146
+ export function parseJwt(token: string) {
147
+ if (!token) {
148
+ return null
149
+ }
150
+ return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
151
+ }
@@ -11,4 +11,8 @@ type Mutation {
11
11
  Subscribes a new person to the newsletter list.
12
12
  """
13
13
  subscribeToNewsletter(data: IPersonNewsletter!): PersonNewsletter
14
+ """
15
+ Cancels user order
16
+ """
17
+ cancelOrder(data: IUserOrderCancel!): UserOrderCancel
14
18
  }
@@ -199,6 +199,13 @@ input IGeoCoordinates {
199
199
  longitude: Float!
200
200
  }
201
201
 
202
+ type ProductCountResult {
203
+ """
204
+ Total product count.
205
+ """
206
+ total: Int!
207
+ }
208
+
202
209
  type Query {
203
210
  """
204
211
  Returns the details of a product based on the specified locator.
@@ -349,6 +356,63 @@ type Query {
349
356
  id: String!
350
357
  ): Profile
351
358
  @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
359
+
360
+ """
361
+ Returns the total product count information based on a specific location accessible through the VTEX segment cookie.
362
+ """
363
+ productCount(
364
+ """
365
+ Search term.
366
+ """
367
+ term: String
368
+ ): ProductCountResult
369
+ @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
370
+
371
+ """
372
+ Returns information about the Details of an User Order.
373
+ """
374
+ userOrder(
375
+ """
376
+ Identifier for the order.
377
+ """
378
+ orderId: String!
379
+ ): UserOrder
380
+ @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
381
+
382
+ """
383
+ Returns information about the list of Orders that the User can view.
384
+ """
385
+ listUserOrders(
386
+ """
387
+ Page number of the list of orders.
388
+ """
389
+ page: Int
390
+ """
391
+ Quantity of items per page of the list of orders.
392
+ """
393
+ perPage: Int
394
+ """
395
+ Status of the list of orders.
396
+ """
397
+ status: [String]
398
+ """
399
+ Initial date of the list of orders.
400
+ """
401
+ dateInitial: String
402
+ """
403
+ Final date of the list of orders.
404
+ """
405
+ dateFinal: String
406
+ """
407
+ Text used to full text filter of the list of orders.
408
+ """
409
+ text: String
410
+ """
411
+ Client email used to filter of the list of orders.
412
+ """
413
+ clientEmail: String
414
+ ): UserOrderListResult
415
+ @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
352
416
  }
353
417
 
354
418
  """
@@ -159,6 +159,10 @@ type StoreSession {
159
159
  """
160
160
  addressType: String
161
161
  """
162
+ Session city.
163
+ """
164
+ city: String
165
+ """
162
166
  Session postal code.
163
167
  """
164
168
  postalCode: String
@@ -182,10 +186,26 @@ type StoreSession {
182
186
 
183
187
  type StoreB2B {
184
188
  customerId: String!
189
+ isRepresentative: Boolean
190
+ unitName: String
191
+ unitId: String
192
+ isCorporate: Boolean
193
+ corporateName: String
194
+ firstName: String
195
+ lastName: String
196
+ userName: String
185
197
  }
186
198
 
187
199
  input IStoreB2B {
188
200
  customerId: String!
201
+ isRepresentative: Boolean
202
+ unitName: String
203
+ unitId: String
204
+ isCorporate: Boolean
205
+ corporateName: String
206
+ firstName: String
207
+ lastName: String
208
+ userName: String
189
209
  }
190
210
 
191
211
  """
@@ -217,6 +237,10 @@ input IStoreSession {
217
237
  """
218
238
  addressType: String
219
239
  """
240
+ Session input city.
241
+ """
242
+ city: String
243
+ """
220
244
  Session input postal code.
221
245
  """
222
246
  postalCode: String