|
1
|
import { HttpTypes } from "@medusajs/types"; |
|
2
|
import { ProductCreateSchemaType } from "../routes/gift-cards/gift-card-products/components/gift-card-product-create-form/types"; |
|
3
|
import { castNumber } from "./validations"; |
|
4
|
|
|
5
|
export const normalizeProductFormValues = ( |
|
6
|
values: ProductCreateSchemaType & { |
|
7
|
status: HttpTypes.AdminProductStatus; |
|
8
|
regionsCurrencyMap: Record<string, string>; |
|
9
|
} |
|
10
|
): HttpTypes.AdminCreateProduct => { |
|
11
|
const thumbnail = values.media?.find((media) => media.isThumbnail)?.url; |
|
12
|
const images = values.media |
|
13
|
?.filter((media) => !media.isThumbnail) |
|
14
|
.map((media) => ({ url: media.url })); |
|
15
|
|
|
16
|
const optionValues = values.denominations.map( |
|
17
|
(denomination) => denomination.value |
|
18
|
); |
|
19
|
|
|
20
|
const options = [ |
|
21
|
{ |
|
22
|
title: "denomination", |
|
23
|
values: optionValues, |
|
24
|
}, |
|
25
|
]; |
|
26
|
|
|
27
|
const variants = values.denominations.map((denomination) => ({ |
|
28
|
title: denomination.value, |
|
29
|
options: { |
|
30
|
denomination: denomination.value, |
|
31
|
}, |
|
32
|
prices: denomination.prices, |
|
33
|
})); |
|
34
|
|
|
35
|
return { |
|
36
|
status: values.status, |
|
37
|
tags: values?.tags?.length |
|
38
|
? values.tags?.map((tag) => ({ id: tag })) |
|
39
|
: undefined, |
|
40
|
sales_channels: values?.sales_channels?.length |
|
41
|
? values.sales_channels?.map((sc) => ({ id: sc.id })) |
|
42
|
: undefined, |
|
43
|
images, |
|
44
|
is_giftcard: true, |
|
45
|
collection_id: values.collection_id || undefined, |
|
46
|
shipping_profile_id: values.shipping_profile_id || undefined, |
|
47
|
categories: values.categories.map((id) => ({ id })), |
|
48
|
type_id: values.type_id || undefined, |
|
49
|
handle: values.handle || undefined, |
|
50
|
origin_country: values.origin_country || undefined, |
|
51
|
material: values.material || undefined, |
|
52
|
mid_code: values.mid_code || undefined, |
|
53
|
hs_code: values.hs_code || undefined, |
|
54
|
thumbnail, |
|
55
|
title: values.title, |
|
56
|
subtitle: values.subtitle || undefined, |
|
57
|
description: values.description || undefined, |
|
58
|
discountable: values.discountable || undefined, |
|
59
|
width: values.width ? parseFloat(values.width) : undefined, |
|
60
|
length: values.length ? parseFloat(values.length) : undefined, |
|
61
|
height: values.height ? parseFloat(values.height) : undefined, |
|
62
|
weight: values.weight ? parseFloat(values.weight) : undefined, |
|
63
|
options, |
|
64
|
variants: normalizeVariants(variants, values.regionsCurrencyMap), |
|
65
|
}; |
|
66
|
}; |
|
67
|
|
|
68
|
export const normalizeVariants = ( |
|
69
|
variants: ProductCreateSchemaType["variants"], |
|
70
|
regionsCurrencyMap: Record<string, string> |
|
71
|
): HttpTypes.AdminCreateProductVariant[] => { |
|
72
|
return variants.map((variant, i) => ({ |
|
73
|
title: variant.title, |
|
74
|
options: variant.options, |
|
75
|
manage_inventory: false, |
|
76
|
allow_backorder: true, |
|
77
|
variant_rank: i, |
|
78
|
prices: Object.entries(variant.prices || {}) |
|
79
|
.map(([key, value]: any) => { |
|
80
|
if (value === "" || value === undefined) { |
|
81
|
return undefined; |
|
82
|
} |
|
83
|
|
|
84
|
if (key.startsWith("reg_")) { |
|
85
|
return { |
|
86
|
currency_code: regionsCurrencyMap[key], |
|
87
|
amount: castNumber(value), |
|
88
|
rules: { region_id: key }, |
|
89
|
}; |
|
90
|
} else { |
|
91
|
return { |
|
92
|
currency_code: key, |
|
93
|
amount: castNumber(value), |
|
94
|
}; |
|
95
|
} |
|
96
|
}) |
|
97
|
.filter((v) => !!v), |
|
98
|
})); |
|
99
|
}; |
|
100
|
|
|
101
|
export const decorateVariantsWithDefaultValues = ( |
|
102
|
variants: ProductCreateSchemaType["variants"] |
|
103
|
) => { |
|
104
|
return variants.map((variant) => ({ |
|
105
|
...variant, |
|
106
|
title: variant.title || "", |
|
107
|
manage_inventory: variant.manage_inventory || false, |
|
108
|
allow_backorder: variant.allow_backorder || false, |
|
109
|
inventory_kit: variant.inventory_kit || false, |
|
110
|
})); |
|
111
|
}; |