Class: Spree::Companies::AddMember

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/companies/add_member.rb

Overview

Adds a person to a company node by email — the single convergent path behind both the dashboard's and the storefront's "add member".

An email that matches an existing customer becomes a membership immediately; an unknown email becomes a Spree::CompanyInvitation, whose creation event sends the invite mail. Extension payloads (e.g. an Enterprise role reference) ride metadata on either record.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(company:, email: nil, customer: nil, inviter: nil, metadata: nil, require_acceptance: false) ⇒ Spree::ServiceModule::Result

Returns value is the created Spree::CompanyMembership or Spree::CompanyInvitation.

Parameters:

  • company (Spree::Company)
  • email (String, nil) (defaults to: nil) —

    who to add, when no customer is given

  • customer (Spree.customer_class, nil) (defaults to: nil) —

    an existing customer, who becomes a member immediately — for a caller that picked them rather than typed an address

  • inviter (Object, nil) (defaults to: nil) —

    the inviting customer, nil when staff

  • metadata (Hash, nil) (defaults to: nil) —

    extension payload — carried on the invitation only, where Enterprise reads it back on acceptance (memberships have no metadata column; a direct-membership payload is applied by the caller through the company_membership.form_fields contract, not stored here)

  • require_acceptance (Boolean) (defaults to: false) —

    always invite, even an existing customer — for callers who cannot vouch that the email is the person they mean (a storefront member typing an address). The emailed token is what proves the invitee owns the address and agrees to join; a direct membership would hand the company to whoever happens to hold that email.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/spree/companies/add_member.rb', line 32

def call(company:, email: nil, customer: nil, inviter: nil, metadata: nil, require_acceptance: false)
  normalized_email = (email.presence || customer&.email).to_s.strip.downcase
  # Acceptance outranks a customer handed in: they are invited by their
  # address like anyone else, so only the holder can agree to join.
  customer = require_acceptance ? nil : (customer || Spree.customer_class.find_by(email: normalized_email))

  record =
    if customer
      company.memberships.new(customer: customer)
    else
      company.invitations.new(email: normalized_email, inviter: inviter, metadata: )
    end

  record.save ? success(record) : failure(record)
end