Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,17 +1,26 @@
// P17 HR-Hub workflows (AK-P17b) — generic HR requests (expense/hardware/
// training/reference) with reusable templates.
//
// Until the M05 form renderer (AK-P16c) lands, the request payload is captured
// as a JSON string in `details` (transitional form) and `formSubmissionId` is
// wired in later. Approval mirrors the leave state machine. Every tenant access
// runs inside withTenantRLS(); transitions are audited (M001) and notify the
// counterpart (M011, fail-open).
// M05 reuse (AK-P16c): a template may link a published `hr_request` form. When
// it does, submitting a request also persists the payload as a real
// FormSubmission (linkedEntityType "hr_request") and wires `formSubmissionId`.
// The lightweight dialog fields stay as the capture surface; the linked form is
// the canonical submission store. Requests without a linked form keep the
// transitional `details` JSON string. Approval mirrors the leave state machine.
// Every tenant access runs inside withTenantRLS(); transitions are audited
// (M001) and notify the counterpart (M011, fail-open).
import { CatalogError, foundationErrors } from "@hivegrid/foundation";
import { CatalogError, foundationErrors, notify } from "@hivegrid/foundation";
import { logActivity } from "@hivegrid/activity";
import { createAuditEvent } from "@hivegrid/db/audit-events.server";
import { createNotificationDelivery } from "@hivegrid/db/notifications.server";
import { FormDefinitionSchema, type FormDefinition } from "@hivegrid/form-widget";
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
import { HrServiceError } from "./hrService.server";
import type { HrM05Values } from "./hrM05Forms";
import {
createSystemHrSubmissionWithTx,
createTemplateHrRequestSubmissionWithTx,
} from "./hrM05Submission.server";
const HR_AUDIT_MODULE = "hr";
@@ -57,7 +66,19 @@ export type RequestRow = {
async function notifyQuiet(input: { tenantId: string; userId: string | null; messageKey: string; sourceKey: string }): Promise<void> {
if (!input.userId) return;
await createNotificationDelivery({ tenantId: input.tenantId, userId: input.userId, channel: "IN_APP", category: "hr", messageKey: input.messageKey, sourceKey: input.sourceKey }).catch(() => undefined);
await withTenantRLS(input.tenantId, (tx) =>
notify(
{
tenantId: input.tenantId,
userId: input.userId,
channel: "IN_APP",
category: "hr",
messageKey: input.messageKey,
sourceKey: input.sourceKey,
},
tx as unknown as Parameters<typeof notify>[1],
),
).catch(() => undefined);
}
async function audit(input: { tenantId: string; actorId: string; action: string; targetId: string }): Promise<void> {
@@ -73,6 +94,60 @@ export async function listTemplates(tenantId: string, opts?: { includeArchived?:
return rows as RequestTemplateRow[];
}
/** Published `hr_request` forms a template can link (M05 reuse, AK-P16c). */
export async function listLinkableHrRequestForms(tenantId: string): Promise<{ id: string; name: string }[]> {
return withTenantRLS(tenantId, (tx) =>
tx.form.findMany({
where: { tenantId, subtype: "hr_request", status: "published" },
select: { id: true, name: true },
orderBy: { name: "asc" },
}),
);
}
export async function listRequestTemplateDefinitions(
tenantId: string,
): Promise<Map<string, FormDefinition>> {
return withTenantRLS(tenantId, async (tx) => {
const templates = await tx.hrRequestTemplate.findMany({
where: {
tenantId,
archivedAt: null,
isActive: true,
linkedFormId: { not: null },
},
select: { id: true, linkedFormId: true },
});
const formIds = templates.flatMap((template) =>
template.linkedFormId ? [template.linkedFormId] : [],
);
if (formIds.length === 0) return new Map();
const forms = await tx.form.findMany({
where: {
tenantId,
id: { in: formIds },
subtype: "hr_request",
status: "published",
isEnabled: true,
},
select: { id: true, definition: true },
});
const byFormId = new Map<string, FormDefinition>();
for (const form of forms) {
const parsed = FormDefinitionSchema.safeParse(form.definition);
if (parsed.success) byFormId.set(form.id, parsed.data);
}
return new Map(
templates.flatMap((template) => {
const definition = template.linkedFormId
? byFormId.get(template.linkedFormId)
: undefined;
return definition ? [[template.id, definition] as const] : [];
}),
);
});
}
export type CreateTemplateInput = { tenantId: string; actorUserId: string; title: string; requestKind: HrRequestKind; description?: string | null; defaultApproverUserId?: string | null; linkedFormId?: string | null; isDemoData?: boolean };
export async function createTemplate(input: CreateTemplateInput): Promise<RequestTemplateRow> {
@@ -116,29 +191,104 @@ export async function archiveTemplate(input: { tenantId: string; actorUserId: st
// --- Requests -------------------------------------------------------------
export type SubmitRequestInput = { tenantId: string; employeeUserId: string; templateId?: string | null; requestKind: HrRequestKind; title: string; details?: string | null; assignedApproverUserId?: string | null; isDemoData?: boolean };
export type SubmitRequestInput = { tenantId: string; employeeUserId: string; templateId?: string | null; requestKind: HrRequestKind; title: string; details?: string | null; assignedApproverUserId?: string | null; isDemoData?: boolean; formValues?: HrM05Values };
export async function submitRequest(input: SubmitRequestInput): Promise<RequestRow> {
if (!REQUEST_KINDS.includes(input.requestKind)) throw new HrServiceError("hr.error.requestKindInvalid");
const title = input.title.trim();
if (!title) throw new HrServiceError("hr.error.requestTitleRequired");
const created = (await withTenantRLS(input.tenantId, (tx) =>
tx.hrRequest.create({
if (!input.templateId && !title) throw new HrServiceError("hr.error.requestTitleRequired");
const created = (await withTenantRLS(input.tenantId, async (tx) => {
const template = input.templateId
? await tx.hrRequestTemplate.findFirst({
where: {
id: input.templateId,
tenantId: input.tenantId,
archivedAt: null,
isActive: true,
},
select: {
id: true,
title: true,
requestKind: true,
defaultApproverUserId: true,
},
})
: null;
if (input.templateId && !template) {
throw new CatalogError(foundationErrors.notFound, {
templateId: input.templateId,
});
}
const effectiveTitle = template?.title ?? title;
const effectiveKind = (template?.requestKind ?? input.requestKind) as HrRequestKind;
const request = await tx.hrRequest.create({
data: {
tenantId: input.tenantId,
employeeUserId: input.employeeUserId,
templateId: input.templateId ?? null,
requestKind: input.requestKind,
title: title.slice(0, 255),
details: input.details?.slice(0, 8000) ?? null,
templateId: template?.id ?? null,
requestKind: effectiveKind,
title: effectiveTitle.slice(0, 255),
details:
input.details?.slice(0, 8000) ??
(input.formValues ? JSON.stringify(input.formValues).slice(0, 8000) : null),
status: "submitted",
assignedApproverUserId: input.assignedApproverUserId ?? null,
assignedApproverUserId:
input.assignedApproverUserId ?? template?.defaultApproverUserId ?? null,
isDemoData: input.isDemoData ?? false,
},
}),
)) as RequestRow;
});
let linkedRequest = request;
if (input.formValues) {
const submissionId = template
? await createTemplateHrRequestSubmissionWithTx(tx, {
tenantId: input.tenantId,
actorUserId: input.employeeUserId,
templateId: template.id,
linkedEntityId: request.id,
values: input.formValues,
})
: await createSystemHrSubmissionWithTx(tx, {
tenantId: input.tenantId,
actorUserId: input.employeeUserId,
process: "request",
linkedEntityType: "hr_request",
linkedEntityId: request.id,
values: input.formValues,
});
linkedRequest = await tx.hrRequest.update({
where: { id: request.id },
data: { formSubmissionId: submissionId },
});
}
await logActivity(
{
tenantId: input.tenantId,
actorUserId: input.employeeUserId,
actorKind: "user",
activityType: "form_submitted",
subjectType: "tenant",
subjectId: input.tenantId,
summaryText: "activity.runtime.hr.requestSubmitted",
metadata: { requestId: request.id, process: "request" },
},
{ client: tx },
);
if (linkedRequest.assignedApproverUserId) {
await notify(
{
tenantId: input.tenantId,
userId: linkedRequest.assignedApproverUserId,
channel: "IN_APP",
category: "hr",
messageKey: "hr.notify.request.submitted",
sourceKey: `hr-request-submit:${request.id}`,
},
tx as unknown as Parameters<typeof notify>[1],
).catch(() => undefined);
}
return linkedRequest;
})) as RequestRow;
await audit({ tenantId: input.tenantId, actorId: input.employeeUserId, action: "HR_REQUEST_SUBMIT", targetId: created.id });
await notifyQuiet({ tenantId: input.tenantId, userId: created.assignedApproverUserId, messageKey: "hr.notify.request.submitted", sourceKey: `hr-request-submit:${created.id}` });
return created;
}