|
1
|
import { |
|
2
|
CreatePriceSetDTO, |
|
3
|
IPricingModuleService, |
|
4
|
MedusaContainer, |
|
5
|
PriceSetDTO, |
|
6
|
} from "@medusajs/types" |
|
7
|
import { Modules } from "@medusajs/utils" |
|
8
|
|
|
9
|
const defaultPrices = [ |
|
10
|
{ |
|
11
|
amount: 3000, |
|
12
|
currency_code: "usd", |
|
13
|
}, |
|
14
|
] |
|
15
|
|
|
16
|
export const createVariantPriceSet = async ({ |
|
17
|
container, |
|
18
|
variantId, |
|
19
|
prices = defaultPrices, |
|
20
|
}: { |
|
21
|
container: MedusaContainer |
|
22
|
variantId: string |
|
23
|
prices?: CreatePriceSetDTO["prices"] |
|
24
|
}): Promise<PriceSetDTO> => { |
|
25
|
const remoteLink = container.resolve("remoteLink") |
|
26
|
const pricingModuleService: IPricingModuleService = container.resolve( |
|
27
|
Modules.PRICING |
|
28
|
) |
|
29
|
|
|
30
|
const priceSet = await pricingModuleService.createPriceSets({ |
|
31
|
prices, |
|
32
|
}) |
|
33
|
|
|
34
|
await remoteLink.create({ |
|
35
|
[Modules.PRODUCT]: { variant_id: variantId }, |
|
36
|
[Modules.PRICING]: { price_set_id: priceSet.id }, |
|
37
|
}) |
|
38
|
|
|
39
|
return await pricingModuleService.retrievePriceSet(priceSet.id, { |
|
40
|
relations: ["prices"], |
|
41
|
}) |
|
42
|
} |