The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
fix(core): verify customer ownership when managing vendor customer group members (#1544)
Viktor Holikd812e4e912d4
diff --git a/integration-tests/http/customer-group/vendor/customer-group.spec.ts b/integration-tests/http/customer-group/vendor/customer-group.spec.tsindex bfc7f87e5..bf77b7e0f 100644--- a/integration-tests/http/customer-group/vendor/customer-group.spec.ts+++ b/integration-tests/http/customer-group/vendor/customer-group.spec.ts@@ -15,6 +15,7 @@ medusaIntegrationTestRunner({let seller1Headers: anylet seller2Headers: anylet seller1: any+ let seller2: anylet customer1: anylet customer2: any@@ -35,6 +36,7 @@ medusaIntegrationTestRunner({email: "[email protected]",name: "Test Seller 2",})+ seller2 = seller2Result.sellerseller2Headers = { headers: { ...seller2Result.headers.headers } }const customer1Result = await createCustomerUser(appContainer, {@@ -57,6 +59,14 @@ medusaIntegrationTestRunner({return res.data.customer_group}+ const linkSellerCustomer = async (sellerId: string, customerId: string) => {+ const link = appContainer.resolve(ContainerRegistrationKeys.LINK)+ await link.create({+ [MercurModules.SELLER]: { seller_id: sellerId },+ [Modules.CUSTOMER]: { customer_id: customerId },+ })+ }+describe("POST /vendor/customer-groups", () => {it("creates a customer group owned by the seller", async () => {const res = await api.post(@@ -178,6 +188,8 @@ medusaIntegrationTestRunner({describe("POST /vendor/customer-groups/:id/customers", () => {it("adds and removes members of the seller's group", async () => {+ await linkSellerCustomer(seller1.id, customer1.id)+ await linkSellerCustomer(seller1.id, customer2.id)const group = await createGroup(seller1Headers, "Members")const addRes = await api.post(@@ -223,6 +235,42 @@ medusaIntegrationTestRunner({expect(res.status).toEqual(404)})++ it("returns 404 adding a customer the seller does not own", async () => {+ await linkSellerCustomer(seller1.id, customer1.id)+ await linkSellerCustomer(seller2.id, customer2.id)+ const group = await createGroup(seller1Headers, "Members")++ const res = await api+ .post(+ `/vendor/customer-groups/${group.id}/customers`,+ { add: [customer1.id, customer2.id] },+ seller1Headers+ )+ .catch((e) => e.response)++ expect(res.status).toEqual(404)++ const check = await api.get(+ `/vendor/customer-groups/${group.id}?fields=id,customers.id`,+ seller1Headers+ )+ expect(check.data.customer_group.customers).toHaveLength(0)+ })++ it("returns 404 removing a customer the seller does not own", async () => {+ const group = await createGroup(seller1Headers, "Members")++ const res = await api+ .post(+ `/vendor/customer-groups/${group.id}/customers`,+ { remove: [customer2.id] },+ seller1Headers+ )+ .catch((e) => e.response)++ expect(res.status).toEqual(404)+ })})describe("POST /vendor/customers/:id/customer-groups", () => {@@ -276,6 +324,7 @@ medusaIntegrationTestRunner({[Modules.CUSTOMER]: { customer_id: customer1.id },})+ await linkSellerCustomer(seller1.id, customer2.id)const group = await createGroup(seller1Headers, "VIP")await api.post(`/vendor/customer-groups/${group.id}/customers`,@@ -310,6 +359,7 @@ medusaIntegrationTestRunner({[MercurModules.SELLER]: { seller_id: seller1.id },[Modules.CUSTOMER]: { customer_id: customer1.id },})+ await linkSellerCustomer(seller2.id, customer1.id)const ownGroup = await createGroup(seller1Headers, "Seller1 VIP")const otherGroup = await createGroup(seller2Headers, "Seller2 VIP")diff --git a/packages/core/src/api/vendor/customer-groups/[id]/customers/route.ts b/packages/core/src/api/vendor/customer-groups/[id]/customers/route.tsindex 577d5d199..45d52bf0b 100644--- a/packages/core/src/api/vendor/customer-groups/[id]/customers/route.ts+++ b/packages/core/src/api/vendor/customer-groups/[id]/customers/route.ts@@ -5,6 +5,7 @@ import {import { linkCustomersToCustomerGroupWorkflow } from "@medusajs/core-flows"import { HttpTypes } from "@mercurjs/types"+import { validateSellerCustomer } from "../../../customers/helpers"import { refetchCustomerGroup, validateSellerCustomerGroup } from "../../helpers"import { VendorManageCustomerGroupCustomersType } from "../../validators"@@ -13,19 +14,17 @@ export const POST = async (res: MedusaResponse<HttpTypes.VendorCustomerGroupResponse>) => {const { id } = req.params+ const sellerId = req.seller_context!.seller_id- await validateSellerCustomerGroup(- req.scope,- req.seller_context!.seller_id,- id- )+ await validateSellerCustomerGroup(req.scope, sellerId, id)++ const add = req.validatedBody.add ?? []+ const remove = req.validatedBody.remove ?? []++ await validateSellerCustomer(req.scope, sellerId, [...add, ...remove])await linkCustomersToCustomerGroupWorkflow(req.scope).run({- input: {- id,- add: req.validatedBody.add ?? [],- remove: req.validatedBody.remove ?? [],- },+ input: { id, add, remove },})const customer_group = await refetchCustomerGroup(diff --git a/packages/core/src/api/vendor/customers/helpers.ts b/packages/core/src/api/vendor/customers/helpers.tsindex afdfeb98c..cb3526423 100644--- a/packages/core/src/api/vendor/customers/helpers.ts+++ b/packages/core/src/api/vendor/customers/helpers.ts@@ -7,25 +7,32 @@ import {export const validateSellerCustomer = async (scope: MedusaContainer,sellerId: string,- customerId: string+ customerId: string | string[]) => {+ const ids = Array.isArray(customerId) ? customerId : [customerId]++ if (ids.length === 0) {+ return+ }+const query = scope.resolve(ContainerRegistrationKeys.QUERY)- const {- data: [sellerCustomer],- } = await query.graph({+ const { data: sellerCustomers } = await query.graph({entity: "seller_customer",filters: {seller_id: sellerId,- customer_id: customerId,+ customer_id: ids,},- fields: ["seller_id", "customer_id"],+ fields: ["customer_id"],})- if (!sellerCustomer) {+ const ownedIds = new Set(sellerCustomers.map((row) => row.customer_id))+ const missingId = ids.find((id) => !ownedIds.has(id))++ if (missingId) {throw new MedusaError(MedusaError.Types.NOT_FOUND,- `Customer with id: ${customerId} was not found`+ `Customer with id: ${missingId} was not found`)}}