Skip to content

Routing by work code

The rule

A town-wide clock is used by the workers of several staffing agencies. A worker placed by two agencies says at the clock, by the work code they enter, which agency the shift is for. A punch without a code, or with a code no route has, must not be guessed: someone routes it by hand.

How the recipe does it

Each of the worker’s routes carries the work code its agency’s shifts are punched with (codes = { work_code = "7" }). The route hook, called for a punch with several routes, returns the route whose code is the one entered (punch.data.work_code), or nil: the punch is then unrouted (punch.unrouted, reason policy), kept aside until it is routed through the API.

For clocks at several sites, picked by the clock’s site first, see multi-agency-clock.

What the scenarios show

tests/01-route.toml: code 7 is agency A’s, code 8 agency B’s; no code, or a code no route has, unrouted.

Get the recipe

The recipe, its Lua and its scenarios are in the example packages repository: policy/route-by-work-code. Test it, or your own copy of it, with hourctl pkg test, and push it to a tenant as any policy package.

The policy package

route-by-work-code/package.toml
name = "route-by-work-code"
version = "1.0.0"
kind = "policy"
description = "A worker placed by several agencies says at the clock which one the shift is for: the work code picks the route."
host_api = 2
route-by-work-code/main.lua
-- A town-wide clock is used by workers of several staffing agencies. A
-- worker placed by two agencies has a route to each, and each route
-- carries the work code the agency's shifts are punched with
-- (codes = { work_code = "7" }). The work code entered at the clock picks
-- the route; a punch without one, or with a code no route has, is left
-- unrouted for someone to route by hand.
local function code_of(route)
return route.codes and route.codes.work_code
end
return {
route = function(punch, candidates)
local entered = punch.data and punch.data.work_code
if entered == nil or entered == "" then
return nil
end
for _, c in ipairs(candidates) do
if code_of(c) == entered then
return c
end
end
return nil
end,
}

Scenarios

Each scenario calls the package’s hooks with what Connect would hand them, and checks what they decide (the format).

The work code entered at the clock picks the agency the punch goes to
tests/01-route.toml
description = "The work code entered at the clock picks the agency the punch goes to"
[[step]]
name = "work code 7 is agency A's"
hook = "route"
punch = { id = "p1", endpoint = "town", device = "T1", user_id = "1001", data = { user_id = "1001", work_code = "7" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { work_code = "8" } },
]
[step.expect]
route = 1
[[step]]
name = "work code 8 is agency B's"
hook = "route"
punch = { id = "p2", endpoint = "town", device = "T1", user_id = "1001", data = { user_id = "1001", work_code = "8" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { work_code = "8" } },
]
[step.expect]
route = 2
[[step]]
name = "no work code: unrouted, for someone to route by hand"
hook = "route"
punch = { id = "p3", endpoint = "town", device = "T1", user_id = "1001", data = { user_id = "1001" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { work_code = "8" } },
]
[step.expect]
route = "unrouted"
[[step]]
name = "a code no route has: unrouted"
hook = "route"
punch = { id = "p4", endpoint = "town", device = "T1", user_id = "1001", data = { user_id = "1001", work_code = "9" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { work_code = "8" } },
]
[step.expect]
route = "unrouted"