-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/medusa/src/scripts/migrate-product-shipping-profile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ExecArgs } from "@medusajs/framework/types" | ||
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils" | ||
|
||
export default async function assignProductsToShippingProfile({ | ||
container, | ||
}: ExecArgs) { | ||
const logger = container.resolve(ContainerRegistrationKeys.LOGGER) | ||
const link = container.resolve(ContainerRegistrationKeys.LINK) | ||
const query = container.resolve(ContainerRegistrationKeys.QUERY) | ||
|
||
const shippingProfiles = await query.graph({ | ||
entity: "shipping_profile", | ||
fields: ["id", "name"], | ||
}) | ||
|
||
if (!shippingProfiles.data.length) { | ||
logger.error( | ||
"No shipping profiles found, please create a default shipping profile first" | ||
) | ||
} | ||
|
||
const products = await query.graph({ | ||
entity: "product", | ||
fields: ["id"], | ||
}) | ||
|
||
const shippingProfileId = ( | ||
shippingProfiles.data.find((p) => | ||
p.name.toLocaleLowerCase().includes("default") | ||
) || shippingProfiles.data[0] | ||
).id | ||
|
||
const links = products.data.map((product) => ({ | ||
[Modules.PRODUCT]: { | ||
product_id: product.id, | ||
}, | ||
[Modules.FULFILLMENT]: { | ||
shipping_profile_id: shippingProfileId, | ||
}, | ||
})) | ||
|
||
await link.create(links) | ||
|
||
logger.info("Products assigned to shipping profile") | ||
} |