Let a shop run its own counter: supervisor and cashier, created from the till

A shop had no way to add the people who work in it. The terminal fell back to
three names and three PINs compiled into the app — the same three on every
install — because there was nothing for it to fall back *from*.

Two roles now exist in `app_roles`: Supervisor (7) runs the terminal and creates
staff, Cashier (8) bills. Fixed ids, written by hand, because that table has no
sequence and every id in it was assigned the same way. configid is left NULL
rather than duplicated per portal: a till is a till whichever portal a tenant
uses, and Admin already appears twice in that table for exactly that reason.

`/pos/users` is CRUD over them, and `/pos/login/pin` signs a cashier on at a
terminal a supervisor has already opened.

The rule every one of these follows: **tenant and outlet come from the caller's
token, never from the request.** There is no location field on the create body
to get wrong. A supervisor at Selvapuram cannot create staff at R mart, for the
same reason a till cannot bill into another shop's books — it is the same
inversion applied to people instead of sales.

PIN sign-in is deliberately behind the guard. Four digits is ten thousand
guesses, which is no barrier to an anonymous caller; requiring a session means a
real password opened the terminal first and the guesses are confined to one
outlet's own staff. The session it mints is fresh rather than derived, so a
cashier taking over from a supervisor drops their permissions instead of
inheriting them.

Three things the schema forced:

- A PIN cannot start with zero. `app_users.pin` is a bigint, so "0451" stores as
  451 and reads back as three digits — a cashier would type four and be refused
  for ever. Live data already holds one such account. Rendering refuses to show
  a PIN it cannot represent, rather than showing a short one nobody can type.
- `app_users` has no sequence either, so the next id is read and written inside
  one transaction behind an advisory lock. Two supervisors creating staff at the
  same moment would otherwise compute the same id and one insert would lose.
- 1234, 1111 and friends are refused outright. Live data has 1234 on eleven
  accounts and 1111 on nine.

Proven against outlet 1135, which had zero staff and was the reason the built-in
PINs were still load-bearing:

    created 9188  Store Supervisor  Supervisor  can_manage_staff=true
    created 9189  Counter Cashier   Cashier     can_manage_staff=false
    /pos/staff now returns 2        an unknown PIN is refused

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-08-06 20:22:16 +05:30
parent c696ec3e79
commit 4b27b84b1f
12 changed files with 1387 additions and 14 deletions

View File

@@ -87,6 +87,20 @@ func (r *posRepository) PosLogin(req models.PosLoginRequest) (*models.PosSession
return nil, errPosLoginRejected
}
return r.sessionFor(row, req.Locationid)
}
// sessionFor turns an authenticated account into the session it is entitled to.
//
// Shared by both ways in — an email and password, or a PIN at an already-open
// terminal. Extracted rather than duplicated because everything after the
// credential check is authorisation, and two copies of an authorisation rule
// is one copy too many.
//
// [requestedLocation] is optional and only means anything for an account that
// reaches more than one outlet. It is checked against that set, never trusted
// on its own.
func (r *posRepository) sessionFor(row posLoginRow, requestedLocation int) (*models.PosSession, error) {
if row.Tenantid <= 0 {
return nil, fmt.Errorf("this account is not attached to a tenant and cannot open a till")
}
@@ -102,30 +116,33 @@ func (r *posRepository) PosLogin(req models.PosLoginRequest) (*models.PosSession
// Which outlet this terminal is standing in. A request may ask for one, but
// only from the set the account already reaches.
chosen := locations[0]
if req.Locationid > 0 {
if requestedLocation > 0 {
match := false
for _, loc := range locations {
if loc.Locationid == req.Locationid {
if loc.Locationid == requestedLocation {
chosen, match = loc, true
break
}
}
if !match {
return nil, fmt.Errorf("this account cannot open a till at outlet %d", req.Locationid)
return nil, fmt.Errorf("this account cannot open a till at outlet %d", requestedLocation)
}
}
session := &models.PosSession{
Userid: row.Userid,
Fullname: strings.TrimSpace(row.Firstname + " " + row.Lastname),
Email: row.Email,
Roleid: row.Roleid,
Tenantid: row.Tenantid,
Storeid: fmt.Sprintf("%d", chosen.Locationid),
Locationid: chosen.Locationid,
Locationname: chosen.Locationname,
Address: chosen.Address,
Locations: locations,
Userid: row.Userid,
Fullname: strings.TrimSpace(row.Firstname + " " + row.Lastname),
Email: row.Email,
Roleid: row.Roleid,
Role: posRoleLabel(row.Roleid),
Configid: row.Configid,
Canmanagestaff: models.PosRoleCanManageStaff(row.Roleid),
Tenantid: row.Tenantid,
Storeid: fmt.Sprintf("%d", chosen.Locationid),
Locationid: chosen.Locationid,
Locationname: chosen.Locationname,
Address: chosen.Address,
Locations: locations,
}
r.decoratePosSession(session)
@@ -141,6 +158,28 @@ func (r *posRepository) PosLogin(req models.PosLoginRequest) (*models.PosSession
return session, nil
}
// posRoleLabel names a role for the terminal.
//
// Prefers the two POS roles this codebase defines, then falls back to whatever
// `app_roles` calls it — which is blank for a great many accounts, because most
// carry a roleid that is not in that table at all.
func posRoleLabel(roleID int) string {
if name := models.PosRoleName(roleID); name != "" {
return name
}
switch roleID {
case 1:
return "Super admin"
case 2:
return "Operations"
case 3, 5:
return "Admin"
case 4, 6:
return "Manager"
}
return ""
}
// posLoginCandidates finds the accounts matching a set of sign-in details.
//
// Returns a list rather than a row because `app_users` does not constrain