123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- import {
- createApiKeysWorkflow,
- createProductCategoryWorkflow,
- createProductsWorkflow,
- createRegionsWorkflow,
- createSalesChannelsWorkflow,
- createShippingOptionsWorkflow,
- createShippingProfilesWorkflow,
- createStockLocationsWorkflow,
- createTaxRegionsWorkflow,
- linkSalesChannelsToApiKeyWorkflow,
- linkSalesChannelsToStockLocationWorkflow,
- updateStoresWorkflow,
- } from "@medusajs/core-flows";
- import { Logger } from "@medusajs/medusa";
- import { ModuleRegistrationName, RemoteLink } from "@medusajs/modules-sdk";
- import {
- ExecArgs,
- IFulfillmentModuleService,
- ISalesChannelModuleService,
- } from "@medusajs/types";
- import {
- ContainerRegistrationKeys,
- Modules,
- ProductStatus,
- } from "@medusajs/utils";
- export default async function seedDemoData({ container }: ExecArgs) {
- const logger: Logger = container.resolve(ContainerRegistrationKeys.LOGGER);
- const remoteLink: RemoteLink = container.resolve(
- ContainerRegistrationKeys.REMOTE_LINK
- );
- const fulfillmentModuleService: IFulfillmentModuleService = container.resolve(
- ModuleRegistrationName.FULFILLMENT
- );
- const salesChannelModuleService: ISalesChannelModuleService =
- container.resolve(ModuleRegistrationName.SALES_CHANNEL);
- const storeModuleService: ISalesChannelModuleService = container.resolve(
- ModuleRegistrationName.STORE
- );
- const countries = ["gb", "de", "dk", "se", "fr", "es", "it"];
- try {
- logger.info("Seeding store data...");
- const [store] = await storeModuleService.list();
- let defaultSalesChannel = await salesChannelModuleService.list({
- name: "Default Sales Channel",
- });
- if (!defaultSalesChannel.length) {
- // create the default sales channel
- const { result: salesChannelResult } = await createSalesChannelsWorkflow(
- container
- ).run({
- input: {
- salesChannelsData: [
- {
- name: "Default Sales Channel",
- },
- ],
- },
- });
- defaultSalesChannel = salesChannelResult;
- }
- await updateStoresWorkflow(container).run({
- input: {
- selector: { id: store.id },
- update: {
- supported_currency_codes: ["usd", "eur"],
- default_sales_channel_id: defaultSalesChannel[0].id,
- },
- },
- });
- logger.info("Seeding region data...");
- const { result: regionResult } = await createRegionsWorkflow(container).run(
- {
- input: {
- regions: [
- {
- name: "Europe",
- currency_code: "eur",
- countries,
- payment_providers: ["pp_system_default"],
- },
- ],
- },
- }
- );
- const region = regionResult[0];
- logger.info("Finished seeding regions.");
- logger.info("Seeding tax regions...");
- await createTaxRegionsWorkflow(container).run({
- input: countries.map((country_code) => ({
- country_code,
- })),
- });
- logger.info("Finished seeding tax regions.");
- logger.info("Seeding fulfillment data...");
- const { result: shippingProfileResult } =
- await createShippingProfilesWorkflow(container).run({
- input: {
- data: [
- {
- name: "Default",
- type: "default",
- },
- ],
- },
- });
- const shippingProfile = shippingProfileResult[0];
- const fulfillmentSet = await fulfillmentModuleService.create({
- name: "European Warehouse delivery",
- type: "delivery",
- service_zones: [
- {
- name: "Europe",
- geo_zones: [
- {
- country_code: "gb",
- type: "country",
- },
- {
- country_code: "de",
- type: "country",
- },
- {
- country_code: "dk",
- type: "country",
- },
- {
- country_code: "se",
- type: "country",
- },
- {
- country_code: "fr",
- type: "country",
- },
- {
- country_code: "es",
- type: "country",
- },
- {
- country_code: "it",
- type: "country",
- },
- ],
- },
- ],
- });
- await createShippingOptionsWorkflow(container).run({
- input: [
- {
- name: "Standard Shipping",
- price_type: "flat",
- provider_id: "manual_manual",
- service_zone_id: fulfillmentSet.service_zones[0].id,
- shipping_profile_id: shippingProfile.id,
- type: {
- label: "Standard",
- description: "Ship in 2-3 days.",
- code: "standard",
- },
- prices: [
- {
- currency_code: "usd",
- amount: 10,
- },
- {
- currency_code: "eur",
- amount: 10,
- },
- {
- region_id: region.id,
- amount: 10,
- },
- ],
- rules: [
- {
- attribute: "enabled_in_store",
- value: '"true"',
- operator: "eq",
- },
- {
- attribute: "is_return",
- value: "false",
- operator: "eq",
- },
- ],
- },
- {
- name: "Express Shipping",
- price_type: "flat",
- provider_id: "manual_manual",
- service_zone_id: fulfillmentSet.service_zones[0].id,
- shipping_profile_id: shippingProfile.id,
- type: {
- label: "Express",
- description: "Ship in 24 hours.",
- code: "express",
- },
- prices: [
- {
- currency_code: "usd",
- amount: 10,
- },
- {
- currency_code: "eur",
- amount: 10,
- },
- {
- region_id: region.id,
- amount: 10,
- },
- ],
- rules: [
- {
- attribute: "enabled_in_store",
- value: '"true"',
- operator: "eq",
- },
- {
- attribute: "is_return",
- value: "false",
- operator: "eq",
- },
- ],
- },
- ],
- });
- logger.info("Finished seeding fulfillment data.");
- logger.info("Seeding stock location data...");
- const { result: stockLocationResult } = await createStockLocationsWorkflow(
- container
- ).run({
- input: {
- locations: [
- {
- name: "European Warehouse",
- address: {
- city: "Copenhagen",
- country_code: "DK",
- address_1: "",
- },
- },
- ],
- },
- });
- const stockLocation = stockLocationResult[0];
- await linkSalesChannelsToStockLocationWorkflow(container).run({
- input: {
- id: stockLocation.id,
- add: [defaultSalesChannel[0].id],
- },
- });
- await remoteLink.create({
- [Modules.STOCK_LOCATION]: {
- stock_location_id: stockLocation.id,
- },
- [Modules.FULFILLMENT]: {
- fulfillment_set_id: fulfillmentSet.id,
- },
- });
- logger.info("Finished seeding stock location data.");
- logger.info("Seeding publishable API key data...");
- const { result: publishableApiKeyResult } = await createApiKeysWorkflow(
- container
- ).run({
- input: {
- api_keys: [
- {
- title: "Webshop",
- type: "publishable",
- created_by: "",
- },
- ],
- },
- });
- const publishableApiKey = publishableApiKeyResult[0];
- await linkSalesChannelsToApiKeyWorkflow(container).run({
- input: {
- id: publishableApiKey.id,
- add: [defaultSalesChannel[0].id],
- },
- });
- logger.info("Finished seeding publishable API key data.");
- logger.info("Seeding product data...");
- const categories = {
- Shirts: "",
- Sweatshirts: "",
- Pants: "",
- Merch: "",
- };
- for (const category in categories) {
- const { result: categoryResult } = await createProductCategoryWorkflow(
- container
- ).run({
- input: {
- product_category: {
- name: category,
- is_active: true,
- },
- },
- });
- categories[category] = categoryResult.id;
- }
- await createProductsWorkflow(container).run({
- input: {
- products: [
- {
- title: "Medusa T-Shirt",
- category_ids: [categories["Shirts"]],
- description:
- "Reimagine the feeling of a classic T-shirt. With our cotton T-shirts, everyday essentials no longer have to be ordinary.",
- handle: "t-shirt",
- weight: 400,
- status: ProductStatus.PUBLISHED,
- images: [
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/tee-black-front.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/tee-black-back.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/tee-white-front.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/tee-white-back.png",
- },
- ],
- options: [
- {
- title: "Size",
- values: ["S", "M", "L", "XL"],
- },
- {
- title: "Color",
- values: ["Black", "White"],
- },
- ],
- variants: [
- {
- title: "S / Black",
- sku: "SHIRT-S-BLACK",
- options: {
- Size: "S",
- Color: "Black",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "S / White",
- sku: "SHIRT-S-WHITE",
- options: {
- Size: "S",
- Color: "White",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "M / Black",
- sku: "SHIRT-M-BLACK",
- options: {
- Size: "M",
- Color: "Black",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "M / White",
- sku: "SHIRT-M-WHITE",
- options: {
- Size: "M",
- Color: "White",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "L / Black",
- sku: "SHIRT-L-BLACK",
- options: {
- Size: "L",
- Color: "Black",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "L / White",
- sku: "SHIRT-L-WHITE",
- options: {
- Size: "L",
- Color: "White",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "XL / Black",
- sku: "SHIRT-XL-BLACK",
- options: {
- Size: "XL",
- Color: "Black",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "XL / White",
- sku: "SHIRT-XL-WHITE",
- options: {
- Size: "XL",
- Color: "White",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- ],
- sales_channels: [
- {
- id: defaultSalesChannel[0].id,
- },
- ],
- },
- {
- title: "Medusa Sweatshirt",
- category_ids: [categories["Sweatshirts"]],
- description:
- "Reimagine the feeling of a classic sweatshirt. With our cotton sweatshirt, everyday essentials no longer have to be ordinary.",
- handle: "sweatshirt",
- weight: 400,
- status: ProductStatus.PUBLISHED,
- images: [
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/sweatshirt-vintage-front.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/sweatshirt-vintage-back.png",
- },
- ],
- options: [
- {
- title: "Size",
- values: ["S", "M", "L", "XL"],
- },
- ],
- variants: [
- {
- title: "S",
- sku: "SWEATSHIRT-S",
- options: {
- Size: "S",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "M",
- sku: "SWEATSHIRT-M",
- options: {
- Size: "M",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "L",
- sku: "SWEATSHIRT-L",
- options: {
- Size: "L",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "XL",
- sku: "SWEATSHIRT-XL",
- options: {
- Size: "XL",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- ],
- sales_channels: [
- {
- id: defaultSalesChannel[0].id,
- },
- ],
- },
- {
- title: "Medusa Sweatpants",
- category_ids: [categories["Pants"]],
- description:
- "Reimagine the feeling of classic sweatpants. With our cotton sweatpants, everyday essentials no longer have to be ordinary.",
- handle: "sweatpants",
- weight: 400,
- status: ProductStatus.PUBLISHED,
- images: [
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/sweatpants-gray-front.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/sweatpants-gray-back.png",
- },
- ],
- options: [
- {
- title: "Size",
- values: ["S", "M", "L", "XL"],
- },
- ],
- variants: [
- {
- title: "S",
- sku: "SWEATPANTS-S",
- options: {
- Size: "S",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "M",
- sku: "SWEATPANTS-M",
- options: {
- Size: "M",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "L",
- sku: "SWEATPANTS-L",
- options: {
- Size: "L",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "XL",
- sku: "SWEATPANTS-XL",
- options: {
- Size: "XL",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- ],
- sales_channels: [
- {
- id: defaultSalesChannel[0].id,
- },
- ],
- },
- {
- title: "Medusa Shorts",
- category_ids: [categories["Merch"]],
- description:
- "Reimagine the feeling of classic shorts. With our cotton shorts, everyday essentials no longer have to be ordinary.",
- handle: "shorts",
- weight: 400,
- status: ProductStatus.PUBLISHED,
- images: [
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/shorts-vintage-front.png",
- },
- {
- url: "https://medusa-public-images.s3.eu-west-1.amazonaws.com/shorts-vintage-back.png",
- },
- ],
- options: [
- {
- title: "Size",
- values: ["S", "M", "L", "XL"],
- },
- ],
- variants: [
- {
- title: "S",
- sku: "SHORTS-S",
- options: {
- Size: "S",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "M",
- sku: "SHORTS-M",
- options: {
- Size: "M",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "L",
- sku: "SHORTS-L",
- options: {
- Size: "L",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- {
- title: "XL",
- sku: "SHORTS-XL",
- options: {
- Size: "XL",
- },
- manage_inventory: false,
- prices: [
- {
- amount: 10,
- currency_code: "eur",
- },
- {
- amount: 15,
- currency_code: "usd",
- },
- ],
- },
- ],
- sales_channels: [
- {
- id: defaultSalesChannel[0].id,
- },
- ],
- },
- ],
- },
- });
- logger.info("Finished seeding product data.");
- } catch (e) {
- logger.error(`Seeding failed`, e);
- }
- }
|