fix(pricing, medusa): resolve minor pricing-module bugs (#5685)
* fix region price updates * pricing migration: include title/name field value migration * update migration scripts for pricing module * move file to script utils * rename file * rename file * add changeset * remove redundant maps * update migration script * nit * remove unnecessary variable * filter before map * array function naming cleanup * chore: address pr reviews --------- Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com> Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { AwilixContainer } from "awilix"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
|
||||
export const createDefaultRuleTypes = async (container: AwilixContainer) => {
|
||||
const pricingModuleService: IPricingModuleService = container.resolve(
|
||||
"pricingModuleService"
|
||||
)
|
||||
const existing = await pricingModuleService.listRuleTypes(
|
||||
{ rule_attribute: ["region_id", "customer_group_id"] },
|
||||
{ take: 2 }
|
||||
)
|
||||
|
||||
if (existing.length === 2) {
|
||||
return
|
||||
}
|
||||
|
||||
if (existing.length === 0) {
|
||||
await pricingModuleService.createRuleTypes([
|
||||
{ name: "region_id", rule_attribute: "region_id" },
|
||||
{ name: "customer_group_id", rule_attribute: "customer_group_id" },
|
||||
])
|
||||
} else if (existing[0].rule_attribute === "region_id") {
|
||||
await pricingModuleService.createRuleTypes([
|
||||
{ name: "customer_group_id", rule_attribute: "customer_group_id" },
|
||||
])
|
||||
} else {
|
||||
await pricingModuleService.createRuleTypes([
|
||||
{ name: "region_id", rule_attribute: "region_id" },
|
||||
])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { IPricingModuleService, MedusaContainer } from "@medusajs/types"
|
||||
import { MedusaError, promiseAll } from "@medusajs/utils"
|
||||
|
||||
import { ProductVariantService } from "../../services"
|
||||
import dotenv from "dotenv"
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const BATCH_SIZE = 100
|
||||
|
||||
export const migrateProductVariantPricing = async function (
|
||||
container: MedusaContainer
|
||||
) {
|
||||
const variantService: ProductVariantService = await container.resolve(
|
||||
"productVariantService"
|
||||
)
|
||||
|
||||
const pricingService: IPricingModuleService = container.resolve(
|
||||
"pricingModuleService"
|
||||
)
|
||||
|
||||
const link = await container.resolve("remoteLink")
|
||||
|
||||
if (!link) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
"Can't migrate money_amounts: Pricing module is not configured correctly"
|
||||
)
|
||||
}
|
||||
|
||||
const [_, totalCount] = await variantService.listAndCount(
|
||||
{},
|
||||
{ take: BATCH_SIZE, order: { id: "ASC" }, relations: ["prices"] }
|
||||
)
|
||||
|
||||
let processedCount = 0
|
||||
while (processedCount < totalCount) {
|
||||
const [variants] = await variantService.listAndCount(
|
||||
{},
|
||||
{
|
||||
skip: processedCount,
|
||||
take: BATCH_SIZE,
|
||||
order: { id: "ASC" },
|
||||
relations: ["prices"],
|
||||
}
|
||||
)
|
||||
|
||||
const links: any[] = []
|
||||
|
||||
await promiseAll(
|
||||
variants.map(async (variant) => {
|
||||
const priceSet = await pricingService.create({
|
||||
rules: [{ rule_attribute: "region_id" }],
|
||||
prices:
|
||||
variant?.prices
|
||||
?.filter(({ price_list_id }) => !price_list_id)
|
||||
.map((price) => ({
|
||||
rules: {
|
||||
...(price.region_id ? { region_id: price.region_id } : {}),
|
||||
},
|
||||
currency_code: price.currency_code,
|
||||
min_quantity: price.min_quantity,
|
||||
max_quantity: price.max_quantity,
|
||||
amount: price.amount,
|
||||
})) ?? [],
|
||||
})
|
||||
|
||||
links.push({
|
||||
productService: {
|
||||
variant_id: variant.id,
|
||||
},
|
||||
pricingService: {
|
||||
price_set_id: priceSet.id,
|
||||
},
|
||||
})
|
||||
})
|
||||
)
|
||||
await link.create(links)
|
||||
|
||||
processedCount += variants.length
|
||||
console.log(`Processed ${processedCount} of ${totalCount}`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user