Integration Module 2.0 for Medusa is outRead the announcement
B

B2b

B2B commerce capabilities for Medusa v2: companies, employees, quotes, approvals, spending limits, and B2B cart workflows.

npm install @vicacha-devs/medusa-plugin-b2b
Category
Built by
Version
0.1.1
Last updated
4 days ago
Monthly downloads
Loading downloads

B2B commerce capabilities for Medusa v2: companies, employees, spending limits, quote negotiation, multi-level cart approvals, and a full admin UI — all as a self-contained, droppable plugin.

Medusa compatibility: — keep peer dependencies aligned with your host application.


Contents


Features

  • Company management — companies with name, contact info, address, logo, and currency
  • Employee roster — link customers to companies with per-employee spending limits
  • Customer-group sync — each company is backed by a Medusa CustomerGroup for price-list targeting
  • Spending limits — configurable per-employee caps with reset windows (daily → yearly)
  • Cart approval flow — require admin or sales-manager approval before a cart can be completed
  • Quote negotiation — request-for-quote lifecycle with draft-order editing, messaging, and acceptance
  • Workflow hooks — cart and order creation hooks that attach B2B context automatically
  • Admin UI — overview dashboard, companies list + detail, quote list + detail + management, approval queue
  • Dual API surface — (Medusa admin JWT) and (customer session/bearer)
  • Role enforcement — middleware guards company-mutating store routes
  • Free-shipping price API — expose threshold prices to storefronts without custom logic

Requirements

DependencyVersion
Node
pnpm (or npm/yarn — adjust scripts accordingly)

Installation

Register the plugin in :

Run migrations after adding the plugin:

medusa db:migrate

Seeding demo data

A seed script is included to create default customer groups, a B2B sales channel, and a base region with shipping options:


Path Aliases

The plugin ships with path aliases for both backend and admin code.

ContextAliasResolves to
Backend (server)
Admin (React)

TypeScript — aliases are configured in both (backend) and (admin) and take effect immediately for type-checking.

Build — the script runs after to rewrite alias paths in the compiled output:

Admin Vite — to resolve aliases in the admin bundle, add the following to the host application's :


Architecture

The plugin registers three custom modules and a set of entity links that tie them to Medusa's core data model.

Quote lifecycle

Approval lifecycle


Modules

Manages companies and their employees.

FieldTypeNotes
string prefix
string
string
stringnullable
stringnullable
stringnullable
stringnullable
stringnullable
stringnullable
stringnullable
stringnullable
enum (default: )

FieldTypeNotes
string prefix
BigNumber = unlimited
booleangrants role
FK → Company

Service: — full CRUD via MedusaService: , , , , , , , , , .


Manages request-for-quote threads between customers and merchants.

FieldTypeNotes
string prefix
enum
string
stringMedusa draft Order
stringactive OrderChange
stringoriginating Cart

FieldTypeNotes
string prefix
string
stringnullable — optional line-item reference
stringnullable — set when sent by admin
stringnullable — set when sent by customer

Service: — full CRUD for and .


Manages cart approval gates and company-level approval configuration.

FieldTypeNotes
string prefix
string
booleandefault:
booleandefault:

FieldTypeNotes
string prefix
string
enum
enum
stringcustomer who submitted
stringnullable — admin or manager who acted

Aggregate status for a cart — denormalized for fast lookup by cart hooks.

FieldTypeNotes
string prefix
string
enum

Service: — full CRUD + .


LinkLeftRightCardinality
(b2b_company) (core)1 : 1
(b2b_company) (core)1 : 1
(b2b_company) (core)1 : N
(core) (b2b_company)1 : 1
(b2b_company) (b2b_approval)1 : 1
(core) (b2b_approval)1 : N
(core) (b2b_approval)1 : 1

Quote → Order, Cart, OrderChange, Customer, and User relationships are registered as read-only virtual links via .


Workflows

Company

IDDescription
Creates a company and its linked in a single atomic operation
Updates company fields
Deletes company and its associated
Adds all current employees to the company's linked
Removes all employees from the linked

Employee

IDDescription
Creates an employee record, links it to a Customer, optionally sets role, and adds the customer to the company's
Updates or
Removes the employee record

Quote

IDDescription
Snapshots the cart into a draft Order + OrderChange, then creates the Quote in status
Advances quote to
Sets quote status to
Confirms the OrderChange, converts the draft Order to a live order, sets status
Sets status to
Generic quote status update
Appends a to the quote thread

Approval

IDDescription
Creates and records and links both to the cart
Updates approval status and syncs the cart's
Creates for a company
Toggles or

Workflow hooks (injected into Medusa core)

Hook pointBehavior
If is set on the cart, creates a Company → Cart link
If is set, creates an Order → Company link
Blocks item add when the cart has a pending approval
Blocks cart update when a pending approval exists
Blocks checkout if a pending approval exists or if the employee's spending limit would be exceeded

REST API

Authentication

SurfaceMechanism
Admin routes ()Medusa admin JWT ()
Store routes ()Customer bearer token or session ()
Role-guarded store routes middleware — resolves role from

Admin endpoints

Companies

MethodPathDescription
List companies. Query: (searches name, email, phone, city), ,
Create one or many companies
Retrieve company
Update company
Delete company
List employees of a company
Create employee
Retrieve employee
Update employee
Delete employee
Update approval settings
Get linked customer group
Link company to a customer group
Unlink customer group

Quotes

MethodPathDescription
List quotes. Query: (customer name/email), , ,
Retrieve quote with messages and order preview
Send quote to customer (→ )
Reject quote (→ )
Append a message to the quote thread

Approvals

MethodPathDescription
List carts pending approval. Query: (company name),
Approve or reject. Body:

Store endpoints

Companies

MethodPathAuthDescription
customerCreate company
customerRetrieve company
customerUpdate company
customerDelete company
customerList employees
company_adminCreate employee
customerRetrieve employee
company_adminUpdate employee
company_adminToggle approval requirements

Quotes

MethodPathAuthDescription
customerList quotes for the authenticated customer
customerCreate a Request for Quote from the current cart
customerRetrieve quote
customerLive order-change preview (pricing, items, totals)
customerAccept quote
customerReject quote
customerAppend message

Carts & approvals

MethodPathAuthDescription
customerSubmit cart for approval
customerAdd multiple line items in one request
customerList approvals for the customer's company (company_admin view)
customerUpdate approval status (company_admin)

Pricing

MethodPathAuthDescription
noneReturns price rules with and conditions — used by storefronts to compute free-shipping thresholds

Admin UI

The plugin registers the following pages in the Medusa admin shell under the route group.

RoutePageDescription
OverviewDashboard with stat cards: total companies, employees, pending quotes, pending approvals
CompaniesSearchable, paginated company table with inline create drawer
Company detailEmployee roster, approval settings toggle, customer-group assignment
QuotesFilterable quote list (status, customer)
Quote detailOrder preview, line items, cost breakdown, message thread, send / reject actions
Quote managementEdit items and pricing within the draft order change
Approval queueCarts pending approval with approve / reject actions

Component library

The plugin includes a shared component library under :

  • — wrapper with column sorting, pagination, and pluggable filter components (, , )
  • — + wrapper
  • / — route-based focus modals for create and edit flows
  • — kebab action dropdown
  • — confirmation dialog for destructive actions
  • — raw JSON section using
  • Table cells — (formatted currency), (image + title)

React Query hooks

All data-fetching is handled via React Query hooks in : , , , , , , , , , , , , , , , , , .


Development

Project layout


Roadmap

  • Fix: check all typing issues and solve them.

  • Implement UI DataTable and useDataTable. Note: maybe not since the ConfigurableDataTable could be the new standard.

  • Implement ConfigurableDataTable for Employees in Company view.

  • For Employee created Customers in Company, make workflow to add them to the B2B Customer Group.

  • Integrate RBAC for approvals?

Approval Settings — How It Works Who is "admin" vs "sales manager"? There is no role system. The two flags on determine which approval types get created for a cart — not who acts on them:

  • → creates an row with
  • → creates an row with

The boolean exists on employees but the approval workflow never checks it. The field on an row is just a label — the > API doesn't enforce that only employees with can approve a row. Any authenticated user who knows the approval ID can call or .

The full flow

  1. Company's ApprovalSettings has flags set (via admin drawer)

  2. Store customer POSTs /store/b2b/carts/:id/approvals → createApprovalStep creates one Approval row per enabled flag → creates an ApprovalStatus row for the cart (status: "pending")

  3. Cart checkout is blocked — completeCartWorkflow.validate hook throws NOT_ALLOWED if any Approval is still "pending"

  4. Someone calls POST /admin/b2b/approvals/:id (or store equivalent) → updateApprovalStep sets that row's status + handled_by → updateApprovalStatusStep re-evaluates the cart: - All approved → ApprovalStatus = "approved" - Any rejected → ApprovalStatus = "rejected" (immediately)

  5. Once all Approval rows are "approved", checkout can proceed Key implication The system is trust-based — there's no enforcement that only a "sales manager" employee can handle a approval. If your use case requires that, it would need to be added: e.g., check or a future field before allowing the approval action.

  • B2B / B2C product and price segmentation — control product visibility and assign prices per audience (B2B only, B2C only, or both), with support for audience-specific price lists. (stashed)

  • Full admin table feature parity — bring all table views in line with Medusa's native admin experience: view configurations (column visibility and ordering), server-side search, composable filter chains, and bulk actions. (use price-lists view as example)


License

MIT — see LICENSE.