Skip to content

Clocks shared by several staffing agencies

The rule

A client uses workers from several staffing agencies, and they all punch at the client’s clocks. Each agency keeps its time in a tenant of its own in Time and Attendance. A worker placed by two agencies at once (at two sites, or at one site on different jobs) must have each punch sent to the agency the shift is for, and to no other.

How the recipe does it

Each placement is a route of the worker’s (routes on the person), to the agency’s tenant, with the worker’s id there, and the placement’s site and work code as codes:

{ "system": "attendance", "tenant": "agency-a", "person_id": "11",
"codes": { "site": "north-dock", "work_code": "7" } }

Each clock is labelled with its site (site = north-dock). A punch of a worker with one route goes there with no hook. With several, the route hook picks:

  1. the one route whose site is the clock’s site;
  2. of several at the clock’s site, the one whose work code the worker entered;
  3. otherwise none: the punch is unrouted (punch.unrouted, reason policy), kept aside for someone to route by hand, and never sent to two agencies.

The route’s codes reach Time and Attendance with the punch, as the punch’s values (site, work_code), for the agency’s own rules.

For one agency’s workers picked by work code alone, see route-by-work-code.

What the scenarios show

tests/01-route.toml: the north dock’s clock picks agency A, the south yard’s agency B; at one site, the work code picks; without a code, or at a site neither placement is at, or at a clock with no site, unrouted.

Get the recipe

The recipe, its Lua and its scenarios are in the example packages repository: policy/multi-agency-clock. 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

multi-agency-clock/package.toml
name = "multi-agency-clock"
version = "1.0.0"
kind = "policy"
description = "Clocks at a client's sites serve the workers of several staffing agencies: the clock's site picks the agency whose placement is there, and the work code picks between two at the same site."
host_api = 2
multi-agency-clock/main.lua
-- A client's sites have clocks used by the workers of several staffing
-- agencies, each agency a tenant of its own in Time and Attendance. A
-- worker placed by two agencies has a route to each, so at any clock both
-- are candidates. Each clock is labelled with the site it is at
-- (site = "north-dock"), and each route carries the site its placement is
-- at (codes = { site = "north-dock" }): the punch goes to the agency whose
-- placement is at the clock's site. Where both placements are at the same
-- site, the work code the worker entered picks between them. Anything else
-- is left unrouted, for someone to route by hand.
-- The candidates whose placement is at `site`.
local function at_site(candidates, site)
local here = {}
if site == nil then
return here
end
for _, c in ipairs(candidates) do
if c.codes ~= nil and c.codes.site == site then
table.insert(here, c)
end
end
return here
end
return {
route = function(punch, candidates)
local here = at_site(candidates, punch.labels and punch.labels.site)
if #here == 1 then
return here[1]
end
local entered = punch.data and punch.data.work_code
if entered == nil or entered == "" then
return nil
end
for _, c in ipairs(here) do
if c.codes.work_code == 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 clock's site picks the agency whose placement is there; at a site with two placements, the work code picks
tests/01-route.toml
description = "The clock's site picks the agency whose placement is there; at a site with two placements, the work code picks"
[[step]]
name = "at the north dock: agency A's placement is there"
hook = "route"
punch = { id = "p1", endpoint = "client-7", device = "N1", user_id = "1001", data = { user_id = "1001" }, labels = { site = "north-dock" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "south-yard" } },
]
[step.expect]
route = 1
[[step]]
name = "at the south yard: agency B's"
hook = "route"
punch = { id = "p2", endpoint = "client-7", device = "S1", user_id = "1001", data = { user_id = "1001" }, labels = { site = "south-yard" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "south-yard" } },
]
[step.expect]
route = 2
[[step]]
name = "both placements at the north dock: the work code picks agency B"
hook = "route"
punch = { id = "p3", endpoint = "client-7", device = "N1", user_id = "1001", data = { user_id = "1001", work_code = "8" }, labels = { site = "north-dock" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock", work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "north-dock", work_code = "8" } },
]
[step.expect]
route = 2
[[step]]
name = "both at the north dock and no work code: unrouted"
hook = "route"
punch = { id = "p4", endpoint = "client-7", device = "N1", user_id = "1001", data = { user_id = "1001" }, labels = { site = "north-dock" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock", work_code = "7" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "north-dock", work_code = "8" } },
]
[step.expect]
route = "unrouted"
[[step]]
name = "a clock at a site neither placement is at: unrouted"
hook = "route"
punch = { id = "p5", endpoint = "client-7", device = "W1", user_id = "1001", data = { user_id = "1001" }, labels = { site = "west-gate" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "south-yard" } },
]
[step.expect]
route = "unrouted"
[[step]]
name = "a clock with no site label: unrouted"
hook = "route"
punch = { id = "p6", endpoint = "client-7", device = "X1", user_id = "1001", data = { user_id = "1001" } }
candidates = [
{ system = "attendance", tenant = "agency-a", person_id = "11", codes = { site = "north-dock" } },
{ system = "attendance", tenant = "agency-b", person_id = "22", codes = { site = "south-yard" } },
]
[step.expect]
route = "unrouted"