Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Rework the Admin API company surface around the tree

Damian Legawiec658b23ac43d7
diff --git a/packages/admin-sdk/src/types/generated/Cart.ts b/packages/admin-sdk/src/types/generated/Cart.ts
index 3b5cc775ec..9157b42225 100644
--- a/packages/admin-sdk/src/types/generated/Cart.ts
+++ b/packages/admin-sdk/src/types/generated/Cart.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 5717facc24acf40387c791698a13996d
+// Typelizer digest 53fccb88c72bae62efd0763a077aa428
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {LineItem, Address, Market, AppliedPromotion} from '@/types'
@@ -8,6 +8,8 @@ interface Cart {
market_id: string | null;
channel_id: string | null;
preferred_stock_location_id: string | null;
+ company_id: string | null;
+ company_name: string | null;
number: string;
token: string;
email: string | null;
diff --git a/packages/admin-sdk/src/types/generated/Catalog.ts b/packages/admin-sdk/src/types/generated/Catalog.ts
new file mode 100644
index 0000000000..cf6c83983e
--- /dev/null
+++ b/packages/admin-sdk/src/types/generated/Catalog.ts
@@ -0,0 +1,19 @@
+// Typelizer digest 704a53e6dc0f43cbe2d8f426c518dbaa
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+import type {CatalogAssignment} from '@/types'
+
+interface Catalog {
+ id: string;
+ name: string;
+ active: boolean;
+ position: number | null;
+ metadata: Record<string, unknown> | null;
+ created_at: string;
+ updated_at: string;
+ price_list_id: string | null;
+ products_count: number;
+ assignments?: Array<CatalogAssignment>;
+}
+
+export default Catalog;
diff --git a/packages/admin-sdk/src/types/generated/CatalogAssignment.ts b/packages/admin-sdk/src/types/generated/CatalogAssignment.ts
new file mode 100644
index 0000000000..ef46cd96cd
--- /dev/null
+++ b/packages/admin-sdk/src/types/generated/CatalogAssignment.ts
@@ -0,0 +1,14 @@
+// Typelizer digest e56595ed7ad8430de5e7e18f97b6bcb9
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+interface CatalogAssignment {
+ id: string;
+ created_at: string;
+ catalog_id: string;
+ assignable_type: string;
+ assignable_id: string;
+ assignable_name: string | null;
+}
+
+export default CatalogAssignment;
diff --git a/packages/admin-sdk/src/types/generated/Channel.ts b/packages/admin-sdk/src/types/generated/Channel.ts
index 76bad0ad9f..e54035b5d1 100644
--- a/packages/admin-sdk/src/types/generated/Channel.ts
+++ b/packages/admin-sdk/src/types/generated/Channel.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 21d71b45666f721799435a0a933febf4
+// Typelizer digest 3cd3bf6193009c67b3d959c1d5c30e97
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -16,6 +16,7 @@ interface Channel {
created_at: string;
updated_at: string;
stock_location_ids: Array<string>;
+ default_catalog_id: string | null;
}
export default Channel;
diff --git a/packages/admin-sdk/src/types/generated/Company.ts b/packages/admin-sdk/src/types/generated/Company.ts
index 19304c2ea0..09bdf86157 100644
--- a/packages/admin-sdk/src/types/generated/Company.ts
+++ b/packages/admin-sdk/src/types/generated/Company.ts
@@ -1,17 +1,23 @@
-// Typelizer digest 3187af0bbdf327a082c7fbf6fbd8dfc6
+// Typelizer digest a718d7dedf90d26cb67f8a85c0dafec0
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {CompanyLocation} from '@/types'
+import type {CompanyAddress, CompanyMembership} from '@/types'
interface Company {
id: string;
name: string;
- external_references: Record<string, string>;
+ external_id: string | null;
+ kind: string;
+ parent_id: string | null;
+ ancestors: Array<{id: string, name: string, kind: string}>;
metadata: Record<string, unknown> | null;
created_at: string;
updated_at: string;
- locations_count: number;
- company_locations?: Array<CompanyLocation>;
+ children_count: number;
+ members_count: number;
+ children?: Array<Company>;
+ addresses?: Array<CompanyAddress>;
+ memberships?: Array<CompanyMembership>;
}
export default Company;
diff --git a/packages/admin-sdk/src/types/generated/CompanyAddress.ts b/packages/admin-sdk/src/types/generated/CompanyAddress.ts
new file mode 100644
index 0000000000..8ac29b80e2
--- /dev/null
+++ b/packages/admin-sdk/src/types/generated/CompanyAddress.ts
@@ -0,0 +1,17 @@
+// Typelizer digest 4dc941a6ef7d72c08a4842f9817ebccb
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+import type {Address} from '@/types'
+
+interface CompanyAddress {
+ id: string;
+ label: string | null;
+ default_billing: boolean;
+ default_shipping: boolean;
+ created_at: string;
+ updated_at: string;
+ company_id: string;
+ address: Address;
+}
+
+export default CompanyAddress;
diff --git a/packages/admin-sdk/src/types/generated/CompanyInvitation.ts b/packages/admin-sdk/src/types/generated/CompanyInvitation.ts
new file mode 100644
index 0000000000..710d375fcd
--- /dev/null
+++ b/packages/admin-sdk/src/types/generated/CompanyInvitation.ts
@@ -0,0 +1,17 @@
+// Typelizer digest fc7d165076ee3bd5218851e810012499
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+interface CompanyInvitation {
+ id: string;
+ email: string;
+ expires_at: string | null;
+ company_id: string;
+ status: string;
+ accepted_at: string | null;
+ revoked_at: string | null;
+ created_at: string;
+ updated_at: string;
+}
+
+export default CompanyInvitation;
diff --git a/packages/admin-sdk/src/types/generated/CompanyLocation.ts b/packages/admin-sdk/src/types/generated/CompanyLocation.ts
deleted file mode 100644
index df01c75ff2..0000000000
--- a/packages/admin-sdk/src/types/generated/CompanyLocation.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-// Typelizer digest c131d6947f6a57f75ea1e17c1f193c41
-//
-// DO NOT MODIFY: This file was automatically generated by Typelizer.
-import type {Address, CompanyContact} from '@/types'
-
-interface CompanyLocation {
- id: string;
- external_references: Record<string, string>;
- name: string;
- metadata: Record<string, unknown> | null;
- created_at: string;
- updated_at: string;
- company_id: string;
- contacts_count: number;
- billing_address: Address;
- shipping_address: Address;
- company_contacts?: Array<CompanyContact>;
-}
-
-export default CompanyLocation;
diff --git a/packages/admin-sdk/src/types/generated/CompanyContact.ts b/packages/admin-sdk/src/types/generated/CompanyMembership.ts
similarity index 58%
rename from packages/admin-sdk/src/types/generated/CompanyContact.ts
rename to packages/admin-sdk/src/types/generated/CompanyMembership.ts
index 298a0e8db5..dcedd609f7 100644
--- a/packages/admin-sdk/src/types/generated/CompanyContact.ts
+++ b/packages/admin-sdk/src/types/generated/CompanyMembership.ts
@@ -1,15 +1,15 @@
-// Typelizer digest 21499b2e7d67af35ef2e3f66c61331f6
+// Typelizer digest a74a8a022b4d52bdf55643a74c84e750
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
-interface CompanyContact {
+interface CompanyMembership {
id: string;
role: string;
- created_at: string;
- updated_at: string;
- company_location_id: string;
+ company_id: string;
customer_id: string;
email: string | null;
+ created_at: string;
+ updated_at: string;
}
-export default CompanyContact;
+export default CompanyMembership;
diff --git a/packages/admin-sdk/src/types/generated/Order.ts b/packages/admin-sdk/src/types/generated/Order.ts
index 0f0c0b84f8..4601c1d4f3 100644
--- a/packages/admin-sdk/src/types/generated/Order.ts
+++ b/packages/admin-sdk/src/types/generated/Order.ts
@@ -1,4 +1,4 @@
-// Typelizer digest a717b917eec36f223eaf5c3ed8b7374a
+// Typelizer digest 2c16570a28d9b05d00513239c5b0d654
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {LineItem, Fulfillment, Payment, Address, GiftCard, Market, Seller, AppliedPromotion, Cart, Channel, StockLocation, PaymentMethod, Customer, Return, Exchange, Claim} from '@/types'
@@ -8,6 +8,8 @@ interface Order {
market_id: string | null;
cart_id: string | null;
channel_id: string | null;
+ company_id: string | null;
+ company_name: string | null;
number: string;
email: string;
customer_note: string | null;
@@ -50,7 +52,6 @@ interface Order {
shipping_address?: Address | null;
gift_card: GiftCard | null;
market: Market | null;
- external_references: Record<string, string>;
status: string;
last_ip_address: string | null;
considered_risky: boolean;
@@ -64,11 +65,9 @@ interface Order {
created_at: string;
updated_at: string;
preferred_stock_location_id: string | null;
- company_location_id: string | null;
seller_id: string | null;
seller?: Seller;
order_group_id: string | null;
- company_id: string | null;
tags: Array<string>;
internal_note: string | null;
internal_note_html: string | null;
diff --git a/packages/admin-sdk/src/types/generated/index.ts b/packages/admin-sdk/src/types/generated/index.ts
index 37fbc53635..a8293e2ced 100644
--- a/packages/admin-sdk/src/types/generated/index.ts
+++ b/packages/admin-sdk/src/types/generated/index.ts
@@ -1,4 +1,4 @@
-// Typelizer digest cd6f30b505a5197901ec71bec46d0194
+// Typelizer digest c9c06e6e174ec5e4ce3dece7585ef77c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
export type { default as Address } from './Address'
@@ -7,6 +7,8 @@ export type { default as AllowedOrigin } from './AllowedOrigin'
export type { default as ApiKey } from './ApiKey'
export type { default as AppliedPromotion } from './AppliedPromotion'
export type { default as Cart } from './Cart'
+export type { default as CatalogAssignment } from './CatalogAssignment'
+export type { default as Catalog } from './Catalog'
export type { default as Category } from './Category'
export type { default as Channel } from './Channel'
export type { default as ClaimLineItem } from './ClaimLineItem'
@@ -17,8 +19,9 @@ export type { default as Collection } from './Collection'
export type { default as CommissionLine } from './CommissionLine'
export type { default as CommissionRate } from './CommissionRate'
export type { default as CommissionRule } from './CommissionRule'
-export type { default as CompanyContact } from './CompanyContact'
-export type { default as CompanyLocation } from './CompanyLocation'
+export type { default as CompanyAddress } from './CompanyAddress'
+export type { default as CompanyInvitation } from './CompanyInvitation'
+export type { default as CompanyMembership } from './CompanyMembership'
export type { default as Company } from './Company'
export type { default as Country } from './Country'
export type { default as CouponCode } from './CouponCode'
@@ -84,7 +87,6 @@ export type { default as SellerRequirement } from './SellerRequirement'
export type { default as SellerRequirementStatus } from './SellerRequirementStatus'
export type { default as SellerRequirementSubmission } from './SellerRequirementSubmission'
export type { default as Seller } from './Seller'
-export type { default as SellerTeamMember } from './SellerTeamMember'
export type { default as SetupTask } from './SetupTask'
export type { default as State } from './State'
export type { default as StockLevel } from './StockLevel'
diff --git a/packages/sdk/src/types/generated/Cart.ts b/packages/sdk/src/types/generated/Cart.ts
index a562f6814f..8fd83523e0 100644
--- a/packages/sdk/src/types/generated/Cart.ts
+++ b/packages/sdk/src/types/generated/Cart.ts
@@ -1,4 +1,4 @@
-// Typelizer digest e2e515c457444f027e6ed790be6e7a63
+// Typelizer digest 75d0e0b3b518c558d0bd78d0fdc885f5
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AppliedPromotion, Fee, LineItem, Fulfillment, Payment, Address, PaymentMethod, GiftCard, Market} from '@/types'
@@ -8,6 +8,8 @@ interface Cart {
market_id: string | null;
channel_id: string | null;
preferred_stock_location_id: string | null;
+ company_id: string | null;
+ company_name: string | null;
number: string;
token: string;
email: string | null;
diff --git a/packages/sdk/src/types/generated/Company.ts b/packages/sdk/src/types/generated/Company.ts
new file mode 100644
index 0000000000..eb8eea1b12
--- /dev/null
+++ b/packages/sdk/src/types/generated/Company.ts
@@ -0,0 +1,14 @@
+// Typelizer digest dfeaed609c71562aeb65c7b9ce72ccbe
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+interface Company {
+ id: string;
+ name: string;
+ external_id: string | null;
+ kind: string;
+ parent_id: string | null;
+ ancestors: Array<{id: string, name: string, kind: string}>;
+}
+
+export default Company;
diff --git a/packages/sdk/src/types/generated/CompanyAddress.ts b/packages/sdk/src/types/generated/CompanyAddress.ts
new file mode 100644
index 0000000000..5783069fcc
--- /dev/null
+++ b/packages/sdk/src/types/generated/CompanyAddress.ts
@@ -0,0 +1,15 @@
+// Typelizer digest 1203cdc5e05faf982622d9d4880e3148
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+import type {Address} from '@/types'
+
+interface CompanyAddress {
+ id: string;
+ label: string | null;
+ default_billing: boolean;
+ default_shipping: boolean;
+ company_id: string;
+ address: Address;
+}
+
+export default CompanyAddress;
diff --git a/packages/sdk/src/types/generated/CompanyInvitation.ts b/packages/sdk/src/types/generated/CompanyInvitation.ts
new file mode 100644
index 0000000000..f796930bad
--- /dev/null
+++ b/packages/sdk/src/types/generated/CompanyInvitation.ts
@@ -0,0 +1,13 @@
+// Typelizer digest 64c9853b6daba6b2afa58959309eb655
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+interface CompanyInvitation {
+ id: string;
+ email: string;
+ expires_at: string | null;
+ company_id: string;
+ status: string;
+}
+
+export default CompanyInvitation;
diff --git a/packages/sdk/src/types/generated/CompanyMembership.ts b/packages/sdk/src/types/generated/CompanyMembership.ts
new file mode 100644
index 0000000000..8eda55dc18
--- /dev/null
+++ b/packages/sdk/src/types/generated/CompanyMembership.ts
@@ -0,0 +1,13 @@
+// Typelizer digest 60e8fc2c76c097d36407f8951fd90a3a
+//
+// DO NOT MODIFY: This file was automatically generated by Typelizer.
+
+interface CompanyMembership {
+ id: string;
+ role: string;
+ company_id: string;
+ customer_id: string;
+ email: string | null;
+}
+
+export default CompanyMembership;
diff --git a/packages/sdk/src/types/generated/Order.ts b/packages/sdk/src/types/generated/Order.ts
index 6cf4bfdac5..a34e3efd55 100644
--- a/packages/sdk/src/types/generated/Order.ts
+++ b/packages/sdk/src/types/generated/Order.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 2aaef3257603d48582113c6bcdea7af9
+// Typelizer digest cae1a481c446403cd251e56a0267e44a
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AppliedPromotion, Fee, LineItem, Fulfillment, Payment, Address, GiftCard, Market} from '@/types'
@@ -8,6 +8,8 @@ interface Order {
market_id: string | null;
cart_id: string | null;
channel_id: string | null;
+ company_id: string | null;
+ company_name: string | null;
number: string;
email: string;
customer_note: string | null;
diff --git a/packages/sdk/src/types/generated/index.ts b/packages/sdk/src/types/generated/index.ts
index e80c109d3a..7579a51bbb 100644
--- a/packages/sdk/src/types/generated/index.ts
+++ b/packages/sdk/src/types/generated/index.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 1a5d67d59598c76d62738f81d67e6d1b
+// Typelizer digest d19dad5af0f46738169cd1bc349796ab
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
export type { default as Address } from './Address'
@@ -11,6 +11,10 @@ export type { default as ClaimLineItem } from './ClaimLineItem'
export type { default as ClaimReason } from './ClaimReason'
export type { default as Claim } from './Claim'
export type { default as Collection } from './Collection'
+export type { default as CompanyAddress } from './CompanyAddress'
+export type { default as CompanyInvitation } from './CompanyInvitation'
+export type { default as CompanyMembership } from './CompanyMembership'
+export type { default as Company } from './Company'
export type { default as Country } from './Country'
export type { default as CreditCard } from './CreditCard'
export type { default as Currency } from './Currency'
diff --git a/packages/sdk/src/zod/generated/Cart.ts b/packages/sdk/src/zod/generated/Cart.ts
index 460a655b84..0f8f539a18 100644
--- a/packages/sdk/src/zod/generated/Cart.ts
+++ b/packages/sdk/src/zod/generated/Cart.ts
@@ -15,6 +15,8 @@ export const CartSchema = z.object({
market_id: z.string().nullable(),
channel_id: z.string().nullable(),
preferred_stock_location_id: z.string().nullable(),
+ company_id: z.string().nullable(),
+ company_name: z.string().nullable(),
number: z.string(),
token: z.string(),
email: z.string().nullable(),
diff --git a/packages/sdk/src/zod/generated/Company.ts b/packages/sdk/src/zod/generated/Company.ts
new file mode 100644
index 0000000000..10156fc368
--- /dev/null
+++ b/packages/sdk/src/zod/generated/Company.ts
@@ -0,0 +1,13 @@
+// This file is auto-generated. Do not edit directly.
+import { z } from 'zod';
+
+export const CompanySchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ external_id: z.string().nullable(),
+ kind: z.string(),
+ parent_id: z.string().nullable(),
+ ancestors: z.array(z.object({ id: z.string(), name: z.string(), kind: z.string() })),
+});
+
+export type Company = z.infer<typeof CompanySchema>;
diff --git a/packages/sdk/src/zod/generated/CompanyAddress.ts b/packages/sdk/src/zod/generated/CompanyAddress.ts
new file mode 100644
index 0000000000..f08e65e63b
--- /dev/null
+++ b/packages/sdk/src/zod/generated/CompanyAddress.ts
@@ -0,0 +1,14 @@
+// This file is auto-generated. Do not edit directly.
+import { z } from 'zod';
+import { AddressSchema } from './Address';
+
+export const CompanyAddressSchema = z.object({
+ id: z.string(),
+ label: z.string().nullable(),
+ default_billing: z.boolean(),
+ default_shipping: z.boolean(),
+ company_id: z.string(),
+ address: AddressSchema,
+});
+
+export type CompanyAddress = z.infer<typeof CompanyAddressSchema>;
diff --git a/packages/sdk/src/zod/generated/CompanyInvitation.ts b/packages/sdk/src/zod/generated/CompanyInvitation.ts
new file mode 100644
index 0000000000..bb11cc1141
--- /dev/null
+++ b/packages/sdk/src/zod/generated/CompanyInvitation.ts
@@ -0,0 +1,12 @@
+// This file is auto-generated. Do not edit directly.
+import { z } from 'zod';
+
+export const CompanyInvitationSchema = z.object({
+ id: z.string(),
+ email: z.string(),
+ expires_at: z.string().nullable(),
+ company_id: z.string(),
+ status: z.string(),
+});
+
+export type CompanyInvitation = z.infer<typeof CompanyInvitationSchema>;
diff --git a/packages/sdk/src/zod/generated/CompanyMembership.ts b/packages/sdk/src/zod/generated/CompanyMembership.ts
new file mode 100644
index 0000000000..2185efaab8
--- /dev/null
+++ b/packages/sdk/src/zod/generated/CompanyMembership.ts
@@ -0,0 +1,12 @@
+// This file is auto-generated. Do not edit directly.
+import { z } from 'zod';
+
+export const CompanyMembershipSchema = z.object({
+ id: z.string(),
+ role: z.string(),
+ company_id: z.string(),
+ customer_id: z.string(),
+ email: z.string().nullable(),
+});
+
+export type CompanyMembership = z.infer<typeof CompanyMembershipSchema>;
diff --git a/packages/sdk/src/zod/generated/Order.ts b/packages/sdk/src/zod/generated/Order.ts
index 247dbfe92f..9d6c498f3e 100644
--- a/packages/sdk/src/zod/generated/Order.ts
+++ b/packages/sdk/src/zod/generated/Order.ts
@@ -14,6 +14,8 @@ export const OrderSchema = z.object({
market_id: z.string().nullable(),
cart_id: z.string().nullable(),
channel_id: z.string().nullable(),
+ company_id: z.string().nullable(),
+ company_name: z.string().nullable(),
number: z.string(),
email: z.string(),
customer_note: z.string().nullable(),
diff --git a/packages/sdk/src/zod/generated/index.ts b/packages/sdk/src/zod/generated/index.ts
index 67c027a351..6bdd7b1223 100644
--- a/packages/sdk/src/zod/generated/index.ts
+++ b/packages/sdk/src/zod/generated/index.ts
@@ -9,6 +9,10 @@ export { ClaimSchema, type Claim } from './Claim';
export { ClaimLineItemSchema, type ClaimLineItem } from './ClaimLineItem';
export { ClaimReasonSchema, type ClaimReason } from './ClaimReason';
export { CollectionSchema, type Collection } from './Collection';
+export { CompanySchema, type Company } from './Company';
+export { CompanyAddressSchema, type CompanyAddress } from './CompanyAddress';
+export { CompanyInvitationSchema, type CompanyInvitation } from './CompanyInvitation';
+export { CompanyMembershipSchema, type CompanyMembership } from './CompanyMembership';
export { CountrySchema, type Country } from './Country';
export { CreditCardSchema, type CreditCard } from './CreditCard';
export { CurrencySchema, type Currency } from './Currency';
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies/addresses_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies/addresses_controller.rb
new file mode 100644
index 0000000000..9615069c76
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/companies/addresses_controller.rb
@@ -0,0 +1,40 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ module Companies
+ # A node's address book. Listing and creating live here; editing and
+ # deleting an entry are addressed directly, since a caller holding
+ # an entry id shouldn't have to know its company.
+ class AddressesController < BaseController
+ include Spree::Api::V3::Admin::Concerns::CompanyAddressParams
+
+ before_action :authorize_parent_access!
+
+ protected
+
+ def model_class
+ Spree::CompanyAddress
+ end
+
+ def serializer_class
+ Spree.api.admin_company_address_serializer
+ end
+
+ def scope
+ @parent.company_addresses
+ end
+
+ def parent_association
+ :company_addresses
+ end
+
+ def collection_includes
+ [:address]
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies/invitations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies/invitations_controller.rb
new file mode 100644
index 0000000000..a9f76f52a9
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/companies/invitations_controller.rb
@@ -0,0 +1,33 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ module Companies
+ # The pending invitations into one node. Revocation is addressed
+ # directly by invitation id (CompanyInvitationsController).
+ class InvitationsController < BaseController
+ before_action :authorize_parent_access!
+
+ protected
+
+ def model_class
+ Spree::CompanyInvitation
+ end
+
+ def serializer_class
+ Spree.api.admin_company_invitation_serializer
+ end
+
+ def scope
+ @parent.invitations
+ end
+
+ def parent_association
+ :invitations
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies/locations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies/locations_controller.rb
deleted file mode 100644
index 75f22e0034..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/companies/locations_controller.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- module Companies
- # Branches of one company. Listing and creating live here; editing and
- # deleting a branch are addressed directly, since a caller holding a
- # branch id shouldn't have to know its company.
- class LocationsController < BaseController
- include Spree::Api::V3::Admin::Concerns::CompanyLocationParams
-
- before_action :authorize_parent_access!
-
- protected
-
- def model_class
- Spree::CompanyLocation
- end
-
- def serializer_class
- Spree.api.admin_company_location_serializer
- end
-
- def scope
- @parent.company_locations
- end
-
- def parent_association
- :company_locations
- end
-
- def collection_includes
- [:billing_address, :shipping_address, :company_contacts]
- end
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies/memberships_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies/memberships_controller.rb
new file mode 100644
index 0000000000..6e2cf8320a
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/companies/memberships_controller.rb
@@ -0,0 +1,67 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ module Companies
+ # The people with standing over one node. Creation takes an email
+ # and does the right thing: a membership for an existing customer,
+ # an invitation otherwise — the same convergent behavior as the
+ # storefront endpoint.
+ class MembershipsController < BaseController
+ # Overriding `scope` bypasses the base class's `accessible_by`, so
+ # without this a role with no customer permission could still list
+ # who buys for a node — and memberships carry customer emails.
+ before_action :authorize_parent_access!
+
+ # POST /api/v3/admin/companies/:company_id/memberships
+ def create
+ result = Spree.company_add_member_service.call(
+ company: @parent,
+ email: params.require(:customer_email),
+ role: params[:role],
+ metadata: params[:metadata].respond_to?(:permit) ? params[:metadata].permit!.to_h : params[:metadata]
+ )
+
+ if result.success?
+ serializer = if result.value.is_a?(Spree::CompanyInvitation)
+ Spree.api.admin_company_invitation_serializer
+ else
+ serializer_class
+ end
+ render json: serializer.new(result.value, params: serializer_params).to_h, status: :created
+ else
+ render_validation_error(result.value.errors)
+ end
+ end
+
+ protected
+
+ def model_class
+ Spree::CompanyMembership
+ end
+
+ def serializer_class
+ Spree.api.admin_company_membership_serializer
+ end
+
+ def scope
+ @parent.memberships
+ end
+
+ def parent_association
+ :memberships
+ end
+
+ def collection_includes
+ [:customer]
+ end
+
+ def permitted_params
+ params.permit(:customer_email, :role, metadata: {})
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/companies_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/companies_controller.rb
index c23a734d4d..30e50256e0 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/companies_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/companies_controller.rb
@@ -2,8 +2,13 @@ module Spree
module Api
module V3
module Admin
- # Business customers. A company is a customer record, so it answers to
- # the customer scopes rather than earning one of its own.
+ # Company nodes — tree-aware CRUD. A company is a customer record, so
+ # it answers to the customer scopes rather than earning one of its own.
+ #
+ # The tree is driven by Ransack: `?q[parent_id_null]=1` lists roots,
+ # `?q[parent_id_eq]=` a node's children. Deleting a node destroys its
+ # subtree. Re-parenting is an ordinary update — the model revalidates
+ # depth, cycle and store.
class CompaniesController < ResourceController
scoped_resource :customers
@@ -22,11 +27,30 @@ module Spree
end
def collection_includes
- [:company_locations, :external_references]
+ [:children, :memberships, :external_references]
end
def permitted_params
- params.permit(*model_additional_permitted_attributes, :name, metadata: {})
+ permitted = params.permit(*model_additional_permitted_attributes,
+ :name, :kind, :parent_id, metadata: {})
+ resolve_parent_param(permitted)
+ end
+
+ private
+
+ # The parent is an incidental lookup like any other: resolved through
+ # the store so another tenant's node 404s rather than becoming this
+ # node's parent.
+ def resolve_parent_param(permitted)
+ return permitted unless permitted.key?(:parent_id)
+
+ permitted[:parent_id] =
+ if permitted[:parent_id].present?
+ current_store.companies.find_by_prefix_id!(permitted[:parent_id]).id
+ else
+ nil
+ end
+ permitted
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_addresses_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_addresses_controller.rb
new file mode 100644
index 0000000000..5c73f7abb3
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/company_addresses_controller.rb
@@ -0,0 +1,34 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # An address-book entry addressed directly. Reached through the
+ # store's companies so an entry of another tenant's company is a 404.
+ class CompanyAddressesController < ResourceController
+ include Spree::Api::V3::Admin::Concerns::CompanyAddressParams
+
+ scoped_resource :customers
+
+ protected
+
+ def model_class
+ Spree::CompanyAddress
+ end
+
+ def serializer_class
+ Spree.api.admin_company_address_serializer
+ end
+
+ def scope
+ Spree::CompanyAddress.where(company_id: current_store.companies.select(:id)).
+ accessible_by(current_ability, ability_action_for_request)
+ end
+
+ def collection_includes
+ [:address]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_contacts_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_contacts_controller.rb
deleted file mode 100644
index 2a1f0baae8..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/company_contacts_controller.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- # Removing a buyer's authority to purchase for a branch, addressed
- # directly by contact id.
- class CompanyContactsController < ResourceController
- scoped_resource :customers
-
- protected
-
- def model_class
- Spree::CompanyContact
- end
-
- def serializer_class
- Spree.api.admin_company_contact_serializer
- end
-
- def scope
- Spree::CompanyContact.where(
- company_location_id: Spree::CompanyLocation.where(company_id: current_store.companies.select(:id)).select(:id)
- ).accessible_by(current_ability, ability_action_for_request)
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_invitations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_invitations_controller.rb
new file mode 100644
index 0000000000..fdf0816a7f
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/company_invitations_controller.rb
@@ -0,0 +1,42 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # An invitation addressed directly. DELETE revokes rather than
+ # destroys — the row is the record that an invite went out, and a
+ # revoked token has to keep 404ing rather than becoming reissuable.
+ class CompanyInvitationsController < ResourceController
+ scoped_resource :customers
+
+ # DELETE /api/v3/admin/company_invitations/:id
+ def destroy
+ if @resource.revoke!
+ head :no_content
+ else
+ render_error(
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:validation_error],
+ message: Spree.t('company_invitations.not_pending'),
+ status: :unprocessable_content
+ )
+ end
+ end
+
+ protected
+
+ def model_class
+ Spree::CompanyInvitation
+ end
+
+ def serializer_class
+ Spree.api.admin_company_invitation_serializer
+ end
+
+ def scope
+ Spree::CompanyInvitation.where(company_id: current_store.companies.select(:id)).
+ accessible_by(current_ability, ability_action_for_request)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_locations/contacts_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_locations/contacts_controller.rb
deleted file mode 100644
index 2dc989403e..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/company_locations/contacts_controller.rb
+++ /dev/null
@@ -1,59 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- module CompanyLocations
- # The buyers who purchase for one branch.
- class ContactsController < ResourceController
- scoped_resource :customers
-
- # Overriding `scope` bypasses the base class's `accessible_by`, so
- # without this a role with no customer permission could still list
- # who buys for a branch — and contacts carry customer emails.
- before_action :authorize_parent_access!
-
- protected
-
- def model_class
- Spree::CompanyContact
- end
-
- def serializer_class
- Spree.api.admin_company_contact_serializer
- end
-
- # Filtered by ability as well as by store: a branch the caller may
- # not act on is not found rather than refused, so its existence
- # doesn't leak.
- def set_parent
- @parent = Spree::CompanyLocation.
- where(company_id: current_store.companies.select(:id)).
- accessible_by(current_ability, parent_ability_action).
- find_by_prefix_id!(params[:company_location_id])
- end
-
- def authorize_parent_access!
- authorize_parent!(@parent)
- end
-
- def scope
- @parent.company_contacts
- end
-
- def parent_association
- :company_contacts
- end
-
- def collection_includes
- [:customer]
- end
-
- def permitted_params
- params.permit(*model_additional_permitted_attributes, :customer_id, :role)
- end
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_locations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_locations_controller.rb
deleted file mode 100644
index 239d42d769..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/company_locations_controller.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- # A branch addressed directly. Reached through the store's companies so a
- # branch of another tenant's company is a 404.
- class CompanyLocationsController < ResourceController
- include Spree::Api::V3::Admin::Concerns::CompanyLocationParams
-
- scoped_resource :customers
-
- protected
-
- def model_class
- Spree::CompanyLocation
- end
-
- def serializer_class
- Spree.api.admin_company_location_serializer
- end
-
- def scope
- Spree::CompanyLocation.where(company_id: current_store.companies.select(:id)).
- accessible_by(current_ability, ability_action_for_request)
- end
-
- def collection_includes
- [:billing_address, :shipping_address, :company_contacts]
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/company_memberships_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/company_memberships_controller.rb
new file mode 100644
index 0000000000..cb9912ec3b
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/company_memberships_controller.rb
@@ -0,0 +1,28 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Removing a buyer's standing over a node, addressed directly by
+ # membership id.
+ class CompanyMembershipsController < ResourceController
+ scoped_resource :customers
+
+ protected
+
+ def model_class
+ Spree::CompanyMembership
+ end
+
+ def serializer_class
+ Spree.api.admin_company_membership_serializer
+ end
+
+ def scope
+ Spree::CompanyMembership.where(company_id: current_store.companies.select(:id)).
+ accessible_by(current_ability, ability_action_for_request)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/concerns/company_address_params.rb b/spree/api/app/controllers/spree/api/v3/admin/concerns/company_address_params.rb
new file mode 100644
index 0000000000..3507260a60
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/concerns/company_address_params.rb
@@ -0,0 +1,24 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ module Concerns
+ # Shared by the two places an address-book entry is written: nested
+ # creation under a company, and direct edits by entry id.
+ module CompanyAddressParams
+ extend ActiveSupport::Concern
+
+ protected
+
+ def permitted_params
+ params.permit(
+ :label, :default_billing, :default_shipping,
+ address: Spree::Api::V3::AddressParams::ADDRESS_KEYS
+ )
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/concerns/company_location_params.rb b/spree/api/app/controllers/spree/api/v3/admin/concerns/company_location_params.rb
deleted file mode 100644
index 3635031c2c..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/concerns/company_location_params.rb
+++ /dev/null
@@ -1,28 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- module Concerns
- # Shared by the two places a branch is written: nested creation under a
- # company, and direct edits by branch id.
- module CompanyLocationParams
- extend ActiveSupport::Concern
-
- # Enumerated rather than borrowing the legacy global list, which
- # permits :id, :user_id and :deleted_at.
- protected
-
- def permitted_params
- params.permit(
- :name,
- metadata: {},
- billing_address: Spree::Api::V3::AddressParams::ADDRESS_KEYS,
- shipping_address: Spree::Api::V3::AddressParams::ADDRESS_KEYS
- )
- end
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
index 5d5abed993..aaf8d404c0 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
@@ -192,7 +192,7 @@ module Spree
:email, :customer_id, :user_id,
:customer_note, :internal_note,
:currency, :locale, :market_id, :channel_id,
- :preferred_stock_location_id, :company_location_id,
+ :preferred_stock_location_id, :company_id,
metadata: {},
tags: [],
shipping_address: address_permitted_keys,
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_address_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_address_serializer.rb
new file mode 100644
index 0000000000..a3f2b331e4
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/admin/company_address_serializer.rb
@@ -0,0 +1,25 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # An entry in a company node's address book. Admin-only shape; the
+ # storefront self-service surface serializes the same record through
+ # its own class.
+ class CompanyAddressSerializer < V3::BaseSerializer
+ typelize label: [:string, nullable: true], company_id: :string,
+ default_billing: :boolean, default_shipping: :boolean
+
+ attributes :label, :default_billing, :default_shipping,
+ created_at: :iso8601, updated_at: :iso8601
+
+ # An entry always belongs to a company, so this is never null.
+ attribute :company_id do |entry|
+ entry.company.prefixed_id
+ end
+
+ one :address, resource: proc { Spree.api.admin_address_serializer }
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_contact_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_contact_serializer.rb
deleted file mode 100644
index d93c4a1223..0000000000
--- a/spree/api/app/serializers/spree/api/v3/admin/company_contact_serializer.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- class CompanyContactSerializer < V3::BaseSerializer
- typelize role: :string, company_location_id: :string, customer_id: :string,
- email: [:string, nullable: true]
-
- attributes :role, created_at: :iso8601, updated_at: :iso8601
-
- # Both associations are required, so these are never null and the
- # types above say so. Safe navigation here would have promised a null
- # the schema cannot produce.
- attribute :company_location_id do |contact|
- contact.company_location.prefixed_id
- end
-
- attribute :customer_id do |contact|
- contact.customer.prefixed_id
- end
-
- # The dashboard lists contacts by who they are, and would otherwise
- # need a customer request per row.
- attribute :email do |contact|
- contact.customer.email
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_invitation_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_invitation_serializer.rb
new file mode 100644
index 0000000000..617353b8dc
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/admin/company_invitation_serializer.rb
@@ -0,0 +1,15 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ class CompanyInvitationSerializer < V3::CompanyInvitationSerializer
+ typelize accepted_at: [:string, nullable: true],
+ revoked_at: [:string, nullable: true]
+
+ attributes accepted_at: :iso8601, revoked_at: :iso8601,
+ created_at: :iso8601, updated_at: :iso8601
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_location_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_location_serializer.rb
deleted file mode 100644
index 6c423c458d..0000000000
--- a/spree/api/app/serializers/spree/api/v3/admin/company_location_serializer.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- # Admin-only: branches are back-office records with no storefront
- # surface, so there is no store serializer to extend. Deliberately not
- # sharing a parent with the company and contact serializers — Typelizer
- # emits one TS type per serializer, and an abstract one would reach the
- # admin SDK without any endpoint returning it.
- class CompanyLocationSerializer < V3::BaseSerializer
- include Concerns::ExternalReferencesAttribute
-
- typelize name: :string,
- company_id: :string, contacts_count: :number,
- metadata: 'Record<string, unknown> | null'
-
- attributes :name, :metadata,
- created_at: :iso8601, updated_at: :iso8601
-
- # A branch always belongs to a company, so this is never null.
- attribute :company_id do |location|
- location.company.prefixed_id
- end
-
- attribute :contacts_count do |location|
- location.company_contacts.size
- end
-
- one :billing_address, resource: proc { Spree.api.admin_address_serializer }
- one :shipping_address, resource: proc { Spree.api.admin_address_serializer }
-
- many :company_contacts,
- resource: proc { Spree.api.admin_company_contact_serializer },
- if: proc { expand?('company_contacts') }
- end
- end
- end
- end
-end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_membership_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_membership_serializer.rb
new file mode 100644
index 0000000000..f3c5c3dce3
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/admin/company_membership_serializer.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ class CompanyMembershipSerializer < V3::CompanyMembershipSerializer
+ attributes created_at: :iso8601, updated_at: :iso8601
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/company_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/company_serializer.rb
index cab8a6a9e5..fd3ff8b0c8 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/company_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/company_serializer.rb
@@ -5,18 +5,32 @@ module Spree
class CompanySerializer < V3::CompanySerializer
include Concerns::ExternalReferencesAttribute
- typelize locations_count: :number, metadata: 'Record<string, unknown> | null'
+ typelize children_count: :number, members_count: :number,
+ metadata: 'Record<string, unknown> | null'
attributes :metadata, created_at: :iso8601, updated_at: :iso8601
- # Saves the dashboard a request per row just to say "3 branches".
- attribute :locations_count do |company|
- company.company_locations.size
+ # Saves the dashboard a request per row just to render the tree
+ # expander and "3 members".
+ attribute :children_count do |company|
+ company.children.size
end
- many :company_locations,
- resource: proc { Spree.api.admin_company_location_serializer },
- if: proc { expand?('company_locations') }
+ attribute :members_count do |company|
+ company.memberships.size
+ end
+
+ many :children,
+ resource: proc { Spree.api.admin_company_serializer },
+ if: proc { expand?('children') }
+
+ many :company_addresses, key: :addresses,
+ resource: proc { Spree.api.admin_company_address_serializer },
+ if: proc { expand?('addresses') }
+
+ many :memberships,
+ resource: proc { Spree.api.admin_company_membership_serializer },
+ if: proc { expand?('memberships') }
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb
index 128d815aea..c7c2187283 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb
@@ -8,8 +8,8 @@ module Spree
include Concerns::ExternalReferencesAttribute
- typelize company_location_id: [:string, nullable: true],
- company_id: [:string, nullable: true],
+ typelize company_id: [:string, nullable: true],
+ company_name: [:string, nullable: true],
seller_id: [:string, nullable: true],
order_group_id: [:string, nullable: true]
@@ -53,11 +53,15 @@ module Spree
order.preferred_stock_location&.prefixed_id
end
- # Which business the order is for, so the dashboard can show and change
- # it. Read back from the column rather than #resolved_company_location:
+ # Which company node the order is for, so the dashboard can show and
+ # change it. Read back from the column rather than #resolved_company:
# a placed order must report what it was stamped with.
- attribute :company_location_id do |order|
- order.company_location&.prefixed_id
+ attribute :company_id do |order|
+ order.company&.prefixed_id
+ end
+
+ attribute :company_name do |order|
+ order.company&.name
end
# Whose sale this is — nil on the operator's own goods. The full
@@ -81,10 +85,6 @@ module Spree
Spree::OrderGroup.prefixed_id_for(order.order_group_id)
end
- attribute :company_id do |order|
- order.company_location&.company&.prefixed_id
- end
-
attribute :tags do |order|
order.tags.map(&:name) # not pluck as we preload tags
end
diff --git a/spree/api/app/serializers/spree/api/v3/company_address_serializer.rb b/spree/api/app/serializers/spree/api/v3/company_address_serializer.rb
new file mode 100644
index 0000000000..ded9563326
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/company_address_serializer.rb
@@ -0,0 +1,20 @@
+module Spree
+ module Api
+ module V3
+ # An entry in a company node's address book, as members manage it.
+ class CompanyAddressSerializer < BaseSerializer
+ typelize label: [:string, nullable: true], company_id: :string,
+ default_billing: :boolean, default_shipping: :boolean
+
+ attributes :label, :default_billing, :default_shipping
+
+ # An entry always belongs to a company, so this is never null.
+ attribute :company_id do |entry|
+ entry.company.prefixed_id
+ end
+
+ one :address, resource: proc { Spree.api.address_serializer }
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/company_invitation_serializer.rb b/spree/api/app/serializers/spree/api/v3/company_invitation_serializer.rb
new file mode 100644
index 0000000000..627582f77c
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/company_invitation_serializer.rb
@@ -0,0 +1,28 @@
+module Spree
+ module Api
+ module V3
+ # A pending (or spent) invitation into a company node. The token is
+ # deliberately absent — it travels only in the invite email.
+ class CompanyInvitationSerializer < BaseSerializer
+ typelize email: :string, company_id: :string, status: :string,
+ expires_at: [:string, nullable: true]
+
+ attributes :email, expires_at: :iso8601
+
+ attribute :company_id do |invitation|
+ invitation.company.prefixed_id
+ end
+
+ # Derived, not a column: expiry is a date fact, acceptance and
+ # revocation are stamps.
+ attribute :status do |invitation|
+ if invitation.accepted? then 'accepted'
+ elsif invitation.revoked? then 'revoked'
+ elsif invitation.expired? then 'expired'
+ else 'pending'
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/company_membership_serializer.rb b/spree/api/app/serializers/spree/api/v3/company_membership_serializer.rb
new file mode 100644
index 0000000000..58eb920f22
--- /dev/null
+++ b/spree/api/app/serializers/spree/api/v3/company_membership_serializer.rb
@@ -0,0 +1,30 @@
+module Spree
+ module Api
+ module V3
+ # A person with standing over a company node, as the storefront
+ # self-service surface lists them. Within a company OSS trusts every
+ # member, so members see each other.
+ class CompanyMembershipSerializer < BaseSerializer
+ typelize role: :string, company_id: :string, customer_id: :string,
+ email: [:string, nullable: true]
+
+ attributes :role
+
+ # Both associations are required, so these are never null and the
+ # types above say so.
+ attribute :company_id do |membership|
+ membership.company.prefixed_id
+ end
+
+ attribute :customer_id do |membership|
+ membership.customer.prefixed_id
+ end
+
+ # Lists members by who they are without a customer request per row.
+ attribute :email do |membership|
+ membership.customer.email
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/serializers/spree/api/v3/company_serializer.rb b/spree/api/app/serializers/spree/api/v3/company_serializer.rb
index d6a508081e..10865fc3f8 100644
--- a/spree/api/app/serializers/spree/api/v3/company_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/company_serializer.rb
@@ -1,14 +1,29 @@
module Spree
module Api
module V3
- # Companies have no Store API — this exists as the payload for the
- # company lifecycle events, the same reason ImportSerializer and
- # ExportSerializer sit in this namespace. The admin serializer extends it
- # so back-office fields stay in one place.
+ # The company node as members and event subscribers see it: the
+ # storefront self-service surface renders it directly, lifecycle events
+ # use it as their payload, and the admin serializer extends it so
+ # back-office fields stay in one place.
class CompanySerializer < BaseSerializer
- typelize name: :string
+ typelize name: :string,
+ kind: :string, parent_id: [:string, nullable: true],
+ ancestors: 'Array<{id: string, name: string, kind: string}>'
- attributes :name
+ attributes :name, :kind
+
+ attribute :parent_id do |company|
+ company.parent&.prefixed_id
+ end
+
+ # The path above this node, root first — lets a client render
+ # "Acme / EMEA / Berlin" without a request per level. Bounded by the
+ # tree's depth cap.
+ attribute :ancestors do |company|
+ company.ancestors.reverse.map do |node|
+ { 'id' => node.prefixed_id, 'name' => node.name, 'kind' => node.kind }
+ end
+ end
end
end
end
diff --git a/spree/api/config/brakeman.ignore b/spree/api/config/brakeman.ignore
index 28c4ab41d2..ee1d1fd3d9 100644
--- a/spree/api/config/brakeman.ignore
+++ b/spree/api/config/brakeman.ignore
@@ -18,30 +18,7 @@
79
],
"note": ""
- },
- {
- "warning_type": "Mass Assignment",
- "warning_code": 105,
- "fingerprint": "26cd4e4bf0db3c32251ebb3814f52d4a72f612b20c6a668f17b3a385526c5663",
- "check_name": "PermitAttributes",
- "message": "Potentially dangerous key allowed for mass assignment",
- "file": "app/controllers/spree/api/v3/admin/company_locations/contacts_controller.rb",
- "line": 52,
- "link": "https://brakemanscanner.org/docs/warning_types/mass_assignment/",
- "code": "params.permit(*model_additional_permitted_attributes, :customer_id, :role)",
- "render_path": null,
- "location": {
- "type": "method",
- "class": "Spree::Api::V3::Admin::CompanyLocations::ContactsController",
- "method": "permitted_params"
- },
- "user_input": ":role",
- "confidence": "Medium",
- "cwe_id": [
- 915
- ],
- "note": "Spree::CompanyContact#role names the buyer's role at a company branch (buyer, approver) and grants nothing \u2014 permissions and purchase authority are not modelled in 6.0. Staff-only endpoint behind the write_customers permission, so it cannot be reached by the customer it describes."
}
],
"brakeman_version": "7.1.0"
-}
+}
\ No newline at end of file
diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rb
index 7ce39279f4..804cb216e9 100644
--- a/spree/api/config/routes.rb
+++ b/spree/api/config/routes.rb
@@ -118,6 +118,29 @@ Spree::Core::Engine.add_routes do
end
end
+ # Company self-service — the buyer's organization directory
+ # (docs/plans/6.0-b2b-companies-and-catalogs.md). Authorization is
+ # standing + the storefront access policy, never roles.
+ get 'account/companies', to: 'account/companies#index'
+
+ resources :companies, only: [:show, :update] do
+ resources :addresses, only: [:index, :create], controller: 'companies/addresses'
+ # POST takes customer_email — a membership for an existing customer,
+ # an invitation otherwise.
+ resources :members, only: [:index, :create], controller: 'companies/members'
+ resources :invitations, only: [:index], controller: 'companies/invitations'
+ # Completed purchases across the node's subtree.
+ resources :orders, only: [:index], controller: 'companies/orders'
+ end
+
+ resources :company_addresses, only: [:update, :destroy]
+ resources :company_members, only: [:destroy]
+ # DELETE revokes (by prefixed id, member-authenticated); GET and
+ # accept are the unauthenticated token flow from the invite email.
+ resources :company_invitations, only: [:destroy]
+ get 'company_invitations/:token', to: 'company_invitations#show', as: :company_invitation_lookup
+ post 'company_invitations/:token/accept', to: 'company_invitations#accept'
+
# Wishlists
resources :wishlists do
resources :items, only: [:create, :update, :destroy], controller: 'wishlist_items'
@@ -508,15 +531,17 @@ Spree::Core::Engine.add_routes do
# everything an operator acts on lives on the orders inside it.
resources :order_groups, only: [:index, :show]
- # Business customers. Branches and their buyers are created under their
- # parent and then addressed directly, so a caller holding a branch id
- # does not have to know which company it belongs to.
+ # Business customers — a tree of company nodes. Nested collections are
+ # listed and created under their node and then addressed directly, so
+ # a caller holding an entry id does not have to know its company.
resources :companies do
- # Listed and created under the company you arrived from; read and
- # written by their own id below, which has to exist anyway as the
- # parent path for contacts (three levels of nesting is over the cap —
- # docs/plans/6.0-admin-api.md).
- resources :locations, controller: 'companies/locations', only: [:index, :create]
+ # The node's address book.
+ resources :addresses, controller: 'companies/addresses', only: [:index, :create]
+
+ # The people with standing over the node. Creation takes an email:
+ # a membership for an existing customer, an invitation otherwise.
+ resources :memberships, controller: 'companies/memberships', only: [:index, :create]
+ resources :invitations, controller: 'companies/invitations', only: [:index]
# The business's own registration — the number on its invoices, which
# outranks the buyer's own. Same shape as the customer's.
@@ -538,11 +563,25 @@ Spree::Core::Engine.add_routes do
end
end
- resources :company_locations, only: [:show, :update, :destroy] do
- resources :contacts, controller: 'company_locations/contacts', only: [:index, :create]
- end
+ resources :company_addresses, only: [:show, :update, :destroy]
+ resources :company_memberships, only: [:show, :destroy]
+ # DELETE revokes the invitation rather than erasing the record.
+ resources :company_invitations, only: [:show, :destroy]
- resources :company_contacts, only: [:show, :destroy]
+ # Catalogs — assortment + optional price list, assigned to an audience
+ # (channel, customer group, market, or company subtree).
+ resources :catalogs do
+ member do
+ post :assign
+ end
+ # Manual assortment membership + ordering within the catalog.
+ resources :products, controller: 'catalogs/products', only: [:index, :create, :destroy] do
+ member do
+ patch :reposition
+ end
+ end
+ end
+ resources :catalog_assignments, only: [:show, :destroy]
# Price lists
resources :price_lists do
diff --git a/spree/api/lib/spree/api/dependencies.rb b/spree/api/lib/spree/api/dependencies.rb
index cee8f6fa93..5dc4abbbd9 100644
--- a/spree/api/lib/spree/api/dependencies.rb
+++ b/spree/api/lib/spree/api/dependencies.rb
@@ -150,9 +150,15 @@ module Spree
admin_tax_identifier_serializer: 'Spree::Api::V3::Admin::TaxIdentifierSerializer',
company_serializer: 'Spree::Api::V3::CompanySerializer',
+ company_address_serializer: 'Spree::Api::V3::CompanyAddressSerializer',
+ company_membership_serializer: 'Spree::Api::V3::CompanyMembershipSerializer',
+ company_invitation_serializer: 'Spree::Api::V3::CompanyInvitationSerializer',
admin_company_serializer: 'Spree::Api::V3::Admin::CompanySerializer',
- admin_company_location_serializer: 'Spree::Api::V3::Admin::CompanyLocationSerializer',
- admin_company_contact_serializer: 'Spree::Api::V3::Admin::CompanyContactSerializer',
+ admin_company_address_serializer: 'Spree::Api::V3::Admin::CompanyAddressSerializer',
+ admin_company_membership_serializer: 'Spree::Api::V3::Admin::CompanyMembershipSerializer',
+ admin_company_invitation_serializer: 'Spree::Api::V3::Admin::CompanyInvitationSerializer',
+ admin_catalog_serializer: 'Spree::Api::V3::Admin::CatalogSerializer',
+ admin_catalog_assignment_serializer: 'Spree::Api::V3::Admin::CatalogAssignmentSerializer',
admin_tax_exemption_certificate_serializer: 'Spree::Api::V3::Admin::TaxExemptionCertificateSerializer',
admin_admin_user_serializer: 'Spree::Api::V3::Admin::AdminUserSerializer',
admin_seller_team_member_serializer: 'Spree::Api::V3::Admin::SellerTeamMemberSerializer',
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/addresses_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/addresses_controller_spec.rb
new file mode 100644
index 0000000000..6e63042246
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/addresses_controller_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::Companies::AddressesController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:company) { create(:company, store: store) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'lists the node address book' do
+ entry = create(:company_address, company: company, label: 'HQ')
+
+ get :index, params: { company_id: company.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ row = json_response['data'].first
+ expect(row['id']).to eq(entry.prefixed_id)
+ expect(row['label']).to eq('HQ')
+ expect(row['address']).to be_present
+ end
+
+ it '404s under another store company' do
+ get :index, params: { company_id: create(:company, store: create(:store)).prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe 'POST #create' do
+ it 'creates an owned address row with a label and defaults' do
+ post :create, params: {
+ company_id: company.prefixed_id,
+ label: 'Plant 2 dock',
+ default_shipping: true,
+ address: { first_name: 'Ops', last_name: 'Team', address1: '1 Dock Rd',
+ city: 'Springfield', postal_code: '62704', country_code: 'US', state_code: 'IL' }
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['label']).to eq('Plant 2 dock')
+ expect(json_response['default_shipping']).to be(true)
+ expect(company.company_addresses.sole.address.city).to eq('Springfield')
+ end
+
+ it 'demotes the previous default of the same kind' do
+ previous = create(:company_address, company: company, default_shipping: true)
+
+ post :create, params: {
+ company_id: company.prefixed_id,
+ default_shipping: true,
+ address: { first_name: 'A', last_name: 'B', address1: '2 Way', city: 'Metropolis',
+ postal_code: '10001', country_code: 'US', state_code: 'NY' }
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(previous.reload.default_shipping).to be(false)
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/invitations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/invitations_controller_spec.rb
new file mode 100644
index 0000000000..1407d3c81d
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/invitations_controller_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::Companies::InvitationsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:company) { create(:company, store: store) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'lists the node invitations with their derived status' do
+ pending_invitation = create(:company_invitation, company: company)
+ expired = create(:company_invitation, company: company)
+ expired.update!(expires_at: 1.day.ago)
+
+ get :index, params: { company_id: company.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ statuses = json_response['data'].to_h { |row| [row['id'], row['status']] }
+ expect(statuses[pending_invitation.prefixed_id]).to eq('pending')
+ expect(statuses[expired.prefixed_id]).to eq('expired')
+ end
+
+ it '404s under another store company' do
+ get :index, params: { company_id: create(:company, store: create(:store)).prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/locations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/locations_controller_spec.rb
deleted file mode 100644
index c15ae46143..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/companies/locations_controller_spec.rb
+++ /dev/null
@@ -1,66 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::Companies::LocationsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:company) { create(:company, store: store) }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #index' do
- it 'lists the branches of the company' do
- create(:company_location, company: company, name: 'Berlin')
-
- get :index, params: { company_id: company.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- row = json_response['data'].first
- expect(row['id']).to start_with('cloc_')
- expect(row['name']).to eq('Berlin')
- expect(row['company_id']).to eq(company.prefixed_id)
- end
-
- it '404s under another store company' do
- get :index, params: { company_id: create(:company, store: create(:store)).prefixed_id }, as: :json
-
- expect(response).to have_http_status(:not_found)
- end
- end
-
- describe 'POST #create' do
- let(:germany) { create(:country, iso: 'DE', name: 'Germany') }
-
- it 'creates a branch' do
- post :create, params: { company_id: company.prefixed_id, name: 'Hamburg' }, as: :json
-
- expect(response).to have_http_status(:created)
- expect(json_response['name']).to eq('Hamburg')
- expect(company.company_locations.count).to eq(1)
- end
-
- # The branch owns its address rather than pointing at a shared row.
- it 'creates the addresses inline' do
- post :create, params: {
- company_id: company.prefixed_id,
- name: 'Munich',
- billing_address: {
- first_name: 'Anna', last_name: 'Muller', address1: 'Leopoldstr 1',
- city: 'Munich', postal_code: '80802', country_code: germany.iso
- }
- }, as: :json
-
- expect(response).to have_http_status(:created)
- location = company.company_locations.sole
- expect(location.billing_address.city).to eq('Munich')
- expect(json_response['billing_address']['city']).to eq('Munich')
- end
-
- it 'rejects a branch with no name' do
- post :create, params: { company_id: company.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/memberships_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/memberships_controller_spec.rb
new file mode 100644
index 0000000000..8378ee13a5
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/memberships_controller_spec.rb
@@ -0,0 +1,67 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::Companies::MembershipsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let!(:company) { create(:company, store: store) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #index' do
+ it 'lists the members with who they are' do
+ membership = create(:company_membership, company: company)
+
+ get :index, params: { company_id: company.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ row = json_response['data'].first
+ expect(row['id']).to eq(membership.prefixed_id)
+ expect(row['email']).to eq(membership.customer.email)
+ expect(row['company_id']).to eq(company.prefixed_id)
+ end
+
+ it '404s under another store company' do
+ get :index, params: { company_id: create(:company, store: create(:store)).prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe 'POST #create' do
+ context 'when the email belongs to an existing customer' do
+ let!(:customer) { create(:customer, email: '[email protected]') }
+
+ it 'creates the membership immediately' do
+ post :create, params: { company_id: company.prefixed_id, customer_email: '[email protected]' }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['id']).to start_with('cmem_')
+ expect(json_response['email']).to eq('[email protected]')
+ expect(company.memberships.sole.customer).to eq(customer)
+ end
+
+ it 'rejects a duplicate membership' do
+ create(:company_membership, company: company, customer: customer)
+
+ post :create, params: { company_id: company.prefixed_id, customer_email: '[email protected]' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+ end
+
+ context 'when the email is unknown' do
+ it 'creates an invitation instead' do
+ post :create, params: { company_id: company.prefixed_id, customer_email: '[email protected]' }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['id']).to start_with('cinv_')
+ expect(json_response['email']).to eq('[email protected]')
+ expect(json_response['status']).to eq('pending')
+ expect(json_response).not_to have_key('token')
+ expect(company.invitations.sole.inviter).to be_nil
+ end
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_exemption_certificates_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_exemption_certificates_controller_spec.rb
index 567cdd12de..efad47584a 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_exemption_certificates_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_exemption_certificates_controller_spec.rb
@@ -72,6 +72,20 @@ RSpec.describe Spree::Api::V3::Admin::Companies::TaxExemptionCertificatesControl
expect(company.tax_exemption_certificates.sole.country_code).to eq(germany.iso)
end
+ # A division's purchases read exemption through the legal entity; the
+ # division itself holds no certificates.
+ it 'refuses a certificate on a division node' do
+ division = create(:company, store: store, kind: 'division', parent: company)
+
+ post :create, params: {
+ company_id: division.prefixed_id,
+ certificate_number: 'DE-RESALE-8',
+ reason_code: 'resale'
+ }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
it 'attaches a document from a direct upload' do
post :create, params: {
company_id: company.prefixed_id,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb
index e7ca8f8f76..9a584db632 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies/tax_identifiers_controller_spec.rb
@@ -42,14 +42,24 @@ RSpec.describe Spree::Api::V3::Admin::Companies::TaxIdentifiersController, type:
it 'takes effect on a sale for that business' do
create(:tax_identifier, owner: company, kind: 'eu_vat', value: 'DE222222222')
- location = create(:company_location, company: company)
customer = create(:customer)
+ create(:company_membership, company: company, customer: customer)
create(:tax_identifier, owner: customer, kind: 'eu_vat', value: 'DE111111111')
- cart = create(:cart, store: store, customer: customer, company_location: location)
+ cart = create(:cart, store: store, customer: customer, company: company)
expect(cart.resolved_tax_identifier.value).to eq('DE222222222')
end
+ # The division's purchases read their registration through the legal
+ # entity; the division itself holds none.
+ it 'refuses a registration on a division node' do
+ division = create(:company, store: store, kind: 'division', parent: company)
+
+ post :create, params: { company_id: division.prefixed_id, kind: 'eu_vat', value: 'DE123456789' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
it 'holds one registration per kind' do
create(:tax_identifier, owner: company, kind: 'eu_vat', value: 'DE123456789')
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/companies_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/companies_controller_spec.rb
index 489fe7fa36..d45158fd04 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/companies_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/companies_controller_spec.rb
@@ -20,6 +20,8 @@ RSpec.describe Spree::Api::V3::Admin::CompaniesController, type: :controller do
expect(row['id']).to start_with('comp_')
expect(row['name']).to eq('Acme Industrial')
expect(row['external_references']).to eq('erp' => 'ACME-1')
+ expect(row['kind']).to eq('company')
+ expect(row['parent_id']).to be_nil
end
it 'hides companies belonging to another store' do
@@ -30,24 +32,47 @@ RSpec.describe Spree::Api::V3::Admin::CompaniesController, type: :controller do
expect(json_response['data'].map { |row| row['id'] }).not_to include(other.prefixed_id)
end
- it 'counts branches without a request per row' do
- create(:company_location, company: company)
+ it 'counts children and members without a request per row' do
+ create(:company, store: store, parent: company, kind: 'division')
+ create(:company_membership, company: company)
get :index, as: :json
- expect(json_response['data'].first['locations_count']).to eq(1)
+ row = json_response['data'].find { |r| r['id'] == company.prefixed_id }
+ expect(row['children_count']).to eq(1)
+ expect(row['members_count']).to eq(1)
+ end
+
+ it 'filters roots and children through Ransack' do
+ child = create(:company, store: store, parent: company, kind: 'division')
+
+ get :index, params: { q: { parent_id_null: 1 } }, as: :json
+ expect(json_response['data'].map { |r| r['id'] }).to contain_exactly(company.prefixed_id)
+
+ get :index, params: { q: { parent_id_eq: company.prefixed_id } }, as: :json
+ expect(json_response['data'].map { |r| r['id'] }).to contain_exactly(child.prefixed_id)
end
end
describe 'GET #show' do
- it 'expands branches on request' do
- location = create(:company_location, company: company, name: 'Berlin')
+ it 'expands children on request' do
+ child = create(:company, store: store, parent: company, kind: 'division', name: 'Berlin Division')
- get :show, params: { id: company.prefixed_id, expand: 'company_locations' }, as: :json
+ get :show, params: { id: company.prefixed_id, expand: 'children' }, as: :json
expect(response).to have_http_status(:ok)
- expect(json_response['company_locations'].first['name']).to eq('Berlin')
- expect(json_response['company_locations'].first['id']).to eq(location.prefixed_id)
+ expect(json_response['children'].first['name']).to eq('Berlin Division')
+ expect(json_response['children'].first['id']).to eq(child.prefixed_id)
+ end
+
+ it 'expands the address book and memberships on request' do
+ create(:company_address, company: company, label: 'HQ')
+ create(:company_membership, company: company)
+
+ get :show, params: { id: company.prefixed_id, expand: 'addresses,memberships' }, as: :json
+
+ expect(json_response['addresses'].first['label']).to eq('HQ')
+ expect(json_response['memberships'].first['email']).to be_present
end
it '404s for another store company' do
@@ -58,14 +83,37 @@ RSpec.describe Spree::Api::V3::Admin::CompaniesController, type: :controller do
end
describe 'POST #create' do
- it 'creates a company bound to the current store' do
+ it 'creates a root company bound to the current store' do
post :create, params: { name: 'Globex' }, as: :json
expect(response).to have_http_status(:created)
expect(json_response['name']).to eq('Globex')
+ expect(json_response['kind']).to eq('company')
expect(Spree::Company.find_by(name: 'Globex').store).to eq(store)
end
+ it 'creates a division under a parent' do
+ post :create, params: { name: 'West Plant', kind: 'division', parent_id: company.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['kind']).to eq('division')
+ expect(json_response['parent_id']).to eq(company.prefixed_id)
+ end
+
+ it '404s a parent belonging to another store' do
+ foreign = create(:company, store: create(:store))
+
+ post :create, params: { name: 'X', parent_id: foreign.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+
+ it 'rejects a division at the root' do
+ post :create, params: { name: 'Rootless', kind: 'division' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
it 'records the external identity the connector sent' do
post :create, params: { name: 'Globex', external_references: [{ system: 'erp', external_id: 'GLBX' }] }, as: :json
@@ -124,17 +172,34 @@ RSpec.describe Spree::Api::V3::Admin::CompaniesController, type: :controller do
expect(response).to have_http_status(:not_found)
expect(other.reload.name).not_to eq('Hijacked')
end
+
+ it 're-parents a node, revalidating the tree' do
+ other_root = create(:company, store: store)
+ child = create(:company, store: store, parent: company, kind: 'division')
+
+ patch :update, params: { id: child.prefixed_id, parent_id: other_root.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(child.reload.parent).to eq(other_root)
+ end
+
+ it 'refuses a cycle' do
+ child = create(:company, store: store, parent: company)
+
+ patch :update, params: { id: company.prefixed_id, parent_id: child.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
end
describe 'DELETE #destroy' do
- it 'removes the company and its branches' do
- create(:company_location, company: company)
+ it 'removes the node and its subtree' do
+ create(:company, store: store, parent: company, kind: 'division')
delete :destroy, params: { id: company.prefixed_id }, as: :json
expect(response).to have_http_status(:no_content)
- expect(Spree::Company.where(id: company.id)).to be_empty
- expect(Spree::CompanyLocation.count).to eq(0)
+ expect(Spree::Company.count).to eq(0)
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/company_addresses_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/company_addresses_controller_spec.rb
new file mode 100644
index 0000000000..3e9d2270cb
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/company_addresses_controller_spec.rb
@@ -0,0 +1,59 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::CompanyAddressesController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let(:company) { create(:company, store: store) }
+ let!(:entry) { create(:company_address, company: company, label: 'HQ') }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #show' do
+ it 'returns the entry with its address' do
+ get :show, params: { id: entry.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['label']).to eq('HQ')
+ expect(json_response['company_id']).to eq(company.prefixed_id)
+ end
+
+ it '404s an entry of another store company' do
+ foreign = create(:company_address, company: create(:company, store: create(:store)))
+
+ get :show, params: { id: foreign.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'edits the address in place instead of replacing it' do
+ original_address_id = entry.address_id
+
+ patch :update, params: { id: entry.prefixed_id, address: { city: 'Shelbyville' } }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(entry.reload.address_id).to eq(original_address_id)
+ expect(entry.address.city).to eq('Shelbyville')
+ end
+
+ it 'promotes an entry to default billing' do
+ patch :update, params: { id: entry.prefixed_id, default_billing: true }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(entry.reload.default_billing).to be(true)
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'removes the entry and its owned address row' do
+ expect {
+ delete :destroy, params: { id: entry.prefixed_id }, as: :json
+ }.to change(Spree::Address, :count).by(-1)
+
+ expect(response).to have_http_status(:no_content)
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/company_invitations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/company_invitations_controller_spec.rb
new file mode 100644
index 0000000000..8eb4a95087
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/company_invitations_controller_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::CompanyInvitationsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let(:company) { create(:company, store: store) }
+ let!(:invitation) { create(:company_invitation, company: company) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #show' do
+ it 'returns the invitation without its token' do
+ get :show, params: { id: invitation.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['status']).to eq('pending')
+ expect(json_response).not_to have_key('token')
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'revokes rather than erases' do
+ delete :destroy, params: { id: invitation.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:no_content)
+ expect(invitation.reload).to be_revoked
+ end
+
+ it 'refuses on an already spent invitation' do
+ invitation.update!(accepted_at: Time.current)
+
+ delete :destroy, params: { id: invitation.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
+ it '404s an invitation of another store company' do
+ foreign = create(:company_invitation, company: create(:company, store: create(:store)))
+
+ delete :destroy, params: { id: foreign.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/company_locations/contacts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/company_locations/contacts_controller_spec.rb
deleted file mode 100644
index cc914dfaf7..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/company_locations/contacts_controller_spec.rb
+++ /dev/null
@@ -1,82 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::CompanyLocations::ContactsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:company) { create(:company, store: store) }
- let!(:location) { create(:company_location, company: company) }
- let!(:customer) { create(:customer, email: '[email protected]') }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #index' do
- it 'lists who buys for the branch' do
- create(:company_contact, company_location: location, customer: customer)
-
- get :index, params: { company_location_id: location.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- row = json_response['data'].first
- expect(row['id']).to start_with('cc_')
- expect(row['email']).to eq('[email protected]')
- expect(row['role']).to eq('buyer')
- expect(row['customer_id']).to eq(customer.prefixed_id)
- end
-
- it '404s under a branch of another store company' do
- elsewhere = create(:company_location, company: create(:company, store: create(:store)))
-
- get :index, params: { company_location_id: elsewhere.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:not_found)
- end
- end
-
- # Overriding `scope` bypasses the base class's ability filter, so this has to
- # be asserted rather than assumed: contacts carry customer emails.
- describe 'a role without customer permission' do
- include_context 'API v3 Admin with custom permissions'
-
- let(:custom_permission_set) do
- Class.new(Spree::PermissionSets::Base) do
- def activate!
- can :manage, Spree::Product
- end
- end
- end
-
- it 'cannot list who buys for a branch' do
- create(:company_contact, company_location: location, customer: customer)
-
- get :index, params: { company_location_id: location.prefixed_id }, as: :json
-
- # Not found rather than forbidden, so the branch's existence doesn't leak.
- expect(response).to have_http_status(:not_found)
- expect(response.body).not_to include('[email protected]')
- end
- end
-
- describe 'POST #create' do
- it 'authorises a customer to buy for the branch' do
- post :create, params: {
- company_location_id: location.prefixed_id,
- customer_id: customer.prefixed_id,
- role: 'admin'
- }, as: :json
-
- expect(response).to have_http_status(:created)
- expect(json_response['role']).to eq('admin')
- expect(location.customers).to eq([customer])
- end
-
- it 'refuses the same customer twice on one branch' do
- create(:company_contact, company_location: location, customer: customer)
-
- post :create, params: { company_location_id: location.prefixed_id, customer_id: customer.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/company_locations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/company_locations_controller_spec.rb
deleted file mode 100644
index 3f3ad0bbeb..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/company_locations_controller_spec.rb
+++ /dev/null
@@ -1,86 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::CompanyLocationsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:company) { create(:company, store: store) }
- let!(:location) { create(:company_location, company: company, name: 'Berlin') }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #show' do
- it 'returns the branch' do
- get :show, params: { id: location.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['name']).to eq('Berlin')
- end
-
- it '404s for a branch of another store company' do
- elsewhere = create(:company_location, company: create(:company, store: create(:store)))
-
- get :show, params: { id: elsewhere.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:not_found)
- end
- end
-
- describe 'PATCH #update' do
- it 'renames the branch' do
- patch :update, params: { id: location.prefixed_id, name: 'Berlin Mitte' }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(location.reload.name).to eq('Berlin Mitte')
- end
-
- context 'with an address already on file' do
- let!(:existing) { create(:address, address1: 'Alte Str 1', city: 'Berlin') }
-
- before { location.update!(billing_address: existing) }
-
- # Rebuilding would leave the old row behind and make one-field edits
- # impossible, since the replacement starts blank.
- it 'edits the address in place rather than replacing it' do
- expect {
- patch :update, params: {
- id: location.prefixed_id,
- billing_address: { address1: 'Neue Str 5' }
- }, as: :json
- }.not_to change(Spree::Address, :count)
-
- expect(response).to have_http_status(:ok)
- expect(location.reload.billing_address_id).to eq(existing.id)
- expect(existing.reload.address1).to eq('Neue Str 5')
- expect(existing.city).to eq('Berlin')
- end
- end
-
- it 'sets an address on a branch that had none' do
- germany = create(:country, iso: 'DE', name: 'Germany')
-
- patch :update, params: {
- id: location.prefixed_id,
- billing_address: {
- first_name: 'Anna', last_name: 'Muller', address1: 'Neue Str 5',
- city: 'Berlin', postal_code: '10115', country_code: germany.iso
- }
- }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(location.reload.billing_address.address1).to eq('Neue Str 5')
- end
- end
-
- describe 'DELETE #destroy' do
- it 'removes the branch and the address it owned' do
- location.update!(billing_address: create(:address))
-
- expect { delete :destroy, params: { id: location.prefixed_id }, as: :json }.
- to change(Spree::Address, :count).by(-1)
-
- expect(response).to have_http_status(:no_content)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/company_memberships_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/company_memberships_controller_spec.rb
new file mode 100644
index 0000000000..0c9b067487
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/company_memberships_controller_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::CompanyMembershipsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin authenticated'
+
+ let(:company) { create(:company, store: store) }
+ let!(:membership) { create(:company_membership, company: company) }
+
+ before { request.headers.merge!(headers) }
+
+ describe 'GET #show' do
+ it 'returns the membership' do
+ get :show, params: { id: membership.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(membership.prefixed_id)
+ end
+
+ it '404s a membership of another store company' do
+ foreign = create(:company_membership, company: create(:company, store: create(:store)))
+
+ get :show, params: { id: foreign.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ it 'removes the standing' do
+ delete :destroy, params: { id: membership.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:no_content)
+ expect(company.memberships.reload).to be_empty
+ end
+ end
+end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
index 2872776716..27b7b8e6f1 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
@@ -520,38 +520,37 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller do
end
end
- # Staff assigning the business an order is for — the only route to an
- # exemption for a buyer who acts for several companies, until session-level
- # selection is settled with channels and catalogs.
+ # Staff assigning the business an order is for. A buyer with several
+ # memberships names the node on their own cart; this is the back-office
+ # equivalent.
context 'assigning the business the order is for' do
let(:company) { create(:company, store: store) }
- let(:location) { create(:company_location, company: company) }
- it 'sets the branch and reports the company' do
- patch :update, params: { id: order.prefixed_id, company_location_id: location.prefixed_id }, as: :json
+ it 'sets the node and reports it' do
+ patch :update, params: { id: order.prefixed_id, company_id: company.prefixed_id }, as: :json
expect(response).to have_http_status(:ok)
- expect(json_response['company_location_id']).to eq(location.prefixed_id)
expect(json_response['company_id']).to eq(company.prefixed_id)
+ expect(json_response['company_name']).to eq(company.name)
expect(order.reload.company).to eq(company)
end
- it 'refuses a branch belonging to another store' do
- elsewhere = create(:company_location, company: create(:company, store: create(:store)))
+ it 'refuses a node belonging to another store' do
+ elsewhere = create(:company, store: create(:store))
- patch :update, params: { id: order.prefixed_id, company_location_id: elsewhere.prefixed_id }, as: :json
+ patch :update, params: { id: order.prefixed_id, company_id: elsewhere.prefixed_id }, as: :json
expect(response).to have_http_status(:unprocessable_content)
- expect(order.reload.company_location_id).to be_nil
+ expect(order.reload.company_id).to be_nil
end
- it 'clears the branch when passed nothing' do
- order.update!(company_location: location)
+ it 'clears the node when passed nothing' do
+ order.update!(company: company)
- patch :update, params: { id: order.prefixed_id, company_location_id: nil }, as: :json
+ patch :update, params: { id: order.prefixed_id, company_id: nil }, as: :json
expect(response).to have_http_status(:ok)
- expect(order.reload.company_location_id).to be_nil
+ expect(order.reload.company_id).to be_nil
end
end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/companies/locations_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/companies/locations_spec.rb
deleted file mode 100644
index 74c36e592c..0000000000
--- a/spree/api/spec/integration/spree/api/v3/admin/companies/locations_spec.rb
+++ /dev/null
@@ -1,161 +0,0 @@
-# frozen_string_literal: true
-
-require 'swagger_helper'
-
-RSpec.describe 'Admin Company Locations API', type: :request, swagger_doc: 'api-reference/admin.yaml' do
- include_context 'API v3 Admin'
-
- let(:Authorization) { "Bearer #{admin_jwt_token}" }
- let!(:company) { create(:company, store: store, name: 'Acme Industrial') }
- let!(:location) { create(:company_location, company: company, name: 'Berlin') }
- let(:germany) { create(:country, iso: 'DE', name: 'Germany') }
-
- path '/api/v3/admin/companies/{company_id}/locations' do
- let(:company_id) { company.prefixed_id }
-
- get 'List company locations' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Returns the branches of a business customer. Each branch owns its own addresses and the buyers authorised to purchase for it.'
- admin_scope :read, :customers
-
- admin_sdk_example 'companies/locations-list'
-
- parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
- parameter name: :Authorization, in: :header, type: :string, required: true
- parameter name: :company_id, in: :path, type: :string, required: true
-
- response '200', 'locations found' do
- let(:'x-spree-api-key') { secret_api_key.plaintext_token }
-
- run_test! do |response|
- data = JSON.parse(response.body)
- expect(data['data'].pluck('id')).to include(location.prefixed_id)
- end
- end
- end
-
- post 'Create a company location' do
- tags 'Companies'
- consumes 'application/json'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Creates a branch. Addresses are supplied inline and belong to the branch, so editing one later changes the branch\'s own record rather than an address the buyer also uses.'
- admin_scope :write, :customers
-
- admin_sdk_example 'companies/locations-create'
-
- parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
- parameter name: :Authorization, in: :header, type: :string, required: true
- parameter name: :company_id, in: :path, type: :string, required: true
- parameter name: :body, in: :body, schema: {
- type: :object,
- properties: {
- name: { type: :string, example: 'Hamburg warehouse' },
- external_id: { type: :string, nullable: true, example: 'HAM-01' },
- billing_address: {
- type: :object,
- description: 'Created inline and owned by the branch.',
- properties: {
- first_name: { type: :string }, last_name: { type: :string },
- address1: { type: :string }, city: { type: :string },
- postal_code: { type: :string }, country_code: { type: :string, example: 'DE' }
- }
- }
- },
- required: ['name']
- }
-
- let(:'x-spree-api-key') { secret_api_key.plaintext_token }
-
- response '201', 'location created' do
- let(:body) do
- {
- name: 'Hamburg warehouse',
- billing_address: {
- first_name: 'Anna', last_name: 'Muller', address1: 'Hafenstr 1',
- city: 'Hamburg', postal_code: '20095', country_code: germany.iso
- }
- }
- end
-
- run_test! do |response|
- expect(JSON.parse(response.body)['name']).to eq('Hamburg warehouse')
- end
- end
-
- response '422', 'invalid request' do
- let(:body) { { external_id: 'NO-NAME' } }
-
- run_test!
- end
- end
- end
-
- path '/api/v3/admin/company_locations/{id}' do
- let(:id) { location.prefixed_id }
- let(:'x-spree-api-key') { secret_api_key.plaintext_token }
-
- parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
- parameter name: :Authorization, in: :header, type: :string, required: true
- parameter name: :id, in: :path, type: :string, required: true
-
- get 'Get a company location' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Branches are addressed by their own id once you hold one — the company id is not needed.'
- admin_scope :read, :customers
-
- admin_sdk_example 'company-locations/get'
-
- response '200', 'location found' do
- run_test! do |response|
- expect(JSON.parse(response.body)['name']).to eq('Berlin')
- end
- end
- end
-
- patch 'Update a company location' do
- tags 'Companies'
- consumes 'application/json'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Address fields sent here edit the branch\'s existing address in place; send only what changes.'
- admin_scope :write, :customers
-
- admin_sdk_example 'company-locations/update'
-
- parameter name: :body, in: :body, schema: {
- type: :object,
- properties: {
- name: { type: :string, example: 'Berlin Mitte' },
- billing_address: { type: :object, properties: { city: { type: :string } } }
- }
- }
-
- response '200', 'location updated' do
- let(:body) { { name: 'Berlin Mitte' } }
-
- run_test! do |response|
- expect(JSON.parse(response.body)['name']).to eq('Berlin Mitte')
- end
- end
- end
-
- delete 'Delete a company location' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Removes the branch, its buyers and the addresses it owned.'
- admin_scope :write, :customers
-
- admin_sdk_example 'company-locations/delete'
-
- response '204', 'location deleted' do
- run_test!
- end
- end
- end
-end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/companies/memberships_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/companies/memberships_spec.rb
new file mode 100644
index 0000000000..bdb5967955
--- /dev/null
+++ b/spree/api/spec/integration/spree/api/v3/admin/companies/memberships_spec.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+require 'swagger_helper'
+
+RSpec.describe 'Admin Company Memberships API', type: :request, swagger_doc: 'api-reference/admin.yaml' do
+ include_context 'API v3 Admin'
+
+ let(:Authorization) { "Bearer #{admin_jwt_token}" }
+ let!(:company) { create(:company, store: store) }
+
+ path '/api/v3/admin/companies/{company_id}/memberships' do
+ let(:company_id) { company.prefixed_id }
+ let(:'x-spree-api-key') { secret_api_key.plaintext_token }
+
+ parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
+ parameter name: :Authorization, in: :header, type: :string, required: true
+ parameter name: :company_id, in: :path, type: :string, required: true
+
+ get 'List company members' do
+ tags 'Companies'
+ produces 'application/json'
+ security [api_key: [], bearer_auth: []]
+ description 'Returns the people with standing over this node. Standing covers the node and everything below it in the tree.'
+ admin_scope :read, :customers
+
+ admin_sdk_example 'companies/memberships/list'
+
+ response '200', 'members found' do
+ let!(:membership) { create(:company_membership, company: company) }
+
+ run_test! do |response|
+ data = JSON.parse(response.body)
+ expect(data['data'].pluck('id')).to include(membership.prefixed_id)
+ end
+ end
+ end
+
+ post 'Add a member by email' do
+ tags 'Companies'
+ consumes 'application/json'
+ produces 'application/json'
+ security [api_key: [], bearer_auth: []]
+ description <<~DESC
+ Adds a person to the node by email. An email matching an existing customer becomes a
+ membership immediately (a `cmem_…` id); an unknown email becomes an invitation
+ (a `cinv_…` id) and the invite email goes out.
+ DESC
+ admin_scope :write, :customers
+
+ admin_sdk_example 'companies/memberships/create'
+
+ parameter name: :body, in: :body, schema: {
+ type: :object,
+ properties: {
+ customer_email: { type: :string, example: '[email protected]' },
+ role: { type: :string, nullable: true,
+ description: 'Cosmetic label; carries no permissions.',
+ example: 'buyer' }
+ },
+ required: ['customer_email']
+ }
+
+ response '201', 'member added' do
+ let!(:customer) { create(:customer, email: '[email protected]') }
+ let(:body) { { customer_email: '[email protected]' } }
+
+ run_test! do |response|
+ expect(JSON.parse(response.body)['email']).to eq('[email protected]')
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/companies_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/companies_spec.rb
index e16c531208..6260a5f7b2 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/companies_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/companies_spec.rb
@@ -39,7 +39,7 @@ RSpec.describe 'Admin Companies API', type: :request, swagger_doc: 'api-referenc
consumes 'application/json'
produces 'application/json'
security [api_key: [], bearer_auth: []]
- description 'Creates a business customer. Exemption certificates and tax registrations hang off it.'
+ description 'Creates a company node. A root node is a legal entity; pass parent_id and kind to build the organization tree. Exemption certificates and tax registrations hang off legal-entity nodes.'
admin_scope :write, :customers
admin_sdk_example 'companies/create'
@@ -50,6 +50,12 @@ RSpec.describe 'Admin Companies API', type: :request, swagger_doc: 'api-referenc
type: :object,
properties: {
name: { type: :string, example: 'Globex Corporation' },
+ kind: { type: :string, enum: %w[company division],
+ description: 'company = legal entity (may hold tax registrations), division = organizational unit. Roots must be company.',
+ example: 'company' },
+ parent_id: { type: :string, nullable: true,
+ description: 'The parent node. Omit for a root.',
+ example: 'comp_86Rf07xd4z' },
external_references: {
type: :array,
description: 'Identities this business has in your own systems. A system you do ' \
diff --git a/spree/api/spec/integration/spree/api/v3/admin/company_locations/contacts_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/company_locations/contacts_spec.rb
deleted file mode 100644
index 8c451b4a6e..0000000000
--- a/spree/api/spec/integration/spree/api/v3/admin/company_locations/contacts_spec.rb
+++ /dev/null
@@ -1,116 +0,0 @@
-# frozen_string_literal: true
-
-require 'swagger_helper'
-
-RSpec.describe 'Admin Company Contacts API', type: :request, swagger_doc: 'api-reference/admin.yaml' do
- include_context 'API v3 Admin'
-
- let(:Authorization) { "Bearer #{admin_jwt_token}" }
- let!(:company) { create(:company, store: store) }
- let!(:location) { create(:company_location, company: company, name: 'Berlin') }
- let!(:customer) { create(:customer, email: '[email protected]') }
- let!(:contact) { create(:company_contact, company_location: location, customer: customer) }
-
- path '/api/v3/admin/company_locations/{company_location_id}/contacts' do
- let(:company_location_id) { location.prefixed_id }
- let(:'x-spree-api-key') { secret_api_key.plaintext_token }
-
- parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
- parameter name: :Authorization, in: :header, type: :string, required: true
- parameter name: :company_location_id, in: :path, type: :string, required: true
-
- get 'List company contacts' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Returns the customers authorised to purchase for a branch. A customer may act for more than one branch.'
- admin_scope :read, :customers
-
- admin_sdk_example 'company-locations/contacts-list'
-
- response '200', 'contacts found' do
- run_test! do |response|
- data = JSON.parse(response.body)
- expect(data['data'].pluck('id')).to include(contact.prefixed_id)
- end
- end
- end
-
- post 'Create a company contact' do
- tags 'Companies'
- consumes 'application/json'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description <<~DESC
- Authorises a customer to purchase for a branch. A customer who acts for exactly one branch has
- their orders attributed to it automatically; one who acts for several needs the branch set on
- the order, because guessing would invoice the wrong business.
- DESC
- admin_scope :write, :customers
-
- admin_sdk_example 'company-locations/contacts-create'
-
- parameter name: :body, in: :body, schema: {
- type: :object,
- properties: {
- customer_id: { type: :string, example: 'cus_UkLWZg9DAJ' },
- role: { type: :string, description: 'Free-form label; carries no permissions in 6.0.', example: 'buyer' }
- },
- required: ['customer_id']
- }
-
- response '201', 'contact created' do
- let(:body) { { customer_id: create(:customer).prefixed_id, role: 'admin' } }
-
- run_test! do |response|
- expect(JSON.parse(response.body)['role']).to eq('admin')
- end
- end
-
- response '422', 'invalid request' do
- # The same customer cannot be added to one branch twice.
- let(:body) { { customer_id: customer.prefixed_id } }
-
- run_test!
- end
- end
- end
-
- path '/api/v3/admin/company_contacts/{id}' do
- let(:id) { contact.prefixed_id }
- let(:'x-spree-api-key') { secret_api_key.plaintext_token }
-
- parameter name: 'x-spree-api-key', in: :header, type: :string, required: true
- parameter name: :Authorization, in: :header, type: :string, required: true
- parameter name: :id, in: :path, type: :string, required: true
-
- get 'Get a company contact' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- admin_scope :read, :customers
-
- admin_sdk_example 'company-contacts/get'
-
- response '200', 'contact found' do
- run_test! do |response|
- expect(JSON.parse(response.body)['email']).to eq('[email protected]')
- end
- end
- end
-
- delete 'Delete a company contact' do
- tags 'Companies'
- produces 'application/json'
- security [api_key: [], bearer_auth: []]
- description 'Withdraws the customer\'s authority to purchase for the branch. The customer account itself is untouched.'
- admin_scope :write, :customers
-
- admin_sdk_example 'company-contacts/delete'
-
- response '204', 'contact deleted' do
- run_test!
- end
- end
- end
-end