1 import {
2 LoaderOptions,
3 Logger,
4 ModulesSdkTypes,
5 } from "@medusajs/framework/types"
6 import {
7 ContainerRegistrationKeys,
8 DefaultsUtils,
9 } from "@medusajs/framework/utils"
10 import { Country } from "@models"
11
12 export default async ({ container }: LoaderOptions): Promise<void> => {
13 // TODO: Add default logger to the container when running tests
14 const logger =
15 container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) ?? console
16 const countryService_: ModulesSdkTypes.IMedusaInternalService<
17 typeof Country
18 > = container.resolve("countryService")
19
20 try {
21 const normalizedCountries = DefaultsUtils.defaultCountries.map((c) => ({
22 iso_2: c.alpha2.toLowerCase(),
23 iso_3: c.alpha3.toLowerCase(),
24 num_code: c.numeric,
25 name: c.name.toUpperCase(),
26 display_name: c.name,
27 }))
28
29 const resp = await countryService_.upsert(normalizedCountries)
30 logger.debug(`Loaded ${resp.length} countries`)
31 } catch (error) {
32 logger.warn(
33 `Failed to load countries, skipping loader. Original error: ${error.message}`
34 )
35 }
36 }