@faststore/api 1.6.15 → 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.
@@ -0,0 +1,7 @@
1
+ export interface RegionInput {
2
+ postalCode: string
3
+ country: string
4
+ salesChannel?: string | null
5
+ }
6
+
7
+ export type Region = Array<{ id: string }>
@@ -3,17 +3,38 @@ import type { Simulation } from '../clients/commerce/types/Simulation'
3
3
 
4
4
  type Resolvers = (root: Simulation & { product: EnhancedSku }) => unknown
5
5
 
6
+ const inStock = (item: Simulation['items'][0]) =>
7
+ item.availability === 'available'
8
+
9
+ // Smallest Available Selling Price First
10
+ export const sortOfferByPrice = (
11
+ items: Simulation['items']
12
+ ): Simulation['items'] =>
13
+ items.sort((a, b) => {
14
+ if (inStock(a) && !inStock(b)) {
15
+ return -1
16
+ }
17
+
18
+ if (!inStock(a) && inStock(b)) {
19
+ return 1
20
+ }
21
+
22
+ return a.sellingPrice - b.sellingPrice
23
+ })
24
+
6
25
  export const StoreAggregateOffer: Record<string, Resolvers> = {
7
- highPrice: ({ items }) =>
8
- items.reduce(
9
- (acc, curr) => (acc > curr.sellingPrice ? acc : curr.sellingPrice),
10
- items[0]?.sellingPrice ?? 0
11
- ) / 1e2,
12
- lowPrice: ({ items }) =>
13
- items.reduce(
14
- (acc, curr) => (acc < curr.sellingPrice ? acc : curr.sellingPrice),
15
- items[0]?.sellingPrice ?? 0
16
- ) / 1e2,
26
+ highPrice: ({ items }) => {
27
+ const availableItems = items.filter(inStock)
28
+ const highPrice = availableItems.pop()?.sellingPrice
29
+
30
+ return (highPrice ?? 0) / 1e2
31
+ },
32
+ lowPrice: ({ items }) => {
33
+ const availableItems = items.filter(inStock)
34
+ const lowPrice = availableItems[0]?.sellingPrice
35
+
36
+ return (lowPrice ?? 0) / 1e2
37
+ },
17
38
  offerCount: ({ items }) => items.length,
18
39
  priceCurrency: () => '',
19
40
  offers: ({ items, product }) => items.map((item) => ({ ...item, product })),
@@ -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
  }
@@ -1,6 +1,7 @@
1
+ import { slugify } from '../utils/slugify'
1
2
  import type { Resolver } from '..'
2
3
  import type { EnhancedSku } from '../utils/enhanceSku'
3
- import { slugify } from '../utils/slugify'
4
+ import { sortOfferByPrice } from './aggregateOffer'
4
5
 
5
6
  type Root = EnhancedSku
6
7
 
@@ -81,7 +82,11 @@ export const StoreProduct: Record<string, Resolver<Root>> = {
81
82
 
82
83
  const simulation = await simulationLoader.load(items)
83
84
 
84
- return { ...simulation, product }
85
+ return {
86
+ ...simulation,
87
+ items: sortOfferByPrice(simulation.items),
88
+ product,
89
+ }
85
90
  },
86
91
  isVariantOf: ({ isVariantOf }) => isVariantOf,
87
92
  additionalProperty: ({ attributes = [] }) =>
@@ -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'