Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

feat: enhance seedDemoData to link data to sellers

Jakub Sieradzki12df3032ba53
diff --git a/templates/basic/packages/api/src/scripts/seed.ts b/templates/basic/packages/api/src/scripts/seed.ts
index 55ed271c5..ab500e0f0 100644
--- a/templates/basic/packages/api/src/scripts/seed.ts
+++ b/templates/basic/packages/api/src/scripts/seed.ts
@@ -1016,6 +1016,14 @@ export default async function seedDemoData({ container }: ExecArgs) {
}
logger.info("Finished seeding product data.");
+ const { data: seededProducts } = await query.graph({
+ entity: "product",
+ fields: ["id"],
+ filters: {
+ handle: productHandles,
+ },
+ });
+
logger.info("Seeding inventory levels.");
const { data: inventoryItems } = await query.graph({
@@ -1063,10 +1071,12 @@ export default async function seedDemoData({ container }: ExecArgs) {
email: sellerEmail,
});
+ let seller;
if (existingSellers.length) {
- logger.info("Seller already exists, skipping.");
+ seller = existingSellers[0];
+ logger.info("Seller already exists, skipping creation.");
} else {
- const seller = await sellerModule.createSellers({
+ seller = await sellerModule.createSellers({
name: "Test Seller",
email: sellerEmail,
});
@@ -1090,5 +1100,116 @@ export default async function seedDemoData({ container }: ExecArgs) {
logger.info("Seller created with email: [email protected] / password: supersecret");
}
+
+ // Link entities to seller (idempotent — runs every time)
+ logger.info("Linking products to seller...");
+ for (const product of seededProducts) {
+ try {
+ await link.create({
+ [Modules.PRODUCT]: {
+ product_id: product.id,
+ },
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+ }
+
+ logger.info("Linking stock location to seller...");
+ try {
+ await link.create({
+ [Modules.STOCK_LOCATION]: {
+ stock_location_id: stockLocation.id,
+ },
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+
+ logger.info("Linking fulfillment set to seller...");
+ try {
+ await link.create({
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ [Modules.FULFILLMENT]: {
+ fulfillment_set_id: fulfillmentSet.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+
+ logger.info("Linking service zones to seller...");
+ const fulfillmentSetWithZones = await fulfillmentModuleService.retrieveFulfillmentSet(
+ fulfillmentSet.id,
+ { relations: ["service_zones"] }
+ );
+ for (const zone of fulfillmentSetWithZones.service_zones) {
+ try {
+ await link.create({
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ [Modules.FULFILLMENT]: {
+ service_zone_id: zone.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+ }
+
+ logger.info("Linking shipping profile to seller...");
+ try {
+ await link.create({
+ [Modules.FULFILLMENT]: {
+ shipping_profile_id: shippingProfile.id,
+ },
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+
+ logger.info("Linking shipping options to seller...");
+ const shippingOptions = await fulfillmentModuleService.listShippingOptions({
+ service_zone_id: fulfillmentSetWithZones.service_zones.map((z) => z.id),
+ });
+ for (const option of shippingOptions) {
+ try {
+ await link.create({
+ [Modules.FULFILLMENT]: {
+ shipping_option_id: option.id,
+ },
+ [MercurModules.SELLER]: {
+ seller_id: seller.id,
+ },
+ });
+ } catch (error: unknown) {
+ if (!(error instanceof Error && error.message.includes("already exists"))) {
+ throw error;
+ }
+ }
+ }
+
logger.info("Finished seeding seller data.");
}