FPSMS-frontend
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.
 
 
 

110 lines
2.8 KiB

  1. "use server";
  2. // import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
  3. // import { BASE_API_URL } from "@/config/api";
  4. import {
  5. serverFetchJson,
  6. serverFetchWithNoContent,
  7. } from "../../utils/fetchUtil";
  8. import { BASE_API_URL } from "../../../config/api";
  9. import { revalidateTag } from "next/cache";
  10. import { EscalationCombo, UserDetail, UserResult } from ".";
  11. import { cache } from "react";
  12. export interface UserInputs {
  13. username: string;
  14. // name: string;
  15. addAuthIds?: number[];
  16. removeAuthIds?: number[];
  17. password?: string;
  18. }
  19. export interface PasswordInputs {
  20. password: string;
  21. newPassword: string;
  22. newPasswordCheck: string;
  23. }
  24. export interface NameList {
  25. id: number;
  26. name: string;
  27. }
  28. export interface NewNameList {
  29. id: number;
  30. name: string;
  31. title: string;
  32. department: string;
  33. }
  34. export const fetchUserDetails = cache(async (id: number) => {
  35. return serverFetchJson<UserDetail>(`${BASE_API_URL}/user/${id}`, {
  36. next: { tags: ["user"] },
  37. });
  38. });
  39. export const fetchNameList = cache(async () => {
  40. return serverFetchJson<NameList[]>(`${BASE_API_URL}/user/name-list`, {
  41. next: { tags: ["user"] },
  42. });
  43. });
  44. export const fetchNewNameList = cache(async () => {
  45. return serverFetchJson<NewNameList[]>(`${BASE_API_URL}/user/new-name-list`, {
  46. next: { tags: ["user"] },
  47. });
  48. });
  49. export const editUser = async (id: number, data: UserInputs) => {
  50. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  51. method: "PUT",
  52. body: JSON.stringify(data),
  53. headers: { "Content-Type": "application/json" },
  54. });
  55. revalidateTag("user");
  56. return newUser;
  57. };
  58. export const createUser = async (data: UserInputs) => {
  59. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/save`, {
  60. method: "POST",
  61. body: JSON.stringify(data),
  62. headers: { "Content-Type": "application/json" },
  63. });
  64. revalidateTag("user");
  65. return newUser;
  66. };
  67. export const deleteUser = async (id: number) => {
  68. const newUser = serverFetchWithNoContent(`${BASE_API_URL}/user/${id}`, {
  69. method: "DELETE",
  70. headers: { "Content-Type": "application/json" },
  71. });
  72. revalidateTag("user");
  73. return newUser;
  74. };
  75. export const changePassword = async (data: any) => {
  76. return serverFetchWithNoContent(`${BASE_API_URL}/user/change-password`, {
  77. method: "PATCH",
  78. body: JSON.stringify(data),
  79. headers: { "Content-Type": "application/json" },
  80. });
  81. };
  82. export const adminChangePassword = async (data: any) => {
  83. return serverFetchWithNoContent(
  84. `${BASE_API_URL}/user/admin-change-password`,
  85. {
  86. method: "PATCH",
  87. body: JSON.stringify(data),
  88. headers: { "Content-Type": "application/json" },
  89. },
  90. );
  91. };
  92. export const fetchEscalationCombo = async () => {
  93. return serverFetchJson<EscalationCombo>(`${BASE_API_URL}/user/escalation-combo`, {
  94. next: { tags: ["escalationCombo"]}
  95. })
  96. };