pelagia-portal/App/tests/integration/onboarding.test.ts
Hardik c82efa71af
All checks were successful
PR checks / checks (pull_request) Successful in 40s
PR checks / integration (pull_request) Successful in 28s
feat(crewing): Phase 3c — onboarding (flagged)
Final slice of Phase 3 (stacked on 3b pipeline). The onboarding transaction that
turns a SELECTED candidate into active crew, per Crewing-Implementation-Spec
§8.5/§9/§11. Behind NEXT_PUBLIC_CREWING_ENABLED; production unchanged.

What's in
- Schema (crewing_onboarding migration): CrewAssignment + AssignmentStatus
  (ACTIVE/ON_LEAVE/SIGNED_OFF — leave/sign-off are Phase 4); ContractLetter
  (salaryRestricted); SalaryStructure += assignmentId; CrewActionType +=
  CREW_ONBOARDED. Employee numbers CRW-xxxx via lib/employee-number.ts.
- Action (onboardCandidate, onboard_crew): one transaction off a SELECTED
  application — assign employeeId, create CrewAssignment(ACTIVE, signOnDate),
  bind the approved SalaryStructure (assignmentId + effectiveFrom), Application →
  ONBOARDED, Requisition → FILLED, CrewMember → EMPLOYEE (+ currentRank); contract
  letter stored after. Guards flag + permission + SELECTED state.
- Screen: the SELECTED action card's "Onboard to crew" modal (joining date,
  contract upload, starts-automatically chips); the CRW- number shows on the
  ONBOARDED card.

Tests & docs
- Integration: onboarding.test.ts (5) — full transaction, requisition FILLED +
  salary binding, joining-date + SELECTED-only guards, permission gating, sequential
  CRW- ids. type-check clean; full unit (234) + integration (168) green.
- CLAUDE.md updated with the Phase 3c surface.

Deferred: SITE_STAFF login creation for management ranks (grantsLogin) — a
follow-up; attendance/experience/PPE records begin in Phase 4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 19:12:53 +05:30

129 lines
5.9 KiB
TypeScript

/**
* Integration tests for the Crewing Phase 3c onboarding action. Onboarding is the
* side-effecting transaction off a SELECTED application (assignment + employeeId +
* salary binding + requisition FILLED + crew EMPLOYEE).
*/
import { vi, describe, it, expect, beforeAll, afterAll, afterEach } from "vitest";
vi.mock("@/auth", () => ({ auth: vi.fn() }));
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
vi.mock("@/lib/feature-flags", () => ({ CREWING_ENABLED: true, INVENTORY_ENABLED: true }));
vi.mock("@/lib/notifier", () => ({ notify: vi.fn(), notifyCrew: vi.fn() }));
import { auth } from "@/auth";
import { db } from "@/lib/db";
import { onboardCandidate } from "@/app/(portal)/crewing/applications/actions";
import { makeSession, getSeedUser, fd } from "./helpers";
import type { Role } from "@prisma/client";
let managerId: string;
let siteStaffId: string;
let rankId: string;
let vesselId: string;
const SS_EMAIL = "sitestaff@itonb.local";
const as = (userId: string, role: Role) =>
vi.mocked(auth as unknown as () => Promise<unknown>).mockResolvedValue(makeSession(userId, role));
let seq = 0;
async function selectedApplication() {
seq += 1;
const req = await db.requisition.create({ data: { code: `REQ-O${seq}`, rankId, vesselId, reason: "NEW_VACANCY", status: "SELECTED" } });
const cand = await db.crewMember.create({ data: { name: "Selected Sam", type: "NEW", status: "CANDIDATE", source: "CAREERS", appliedRankId: rankId } });
const app = await db.application.create({ data: { requisitionId: req.id, crewMemberId: cand.id, stage: "SELECTED", type: "NEW" } });
await db.salaryStructure.create({ data: { applicationId: app.id, rateBasis: "MONTHLY", basic: 50000, approvedById: managerId } });
return { appId: app.id, reqId: req.id, candId: cand.id };
}
beforeAll(async () => {
managerId = (await getSeedUser("manager@pelagia.local")).id;
const ss = await db.user.upsert({ where: { email: SS_EMAIL }, update: { role: "SITE_STAFF", isActive: true }, create: { employeeId: "ITONB-SS", email: SS_EMAIL, name: "SS Onb", role: "SITE_STAFF" } });
siteStaffId = ss.id;
rankId = (await db.rank.findFirstOrThrow()).id;
vesselId = (await db.vessel.findFirstOrThrow()).id;
});
afterEach(async () => {
await db.contractLetter.deleteMany({});
await db.crewAction.deleteMany({});
await db.salaryStructure.deleteMany({});
await db.applicationGate.deleteMany({});
await db.referenceCheck.deleteMany({});
await db.crewAssignment.deleteMany({});
await db.application.deleteMany({});
await db.bankDetail.deleteMany({});
await db.epfDetail.deleteMany({});
await db.requisition.deleteMany({});
await db.crewMember.deleteMany({});
vi.clearAllMocks();
});
afterAll(async () => {
await db.user.deleteMany({ where: { email: SS_EMAIL } });
});
describe("onboardCandidate", () => {
it("onboards a SELECTED candidate end-to-end in one transaction", async () => {
const { appId, reqId, candId } = await selectedApplication();
as(managerId, "MANAGER");
const res = await onboardCandidate(fd({ applicationId: appId, joiningDate: "2026-07-01" }));
expect("ok" in res && res.ok).toBe(true);
const assignment = await db.crewAssignment.findFirstOrThrow({ where: { crewMemberId: candId } });
expect(assignment.status).toBe("ACTIVE");
expect(assignment.requisitionId).toBe(reqId);
expect(assignment.rankId).toBe(rankId);
const cm = await db.crewMember.findUniqueOrThrow({ where: { id: candId } });
expect(cm.status).toBe("EMPLOYEE");
expect(cm.employeeId).toMatch(/^CRW-\d+$/);
expect(cm.currentRankId).toBe(rankId);
expect((await db.application.findUniqueOrThrow({ where: { id: appId } })).stage).toBe("ONBOARDED");
expect((await db.requisition.findUniqueOrThrow({ where: { id: reqId } })).status).toBe("FILLED");
const sal = await db.salaryStructure.findFirstOrThrow({ where: { applicationId: appId } });
expect(sal.assignmentId).toBe(assignment.id);
expect(sal.effectiveFrom).not.toBeNull();
const action = await db.crewAction.findFirstOrThrow({ where: { actionType: "CREW_ONBOARDED" } });
expect(action.actorId).toBe(managerId);
});
it("requires a joining date", async () => {
const { appId } = await selectedApplication();
as(managerId, "MANAGER");
const res = await onboardCandidate(fd({ applicationId: appId }));
expect("error" in res).toBe(true);
expect(await db.crewAssignment.count()).toBe(0);
});
it("only onboards from SELECTED", async () => {
const { appId } = await selectedApplication();
await db.application.update({ where: { id: appId }, data: { stage: "INTERVIEW" } });
as(managerId, "MANAGER");
const res = await onboardCandidate(fd({ applicationId: appId, joiningDate: "2026-07-01" }));
expect("error" in res).toBe(true);
expect(await db.crewAssignment.count()).toBe(0);
});
it("is rejected for roles without onboard_crew (site staff, accounts)", async () => {
const { appId } = await selectedApplication();
as(siteStaffId, "SITE_STAFF");
expect(await onboardCandidate(fd({ applicationId: appId, joiningDate: "2026-07-01" }))).toEqual({ error: "Unauthorized" });
as(managerId, "ACCOUNTS");
expect(await onboardCandidate(fd({ applicationId: appId, joiningDate: "2026-07-01" }))).toEqual({ error: "Unauthorized" });
expect(await db.crewAssignment.count()).toBe(0);
});
it("assigns sequential CRW- employee numbers", async () => {
const a = await selectedApplication();
const b = await selectedApplication();
as(managerId, "MANAGER");
await onboardCandidate(fd({ applicationId: a.appId, joiningDate: "2026-07-01" }));
await onboardCandidate(fd({ applicationId: b.appId, joiningDate: "2026-07-02" }));
const ids = (await db.crewMember.findMany({ where: { employeeId: { not: null } }, select: { employeeId: true } })).map((c) => c.employeeId);
expect(new Set(ids).size).toBe(2);
expect(ids.every((i) => /^CRW-\d+$/.test(i!))).toBe(true);
});
});