@faststore/api 1.6.17 → 1.6.18

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.
@@ -1,5 +1,7 @@
1
1
  import { validateCart } from './validateCart'
2
+ import { updateSession } from './updateSession'
2
3
 
3
4
  export const Mutation = {
4
5
  validateCart,
6
+ updateSession,
5
7
  }
@@ -0,0 +1,26 @@
1
+ import type { Context } from '..'
2
+ import type {
3
+ MutationUpdateSessionArgs,
4
+ StoreSession,
5
+ } from '../../../__generated__/schema'
6
+ import ChannelMarshal from '../utils/channel'
7
+
8
+ export const updateSession = async (
9
+ _: any,
10
+ { session }: MutationUpdateSessionArgs,
11
+ { clients }: Context
12
+ ): Promise<StoreSession> => {
13
+ const channel = ChannelMarshal.parse(session.channel ?? '')
14
+ const regionData = await clients.commerce.checkout.region({
15
+ postalCode: String(session.postalCode ?? '').replace(/\D/g, ''),
16
+ country: session.country ?? '',
17
+ })
18
+
19
+ return {
20
+ ...session,
21
+ channel: ChannelMarshal.stringify({
22
+ ...channel,
23
+ regionId: regionData?.[0]?.id,
24
+ }),
25
+ }
26
+ }
@@ -0,0 +1,25 @@
1
+ export interface Channel {
2
+ regionId?: string
3
+ salesChannel?: string
4
+ }
5
+
6
+ export default class ChannelMarshal {
7
+ public static parse(channelString: string): Channel {
8
+ try {
9
+ const parsedChannel = JSON.parse(channelString) as Channel
10
+
11
+ return {
12
+ regionId: parsedChannel.regionId ?? '',
13
+ salesChannel: parsedChannel.salesChannel ?? '',
14
+ }
15
+ } catch (error) {
16
+ console.error(error)
17
+
18
+ throw new Error('Malformed channel string')
19
+ }
20
+ }
21
+
22
+ public static stringify(channel: Channel): string {
23
+ return JSON.stringify(channel)
24
+ }
25
+ }
@@ -1,4 +1,18 @@
1
+ type StoreSession {
2
+ channel: String
3
+ country: String
4
+ postalCode: String
5
+ }
6
+
7
+ input IStoreSession {
8
+ channel: String
9
+ country: String
10
+ postalCode: String
11
+ }
12
+
1
13
  type Mutation {
2
14
  # Returns the order if anything changed with the order. Null if the order is valid
3
15
  validateCart(cart: IStoreCart!): StoreCart
16
+
17
+ updateSession(session: IStoreSession!): StoreSession!
4
18
  }
@@ -0,0 +1 @@
1
+ export type Platform = 'vtex'