Linking people by their PIN
The rule
A small business enrolls people at the clock under their payroll number, and nobody decides anything elsewhere: the PIN is the payroll id. An administrator’s PIN, or one with letters, is no payroll number, and is left for someone to look at.
How the recipe does it
The enrolled hook sets the person’s external id payroll to their PIN
when it is all digits and not 0. Setting an id links them: their
punches carry it, and they are sent to every clock of the group as its
placement says. Otherwise it returns nil, and they stay unlinked on the
clock that enrolled them.
What the scenarios show
tests/01-enrolled.toml: a numeric PIN links; an administrator’s
(admin) does not.
Get the recipe
The recipe, its Lua and its scenarios are in the example packages repository: policy/link-by-pin. 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
name = "link-by-pin"version = "1.0.0"kind = "policy"description = "A person enrolled at a clock is linked at once: their payroll id is the PIN they were enrolled under."host_api = 2-- A small business enrolls people at the clock under their payroll-- number. Nobody decides anything elsewhere: the PIN is the payroll id,-- so setting it links the person at once, and they are sent to every-- clock of the group. A PIN that is not a payroll number (letters, an-- administrator's "0") is left unlinked, on the clock it was enrolled at,-- for someone to look at.
return { enrolled = function(person, device) local pin = person.user_id if not string.match(pin, "^%d+$") or pin == "0" then return nil end return { person = { external_ids = { payroll = pin } } } end,}Scenarios
Each scenario calls the package’s hooks with what Connect would hand them, and checks what they decide (the format).
A person enrolled under a payroll number is linked by it; anyone else stays unlinked
description = "A person enrolled under a payroll number is linked by it; anyone else stays unlinked"
[[step]]name = "enrolled as 1001: payroll id 1001"hook = "enrolled"person = { user_id = "1001", person = "9b7c0a52-5f3e-4d0e-9a51-0c2f6d1e8a11", fields = { name = "Ann Lee" } }device = { endpoint = "office", device = "C1", group = "hq" }[step.expect]effects = { person = { external_ids = { payroll = "1001" } } }
[[step]]name = "enrolled as admin: left unlinked"hook = "enrolled"person = { user_id = "admin", fields = { name = "Setup" } }device = { endpoint = "office", device = "C1", group = "hq" }[step.expect]effects = {}