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.

207 lines
4.5 KiB

  1. import { serverFetchJson } from "@/app/utils/fetchUtil";
  2. import { BASE_API_URL } from "@/config/api";
  3. import { cache } from "react";
  4. import "server-only";
  5. import { Task, TaskGroup } from "../tasks";
  6. import { CreateProjectInputs } from "./actions";
  7. export interface ProjectResult {
  8. id: number;
  9. code: string;
  10. name: string;
  11. category: string;
  12. team: string;
  13. teamCodeName: string;
  14. teamId: number;
  15. client: string;
  16. status: string;
  17. mainProject: string;
  18. }
  19. export interface MainProject {
  20. projectId: number;
  21. projectCode: string;
  22. projectName: string;
  23. projectCategoryId: number;
  24. projectDescription: string;
  25. projectLeadId: number;
  26. projectSubLeadId: number;
  27. projectStatus: string;
  28. isClpProject: boolean;
  29. serviceTypeId: number;
  30. fundingTypeId: number;
  31. contractTypeId: number;
  32. locationId: number;
  33. buildingTypeIds: number[];
  34. workNatureIds: number[];
  35. clientId: number;
  36. clientContactId: number;
  37. clientSubsidiaryId: number;
  38. expectedProjectFee: number;
  39. subContractFee: number;
  40. }
  41. export interface ProjectCategory {
  42. id: number;
  43. name: string;
  44. }
  45. export interface ServiceType {
  46. id: number;
  47. name: string;
  48. }
  49. export interface FundingType {
  50. id: number;
  51. name: string;
  52. }
  53. export interface ContractType {
  54. id: number;
  55. name: string;
  56. }
  57. export interface LocationType {
  58. id: number;
  59. name: string;
  60. }
  61. export interface BuildingType {
  62. id: number;
  63. name: string;
  64. }
  65. export interface WorkNature {
  66. id: number;
  67. name: string;
  68. }
  69. export interface ProjectWithTasks {
  70. id: number;
  71. code: string;
  72. status?: string;
  73. actualEnd?: string;
  74. description?: string;
  75. name: string;
  76. tasks: Task[];
  77. milestones: {
  78. [taskGroupId: TaskGroup["id"]]: {
  79. startDate?: string;
  80. endDate?: string;
  81. };
  82. };
  83. }
  84. export interface AssignedProject extends ProjectWithTasks {
  85. // Manhour info
  86. hoursSpent: number;
  87. hoursSpentOther: number;
  88. currentStaffHoursSpent: number;
  89. currentStaffHoursSpentOther: number;
  90. hoursAllocated: number;
  91. }
  92. export const preloadProjects = () => {
  93. fetchProjectCategories();
  94. fetchProjects();
  95. };
  96. export const fetchProjects = cache(async () => {
  97. return serverFetchJson<ProjectResult[]>(`${BASE_API_URL}/projects`, {
  98. next: { tags: ["projects"] },
  99. });
  100. });
  101. export const fetchMainProjects = cache(async () => {
  102. return serverFetchJson<MainProject[]>(`${BASE_API_URL}/projects/main`, {
  103. next: { tags: ["projects"] },
  104. });
  105. });
  106. export const fetchProjectCategories = cache(async () => {
  107. return serverFetchJson<ProjectCategory[]>(
  108. `${BASE_API_URL}/projects/categories`,
  109. {
  110. next: { tags: ["projectCategories"] },
  111. },
  112. );
  113. });
  114. export const fetchProjectServiceTypes = cache(async () => {
  115. return serverFetchJson<ServiceType[]>(
  116. `${BASE_API_URL}/projects/serviceTypes`,
  117. {
  118. next: { tags: ["projectServiceTypes"] },
  119. },
  120. );
  121. });
  122. export const fetchProjectFundingTypes = cache(async () => {
  123. return serverFetchJson<FundingType[]>(
  124. `${BASE_API_URL}/projects/fundingTypes`,
  125. {
  126. next: { tags: ["projectFundingTypes"] },
  127. },
  128. );
  129. });
  130. export const fetchProjectContractTypes = cache(async () => {
  131. return serverFetchJson<ContractType[]>(
  132. `${BASE_API_URL}/projects/contractTypes`,
  133. {
  134. next: { tags: ["projectContractTypes"] },
  135. },
  136. );
  137. });
  138. export const fetchProjectLocationTypes = cache(async () => {
  139. return serverFetchJson<LocationType[]>(
  140. `${BASE_API_URL}/projects/locationTypes`,
  141. {
  142. next: { tags: ["projectLocationTypes"] },
  143. },
  144. );
  145. });
  146. export const fetchProjectBuildingTypes = cache(async () => {
  147. return serverFetchJson<BuildingType[]>(
  148. `${BASE_API_URL}/projects/buildingTypes`,
  149. {
  150. next: { tags: ["projectBuildingTypes"] },
  151. },
  152. );
  153. });
  154. export const fetchProjectWorkNatures = cache(async () => {
  155. return serverFetchJson<WorkNature[]>(`${BASE_API_URL}/projects/workNatures`, {
  156. next: { tags: ["projectWorkNatures"] },
  157. });
  158. });
  159. export const fetchAssignedProjects = cache(async () => {
  160. return serverFetchJson<AssignedProject[]>(
  161. `${BASE_API_URL}/projects/assignedProjects`,
  162. {
  163. next: { tags: [`assignedProjects`] },
  164. },
  165. );
  166. });
  167. export const fetchProjectWithTasks = cache(async () => {
  168. return serverFetchJson<ProjectWithTasks[]>(
  169. `${BASE_API_URL}/projects/allProjectWithTasks`,
  170. {
  171. next: { tags: ["allProjectWithTasks"] },
  172. },
  173. );
  174. });
  175. export const fetchProjectDetails = cache(async (projectId: string) => {
  176. return serverFetchJson<CreateProjectInputs>(
  177. `${BASE_API_URL}/projects/projectDetails/${projectId}`,
  178. {
  179. next: { tags: [`projectDetails_${projectId}`] },
  180. },
  181. );
  182. });