You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 rivejä
2.9 KiB

  1. "use server";
  2. import { serverFetchJson } from "@/app/utils/fetchUtil";
  3. import { ProjectResult } from "../projects";
  4. import { Task, TaskGroup } from "../tasks";
  5. import { BASE_API_URL } from "@/config/api";
  6. import { revalidatePath, revalidateTag } from "next/cache";
  7. export interface TimeEntry {
  8. id: number;
  9. projectId?: ProjectResult["id"];
  10. taskGroupId?: TaskGroup["id"];
  11. taskId?: Task["id"];
  12. inputHours?: number;
  13. otHours?: number;
  14. remark?: string;
  15. }
  16. export interface RecordTimesheetInput {
  17. [date: string]: TimeEntry[];
  18. }
  19. export interface LeaveEntry {
  20. id: number;
  21. inputHours: number;
  22. leaveTypeId: number;
  23. remark?: string;
  24. }
  25. export interface RecordLeaveInput {
  26. [date: string]: LeaveEntry[];
  27. }
  28. export const saveTimesheet = async (
  29. data: RecordTimesheetInput,
  30. username: string,
  31. ) => {
  32. const savedRecords = await serverFetchJson<RecordTimesheetInput>(
  33. `${BASE_API_URL}/timesheets/save`,
  34. {
  35. method: "POST",
  36. body: JSON.stringify(data),
  37. headers: { "Content-Type": "application/json" },
  38. },
  39. );
  40. revalidateTag(`timesheets_${username}`);
  41. return savedRecords;
  42. };
  43. export const saveLeave = async (data: RecordLeaveInput, username: string) => {
  44. const savedRecords = await serverFetchJson<RecordLeaveInput>(
  45. `${BASE_API_URL}/timesheets/saveLeave`,
  46. {
  47. method: "POST",
  48. body: JSON.stringify(data),
  49. headers: { "Content-Type": "application/json" },
  50. },
  51. );
  52. revalidateTag(`leaves_${username}`);
  53. return savedRecords;
  54. };
  55. export const saveMemberEntry = async (data: {
  56. staffId: number;
  57. entry: TimeEntry;
  58. recordDate?: string;
  59. }) => {
  60. return serverFetchJson<RecordTimesheetInput>(
  61. `${BASE_API_URL}/timesheets/saveMemberEntry`,
  62. {
  63. method: "POST",
  64. body: JSON.stringify(data),
  65. headers: { "Content-Type": "application/json" },
  66. },
  67. );
  68. };
  69. export const saveMemberLeave = async (data: {
  70. staffId: number;
  71. entry: LeaveEntry;
  72. recordDate?: string;
  73. }) => {
  74. return serverFetchJson<RecordLeaveInput>(
  75. `${BASE_API_URL}/timesheets/saveMemberLeave`,
  76. {
  77. method: "POST",
  78. body: JSON.stringify(data),
  79. headers: { "Content-Type": "application/json" },
  80. },
  81. );
  82. };
  83. export const deleteMemberEntry = async (data: {
  84. staffId: number;
  85. entryId: number;
  86. }) => {
  87. return serverFetchJson<RecordTimesheetInput>(
  88. `${BASE_API_URL}/timesheets/deleteMemberEntry`,
  89. {
  90. method: "POST",
  91. body: JSON.stringify(data),
  92. headers: { "Content-Type": "application/json" },
  93. },
  94. );
  95. };
  96. export const deleteMemberLeave = async (data: {
  97. staffId: number;
  98. entryId: number;
  99. }) => {
  100. return serverFetchJson<RecordLeaveInput>(
  101. `${BASE_API_URL}/timesheets/deleteMemberLeave`,
  102. {
  103. method: "POST",
  104. body: JSON.stringify(data),
  105. headers: { "Content-Type": "application/json" },
  106. },
  107. );
  108. };
  109. export const revalidateCacheAfterAmendment = () => {
  110. revalidatePath("/(main)/home");
  111. };