feat(auth): merge reporter, supervisor, capa_owner into ssa role
Add Site Safety Assistant (ssa) role replacing reporter, supervisor and capa_owner. Portals move under /ssa (overview, my-reports, incidents, capas). Update RLS, schema, API role checks, notification recipients, nav and tests. Add db/ops/2026-11-06-merge-roles-into-ssa.sql (not yet applied; needs owner go-ahead and a pg_dump first). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
701fefd286
commit
813395700f
@@ -69,7 +69,7 @@ export async function POST(request: NextRequest) {
|
||||
name: body.name ?? '',
|
||||
email,
|
||||
phone: (body.phone ?? '').trim() || null,
|
||||
role: (body.role as typeof users.$inferInsert['role']) ?? 'reporter',
|
||||
role: (body.role as typeof users.$inferInsert['role']) ?? 'ssa',
|
||||
siteId: body.site_id ?? null,
|
||||
department: body.department ?? null,
|
||||
passwordHash,
|
||||
@@ -78,7 +78,7 @@ export async function POST(request: NextRequest) {
|
||||
if (!created) throw new Error('User creation failed')
|
||||
createdId = created.id
|
||||
await writeAuditLog(tx, 'users', created.id, 'created', {
|
||||
email, role: body.role ?? 'reporter', site_id: body.site_id ?? null,
|
||||
email, role: body.role ?? 'ssa', site_id: body.site_id ?? null,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -50,7 +50,7 @@ export async function GET(
|
||||
|
||||
const role = session.role
|
||||
const isOwner = row.ownerUserId === session.sub
|
||||
const canRead = ['hse', 'admin', 'supervisor'].includes(role) || isOwner
|
||||
const canRead = ['hse', 'admin', 'ssa'].includes(role) || isOwner
|
||||
if (!canRead) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
||||
|
||||
const ownerField = ['hse', 'admin'].includes(role)
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function GET() {
|
||||
.leftJoin(incidents, eq(capaActions.incidentId, incidents.id))
|
||||
.orderBy(asc(capaActions.dueDate))
|
||||
|
||||
if (session.role === 'supervisor' || session.role === 'worker') {
|
||||
if (session.role === 'ssa') {
|
||||
q = q.where(eq(capaActions.ownerUserId, session.sub)) as typeof q
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function GET(
|
||||
const { id } = await params
|
||||
const session = await getSession()
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
if (!['hse', 'admin', 'supervisor'].includes(session.role))
|
||||
if (!['hse', 'admin', 'ssa'].includes(session.role))
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
||||
|
||||
const authorAlias = aliasedTable(users, 'author_user')
|
||||
@@ -45,7 +45,7 @@ export async function POST(
|
||||
const { id } = await params
|
||||
const session = await getSession()
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
if (!['hse', 'admin', 'supervisor'].includes(session.role))
|
||||
if (!['hse', 'admin', 'ssa'].includes(session.role))
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
||||
|
||||
const body: { body?: string } = await request.json().catch(() => ({}))
|
||||
|
||||
@@ -51,7 +51,7 @@ export async function POST(
|
||||
await createInAppNotifications([{
|
||||
userId: incident.reportedBy,
|
||||
title: `Your incident report ${incident.referenceNo ?? ''} has been closed`,
|
||||
link: '/reporter',
|
||||
link: '/ssa/my-reports',
|
||||
incidentId: id,
|
||||
}])
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ export async function GET(_req: Request, { params }: { params: Promise<{ id: str
|
||||
if (!incident) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
const isOwner = incident.reporterId === session.sub
|
||||
const isSiteStaff = ['hse', 'supervisor', 'admin'].includes(session.role)
|
||||
const isSiteStaff = ['hse', 'ssa', 'admin'].includes(session.role)
|
||||
if (!isOwner && !isSiteStaff) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
||||
|
||||
const evidenceFileRows = await withUser(session.sub, async tx =>
|
||||
|
||||
@@ -188,7 +188,7 @@ async function handlePost(request: Request) {
|
||||
db.select({ id: users.id, phone: users.phone })
|
||||
.from(users)
|
||||
.where(and(
|
||||
inArray(users.role, ['supervisor', 'hse']),
|
||||
inArray(users.role, ['ssa', 'hse']),
|
||||
eq(users.siteId, zone.siteId),
|
||||
))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user