pelagia-portal/App/lib/id-generators.ts
Hardik a2c35d0a93 feat(admin): auto-generate structured IDs for users, vendors, accounts and cost centres
Users: employeeId auto-generated from role prefix (TCH/MAN/ACC/MGR/SUP/AUD/ADM)
followed by next sequential number; shown read-only in edit form, removed
from create form. Cost Centres: new code field (SITE-001 ...) added to
Vessel model with migration + backfill; auto-generated on create, read-only
in edit. Vendors and Accounts: code/vendorId inputs pre-filled with the
next suggested ID (VND-001, ACC-001) from the server page; user can override
with any PREFIX-NUMBER format, validated by regex.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 15:02:50 +05:30

21 lines
641 B
TypeScript

export const ROLE_PREFIX: Record<string, string> = {
TECHNICAL: "TCH",
MANNING: "MAN",
ACCOUNTS: "ACC",
MANAGER: "MGR",
SUPERUSER: "SUP",
AUDITOR: "AUD",
ADMIN: "ADM",
};
/** Find max existing number for prefix and return prefix-(max+1), zero-padded to 3 digits */
export function nextId(prefix: string, existingIds: (string | null | undefined)[]): string {
const re = new RegExp(`^${prefix}-(\\d+)$`, "i");
let max = 0;
for (const id of existingIds) {
if (!id) continue;
const m = id.match(re);
if (m) max = Math.max(max, parseInt(m[1], 10));
}
return `${prefix}-${String(max + 1).padStart(3, "0")}`;
}