|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
|
|
3
|
import { countries, getCountryByIso2 } from "./data/countries" |
|
4
|
|
|
5
|
export const isSameAddress = ( |
|
6
|
a?: HttpTypes.AdminOrderAddress | null, |
|
7
|
b?: HttpTypes.AdminOrderAddress | null |
|
8
|
) => { |
|
9
|
if (!a || !b) { |
|
10
|
return false |
|
11
|
} |
|
12
|
|
|
13
|
return ( |
|
14
|
a.first_name === b.first_name && |
|
15
|
a.last_name === b.last_name && |
|
16
|
a.address_1 === b.address_1 && |
|
17
|
a.address_2 === b.address_2 && |
|
18
|
a.city === b.city && |
|
19
|
a.postal_code === b.postal_code && |
|
20
|
a.province === b.province && |
|
21
|
a.country_code === b.country_code |
|
22
|
) |
|
23
|
} |
|
24
|
|
|
25
|
export const getFormattedAddress = ({ |
|
26
|
address, |
|
27
|
}: { |
|
28
|
address?: HttpTypes.AdminOrderAddress | null |
|
29
|
}) => { |
|
30
|
if (!address) { |
|
31
|
return [] |
|
32
|
} |
|
33
|
|
|
34
|
const { |
|
35
|
first_name, |
|
36
|
last_name, |
|
37
|
company, |
|
38
|
address_1, |
|
39
|
address_2, |
|
40
|
city, |
|
41
|
postal_code, |
|
42
|
province, |
|
43
|
country, |
|
44
|
country_code, |
|
45
|
} = address |
|
46
|
|
|
47
|
const name = [first_name, last_name].filter(Boolean).join(" ") |
|
48
|
|
|
49
|
const formattedAddress: string[] = [] |
|
50
|
|
|
51
|
if (name) { |
|
52
|
formattedAddress.push(name) |
|
53
|
} |
|
54
|
|
|
55
|
if (company) { |
|
56
|
formattedAddress.push(company) |
|
57
|
} |
|
58
|
|
|
59
|
if (address_1) { |
|
60
|
formattedAddress.push(address_1) |
|
61
|
} |
|
62
|
|
|
63
|
if (address_2) { |
|
64
|
formattedAddress.push(address_2) |
|
65
|
} |
|
66
|
|
|
67
|
const cityProvincePostal = [city, province, postal_code] |
|
68
|
.filter(Boolean) |
|
69
|
.join(" ") |
|
70
|
|
|
71
|
if (cityProvincePostal) { |
|
72
|
formattedAddress.push(cityProvincePostal) |
|
73
|
} |
|
74
|
|
|
75
|
if (country) { |
|
76
|
formattedAddress.push(country.display_name!) |
|
77
|
} else if (country_code) { |
|
78
|
const country = getCountryByIso2(country_code) |
|
79
|
|
|
80
|
if (country) { |
|
81
|
formattedAddress.push(country.display_name) |
|
82
|
} else { |
|
83
|
formattedAddress.push(country_code.toUpperCase()) |
|
84
|
} |
|
85
|
} |
|
86
|
|
|
87
|
return formattedAddress |
|
88
|
} |
|
89
|
|
|
90
|
export const getFormattedCountry = (countryCode: string | null | undefined) => { |
|
91
|
if (!countryCode) { |
|
92
|
return "" |
|
93
|
} |
|
94
|
|
|
95
|
const country = countries.find((c) => c.iso_2 === countryCode) |
|
96
|
return country ? country.display_name : countryCode |
|
97
|
} |