123 lines
4.1 KiB
TypeScript
123 lines
4.1 KiB
TypeScript
import type { VMState } from "../index.js";
|
|
|
|
/**
|
|
* HR survey view-models (AK-P17b). Pure functions. List, participation (items)
|
|
* and the gated results view (per-item distribution + average; chart rendering
|
|
* is the UI's job). No React, no Prisma, no app imports.
|
|
*/
|
|
|
|
export type HrSurveyItemType = "text" | "rating_5" | "nps" | "choice" | "boolean" | "date";
|
|
export type HrSurveyStatus = "draft" | "open" | "closed";
|
|
|
|
export interface HrSurveyListRow {
|
|
readonly id: string;
|
|
readonly title: string;
|
|
readonly status: HrSurveyStatus;
|
|
readonly statusLabelKey: string;
|
|
readonly isAnonymous: boolean;
|
|
readonly hasSubmitted: boolean;
|
|
readonly canViewResults: boolean;
|
|
readonly participateHref: string;
|
|
readonly resultsHref: string;
|
|
}
|
|
|
|
export interface HrSurveyListData {
|
|
readonly rows: readonly HrSurveyListRow[];
|
|
readonly canManage: boolean;
|
|
readonly createHref: string;
|
|
readonly emptyHeadlineKey: string;
|
|
readonly emptyBodyKey: string;
|
|
}
|
|
|
|
export type HrSurveyListVM = VMState<HrSurveyListData>;
|
|
|
|
export interface HrSurveyItem {
|
|
readonly id: string;
|
|
readonly itemType: HrSurveyItemType;
|
|
readonly typeLabelKey: string;
|
|
readonly prompt: string;
|
|
readonly required: boolean;
|
|
readonly options: readonly string[];
|
|
}
|
|
|
|
export interface HrSurveyParticipationData {
|
|
readonly surveyId: string;
|
|
readonly title: string;
|
|
readonly description: string | null;
|
|
readonly isAnonymous: boolean;
|
|
readonly anonymousHintKey: string;
|
|
readonly items: readonly HrSurveyItem[];
|
|
readonly alreadySubmitted: boolean;
|
|
readonly submittedHintKey: string;
|
|
}
|
|
|
|
export type HrSurveyParticipationVM = VMState<HrSurveyParticipationData>;
|
|
|
|
export interface HrSurveyItemResult {
|
|
readonly itemId: string;
|
|
readonly prompt: string;
|
|
readonly itemType: HrSurveyItemType;
|
|
readonly responseCount: number;
|
|
readonly distribution: Readonly<Record<string, number>>;
|
|
readonly average: number | null;
|
|
readonly textResponses: readonly string[];
|
|
}
|
|
|
|
export interface HrSurveyResultsData {
|
|
readonly surveyId: string;
|
|
readonly title: string;
|
|
readonly submissionCount: number;
|
|
readonly isAnonymous: boolean;
|
|
readonly items: readonly HrSurveyItemResult[];
|
|
readonly emptyHeadlineKey: string;
|
|
readonly emptyBodyKey: string;
|
|
}
|
|
|
|
export type HrSurveyResultsVM = VMState<HrSurveyResultsData>;
|
|
|
|
interface SurveyListInputRow {
|
|
readonly id: string;
|
|
readonly title: string;
|
|
readonly status: HrSurveyStatus;
|
|
readonly isAnonymous: boolean;
|
|
readonly hasSubmitted: boolean;
|
|
readonly canViewResults: boolean;
|
|
readonly participateHref: string;
|
|
readonly resultsHref: string;
|
|
}
|
|
|
|
export function buildHrSurveyListVM(input: { rows: readonly SurveyListInputRow[]; canManage: boolean; createHref: string }): HrSurveyListVM {
|
|
const rows: HrSurveyListRow[] = input.rows.map((row) => ({ ...row, statusLabelKey: `hr.surveys.status.${row.status}` }));
|
|
return { status: "ready", data: { rows, canManage: input.canManage, createHref: input.createHref, emptyHeadlineKey: "hr.surveys.empty.headline", emptyBodyKey: "hr.surveys.empty.body" } };
|
|
}
|
|
|
|
export function buildHrSurveyParticipationVM(input: {
|
|
surveyId: string;
|
|
title: string;
|
|
description: string | null;
|
|
isAnonymous: boolean;
|
|
items: ReadonlyArray<Omit<HrSurveyItem, "typeLabelKey">>;
|
|
alreadySubmitted: boolean;
|
|
}): HrSurveyParticipationVM {
|
|
return {
|
|
status: "ready",
|
|
data: {
|
|
surveyId: input.surveyId,
|
|
title: input.title,
|
|
description: input.description,
|
|
isAnonymous: input.isAnonymous,
|
|
anonymousHintKey: "hr.surveys.participation.anonymousHint",
|
|
items: input.items.map((item) => ({ ...item, typeLabelKey: `hr.surveys.itemType.${item.itemType}` })),
|
|
alreadySubmitted: input.alreadySubmitted,
|
|
submittedHintKey: "hr.surveys.participation.submittedHint",
|
|
},
|
|
};
|
|
}
|
|
|
|
export function buildHrSurveyResultsVM(input: { surveyId: string; title: string; submissionCount: number; isAnonymous: boolean; items: readonly HrSurveyItemResult[] }): HrSurveyResultsVM {
|
|
return {
|
|
status: "ready",
|
|
data: { ...input, emptyHeadlineKey: "hr.surveys.results.empty.headline", emptyBodyKey: "hr.surveys.results.empty.body" },
|
|
};
|
|
}
|