1 import {
2 LoaderOptions,
3 Logger,
4 ModulesSdkTypes,
5 } from "@medusajs/framework/types"
6 import {
7 ContainerRegistrationKeys,
8 defaultCurrencies,
9 } from "@medusajs/framework/utils"
10 import { Currency } from "@models"
11
12 export default async ({
13 container,
14 options,
15 }: LoaderOptions<
16 | ModulesSdkTypes.ModuleServiceInitializeOptions
17 | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
18 >): Promise<void> => {
19 // TODO: Add default logger to the container when running tests
20 const logger =
21 container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) ?? console
22 const { currencyService_ } = container.resolve<{
23 currencyService_: ModulesSdkTypes.IMedusaInternalService<typeof Currency>
24 }>("currencyModuleService")
25
26 try {
27 const normalizedCurrencies = Object.values(defaultCurrencies).map((c) => ({
28 ...c,
29 code: c.code.toLowerCase(),
30 }))
31 const resp = await currencyService_.upsert(normalizedCurrencies)
32 logger.debug(`Loaded ${resp.length} currencies`)
33 } catch (error) {
34 logger.warn(
35 `Failed to load currencies, skipping loader. Original error: ${error.message}`
36 )
37 }
38 }