Skip to content

The Partners API ​

since 4.2

A partner's master data is what other modules build on. A department change moves the cost centre an employee's hours go to. A customer ended with open items leaves invoices without an owner. A leaver keeps projects and owes hours. Each of these modules has to hear of the change, and some of them have to be able to stop it.

So partner master data changes in one place: five ports, each a Laravel service. Every change that goes through them passes every vetoer a module registered. A vetoer either objects or acts. Nobody writes the partner models directly, and that includes the SDK, its Partners app and its tests.

The five ports ​

Inject a port like any Laravel service. Each is @api, bound with singletonIf to a final default.

PortChanges
PartnersInterfacea partner: create, update (its descriptive columns), archive, restore; its profiles
RelationshipsInterfacean edge between two partners: begin, end, describe (the function title), setPrimary
PartnerRolesInterfacewhat a partner is to the business: assign/end at a home, linkContact/unlinkContact at an organisation
AddressesInterfacean address, by its id: addAddress, updateAddress, deleteAddress
AuthorizationsInterfaceroles, scoped roles, groups and abilities: assign…, end…, grantAbility, revokeAbility

All of them live in LaravelUi5\Sdk\Partners\Contracts.

php
use LaravelUi5\Sdk\Partners\Contracts\RelationshipsInterface;
use LaravelUi5\Sdk\Partners\Enums\RelationshipTypeCode;

// Eva moves to Acme on the first of March — the old employment ends the day before
$relationships->end($eva, $widgets, RelationshipTypeCode::EmployedBy, $march);
$relationships->begin($eva, $acme, RelationshipTypeCode::EmployedBy, $march, primary: true);

Time is a day you pass. A begin holds from $from. An end takes $at, the first day the row no longer holds, so its valid_until is the second before. A row that never held, because it begins on that day or later, is deleted instead of ended. The end methods return what they did: ChangeVerb::End, ChangeVerb::Delete or null when nothing held. A change is retroactive when you pass a day in the past. How far back that may go is a vetoer's question (the Foundation's period lock answers it), not the API's.

A port writes in a run. An action, an audited job or an audited command opens one. The change lands in the change log with the person who made it, or with the automation actor. A call outside a run is a programming error.

A misfit call is the code's error. An unknown role code, a functional partner role assigned as a structural one, or a scoped role without its object throws a LogicException (for roles, RoleScopeViolationException). These calls are mistakes in code, not objections a person reads.

Vetoers ​

A vetoer takes part in every change of partner master data. It has two methods. The two run in two phases, and every vetoer takes part in each phase:

php
use LaravelUi5\Sdk\Partners\Changes\Objection;
use LaravelUi5\Sdk\Partners\Changes\PartnerChange;
use LaravelUi5\Sdk\Partners\Contracts\PartnerChangeVetoerInterface;
use LaravelUi5\Sdk\Partners\Enums\ChangePort;
use LaravelUi5\Sdk\Partners\Enums\ChangeVerb;

final readonly class HomeCostCentreVetoer implements PartnerChangeVetoerInterface
{
    public function review(PartnerChange $change): array
    {
        if ($change->port !== ChangePort::Relationship || $change->verb !== ChangeVerb::End
            || $change->type !== 'employed_by' || ! $this->stillHome($change->partner, $change->until)) {
            return [];
        }

        return [Objection::of('controlling.homeCostCentre.open', from: ControllingApp::NAMESPACE,
            params: ['name' => $change->partner->name],
            action: new Ui5ArtifactOpenIntent(ControllingApp::NAMESPACE))];
    }

    public function apply(PartnerChange $change): void
    {
        // the change went through — re-stamp, re-read, end what hangs on it
    }
}
  1. review(). The port asks every vetoer and collects all objections. If there is one or more, nothing is written. The port throws one PartnerChangeRefusedException that carries them all. review() reads and never writes.
  2. apply(). If nobody objects, the port writes and then calls every vetoer's apply(). This happens in the same unit of work, before the commit. An exception in apply() is a technical failure, and the whole unit rolls back.

Register a vetoer by container tag in your module's service provider. Several modules take part, and none of them knows the others:

php
$this->app->tag([HomeCostCentreVetoer::class], PartnerChangeVetoerInterface::class);

What a vetoer sees ​

PartnerChange describes one change as the port is about to make it:

FieldCarries
port, verbwhich port (Partner, Profile, Address, Relationship, PartnerRole, Role, ScopedRole, Group, Ability) and what it does (Create, Update, Begin, End, Delete, Archive, Restore)
partnerthe partner the change is about
typethe code: the relationship type, the partner role, the role, the group, the address kind, the ability
related, onthe other partner of an edge or a role's home; the object a scoped role is held on
from, untilthe validity a Begin gives; the day an End ends it
before, afterthe columns an Update changes, for an address the country among them
runwho changes it, through which trigger, and whether it is automation

The objection ​

An Objection is a reason and, optionally, an action. The reason is a MessageText in your module's bundle (Objection::of(key, from: …, params: …)), so the person reads it in their language. The action is an AbstractIntent that leads into the app where the reason is fixed: the open item, the cost centre, the period. On the wire it travels as action: {name, parameters} beside the message's text. The client shows it as the message's active title in the message popover; a press jumps (Core 8.4). An app whose controllers extend Core's BaseController opens that popover on a refusal by itself (Core 8.5, Form validation).

Handling a refusal ​

In an action's handler, catch the refusal and roll back with it. Each objection becomes an error message, followed by the headline (sdk.partners.change.refused), and the client gets a 422:

php
try {
    $this->relationships->end($partner, $related, $type, $at);
} catch (PartnerChangeRefusedException $refused) {
    return $refused->rollback($transaction);
}

The SDK's own vetoers ​

The rules the SDK keeps are vetoers too. They object in the same way and are collected together with yours:

VetoerObjects to
ShapeVetoera relationship to itself; employment at a department (a person is member_of it); the same relationship, partner role, role, group or ability twice at a time; a second primary; a department without its owner; an incomplete address, half a coordinate pair, a second primary address
LadderVetoeroperator_for outside automation; a rung exchanged by someone who may not, or a holder on the wrong level; a rung already taken — since 4.3 with an action that opens the holder, where its relationship ends
PlatformOwnerVetoermoving Pragmatiqu IT or its support department away from what the SDK ships: their declared columns, the department's owns, its role support
changed in 4.2The models carry no write guard any more. Before 4.2 the assignment and address models refused some of these states in their `saving` event; those guards are gone, and so are the exceptions only they threw. The write surface of the partner models is internal. Their `#[ReadSurface]` is the read contract.

In tests ​

laravelui5/sdk-testing holds the test kit. Require it for dev:

bash
composer require --dev laravelui5/sdk-testing

given() writes partner master data through the same ports. Each verb runs in an automation run of its own, so your test meets the vetoers that production meets:

php
$eva  = given()->partner(['name' => 'Eva Gruber', 'system_level' => SystemLevel::TenantAdmin]);
$acme = given()->partner(['name' => 'Acme GmbH', 'type' => PartnerType::Organization]);
given()->relate($eva, $acme, RelationshipTypeCode::EmployedBy, primary: true);
given()->abilityOf($eva, 'com.example.app');

A validity you leave open begins yesterday and never ends. See Testing Your Modules.