fix(GRO-1272): auto-provision staff record on first OIDC login #19

Merged
groombook-engineer[bot] merged 4 commits from fleaflicker/gro-1272-auto-provision-staff-dev into dev 2026-05-21 14:16:42 +00:00
3 changed files with 137 additions and 26 deletions
+1 -2
View File
@@ -26,14 +26,13 @@ GroomBook API is a Hono-based REST service (TypeScript/Node.js) powering the pet
| # | Scenario | Steps | Expected |
|---|----------|-------|----------|
| TC-API-1.1 | Login via OIDC | POST to OIDC provider callback, verify JWT token issued | 200 OK, JWT returned with valid claims |
| TC-API-1.2 | Session persistence | Make authenticated request, verify session token valid | 200 OK, request succeeds |
| TC-API-1.3 | Logout | Call logout endpoint, verify token invalidated | 200 OK, subsequent requests return 401 |
| TC-API-1.4 | Email+password login (UAT) | POST /api/auth/sign-in/email with uat-super@groombook.dev + SEED_UAT_SUPER_PASSWORD | 200 OK, session cookie returned |
| TC-API-1.5 | Email+password login — groomer | POST /api/auth/sign-in/email with uat-groomer@groombook.dev + SEED_UAT_GROOMER_PASSWORD | 200 OK, session cookie returned |
| TC-API-1.6 | Email+password login — customer | POST /api/auth/sign-in/email with uat-customer@groombook.dev + SEED_UAT_CUSTOMER_PASSWORD | 200 OK, session cookie returned |
| TC-API-1.7 | Email+password login — tester | POST /api/auth/sign-in/email with uat-tester@groombook.dev + SEED_UAT_TESTER_PASSWORD | 200 OK, session cookie returned |
| TC-API-1.8 | Email+password — invalid password | POST /api/auth/sign-in/email with wrong password | 400 Bad Request, error returned |
| TC-API-1.9 | Email+password — unknown user | POST /api/auth/sign-in/email with non-existent email | 400 Bad Request, error returned |
| TC-API-1.10 | Auto-provision on first OIDC login | First login as a Better-Auth user with no existing staff record | 200 OK, access granted; groomer staff record auto-created with name/email from user table |
### 4.2 Client Management
+108 -23
View File
@@ -45,40 +45,76 @@ const GROOMER: StaffRow = {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
let staffLookupResult: StaffRow | null = null;
let managerFallbackResult: StaffRow | null = MANAGER;
let userLookupResult: { id: string; name: string | null; email: string | null } | null = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
let _insertedStaff: StaffRow | null = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
vi.mock("../db", () => {
const staff = new Proxy(
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
{ _name: "staff" },
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
{
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
get(target, prop) {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (prop === "_name") return "staff";
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (prop === "$inferSelect") return {};
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return { table: "staff", column: prop };
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const makeTableProxy = (name: string) =>
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
new Proxy(
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
{ _name: name },
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
{
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
get(target, prop) {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (prop === "_name") return name;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (prop === "$inferSelect") return {};
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return { table: name, column: prop };
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const staff = makeTableProxy("staff");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const user = makeTableProxy("user");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const buildQuery = (result: unknown, fallback: unknown) => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
[Symbol.iterator]: function* () {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (result) yield result;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
limit: (_n: number) => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const item = result ?? fallback;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
[Symbol.iterator]: function* () { if (item) yield item; },
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
0: item,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
length: item ? 1 : 0,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
};
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return {
getDb: () => ({
select: () => ({
from: () => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
where: () => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
limit: () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
// dev mode fallback to first manager
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return managerFallbackResult ? [managerFallbackResult] : [];
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
[Symbol.iterator]: function* () {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
if (staffLookupResult) yield staffLookupResult;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
0: staffLookupResult,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
length: staffLookupResult ? 1 : 0,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
from: (table: unknown) => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
where: () => buildQuery(
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
table === staff ? staffLookupResult : userLookupResult,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
table === staff ? managerFallbackResult : null
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
insert: (_table: unknown) => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
values: (vals: Record<string, unknown>) => ({
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
returning: () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const newStaff: StaffRow = {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
id: "new-staff-id",
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
oidcSub: null,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
userId: vals.userId as string,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
role: vals.role as StaffRow["role"],
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
isSuperUser: false,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
name: vals.name as string,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
email: vals.email as string,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
active: true,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
icalToken: null,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
createdAt: new Date(),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
updatedAt: new Date(),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
};
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
_insertedStaff = newStaff;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return [newStaff];
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
},
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}),
}),
}),
staff,
user,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
eq: vi.fn((_col: unknown, _val: unknown) => ({ col: _col, val: _val })),
and: vi.fn((..._clauses: unknown[]) => ({})),
sql: vi.fn((..._args: unknown[]) => ({})),
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
};
});
@@ -87,6 +123,8 @@ vi.mock("../db", () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
function resetMocks() {
staffLookupResult = null;
managerFallbackResult = MANAGER;
userLookupResult = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
_insertedStaff = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
}
/** Build a minimal Hono app with jwtPayload pre-set, then apply a middleware. */
@@ -96,7 +134,10 @@ function buildApp(
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
) {
const app = new Hono<AppEnv>();
app.use("*", async (c, next) => {
c.set("jwtPayload", { sub: staffLookupResult?.userId ?? "unknown-sub" });
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
c.set("jwtPayload", {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
sub: userLookupResult?.id ?? staffLookupResult?.userId ?? "unknown-sub",
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
email: userLookupResult?.email,
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
await next();
});
app.use("*", middleware);
@@ -202,6 +243,50 @@ describe("resolveStaffMiddleware", () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const body = await res.json();
expect(body.error).toMatch(/no staff records found/i);
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
it("auto-provision: creates groomer staff record on first login when Better-Auth user exists", async () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
staffLookupResult = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
userLookupResult = { id: "ba-user-new", name: "New User", email: "newuser@example.com" };
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
let capturedStaff: StaffRow | null = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const app = buildApp(resolveStaffMiddleware, (c) => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
capturedStaff = c.get("staff");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return c.json({ ok: true });
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const res = await app.request("/test");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(res.status).toBe(200);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff).not.toBeNull();
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.role).toBe("groomer");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.userId).toBe("ba-user-new");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.name).toBe("New User");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.email).toBe("newuser@example.com");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.isSuperUser).toBe(false);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
it("auto-provision: falls back to email prefix when user has no name", async () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
staffLookupResult = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
userLookupResult = { id: "ba-user-noname", name: null, email: "firstlogin@example.com" };
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
let capturedStaff: StaffRow | null = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const app = buildApp(resolveStaffMiddleware, (c) => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
capturedStaff = c.get("staff");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
return c.json({ ok: true });
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const res = await app.request("/test");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(res.status).toBe(200);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(capturedStaff!.name).toBe("firstlogin");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
it("auto-provision: returns 403 when no staff record and no Better-Auth user exists", async () => {
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
staffLookupResult = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
userLookupResult = null;
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const app = buildApp(resolveStaffMiddleware);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const res = await app.request("/test");
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(res.status).toBe(403);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
const body = await res.json();
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
expect(body.error).toMatch(/no staff record found for authenticated user/i);
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
});
// ─── requireRole tests ────────────────────────────────────────────────────────
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
Review

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.

returns with no . All non-limit WHERE queries throw TypeError → 500. Add to the returned object, and make fall back to when is null.
+28 -1
View File
@@ -1,5 +1,5 @@
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
import type { MiddlewareHandler } from "hono";
import { and, eq, getDb, sql, staff } from "../db/index.js";
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
import { and, eq, getDb, sql, staff, user } from "../db/index.js";
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
export type StaffRole = "groomer" | "receptionist" | "manager";
export type StaffRow = typeof staff.$inferSelect;
@@ -110,6 +110,33 @@ export const resolveStaffMiddleware: MiddlewareHandler<AppEnv> = async (
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
return;
}
}
// Auto-provision: no staff record exists for this user at all, but a valid
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
// Better-Auth user session exists (jwt.sub = user.id from user table).
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
// Create a minimal groomer staff record on first login.
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
const [userRow] = await db
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.select({ id: user.id, name: user.name, email: user.email })
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.from(user)
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.where(eq(user.id, jwt.sub))
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.limit(1);
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
if (userRow) {
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
const [newStaff] = await db
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.insert(staff)
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.values({
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
name: userRow.name ?? jwt.email?.split("@")[0] ?? "Unknown",
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
email: userRow.email ?? jwt.email ?? "",
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
userId: jwt.sub,
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
role: "groomer",
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
isSuperUser: false,
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
active: true,
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
})
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
.returning();
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
if (!newStaff) {
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
return c.json({ error: "Internal error: staff record creation failed" }, 500);
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
}
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
c.set("staff", newStaff);
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
await next();
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
return;
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
}
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
return c.json(
{ error: "Forbidden: no staff record found for authenticated user" },
403
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .
Review

TS2769: is — add a guard before to avoid passing .

TS2769: is — add a guard before to avoid passing .