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.
 
 

113 lines
2.5 KiB

  1. "use server";
  2. import {
  3. serverFetchJson,
  4. serverFetchWithNoContent,
  5. } from "@/app/utils/fetchUtil";
  6. import { BASE_API_URL } from "@/config/api";
  7. import { Task, TaskGroup } from "../tasks";
  8. import { Customer } from "../customer";
  9. import { revalidatePath, revalidateTag } from "next/cache";
  10. export interface CreateProjectInputs {
  11. // Project
  12. projectId: number | null;
  13. projectDeleted: boolean | null;
  14. projectCode: string;
  15. projectName: string;
  16. projectCategoryId: number;
  17. projectDescription: string;
  18. projectLeadId: number;
  19. projectActualStart: string;
  20. projectActualEnd: string;
  21. projectStatus: string;
  22. isClpProject: boolean;
  23. mainProjectId?: number | null;
  24. // Project info
  25. serviceTypeId: number;
  26. fundingTypeId: number;
  27. contractTypeId: number;
  28. locationId: number;
  29. buildingTypeIds: number[];
  30. workNatureIds: number[];
  31. taskTemplateId?: number | "All";
  32. // Client details
  33. clientId: Customer["id"];
  34. clientContactId?: number;
  35. clientSubsidiaryId?: number;
  36. subsidiaryContactId: number;
  37. isSubsidiaryContact?: boolean;
  38. // Allocation
  39. totalManhour: number;
  40. manhourPercentageByGrade: ManhourAllocation;
  41. taskGroups: {
  42. [taskGroup: TaskGroup["id"]]: {
  43. taskIds: Task["id"][];
  44. percentAllocation: number;
  45. };
  46. };
  47. allocatedStaffIds: number[];
  48. // Milestones
  49. milestones: {
  50. [taskGroupId: TaskGroup["id"]]: {
  51. startDate: string;
  52. endDate: string;
  53. payments: PaymentInputs[];
  54. };
  55. };
  56. // Miscellaneous
  57. expectedProjectFee: number;
  58. }
  59. export interface ManhourAllocation {
  60. [gradeId: number]: number;
  61. }
  62. export interface PaymentInputs {
  63. id: number;
  64. description: string;
  65. date: string;
  66. amount: number;
  67. }
  68. export interface CreateProjectResponse {
  69. id: number;
  70. name: string;
  71. code: string;
  72. category: string;
  73. team: string;
  74. client: string;
  75. }
  76. export const saveProject = async (data: CreateProjectInputs) => {
  77. const newProject = await serverFetchJson<CreateProjectResponse>(
  78. `${BASE_API_URL}/projects/new`,
  79. {
  80. method: "POST",
  81. body: JSON.stringify(data),
  82. headers: { "Content-Type": "application/json" },
  83. },
  84. );
  85. revalidateTag("projects");
  86. return newProject;
  87. };
  88. export const deleteProject = async (id: number) => {
  89. const project = await serverFetchWithNoContent(
  90. `${BASE_API_URL}/projects/${id}`,
  91. {
  92. method: "DELETE",
  93. headers: { "Content-Type": "application/json" },
  94. },
  95. );
  96. revalidateTag("projects");
  97. revalidatePath("/(main)/home");
  98. return project;
  99. };