From 26b9eff40958c112212dd15aa63c5dfed4df42da Mon Sep 17 00:00:00 2001 From: "tomosa.sarkar" Date: Tue, 4 Nov 2025 13:05:00 +0530 Subject: [PATCH] feat : screen for user create and update feat : unlocked users. --- src/app/_util/otp.ts | 4 +- src/app/administrator/home/UnlockedUsers.tsx | 265 ++++++++++ .../administrator/home/UserConfiguration.tsx | 292 ++++++++--- .../home/ViewUserConfiguration.tsx | 342 +++++++------ src/app/administrator/home/page.tsx | 475 ++++++++++++------ src/app/login/page.tsx | 8 +- 6 files changed, 982 insertions(+), 404 deletions(-) create mode 100644 src/app/administrator/home/UnlockedUsers.tsx diff --git a/src/app/_util/otp.ts b/src/app/_util/otp.ts index 7a70463..0e1fc67 100644 --- a/src/app/_util/otp.ts +++ b/src/app/_util/otp.ts @@ -14,8 +14,8 @@ interface SendOtpPayload { } function getStoredMobileNumber(): string { - const mobileNumber = localStorage.getItem('remitter_mobile_no'); - // const mobileNumber = "7890544527"; + // const mobileNumber = localStorage.getItem('remitter_mobile_no'); + const mobileNumber = "7890544527"; if (!mobileNumber) throw new Error('Mobile number not found.'); return mobileNumber; } diff --git a/src/app/administrator/home/UnlockedUsers.tsx b/src/app/administrator/home/UnlockedUsers.tsx new file mode 100644 index 0000000..12842ff --- /dev/null +++ b/src/app/administrator/home/UnlockedUsers.tsx @@ -0,0 +1,265 @@ +"use client"; + +import React, { useState, useEffect } from "react"; +import { + TextInput, + Button, + Title, + Stack, + Group, + Text, + Paper, + LoadingOverlay, + Divider, + Select, + Center, +} from "@mantine/core"; +import { notifications } from "@mantine/notifications"; +import { useRouter } from "next/navigation"; + +export default function UserUnlockPage() { + const router = useRouter(); + const [CIF, setCIF] = useState(""); + const [username, setUsername] = useState(""); + const [userDetails, setUserDetails] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + // Redirect if not logged in + useEffect(() => { + const token = localStorage.getItem("admin_access_token"); + if (!token) { + localStorage.clear(); + sessionStorage.clear(); + router.push("/administrator/login"); + } + }, [router]); + + const checkAuth = () => { + const token = localStorage.getItem("admin_access_token"); + if (!token) { + localStorage.clear(); + sessionStorage.clear(); + router.push("/administrator/login"); + return null; + } + return token; + }; + + // Fetch user info + const handleSearch = async () => { + setError(null); + setUserDetails(null); + + if (!CIF && !username) { + notifications.show({ + color: "red", + title: "Missing Input", + message: "Please enter either CIF number or username.", + }); + return; + } + + setLoading(true); + const token = checkAuth(); + if (!token) return; + + try { + const query = CIF ? CIF : username; + const response = await fetch(`/api/auth/admin/user/rights?CIF=${query}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + "X-Login-Type": "Admin", + Authorization: `Bearer ${token}`, + }, + }); + + const data = await response.json(); + + if (!response.ok || !data?.customer_no) { + setError("User not found or invalid CIF/username."); + notifications.show({ + color: "red", + title: "User Not Found", + message: "Invalid CIF or username.", + }); + return; + } + + setUserDetails(data); + + notifications.show({ + color: data.locked ? "red" : "green", + title: "User Found", + message: `User ${data.customer_no} is currently ${data.locked ? "Locked 🔒" : "Unlocked 🔓" + }`, + }); + } catch (err) { + console.error(err); + notifications.show({ + color: "red", + title: "Error", + message: "Failed to fetch user details.", + }); + } finally { + setLoading(false); + } + }; + + // 🔄 Unlock or lock user + const handleStatusChange = async (value: string) => { + if (!userDetails?.customer_no) return; + const isUnlock = value === "unlocked"; + + const token = checkAuth(); + if (!token) return; + + setLoading(true); + try { + const response = await fetch("/api/auth/admin/user/unlock", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-Login-Type": "Admin", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + user: userDetails.customer_no, // can also be username if API supports + action: !isUnlock, // false => unlock, true => lock + }), + }); + + const data = await response.json(); + + if (response.ok) { + setUserDetails({ ...userDetails, locked: !isUnlock }); + notifications.show({ + color: "green", + title: "Success", + message: `User ${userDetails.customer_no} ${isUnlock ? "unlocked" : "locked" + } successfully.`, + }); + } else { + notifications.show({ + color: "red", + title: "Failed", + message: data.message || "Unable to update lock status.", + }); + } + } catch (err) { + console.error(err); + notifications.show({ + color: "red", + title: "Error", + message: "Something went wrong while updating user status.", + }); + } finally { + setLoading(false); + } + }; + + const handleReset = () => { + setCIF(""); + setUsername(""); + setUserDetails(null); + setError(null); + }; + + return ( + <> + User Unlock Management + + + + + + { + const input = e.currentTarget.value.replace(/\D/g, ""); + if (input.length <= 11) setCIF(input); + setError(null); + }} + disabled={!!username} + /> + + + OR + + + { + const sanitized = e.currentTarget.value.replace(/[^a-zA-Z0-9@_]/g, ""); + if (sanitized.length <= 15) setUsername(sanitized); + setError(null); + }} + disabled={!!CIF} + /> + + + {error && ( +
+ + {error} + +
+ )} + + + + + +
+ + {userDetails && ( + + + + + + Customer No: {userDetails.customer_no} + + + + Registration Status:{" "} + {userDetails.last_login === null ? ( + + Not Registered + + ) : ( + + Registered + + )} + + +