FPSMS-frontend
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

97 wiersze
2.2 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. export interface UserResult {
  6. action: any;
  7. id: number;
  8. username: string;
  9. staffNo: number;
  10. name: string;
  11. }
  12. // export interface DetailedUser extends UserResult {
  13. // username: string;
  14. // password: string
  15. // }
  16. export interface UserDetail {
  17. data: UserResult;
  18. authIds: number[];
  19. groupIds: number[];
  20. auths: any[];
  21. }
  22. export type passwordRule = {
  23. min: number;
  24. max: number;
  25. number: boolean;
  26. upperEng: boolean;
  27. lowerEng: boolean;
  28. specialChar: boolean;
  29. };
  30. export interface EscalationCombo {
  31. id: number;
  32. value: number;
  33. label: string;
  34. name: string;
  35. title: string;
  36. department: string;
  37. }
  38. export const preloadUser = () => {
  39. fetchUser();
  40. };
  41. export const preloadUserDetail = (id: number) => {
  42. fetchUserDetail(id);
  43. };
  44. export const fetchUser = cache(async () => {
  45. return serverFetchJson<UserResult[]>(`${BASE_API_URL}/user`, {
  46. next: { tags: ["user"] },
  47. });
  48. });
  49. export const fetchUserDetail = cache(async (id: number) => {
  50. return serverFetchJson<UserResult[]>(`${BASE_API_URL}/user/${id}`, {
  51. next: { tags: ["user"] },
  52. });
  53. });
  54. export const fetchPwRules = cache(async () => {
  55. return serverFetchJson<passwordRule>(`${BASE_API_URL}/user/password-rule`, {
  56. next: { tags: ["pwRule"] },
  57. });
  58. });
  59. export const fetchEscalationCombo = cache(async () => {
  60. return serverFetchJson<EscalationCombo[]>(`${BASE_API_URL}/user/escalation-combo`, {
  61. next: { tags: ["escalationCombo"]}
  62. })
  63. })
  64. export const exportUserQrCode = async (userIds: number[]): Promise<{ blobValue: Uint8Array; filename: string }> => {
  65. const response = await fetch(`${BASE_API_URL}/user/export-qrcode`, {
  66. method: "POST",
  67. headers: {
  68. "Content-Type": "application/json",
  69. },
  70. body: JSON.stringify({ userIds }),
  71. });
  72. if (!response.ok) {
  73. throw new Error("Failed to export QR code");
  74. }
  75. const filename = response.headers.get("Content-Disposition")?.split("filename=")[1]?.replace(/"/g, "") || "user_qrcode.pdf";
  76. const blob = await response.blob();
  77. const arrayBuffer = await blob.arrayBuffer();
  78. const blobValue = new Uint8Array(arrayBuffer);
  79. return { blobValue, filename };
  80. };