What this does for your team
An insured asks to cancel. The procedure checks that the policy can be cancelled and does the refund arithmetic. It then puts the request, the dates and the amount in front of an approver. Nothing changes in your policy system and no money moves until they approve. After approval it cancels the policy, refunds the premium and sends the notice on the cancellation date, without anyone having to remember the date. The run keeps a record of what it read, the amount, who approved it, and the IDs your systems gave the cancellation, refund and notice. If the same request is run twice, your systems answer with what they already did instead of cancelling, refunding or mailing again, as long as they honour the keys the procedure sends. The example carrier is Northwind Mutual. Its hosts, policy numbers, amounts and IDs are made up; replace them with your own.Before you start
You need:- An API key for a member who is not read-only, and
curlandjq. See Run procedures from your system, which also covers publishing and starting runs. A draft also needs permission to create procedures, or to publish this one once it exists. Resource-scoped and app API keys can’t start drafts. - A group in Effective for the people who approve refunds. The procedure assigns its task to
group:operations-approvers; replace that with your group. - HTTPS APIs for your policy system and payment processor that accept an
Idempotency-Keyheader on writes and answer a repeated key with their first result. Your policy system must also record which request cancelled a policy. The procedure’s code calls these endpoints; change the hosts, paths and field names to match yours:
The procedure
Save this ascancel-a-policy.procedure.md. The sections after it walk through each part.
cancel-a-policy.procedure.md
How it works
Inputs and output
The front matter declares four inputs.request_id is your own ID for this cancellation request,
such as the case number your intake system gave it. The procedure builds its write keys from it,
so use a value that is unique to the request and stays the same if the request is sent again.
cancellation_date is a YYYY-MM-DD date.
The output, cancellation, follows the cancellation-result schema. All three endings return it:
status is cancelled, rejected or not-cancellable. The IDs of what was written appear only
on the cancelled path, and policyStatus only on the not-cancellable path. run.title names
each run after its policy and request, so runs are easy to find.
Read the policy
Theread-policy node is a call to the local effect
get-policy, declared under Code. A ts effect is TypeScript you write and publish with the
procedure. This one may make HTTPS requests with fetch and read one secret,
policy-system-token, because its header says uses=fetch,secrets secrets=policy-system-token.
The secret’s value is never in the source; you set it in Effective after you publish. See
Functions and effects and
secret.
The effect checks the shape of the response before it returns it, because the result must match
the policy schema. A 4xx answer other than 429 throws FatalError: another attempt can’t change
it, so the run fails at once. A 429, a 5xx answer or a network error throws an ordinary error,
which is retried.
The effect also returns the policy’s standing for this request, worked out from status and
cancellationRequestId:
check-policy is a choice on standing. The first two
cases continue to the rest of the procedure. not-cancellable runs Not cancellable, which
completes the run with status: not-cancellable and the policy’s status, before anyone is asked
to approve anything. Returning the standing as data, instead of failing the run, gives your system
a typed answer it can act on, and lets a second run for the same request get past this step. See
If a run fails partway.
Work out the return premium
compute-return-premium calls pro-rata-return, a ts function. A function is pure: it gets only
its input, with no network, secrets or clock. The same input always gives the same result, so the
step is safe to compute again. A pure function can’t use Date, so it counts days with calendar
arithmetic.
Pro rata means the insured gets back the share of the written premium for the days left in the
term after the cancellation date:
383.12 for the approver to read. It also returns
noticeAt, the time to send the notice: 14:00 UTC on the cancellation date. If your carrier uses
short-rate tables or a minimum earned premium, change this function; the rest of the procedure
doesn’t change.
A cancellation date before the term starts or after it ends makes the function throw. A
function’s error is final, so the run fails at compute-return-premium, before anyone is asked to
approve, and its failure doesn’t say which date was wrong. Check the date in your own system
before you start the run.
Approve the refund
approve-refund is a task. It creates a task for the
operations-approvers group and the run waits, without holding anything, until someone in that
group answers. The title and instructions fill in the policy’s facts and the arithmetic with
{{ ... }}, so the approver sees what they’re approving. The answer must match the
refund-decision schema: approved, and an optional note.
read-decision is a value node. It copies the answer and
turns a missing note into an empty string with ??, so later steps can rely on decision.note.
Approved or rejected
route-decision is a choice on decision.approved. A
choice has one case for each value: here 'true' and 'false'. Each case names a heading
directly under the choice’s section, and that heading’s nodes run for that case.
- Rejected. The
rejectednode completes the run withstatus: rejected. The run writes nothing to your systems. On a first run the policy stays in force; for a run that follows a failed one, see If a run fails partway. - Approved. The run continues under Cancel and refund.
complete node, so the choice needs
nothing after it.
Cancel, refund and notify, each with a key
The approved path makes three writes, each in its owncall: write-cancellation,
issue-refund and send-notice. Separate nodes give each write its own recorded result. If the
refund fails after the cancellation succeeded, the run’s record shows exactly which write
happened.
Each effect sends a key built from the request ID:
create-refundsends the refund withIdempotency-Key: refund-CR-20931. The processor makes the refund, but the connection drops before the reply arrives.fetchthrows, so the attempt fails. Effects get three attempts by default, with a short backoff between them.- The second attempt sends the same request with the same key. The processor recognizes the key and returns the refund it already made. There is one refund.
- The run records that result and moves on. If the run is interrupted later and resumes,
issue-refunddoesn’t run again: the run uses the result it recorded.
maximum_attempts: 1 stops the effect from
retrying within one call, and a failure there becomes something a person checks. It doesn’t help
across runs: a second run for the same request makes its own first attempt. For that, the
receiving system has to deduplicate, or the procedure has to look for the earlier write by a
business key, such as the request ID, before it writes. See
Writes to your systems.
The keys come from the request, not from the run. The context’s operation.idempotencyKey is
also stable across every attempt of one call, but it differs between runs. A key derived from the
request is the same in every run for that request, so if a second run for the same request gets
past check-policy, your systems answer it with the cancellation, refund and notice they already
made, for as long as they keep the key.
Send the notice on the cancellation date
wait-for-notice-date is a wait with until. The run
sleeps until quote.noticeAt and then continues; it holds nothing while it waits. A date in the
past ends the wait at once, so a backdated cancellation sends its notice straight away. Then
send-notice sends the notice with its own key, notice-CR-20931.
To send the notice as soon as the refund is issued instead, delete the wait node.
Finish
Thecancelled node completes the run. Its value must match cancellation-result, which
the procedure checks when you publish it and again when the run completes.
If a run fails partway
A run can fail after some writes and before others: for example, the payment processor refuses the refund after the cancellation was written. The run endsfailed, and the cancellation stays
written; failing the run doesn’t undo it. There is no step that catches the failure. A failed
run’s status is final, and the API has no way to resume it.
The run’s failure names the step that failed, issue-refund, and its code, but not the message
the effect threw: Effective doesn’t show thrown messages, because they could hold secret values.
Look in the processor’s own logs for why it refused. A refusal the procedure has to act on belongs
in a typed result instead, as get-policy returns standing.
To recover, fix what made the refund fail and start a new run with the same inputs, including the
same request_id. The new run:
- Reads the policy. It is now
cancelled, withcancellationRequestIdset to this request, so its standing iscancelled-by-this-requestand the run goes on. - Works out the same return premium from the same term and premium, and asks for approval
again. The task shows the policy status, so the approver can see it is already cancelled. The
amount was approved in the failed run, so approve it again to finish. A rejection ends the run
rejectedand writes nothing more, but the cancellation the failed run wrote stays. The policy is then cancelled with no refund, until you reverse the cancellation in your policy system or approve another run for the request. - Sends the cancellation with
cancel-CR-20931again. Your policy system honours the key, so it answers with the cancellation it already made, and nothing is written twice. - Sends the refund and the notice, which the failed run never made.
Idempotency-Key, such as cancel-CR-20931-2. The run key only
deduplicates the start request: the same key as the failed run’s start returns that failed run
instead of starting a new one.
In the Effective app, Retry on the failed run’s page also starts a new run, from a checkpoint
of the failed run that you pick. The page shows what starts again from there, and each call from
that point on runs again. The keys above make your systems answer a write they already applied
with its first result.
Try it as a draft
A draft runs source without publishing it. See Try a draft. A draft can’t declare secrets, and this procedure declares two, so sendingcancel-a-policy.procedure.md as a draft is refused with
422 invalid_procedure and no run starts.
To try the flow before you connect your systems, make a stand-in copy that runs without secrets.
Save it as cancel-a-policy.draft.procedure.md and change three things:
-
Delete the two
yaml secretblocks under Secrets. -
Replace each
ts effectwith one that returns sample data and declares nouses=orsecrets=. For example:effect id=get-policy input=policy-request result=policyDo the same foreffect id=create-refund input=refund-request result=refund-recordcreate-cancellationandcreate-notice, returning acancellationIdand anoticeId. -
Assign the task to yourself, so you can answer it:
GET /procedures/tasks?status=open&assignedToMe=true, read it to check the title and instructions,
and answer it as in Answer a task:
running until then. Use
a past date to see it finish straight away; the return premium changes with the date.
Each edit to the draft needs a new Idempotency-Key.
Check the result
Read the run withGET /procedures/runs/{runId} until status is completed. For the inputs
above and an approval, output looks like this (illustrative):
returnPremiumCentsis38312, the $383.12 worked out above. Open the run’swebUrlto seecompute-return-premium’s full result, withtermDays: 365andunearnedDays: 76.taskslists the approval task ascompleted.- A rejected draft (answer
{ "approved": false }) completes withstatus: "rejected", an emptyapprovalNote, and nocancellationId,refundIdornoticeId. - With
status: 'lapsed'andstanding: 'not-cancellable'in the stand-inget-policy, the run completes withstatus: "not-cancellable"andpolicyStatus: "lapsed", and creates no task.
Run it against your systems
When the flow does what you want, publish the realcancel-a-policy.procedure.md, as in
Publish a version. Reading the version lists
the two secrets it needs under secrets. Before the first run, set their values on the
procedure’s page in the Effective app, under Secrets: a shared default for every run, or a
personal override for the runs that run as you. See
Give a secret a value.
Try it on a test policy first. If your policy system and payment processor have test environments,
publish a copy with its own id, such as cancel-a-policy-test, whose effects call your test
hosts, and give that copy test credentials. Secrets are set for each procedure, so the copy’s
values don’t affect the real procedure’s.
Then check what only your systems can show:
- One cancellation, one refund and one notice for the request, with the IDs the run returned.
- Start a second run with the same
request_idand approve it. Itsread-policystep should report the standingcancelled-by-this-request, and your systems should answer with the same cancellation, refund and notice IDs; nothing should be written twice. If the run endsnot-cancellable, your policy system isn’t returningcancellationRequestId. If an ID differs, that endpoint isn’t honoring the key. A one-attempt retry policy doesn’t help across runs, so before you go live, make the endpoint deduplicate on the key, or have its effect look up the earlier write byrequest_idbefore it writes. - Start a run with a different
request_idfor the same policy. It should endnot-cancellablewithout creating a task.
Idempotency-Key derived from the request, such
as cancel-CR-20931, as in Start a run.
Next steps
- Reconcile payments: loop over a day’s payments and hand only the mismatches the procedure can’t explain to a person.
- Writes to your systems: retries, idempotency keys and what to do when an endpoint has no key.
taskandchoice: every field of the nodes this tutorial uses for the approval.