HR-ATS-Portal/frontend/src/lib/useApplications.js

59 lines
2.2 KiB
JavaScript

/* ============================================================
useApplications — the one read three screens need for the same reason.
Interviews, Calendar and Offers all have to turn an `inbox_id` into a person
and a role. GET /pipeline/candidates/fetch is the only payload that carries
inbox_id, user_id, name, email and the assigned job title on a single row, so
it is the join table for all three.
INBOX ROWS ONLY. `interviews.inbox_id` and `offers.inbox_id` are the sole
links those tables have, so a manual-upload candidate — who has no inbox row
— cannot carry an interview or an offer at all. Returning them here would
populate pickers with people the write would then reject.
One query key shared by all three callers, so navigating between them is a
cache hit rather than a third identical request.
============================================================ */
import { useQuery } from '@tanstack/react-query'
import { qk } from './queryKeys'
import * as pipelineApi from '../api/pipeline'
const BOARD_LIMIT = 300
export function useApplications() {
return useQuery({
queryKey: qk.pipeline.board({ limit: BOARD_LIMIT, source: 'inbox' }),
queryFn: async () => {
const res = await pipelineApi.listApplications({ limit: BOARD_LIMIT })
const rows = Array.isArray(res?.data?.inbox) ? res.data.inbox : []
return rows.map((row) => ({
inboxId: row.inbox_id,
userId: row.user_id ?? null,
name: row.name || row.email || 'Unknown',
email: row.email ?? null,
jobTitle: row.title ?? null,
jobPostId: row.assigned_job_post_id ?? null,
stage: pipelineApi.STAGE_FROM_STATUS[row.application_status] ?? 'Shortlist',
}))
},
})
}
/** inbox_id -> application, for hydrating rows that only carry the id. */
export function byInboxId(rows) {
const map = new Map()
for (const row of rows ?? []) map.set(row.inboxId, row)
return map
}
/** candidate user_id -> application, for rows keyed by the person instead. */
export function byUserId(rows) {
const map = new Map()
for (const row of rows ?? []) {
if (row.userId) map.set(String(row.userId), row)
}
return map
}