Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+162
@@ -0,0 +1,162 @@
|
||||
import {
|
||||
filterVisibleFields,
|
||||
FormDefinitionSchema,
|
||||
validateFormValues,
|
||||
type FormDefinition,
|
||||
type FormSubmissionPayload,
|
||||
} from "@hivegrid/form-widget";
|
||||
import type { TenantRlsClient } from "~/utils/db/.server/tenantRls.server";
|
||||
import { HrServiceError } from "./hrService.server";
|
||||
import {
|
||||
buildHrLeaveFormDefinition,
|
||||
buildHrRequestFormDefinition,
|
||||
buildHrSickFormDefinition,
|
||||
type HrM05Process,
|
||||
} from "./hrM05Forms";
|
||||
|
||||
type HrM05Values = FormSubmissionPayload["values"];
|
||||
|
||||
const SYSTEM_DEFINITIONS: Record<HrM05Process, () => FormDefinition> = {
|
||||
leave: buildHrLeaveFormDefinition,
|
||||
sick: buildHrSickFormDefinition,
|
||||
request: buildHrRequestFormDefinition,
|
||||
};
|
||||
|
||||
function validatedValues(
|
||||
definition: FormDefinition,
|
||||
values: HrM05Values,
|
||||
): HrM05Values {
|
||||
const parsed = FormDefinitionSchema.parse(definition);
|
||||
const visibleKeys = new Set(filterVisibleFields(parsed.fields, values).map((field) => field.key));
|
||||
const filtered = Object.fromEntries(
|
||||
Object.entries(values).filter(([key]) => visibleKeys.has(key)),
|
||||
) as HrM05Values;
|
||||
const validationError = validateFormValues(filtered, parsed);
|
||||
if (validationError) {
|
||||
throw new HrServiceError(`hr.m05.validation.${validationError.code}`);
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
async function ensureSystemFormWithTx(
|
||||
tx: TenantRlsClient,
|
||||
input: {
|
||||
tenantId: string;
|
||||
actorUserId: string;
|
||||
process: HrM05Process;
|
||||
},
|
||||
): Promise<{ id: string; definition: FormDefinition }> {
|
||||
const definition = SYSTEM_DEFINITIONS[input.process]();
|
||||
const slug = `__hivegrid_hr_${input.process}`;
|
||||
const form = await tx.form.upsert({
|
||||
where: { tenantId_slug: { tenantId: input.tenantId, slug } },
|
||||
update: {
|
||||
name: `Internal HR ${input.process}`,
|
||||
subtype: "internal",
|
||||
status: "published",
|
||||
isPublic: false,
|
||||
isEnabled: true,
|
||||
definition: definition as object,
|
||||
},
|
||||
create: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.actorUserId,
|
||||
name: `Internal HR ${input.process}`,
|
||||
slug,
|
||||
subtype: "internal",
|
||||
status: "published",
|
||||
publishedAt: new Date(),
|
||||
isPublic: false,
|
||||
isEnabled: true,
|
||||
definition: definition as object,
|
||||
allowedOrigins: [],
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
return { id: form.id, definition };
|
||||
}
|
||||
|
||||
export async function createSystemHrSubmissionWithTx(
|
||||
tx: TenantRlsClient,
|
||||
input: {
|
||||
tenantId: string;
|
||||
actorUserId: string;
|
||||
process: HrM05Process;
|
||||
linkedEntityType: string;
|
||||
linkedEntityId: string;
|
||||
values: HrM05Values;
|
||||
},
|
||||
): Promise<string> {
|
||||
const form = await ensureSystemFormWithTx(tx, input);
|
||||
const values = validatedValues(form.definition, input.values);
|
||||
const submission = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.actorUserId,
|
||||
payload: { values },
|
||||
status: "received",
|
||||
linkedEntityType: input.linkedEntityType,
|
||||
linkedEntityId: input.linkedEntityId,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
return submission.id;
|
||||
}
|
||||
|
||||
export async function createTemplateHrRequestSubmissionWithTx(
|
||||
tx: TenantRlsClient,
|
||||
input: {
|
||||
tenantId: string;
|
||||
actorUserId: string;
|
||||
templateId: string;
|
||||
linkedEntityId: string;
|
||||
values: HrM05Values;
|
||||
},
|
||||
): Promise<string> {
|
||||
const template = await tx.hrRequestTemplate.findFirst({
|
||||
where: {
|
||||
id: input.templateId,
|
||||
tenantId: input.tenantId,
|
||||
archivedAt: null,
|
||||
isActive: true,
|
||||
},
|
||||
select: {
|
||||
linkedFormId: true,
|
||||
},
|
||||
});
|
||||
if (!template?.linkedFormId) {
|
||||
throw new HrServiceError("hr.m05.templateFormRequired");
|
||||
}
|
||||
const form = await tx.form.findFirst({
|
||||
where: {
|
||||
id: template.linkedFormId,
|
||||
tenantId: input.tenantId,
|
||||
subtype: "hr_request",
|
||||
status: "published",
|
||||
isEnabled: true,
|
||||
},
|
||||
select: { id: true, definition: true },
|
||||
});
|
||||
if (!form) {
|
||||
throw new HrServiceError("hr.m05.templateFormUnavailable");
|
||||
}
|
||||
const parsedDefinition = FormDefinitionSchema.safeParse(form.definition);
|
||||
if (!parsedDefinition.success) {
|
||||
throw new HrServiceError("hr.m05.templateFormInvalid");
|
||||
}
|
||||
const values = validatedValues(parsedDefinition.data, input.values);
|
||||
const submission = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.actorUserId,
|
||||
payload: { values },
|
||||
status: "received",
|
||||
linkedEntityType: "hr_request",
|
||||
linkedEntityId: input.linkedEntityId,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
return submission.id;
|
||||
}
|
||||
Reference in new issue
Block a user