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
+103
-17
@@ -7,8 +7,9 @@
|
||||
// employee list — this service only persists the confirmed assignments.
|
||||
// Every tenant access runs inside withTenantRLS().
|
||||
|
||||
import { CatalogError, foundationErrors } from "@hivegrid/foundation";
|
||||
import { createAuditEvent } from "@hivegrid/db/audit-events.server";
|
||||
import { CatalogError, foundationErrors, notify } from "@hivegrid/foundation";
|
||||
import { logActivity } from "@hivegrid/activity";
|
||||
import { createAuditEvent, createAuditEventInExistingTransaction } from "@hivegrid/db/audit-events.server";
|
||||
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
import { HrServiceError } from "./hrService.server";
|
||||
|
||||
@@ -106,8 +107,32 @@ export type CreatePayslipInput = {
|
||||
/** Create (or replace) one payslip for an employee + period. Audited. */
|
||||
export async function createPayslip(input: CreatePayslipInput): Promise<PayslipRow> {
|
||||
assertPeriod(input.periodYear, input.periodMonth);
|
||||
const created = (await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.employeePayslip.upsert({
|
||||
const created = (await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const [employee, file, previous] = await Promise.all([
|
||||
tx.hrEmployeeProfile.findFirst({
|
||||
where: { tenantId: input.tenantId, userId: input.employeeUserId },
|
||||
select: { userId: true },
|
||||
}),
|
||||
tx.file.findFirst({
|
||||
where: { tenantId: input.tenantId, id: input.fileId, archivedAt: null },
|
||||
select: { id: true },
|
||||
}),
|
||||
tx.employeePayslip.findUnique({
|
||||
where: {
|
||||
tenantId_employeeUserId_periodYear_periodMonth: {
|
||||
tenantId: input.tenantId,
|
||||
employeeUserId: input.employeeUserId,
|
||||
periodYear: input.periodYear,
|
||||
periodMonth: input.periodMonth,
|
||||
},
|
||||
},
|
||||
select: { fileId: true },
|
||||
}),
|
||||
]);
|
||||
if (!employee || !file) {
|
||||
throw new CatalogError(foundationErrors.notFound);
|
||||
}
|
||||
const payslip = await tx.employeePayslip.upsert({
|
||||
where: { tenantId_employeeUserId_periodYear_periodMonth: { tenantId: input.tenantId, employeeUserId: input.employeeUserId, periodYear: input.periodYear, periodMonth: input.periodMonth } },
|
||||
update: { fileId: input.fileId, uploadedByUserId: input.uploadedByUserId, isRead: false, readAt: null, archivedAt: null },
|
||||
create: {
|
||||
@@ -119,19 +144,80 @@ export async function createPayslip(input: CreatePayslipInput): Promise<PayslipR
|
||||
fileId: input.fileId,
|
||||
isDemoData: input.isDemoData ?? false,
|
||||
},
|
||||
}),
|
||||
)) as PayslipRow;
|
||||
await createAuditEvent({
|
||||
tenantId: input.tenantId,
|
||||
actorId: input.uploadedByUserId,
|
||||
actorType: "user",
|
||||
action: "HR_PAYSLIP_UPLOAD",
|
||||
targetType: "EmployeePayslip",
|
||||
targetId: created.id,
|
||||
sourceModule: HR_AUDIT_MODULE,
|
||||
outcome: "success",
|
||||
metadata: { employeeUserId: input.employeeUserId },
|
||||
});
|
||||
});
|
||||
if (previous?.fileId && previous.fileId !== input.fileId) {
|
||||
await tx.fileEntityLink.deleteMany({
|
||||
where: {
|
||||
tenantId: input.tenantId,
|
||||
fileId: previous.fileId,
|
||||
entityType: "user",
|
||||
entityId: input.employeeUserId,
|
||||
linkRole: "hr_payslip",
|
||||
},
|
||||
});
|
||||
}
|
||||
await tx.fileEntityLink.upsert({
|
||||
where: {
|
||||
fileId_entityType_entityId: {
|
||||
fileId: input.fileId,
|
||||
entityType: "user",
|
||||
entityId: input.employeeUserId,
|
||||
},
|
||||
},
|
||||
update: { linkRole: "hr_payslip" },
|
||||
create: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.uploadedByUserId,
|
||||
fileId: input.fileId,
|
||||
entityType: "user",
|
||||
entityId: input.employeeUserId,
|
||||
linkRole: "hr_payslip",
|
||||
},
|
||||
});
|
||||
await logActivity(
|
||||
{
|
||||
tenantId: input.tenantId,
|
||||
actorUserId: input.uploadedByUserId,
|
||||
actorKind: "user",
|
||||
activityType: "file_uploaded",
|
||||
subjectType: "tenant",
|
||||
subjectId: input.tenantId,
|
||||
summaryText: "activity.runtime.hr.payslipProvided",
|
||||
metadata: {
|
||||
payslipId: payslip.id,
|
||||
employeeUserId: input.employeeUserId,
|
||||
period: `${input.periodYear}-${String(input.periodMonth).padStart(2, "0")}`,
|
||||
},
|
||||
},
|
||||
{ client: tx },
|
||||
);
|
||||
await notify(
|
||||
{
|
||||
tenantId: input.tenantId,
|
||||
userId: input.employeeUserId,
|
||||
channel: "IN_APP",
|
||||
category: "hr",
|
||||
messageKey: "hr.notify.payslip.uploaded",
|
||||
sourceKey: `hr-payslip:${payslip.id}:${payslip.updatedAt.toISOString()}`,
|
||||
},
|
||||
tx as unknown as Parameters<typeof notify>[1],
|
||||
).catch(() => undefined);
|
||||
await createAuditEventInExistingTransaction(
|
||||
{
|
||||
tenantId: input.tenantId,
|
||||
actorId: input.uploadedByUserId,
|
||||
actorType: "user",
|
||||
action: "HR_PAYSLIP_UPLOAD",
|
||||
targetType: "EmployeePayslip",
|
||||
targetId: payslip.id,
|
||||
sourceModule: HR_AUDIT_MODULE,
|
||||
outcome: "success",
|
||||
metadata: { employeeUserId: input.employeeUserId },
|
||||
},
|
||||
tx as unknown as Parameters<typeof createAuditEventInExistingTransaction>[1],
|
||||
);
|
||||
return payslip;
|
||||
})) as PayslipRow;
|
||||
return created;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user