feat(fulfillment): shipping options context field (#7169)
This commit is contained in:
1 parent
25b0ccc60a
commit
9d3f495314
22 files changed
+973
-371
No files matched your search
@@ -0,0 +1,203 @@
|
||||
import { deepFlatMap } from "../deep-flat-map"
|
||||
|
||||
describe("deepFlatMap", function () {
|
||||
it("should return flat map of nested objects", function () {
|
||||
const data = [
|
||||
{
|
||||
id: "sales_channel_1",
|
||||
stock_locations: [
|
||||
{
|
||||
id: "location_1",
|
||||
fulfillment_sets: [
|
||||
{
|
||||
id: "fset_1",
|
||||
name: "Test 123",
|
||||
service_zones: [
|
||||
{
|
||||
id: "zone_123",
|
||||
shipping_options: [
|
||||
{
|
||||
id: "so_zone_123 1111",
|
||||
calculated_price: {
|
||||
calculated_amount: 3000,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "so_zone_123 22222",
|
||||
calculated_price: {
|
||||
calculated_amount: 6000,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "zone_567",
|
||||
shipping_options: [
|
||||
{
|
||||
id: "zone 567 11111",
|
||||
calculated_price: {
|
||||
calculated_amount: 1230,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "zone 567 22222",
|
||||
calculated_price: {
|
||||
calculated_amount: 1230,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "location_2",
|
||||
fulfillment_sets: [
|
||||
{
|
||||
id: "fset_2",
|
||||
name: "fset name 2",
|
||||
service_zones: [
|
||||
{
|
||||
id: "zone_ABC",
|
||||
shipping_options: [
|
||||
{
|
||||
id: "zone_abc_unique",
|
||||
calculated_price: {
|
||||
calculated_amount: 70,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
{
|
||||
id: "sales_channel_2",
|
||||
stock_locations: [
|
||||
{
|
||||
id: "location_5",
|
||||
fulfillment_sets: [
|
||||
{
|
||||
id: "fset_aaa",
|
||||
name: "Test aaa",
|
||||
service_zones: [
|
||||
{
|
||||
id: "zone_aaa",
|
||||
shipping_options: [
|
||||
{
|
||||
id: "so_zone_aaa aaaa",
|
||||
calculated_price: {
|
||||
calculated_amount: 500,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "so_zone_aaa bbbb",
|
||||
calculated_price: {
|
||||
calculated_amount: 12,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
const result = deepFlatMap(
|
||||
data,
|
||||
"stock_locations.fulfillment_sets.service_zones.shipping_options.calculated_price",
|
||||
({
|
||||
root_,
|
||||
stock_locations,
|
||||
fulfillment_sets,
|
||||
service_zones,
|
||||
shipping_options,
|
||||
calculated_price,
|
||||
}) => {
|
||||
return {
|
||||
sales_channel_id: root_.id,
|
||||
stock_location_id: stock_locations.id,
|
||||
fulfillment_set_id: fulfillment_sets.id,
|
||||
fulfillment_set_name: fulfillment_sets.name,
|
||||
service_zone_id: service_zones.id,
|
||||
shipping_option_id: shipping_options.id,
|
||||
price: calculated_price.calculated_amount,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
sales_channel_id: "sales_channel_1",
|
||||
stock_location_id: "location_1",
|
||||
fulfillment_set_id: "fset_1",
|
||||
fulfillment_set_name: "Test 123",
|
||||
service_zone_id: "zone_123",
|
||||
shipping_option_id: "so_zone_123 1111",
|
||||
price: 3000,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_1",
|
||||
stock_location_id: "location_1",
|
||||
fulfillment_set_id: "fset_1",
|
||||
fulfillment_set_name: "Test 123",
|
||||
service_zone_id: "zone_123",
|
||||
shipping_option_id: "so_zone_123 22222",
|
||||
price: 6000,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_1",
|
||||
stock_location_id: "location_1",
|
||||
fulfillment_set_id: "fset_1",
|
||||
fulfillment_set_name: "Test 123",
|
||||
service_zone_id: "zone_567",
|
||||
shipping_option_id: "zone 567 11111",
|
||||
price: 1230,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_1",
|
||||
stock_location_id: "location_1",
|
||||
fulfillment_set_id: "fset_1",
|
||||
fulfillment_set_name: "Test 123",
|
||||
service_zone_id: "zone_567",
|
||||
shipping_option_id: "zone 567 22222",
|
||||
price: 1230,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_1",
|
||||
stock_location_id: "location_2",
|
||||
fulfillment_set_id: "fset_2",
|
||||
fulfillment_set_name: "fset name 2",
|
||||
service_zone_id: "zone_ABC",
|
||||
shipping_option_id: "zone_abc_unique",
|
||||
price: 70,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_2",
|
||||
stock_location_id: "location_5",
|
||||
fulfillment_set_id: "fset_aaa",
|
||||
fulfillment_set_name: "Test aaa",
|
||||
service_zone_id: "zone_aaa",
|
||||
shipping_option_id: "so_zone_aaa aaaa",
|
||||
price: 500,
|
||||
},
|
||||
{
|
||||
sales_channel_id: "sales_channel_2",
|
||||
stock_location_id: "location_5",
|
||||
fulfillment_set_id: "fset_aaa",
|
||||
fulfillment_set_name: "Test aaa",
|
||||
service_zone_id: "zone_aaa",
|
||||
shipping_option_id: "so_zone_aaa bbbb",
|
||||
price: 12,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,103 @@
|
||||
import { isDefined } from "./is-defined"
|
||||
import { isObject } from "./is-object"
|
||||
|
||||
/**
|
||||
* @description
|
||||
* This function is used to flatten nested objects and arrays
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* ```ts
|
||||
* const data = {
|
||||
* root_level_property: "root level",
|
||||
* products: [
|
||||
* {
|
||||
* id: "1",
|
||||
* name: "product 1",
|
||||
* variants: [
|
||||
* { id: "1.1", name: "variant 1.1" },
|
||||
* { id: "1.2", name: "variant 1.2" },
|
||||
* ],
|
||||
* },
|
||||
* {
|
||||
* id: "2",
|
||||
* name: "product 2",
|
||||
* variants: [
|
||||
* { id: "2.1", name: "variant 2.1" },
|
||||
* { id: "2.2", name: "variant 2.2" },
|
||||
* ],
|
||||
* },
|
||||
* ],
|
||||
* }
|
||||
*
|
||||
* const flat = deepFlatMap(
|
||||
* data,
|
||||
* "products.variants",
|
||||
* ({ root_, products, variants }) => {
|
||||
* return {
|
||||
* root_level_property: root_.root_level_property,
|
||||
* product_id: products.id,
|
||||
* product_name: products.name,
|
||||
* variant_id: variants.id,
|
||||
* variant_name: variants.name,
|
||||
* }
|
||||
* }
|
||||
* )
|
||||
* ```
|
||||
*/
|
||||
|
||||
export function deepFlatMap(
|
||||
data: any,
|
||||
path: string,
|
||||
callback: (context: Record<string, any>) => any
|
||||
) {
|
||||
const ROOT_LEVEL = "root_"
|
||||
const keys = path.split(".")
|
||||
keys.unshift(ROOT_LEVEL)
|
||||
|
||||
const lastKey = keys[keys.length - 1]
|
||||
const stack: {
|
||||
element: any
|
||||
path: string[]
|
||||
context: Record<string, any>
|
||||
}[] = [{ element: { [ROOT_LEVEL]: data }, path: keys, context: {} }]
|
||||
|
||||
const results: any[] = []
|
||||
while (stack.length > 0) {
|
||||
const { element, path, context } = stack.shift()!
|
||||
const currentKey = path[0]
|
||||
const remainingPath = path.slice(1)
|
||||
|
||||
if (!isDefined(element[currentKey])) {
|
||||
callback({ ...context })
|
||||
continue
|
||||
}
|
||||
|
||||
if (remainingPath.length === 0) {
|
||||
if (Array.isArray(element[currentKey])) {
|
||||
element[currentKey].forEach((item) => {
|
||||
results.push(callback({ ...context, [lastKey]: item }))
|
||||
})
|
||||
} else if (isObject(element[currentKey])) {
|
||||
results.push(callback({ ...context, [lastKey]: element[currentKey] }))
|
||||
}
|
||||
} else {
|
||||
if (Array.isArray(element[currentKey])) {
|
||||
element[currentKey].forEach((item) => {
|
||||
stack.push({
|
||||
element: item,
|
||||
path: remainingPath,
|
||||
context: { ...context, [currentKey]: item },
|
||||
})
|
||||
})
|
||||
} else if (isObject(element[currentKey])) {
|
||||
stack.push({
|
||||
element: element[currentKey],
|
||||
path: remainingPath,
|
||||
context: { ...context, [currentKey]: element[currentKey] },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
@@ -10,6 +10,7 @@ export * from "./create-psql-index-helper"
|
||||
export * from "./deduplicate"
|
||||
export * from "./deep-copy"
|
||||
export * from "./deep-equal-obj"
|
||||
export * from "./deep-flat-map"
|
||||
export * from "./errors"
|
||||
export * from "./generate-entity-id"
|
||||
export * from "./generate-linkable-keys-map"
|
||||
|
||||
Reference in new issue
Block a user