Enforce roles and tenant status on the server, and use the Kenyan business day
The admin pages redirected cashiers away, but the API itself let them read profit, expenses and purchases and record expenses and purchases. Branch managers could also change product prices for every branch. Every gated route now goes through authorize(), which answers 401 when signed out and 403 for the wrong role. - Owners and managers only: profit report, expenses, purchases, users, morning dispatch and evening count. Owner only: create and edit products, rename branches. - PATCH /api/users/:id deactivates or reactivates staff (managers: their own cashiers only) and revokes the user's device sessions. PATCH /api/branches/:id renames a branch. - Login and token refresh refuse inactive or SUSPENDED businesses. - Public signup refuses PLATFORM_ADMIN_EMAILS addresses, so an unregistered operator email can no longer be claimed. - M-Pesa sits behind FEATURE_MPESA (off by default, out of scope for the pilot): its routes return 404 and the Daraja variables are optional. Turning it on in production requires the callback IP allowlist. - "Today" and the default month now follow Africa/Nairobi instead of the server's UTC clock, so the business day no longer starts at 03:00 EAT. - Malformed ids in query strings and bodies return 400 instead of a 500, the inventory routes check the product belongs to the tenant, and the reconciliation lookups are tenant-scoped. - Branch-scoped staff see their branch's override price from GET /api/products. - Adds a vitest suite against an in-memory MongoDB (npm test). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import { randomUUID } from "crypto";
|
||||
import mongoose from "mongoose";
|
||||
import { NextRequest } from "next/server";
|
||||
import { Business } from "@/lib/server/models/Business";
|
||||
import { Branch } from "@/lib/server/models/Branch";
|
||||
import { User, UserRole } from "@/lib/server/models/User";
|
||||
import { Product } from "@/lib/server/models/Product";
|
||||
import { signAccessToken } from "@/lib/server/jwt";
|
||||
import { ACCESS_COOKIE } from "@/lib/server/auth";
|
||||
|
||||
let phoneSeq = 0;
|
||||
function nextPhone(): string {
|
||||
phoneSeq += 1;
|
||||
return "2547" + String(10_000_000 + phoneSeq).slice(-8);
|
||||
}
|
||||
|
||||
export interface Actor {
|
||||
id: string;
|
||||
businessId: string;
|
||||
role: UserRole;
|
||||
branchId: string | null;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface Tenant {
|
||||
businessId: string;
|
||||
owner: Actor;
|
||||
branchA: string;
|
||||
branchB: string;
|
||||
managerA: Actor;
|
||||
cashierA: Actor;
|
||||
cashierB: Actor;
|
||||
water20: string;
|
||||
water5: string;
|
||||
}
|
||||
|
||||
async function makeUser(
|
||||
businessId: string,
|
||||
role: UserRole,
|
||||
branchId: string | null,
|
||||
label: string
|
||||
): Promise<Actor> {
|
||||
const email = `${label}-${randomUUID().slice(0, 8)}@aquaflow.test`;
|
||||
const user = await User.create({
|
||||
businessId,
|
||||
name: label,
|
||||
role,
|
||||
branchId,
|
||||
phoneNumber: nextPhone(),
|
||||
email,
|
||||
passwordHash: "$2a$12$abcdefghijklmnopqrstuuJ0dBz7m6Y8yCjvC7vQ7hZqXbY8yCjvC", // never used to log in
|
||||
});
|
||||
return { id: user._id.toString(), businessId, role, branchId, email };
|
||||
}
|
||||
|
||||
/** A complete tenant: owner, two branches, a manager, a cashier per branch, two products. */
|
||||
export async function makeTenant(name = "Tenant"): Promise<Tenant> {
|
||||
const business = await Business.create({
|
||||
name,
|
||||
ownerId: new mongoose.Types.ObjectId(),
|
||||
tier: "ENTERPRISE",
|
||||
subscriptionStatus: "ACTIVE",
|
||||
});
|
||||
const businessId = business._id.toString();
|
||||
const owner = await makeUser(businessId, "SUPER_ADMIN", null, name + "-owner");
|
||||
business.ownerId = new mongoose.Types.ObjectId(owner.id);
|
||||
await business.save();
|
||||
|
||||
const a = await Branch.create({ businessId, branchName: name + " A", locationCity: "Nairobi" });
|
||||
const b = await Branch.create({ businessId, branchName: name + " B", locationCity: "Thika" });
|
||||
const managerA = await makeUser(businessId, "BRANCH_MANAGER", a._id.toString(), name + "-mgrA");
|
||||
const cashierA = await makeUser(businessId, "DELIVERY", a._id.toString(), name + "-cashA");
|
||||
const cashierB = await makeUser(businessId, "DELIVERY", b._id.toString(), name + "-cashB");
|
||||
|
||||
const [p20, p5] = await Product.insertMany([
|
||||
{ businessId, label: "20L Jerrycan", sizeLiters: 20, unitPrice: 200, sortOrder: 1 },
|
||||
{ businessId, label: "5L Jerrycan", sizeLiters: 5, unitPrice: 60, sortOrder: 2 },
|
||||
]);
|
||||
|
||||
return {
|
||||
businessId,
|
||||
owner,
|
||||
branchA: a._id.toString(),
|
||||
branchB: b._id.toString(),
|
||||
managerA,
|
||||
cashierA,
|
||||
cashierB,
|
||||
water20: p20._id.toString(),
|
||||
water5: p5._id.toString(),
|
||||
};
|
||||
}
|
||||
|
||||
export function tokenFor(actor: Actor, deviceId = "00000000-0000-4000-8000-000000000001"): string {
|
||||
return signAccessToken({
|
||||
sub: actor.id,
|
||||
businessId: actor.businessId,
|
||||
role: actor.role,
|
||||
branchId: actor.branchId,
|
||||
deviceId,
|
||||
});
|
||||
}
|
||||
|
||||
export function req(
|
||||
method: string,
|
||||
path: string,
|
||||
opts: { as?: Actor; body?: unknown; cookies?: Record<string, string> } = {}
|
||||
): NextRequest {
|
||||
const cookies: Record<string, string> = { ...(opts.cookies ?? {}) };
|
||||
if (opts.as) cookies[ACCESS_COOKIE] = tokenFor(opts.as);
|
||||
const cookieHeader = Object.entries(cookies)
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join("; ");
|
||||
return new NextRequest(new URL(path, "http://localhost"), {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
...(cookieHeader ? { cookie: cookieHeader } : {}),
|
||||
},
|
||||
body: opts.body === undefined ? undefined : JSON.stringify(opts.body),
|
||||
});
|
||||
}
|
||||
|
||||
export function params<T extends Record<string, string>>(p: T) {
|
||||
return { params: p };
|
||||
}
|
||||
|
||||
export async function json<T = any>(res: Response): Promise<{ status: number; body: T }> {
|
||||
return { status: res.status, body: (await res.json()) as T };
|
||||
}
|
||||
|
||||
export function uuid(): string {
|
||||
return randomUUID();
|
||||
}
|
||||
Reference in New Issue
Block a user