All files / services/company-system requests.ts

0% Statements 0/27
0% Branches 0/16
0% Functions 0/11
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                                                                                                                   
import "server-only";
 
import { COMPANY_SYSTEM_ENDPOINT_PATH } from "@/constants/db";
import type { TimeOffRequest } from "@/lib/db/schema";
import { cloneRows, request } from "@/services/company-system/client";
 
function isTimeOffRequest(value: unknown): value is TimeOffRequest {
  if (!value || typeof value !== "object") return false;
  const candidate = value as Partial<TimeOffRequest>;
 
  return (
    typeof candidate.id === "string" &&
    typeof candidate.employeeId === "string" &&
    typeof candidate.leaveType === "string" &&
    typeof candidate.startDate === "string" &&
    typeof candidate.endDate === "string" &&
    typeof candidate.days === "number" &&
    typeof candidate.status === "string" &&
    typeof candidate.reason === "string" &&
    typeof candidate.createdAt === "string" &&
    typeof candidate.updatedAt === "string"
  );
}
 
async function createRequest(nextRequest: TimeOffRequest) {
  return request<TimeOffRequest>(
    COMPANY_SYSTEM_ENDPOINT_PATH.timeOffRequests,
    {
      method: "POST",
      body: JSON.stringify(nextRequest),
    },
  );
}
 
async function updateRequest(nextRequest: TimeOffRequest) {
  return request<TimeOffRequest>(
    `${COMPANY_SYSTEM_ENDPOINT_PATH.timeOffRequests}/${encodeURIComponent(nextRequest.id)}`,
    {
      method: "PUT",
      body: JSON.stringify(nextRequest),
    },
  );
}
 
async function deleteRequest(requestId: string) {
  return request<void>(
    `${COMPANY_SYSTEM_ENDPOINT_PATH.timeOffRequests}/${encodeURIComponent(requestId)}`,
    {
      method: "DELETE",
    },
  );
}
 
export async function listTimeOffRequests(): Promise<TimeOffRequest[]> {
  const rows = await request<unknown[]>(
    COMPANY_SYSTEM_ENDPOINT_PATH.timeOffRequests,
  );
  return cloneRows(rows.filter(isTimeOffRequest));
}
 
export async function replaceTimeOffRequests(
  requests: TimeOffRequest[],
): Promise<TimeOffRequest[]> {
  const nextRequests = cloneRows(requests.filter(isTimeOffRequest));
  const existingRequests = await listTimeOffRequests();
  const existingRequestById = new Map(
    existingRequests.map((r) => [r.id, r]),
  );
  const nextRequestIdSet = new Set(nextRequests.map((r) => r.id));
 
  const writeOps = nextRequests.map((r) =>
    existingRequestById.has(r.id) ? updateRequest(r) : createRequest(r),
  );
  const deleteOps = existingRequests
    .filter((r) => !nextRequestIdSet.has(r.id))
    .map((r) => deleteRequest(r.id));
 
  await Promise.all([...writeOps, ...deleteOps]);
 
  return nextRequests;
}