When someone reports a claim and your assessor service is down, the worst answer is a 503. They will try again, and you will have two claims. This post builds ins-claims, a first notice of loss API on the Nano Gateway that takes the claim, checks it, and answers 202 QUEUED when the downstream service cannot be reached.

Read this first

I have not run this API on a Nano Gateway. I had no management login for the cluster, so I could not publish it. Most posts in this series were measured on a live gateway. This one was not.

Checked offline:

  • apic lint passes.
  • Both Lua scripts run under Lua 5.5 and LuaJIT in a test harness. That includes the date window boundaries and the triage rules.
  • The JSONata expressions pass a subset check, and the guards have unit tests.
  • tests/ins-claims.sh passes a bash syntax check and a stub-server smoke run.

Not checked: any gateway behaviour. The assumptions are listed at the end.

The API

Operation Purpose
POST /claims Reports a claim. Returns a reference, a triage band and the queue it went to.
GET /claims/{claimId} Looks a claim up by reference.

The POST flow:

- parse-request
- validate-request          # 400
- claim-checks              # Lua: date window and triage
- guard-incident-invalid    # 422
- guard-incident-future     # 422
- guard-incident-late       # 422
- set-claim-ref             # X-Claim-Ref header
- duplicate-check           # CountLimit, 429
- remove-pii
- queue-switch              # which assessor queue
- assessor-try              # Retry in Try, with a 202 fallback

Date window and triage in Lua

The claim must be reported within 30 days of the incident, and not before it happened. JSONata has no clock, so the date arithmetic is Lua. The script works in whole days:

local MAX_DAYS = 30
...
days = math.floor((noon(now.year, now.month, now.day) - t) / 86400 + 0.5)
if days < 0 then
  status = 'future'
elseif days > MAX_DAYS then
  status = 'late'
else
  status = 'ok'
end

Both dates are taken at noon so a clock change cannot move a day. A date that does not exist, such as 31 February, is caught the same way as in the open banking post: format it back and compare. The result is invalid, future, late or ok, in an X-Incident-Status header. Three If and Throw pairs turn the first three into 422.

The triage is in the same script:

local text = string.lower(tostring(incident.description or ''))
local injury = (string.find(text, 'injur', 1, true) ~= nil)
  or (string.find(text, 'hospital', 1, true) ~= nil)

local loss = tonumber(incident.estimatedLoss) or 0
local triage = 'standard'
if injury or loss > 10000 then
  triage = 'complex'
elseif loss <= 500 then
  triage = 'fast-track'
end

Any mention of an injury makes the claim complex, however small. A loss of 500 or less is fast-track, up to 10,000 is standard, and above that is complex. The test checks both sides of 500, a loss of 20,000, Driver injured and Taken to HOSPITAL.

Plain text matching on injur and hospital is crude. It would flag “no injuries”, which a claims handler would then clear. It fails safe, which is the right direction for triage.

The script writes X-Incident-Status, X-Incident-Days, X-Triage and X-Injury, always with set. A caller who sends X-Triage: fast-track with a 20,000 loss has it overwritten, and the test checks that.

A claim reference from the transaction ID

kind: Set
spec:
  valueType: jsonata
  messageHeader:
    messageName: request
    action: set
    headerName: X-Claim-Ref
    value: "'CLM-' & $gtid()"

$gtid() is the gateway’s transaction ID, as the JSONata post showed. Putting it on the request as a header means every later policy and the 202 fallback use the same reference. A claim reference should come from the claims system in real life. This one is for demonstration.

Duplicate detection with CountLimit

One claim per policy, incident date and type:

kind: CountLimitDef
spec:
  max: 1
  autoDecrement: false
  extensions:
    dp-nano-gateway:
      apiVersion: api.ibm.com/v1
      spec:
        dynamicValue: $body('request').policyNumber & '|' & $body('request').incident.date & '|' & $body('request').incident.type
---
kind: CountLimit
spec:
  limits:
    - limitDef:
        name: one-claim-per-incident
        definition:
          $ref: ins-claims:ins-claims-duplicate-check-def:1.0.0
        operation: increment

A count limit has no time window. The traffic post covers CountLimit and showed that with autoDecrement: false it counts calls and never resets. Once it is full it stays full. Here that is a feature. A second report of the same incident gets 429 for ever.

The test makes five points:

  • The first report is accepted and the same one again gets 429.
  • A different incident date, a different type and a different policy are each accepted.
  • A late claim is refused with 422 twice, both times as 422 and not as a duplicate. The date guard runs before the count.
  • A claim refused as late does not use the policy’s one report. A good claim for the same policy afterwards returns 200.
  • A claim that was answered with 202 QUEUED counts as filed. Filing it again is a duplicate.

The 429 status is a bad fit for a duplicate. 409 is better. I did not look for a way to change what CountLimit answers. A custom answer would probably need a Throw behind an If, and a separate way to read the count.

Remove the personal data

The assessor does not need the claimant’s national ID or date of birth to assign a queue:

kind: Remove
spec:
  message:
    messageName: request
    body:
      captures:
        jsonata:
          - $.claimant.nationalId
          - $.claimant.dateOfBirth

The echo service returns what it received, so the receipt can report receivedNationalId and receivedDateOfBirth. The test expects false for both. Remove deletes the members, as the shape post showed.

Route by claim type

A Switch on incident.type sets a variable:

kind: Switch
spec:
  cases:
    - condition: $body('request').incident.type = 'motor'
      execute:
        - $ref: ins-claims:ins-claims-set-queue-motor:1.0.0
    - condition: $body('request').incident.type = 'property'
      execute:
        - $ref: ins-claims:ins-claims-set-queue-property:1.0.0
    - condition: $body('request').incident.type = 'travel'
      execute:
        - $ref: ins-claims:ins-claims-set-queue-travel:1.0.0

Each case is a Set that writes motor-assessors, property-assessors or travel-assessors to the queue variable. The assessor call puts that on its URL:

url: "'http://echo.pi-sample.svc.cluster.local/echo?queue=' & $variable('queue')"
urlType: jsonata

The Switch has no otherwise. The OpenAPI document restricts incident.type to the three values, so validation has already rejected anything else. If you add a type to the document and forget the switch, queue is empty. I would add an otherwise that throws.

A 202 when the assessor is down

Try wraps Retry, as in the card authorisation post. One retry after 200 ms. If that fails, the catch runs a Lua script that builds the response itself:

local req = context:get_message('request')
local resp = context:create_message('response')

resp.status = 202
resp.headers:set('Content-Type', 'application/json')
resp.body:write({
  claimRef = req.headers:get('X-Claim-Ref'),
  status = 'QUEUED',
  triage = req.headers:get('X-Triage'),
  message = 'Your claim has been received. An assessor will be assigned shortly.',
})

The claimant gets a reference, a triage band and a message. They do not see an error. The claim has passed every check and counted against the duplicate limit, so a retry gets a 429 rather than a second claim. Something downstream has to pick the queued claim up, and this API does not do that.

The Lua post verified that Lua can set resp.status to a 4xx and return. A 2xx built with context:create_message('response') and a body written from a table is not something I verified.

The test hook is a header, X-Test-Assessor: down, that routes the call to /fail. It is a test aid and must not stay. The test checks the 202, QUEUED, a triage of fast-track and that filing the claim again returns 429.

Lookup

GET /claims/{claimId} has an SQLInjectionFilter on the URI, a guard that the ID starts with CLM-, and a call to the claims system. It follows the pattern in the payments post. An ID without the prefix returns 400.

Running the tests

export NANOGW_CLIENT_ID=<client-id>
export NANOGW_CLIENT_SECRET=<client-secret>
export NANOGW_DOMAIN=<gateway-ingress-domain>
./tests/ins-claims.sh

43 assertions. Because CountLimit never resets, each run uses new policy numbers. If you reuse one, you get 429 where the test expects 200.

Summary

  • Answer 202 when you have taken responsibility for the work but cannot finish it. A 5xx invites a duplicate.
  • Order the checks so a rejected request does not use anything up. The date guard runs before the duplicate count.
  • A CountLimit is a “once only” check. It does not expire.
  • Strip data the next hop does not need, close to where it leaves the gateway.
  • Fail safe in triage. A false “complex” costs a person a minute. A false “fast-track” costs more.

Limitations and Notes

  • Not run on a gateway. The checks above are offline.
  • Assumptions to confirm on the first run:
    • A Lua script in the catch can build a 202 response and end the request, like the 4xx I verified.
    • Try catches an error that Retry gave up on.
    • CountLimit with autoDecrement: false and a dynamicValue key works as written.
    • A header set by Lua is visible to a later JSONata If.
    • $gtid() returns the same value each time it is used in one request.
  • The duplicate answer is 429, not 409.
  • The Switch has no otherwise.
  • Duplicate state is held by the gateway and never expires. A claim that needs to be filed again for the same incident needs a manual reset.
  • The assessor is the echo service. It shows what the gateway sent. It does not assign anything.