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.
 
 

137 lines
4.1 KiB

  1. "use client";
  2. import { NEXT_PUBLIC_API_URL } from "@/config/api";
  3. import { UserResult } from "./index";
  4. export const exportUserQrCode = async (userIds: number[]): Promise<{ blobValue: Uint8Array; filename: string }> => {
  5. const token = localStorage.getItem("accessToken");
  6. const response = await fetch(`${NEXT_PUBLIC_API_URL}/user/export-qrcode`, {
  7. method: "POST",
  8. headers: {
  9. "Content-Type": "application/json",
  10. ...(token && { Authorization: `Bearer ${token}` }),
  11. },
  12. body: JSON.stringify({ userIds }),
  13. });
  14. if (!response.ok) {
  15. if (response.status === 401) {
  16. throw new Error("Unauthorized: Please log in again");
  17. }
  18. throw new Error(`Failed to export QR code: ${response.status} ${response.statusText}`);
  19. }
  20. const filename = response.headers.get("Content-Disposition")?.split("filename=")[1]?.replace(/"/g, "") || "user_qrcode.pdf";
  21. const blob = await response.blob();
  22. const arrayBuffer = await blob.arrayBuffer();
  23. const blobValue = new Uint8Array(arrayBuffer);
  24. return { blobValue, filename };
  25. };
  26. export interface PrintUserQrCodeRequest {
  27. userIds: number[];
  28. printerId: number;
  29. printQty?: number;
  30. }
  31. export const printUserQrCode = async (data: PrintUserQrCodeRequest): Promise<void> => {
  32. const token = localStorage.getItem("accessToken");
  33. const response = await fetch(`${NEXT_PUBLIC_API_URL}/user/print-qrcode`, {
  34. method: "POST",
  35. headers: {
  36. "Content-Type": "application/json",
  37. ...(token && { Authorization: `Bearer ${token}` }),
  38. },
  39. body: JSON.stringify(data),
  40. });
  41. if (!response.ok) {
  42. if (response.status === 401) {
  43. throw new Error("Unauthorized: Please log in again");
  44. }
  45. throw new Error(`Failed to print QR code: ${response.status} ${response.statusText}`);
  46. }
  47. };
  48. export const searchUsersByUsernameOrName = async (searchTerm: string): Promise<UserResult[]> => {
  49. if (!searchTerm.trim()) {
  50. return [];
  51. }
  52. const token = localStorage.getItem("accessToken");
  53. const [usernameResults, nameResults] = await Promise.all([
  54. fetch(`${NEXT_PUBLIC_API_URL}/user?username=${encodeURIComponent(searchTerm)}`, {
  55. method: "GET",
  56. headers: {
  57. "Content-Type": "application/json",
  58. ...(token && { Authorization: `Bearer ${token}` }),
  59. },
  60. }).then(res => {
  61. if (!res.ok) {
  62. if (res.status === 401) {
  63. throw new Error("Unauthorized: Please log in again");
  64. }
  65. throw new Error("Failed to search by username");
  66. }
  67. return res.json();
  68. }),
  69. fetch(`${NEXT_PUBLIC_API_URL}/user?name=${encodeURIComponent(searchTerm)}`, {
  70. method: "GET",
  71. headers: {
  72. "Content-Type": "application/json",
  73. ...(token && { Authorization: `Bearer ${token}` }),
  74. },
  75. }).then(res => {
  76. if (!res.ok) {
  77. if (res.status === 401) {
  78. throw new Error("Unauthorized: Please log in again");
  79. }
  80. throw new Error("Failed to search by name");
  81. }
  82. return res.json();
  83. }),
  84. ]);
  85. const mergedResults = [...usernameResults, ...nameResults];
  86. const uniqueResults = mergedResults.filter(
  87. (user, index, self) => index === self.findIndex((u) => u.id === user.id)
  88. );
  89. return uniqueResults;
  90. };
  91. export const searchUsers = async (searchParams: {
  92. username?: string;
  93. name?: string;
  94. staffNo?: string;
  95. }): Promise<UserResult[]> => {
  96. const token = localStorage.getItem("accessToken");
  97. const params = new URLSearchParams();
  98. if (searchParams.username) params.append("username", searchParams.username);
  99. if (searchParams.name) params.append("name", searchParams.name);
  100. if (searchParams.staffNo) params.append("staffNo", searchParams.staffNo);
  101. const response = await fetch(`${NEXT_PUBLIC_API_URL}/user?${params.toString()}`, {
  102. method: "GET",
  103. headers: {
  104. "Content-Type": "application/json",
  105. ...(token && { Authorization: `Bearer ${token}` }),
  106. },
  107. });
  108. if (!response.ok) {
  109. if (response.status === 401) {
  110. throw new Error("Unauthorized: Please log in again");
  111. }
  112. throw new Error(`Failed to search users: ${response.status} ${response.statusText}`);
  113. }
  114. return response.json();
  115. };