From b7a454f53bce82c2773a48e9831841b64e6eae6e Mon Sep 17 00:00:00 2001 From: "tomosa.sarkar" Date: Fri, 12 Sep 2025 15:35:52 +0530 Subject: [PATCH] feat : update rights from admin user. --- src/app/(main)/layout.tsx | 9 +- .../administrator/home/UserConfiguration.tsx | 676 +++++++++--------- src/app/administrator/home/page.tsx | 13 +- 3 files changed, 354 insertions(+), 344 deletions(-) diff --git a/src/app/(main)/layout.tsx b/src/app/(main)/layout.tsx index c245356..b41b5fb 100644 --- a/src/app/(main)/layout.tsx +++ b/src/app/(main)/layout.tsx @@ -62,7 +62,9 @@ export default function RootLayout({ children }: { children: React.ReactNode }) if (response.ok && Array.isArray(data)) { if (data.length > 0) { const name = data[0].custname; + const mobileNumber = data[0].mobileno; localStorage.setItem("remitter_name", name); + localStorage.setItem("remitter_mobile_no", mobileNumber); setCustname(name); } } else { @@ -249,7 +251,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }) @@ -277,11 +279,6 @@ export default function RootLayout({ children }: { children: React.ReactNode }) - - - - - diff --git a/src/app/administrator/home/UserConfiguration.tsx b/src/app/administrator/home/UserConfiguration.tsx index 7fae96e..dea08d3 100644 --- a/src/app/administrator/home/UserConfiguration.tsx +++ b/src/app/administrator/home/UserConfiguration.tsx @@ -1,79 +1,91 @@ -import React, { useState } from 'react'; -import { TextInput, Button, Title, Stack, Group, Text, Divider, LoadingOverlay, Box, Modal, Checkbox } from '@mantine/core'; -import { notifications } from '@mantine/notifications'; +"use client"; +import React, { useState } from "react"; +import { + TextInput, + Button, + Title, + Stack, + Group, + Text, + Divider, + LoadingOverlay, + Box, + Modal, + Checkbox, + Paper, + Tabs, + ScrollArea, +} from "@mantine/core"; +import { notifications } from "@mantine/notifications"; import { useRouter } from "next/navigation"; +import { IconGavel, IconUser } from "@tabler/icons-react"; export default function UserConfiguration() { const router = useRouter(); - const [CIF, setCIF] = useState(''); + const [CIF, setCIF] = useState(""); const [userDetails, setUserDetails] = useState(null); const [loading, setLoading] = useState(false); const [savingsAccount, setSavingsAccount] = useState(null); - const [accountError, setAccountError] = useState(''); - const [detailsExpanded, setDetailsExpanded] = useState(true); const [showPreviewModal, setShowPreviewModal] = useState(false); const [confirmedPreview, setConfirmedPreview] = useState(false); const [ibEnabled, setIbEnabled] = useState(false); const [ibAccess, setIbAccess] = useState<"2" | "1" | null>(null); const [mbEnabled, setMbEnabled] = useState(false); const [mbAccess, setMbAccess] = useState<"2" | "1" | null>(null); + const isValidated = !!userDetails; - const canSubmit = isValidated && !!savingsAccount && confirmedPreview; + const canSubmit = + isValidated && savingsAccount && userDetails?.mobile && confirmedPreview; const handleValidate = async () => { if (!CIF) { notifications.show({ - color: 'red', - title: 'CIF Missing', - message: 'Please enter a CIF number to proceed.' + color: "red", + title: "CIF Missing", + message: "Please enter a CIF number to proceed.", }); return; } setLoading(true); - setAccountError(''); - setSavingsAccount(null); - setUserDetails(null); - try { const token = localStorage.getItem("admin_access_token"); - const response = await fetch(`/api/auth/admin/fetch/customer_details?CIF=${CIF}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - }, - }); + const response = await fetch( + `/api/auth/admin/fetch/customer_details?CIF=${CIF}`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + } + ); const data = await response.json(); if (response.ok && Array.isArray(data) && data.length > 0) { - const saAccount = data.find(acc => acc.stAccountType === 'SA'); - // console.log(saAccount); - if (saAccount) { - setSavingsAccount(saAccount.stAccountNo); - } else { - setAccountError('At least one savings account is required for requesting.'); - } + const saAccount = data.find((acc) => acc.stAccountType === "SA"); + setSavingsAccount(saAccount?.stAccountNo ?? null); const user = data[0]; setUserDetails({ name: user.custname, userId: user.id, - mobile: user.mobileno, + mobile: user.mobileno ?? null, address: user.custaddress, activeAccount: user.activeAccounts, }); - if (!user.mobileno) { - localStorage.setItem("user_mob_no", user.mobileno); - setAccountError('User not have any registered Mobile Number'); - } } else { - throw new Error('User not found or data format incorrect'); + notifications.show({ + color: "red", + title: "Validation Failed", + message: "User not found", + }); } - } catch (err: any) { + } catch (err) { + console.error(err); notifications.show({ - color: 'red', - title: 'Validation Failed', - message: 'User not found', + color: "red", + title: "Validation Failed", + message: "User not found", }); } finally { setLoading(false); @@ -81,22 +93,14 @@ export default function UserConfiguration() { }; const handlePreview = () => { - const hasRight = ibAccess || mbAccess; - const hasSavings = savingsAccount; - - if (!hasRight) { - notifications.show({ - title: "Rights Required", - message: "Please select at least one rights (Internet Banking or Mobile Banking).", - color: "red", - }); + if (!savingsAccount || !userDetails?.mobile) { return; } - - if (!hasSavings) { + if (!ibAccess && !mbAccess) { notifications.show({ - title: "Savings Account Required", - message: "User must have at least one Savings account.", + title: "Rights Required", + message: + "Please select at least one right (Internet Banking or Mobile Banking).", color: "red", }); return; @@ -105,302 +109,310 @@ export default function UserConfiguration() { }; const handleSubmit = async () => { - if (!canSubmit) { - notifications.show({ - color: 'red', - title: 'Required', - message: 'One savings account is required for enable rights', - }); - return; - } - else { - try { - const token = localStorage.getItem("admin_access_token"); - const response = await fetch('/api/auth/admin/user/rights', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` - }, - body: JSON.stringify({ - CIF: CIF, - ib_access_level: ibAccess, - mb_access_level: mbAccess - }), - }); - const data = await response.json(); - if (response.ok) { - if (data?.otp) { - const otp = data.otp; - try { - const otp_response = await fetch('/api/otp/send', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - mobileNumber: localStorage.getItem("user_mob_no"), - type: "REGISTRATION", - userOtp: otp - }), - }); - // const otp_result = await otp_response.json(); - if (otp_response.ok) { - notifications.show({ - color: 'blue', - title: 'Submitted', - message: `User ${CIF} rights submitted successfully. The password has been sent to the user as an OTP.`, - }); - } - } - catch (err: any) { - notifications.show({ - color: 'red', - title: 'Failed to Send', - message: `Rights for User ${CIF} saved successfully. Message delivery failed.`, - autoClose: false, - }); - } - } - if(data?.message){ - try { - const otp_response = await fetch('/api/otp/send', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - mobileNumber: localStorage.getItem("user_mob_no"), - type: "RIGHT_UPDATE", - }), - }); - if (otp_response.ok) { - notifications.show({ - color: 'blue', - title: 'Submitted', - message: `User ${CIF} rights updated successfully. The confirmation has been sent to the user.`, - }); - } - } - catch (err: any) { - notifications.show({ - color: 'red', - title: 'Failed to Send', - message: `Rights for User ${CIF} saved successfully. Message delivery failed.`, - autoClose: false, - }); - } - } - } - else if (response.status === 401 || data.message === 'invalid or expired token') { - localStorage.removeItem("admin_access_token"); - localStorage.clear(); - sessionStorage.clear(); - router.push('/administrator/login'); - } - } - catch (err: any) { - notifications.show({ - color: 'red', - title: 'Failed -Right', - message: `Can not Processed user -${CIF} rights.`, - autoClose: false, - }); + if (!canSubmit) return; + try { + const token = localStorage.getItem("admin_access_token"); + const response = await fetch("/api/auth/admin/user/rights", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + CIF, + ib_access_level: ibAccess, + mb_access_level: mbAccess, + }), + }); + + const data = await response.json(); + if (response.ok) { + if (data?.otp) { + // Registration case + await fetch('/api/otp/send', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + mobileNumber: userDetails.mobile, + type: 'REGISTRATION', + userOtp: data.otp, + }), + }); + notifications.show({ + color: 'blue', + title: 'Submitted', + message: `User ${CIF} rights submitted successfully. OTP sent to the user.`, + }); + } + else if (data?.message) { + // Rights update case + await fetch('/api/otp/send', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + mobileNumber: userDetails.mobile, + type: 'RIGHT_UPDATE', + }), + }); + notifications.show({ + color: 'blue', + title: 'Submitted', + message: `User ${CIF} rights updated successfully. Confirmation sent to the user.`, + }); + } } - finally { - setSavingsAccount(null); - setUserDetails(null); - setCIF(''); - setConfirmedPreview(false); + else if (response.status === 401 || data.message === "invalid or expired token") { + localStorage.clear(); + sessionStorage.clear(); + router.push("/administrator/login"); } - }; - } + } + catch (err) { + console.error(err); + notifications.show({ + color: "red", + title: "Failed - Right", + message: `Cannot process user ${CIF} rights.`, + autoClose: false, + }); + } finally { + handleReset(); + } + }; + + const handleReset = () => { + setSavingsAccount(null); + setUserDetails(null); + setCIF(""); + setConfirmedPreview(false); + setIbEnabled(false); + setIbAccess(null); + setMbEnabled(false); + setMbAccess(null); + }; + const handleMainAction = () => { if (!isValidated) { handleValidate(); } else if (!confirmedPreview) { handlePreview(); - } else if (isValidated && confirmedPreview) { + } else { handleSubmit(); } }; return ( - - + <> User Configuration - { - const input = e.currentTarget.value.replace(/\D/g, ''); - if (input.length <= 11) setCIF(input); + + - - {isValidated && ( - <> - - - - Preview User Details - - - - {detailsExpanded && ( - <> - - - - - - - - - - - - )} - - - - - {/* Header row */} - - - Services - - - Enable - - - Transaction - - - Read - - - - {/* Internet Banking row */} - - - Internet Banking - - - { - setIbEnabled(e.currentTarget.checked); - if (!e.currentTarget.checked) setIbAccess(null); - }} - /> - - - setIbAccess("1")} - /> - - - setIbAccess("2")} - /> - - - - {/* Mobile Banking row */} - - - Mobile Banking - - - { - setMbEnabled(e.currentTarget.checked); - if (!e.currentTarget.checked) setMbAccess(null); - }} - /> - - - setMbAccess("1")} - /> - - - setMbAccess("2")} - /> - - - - {accountError && ( - {accountError} - )} - - - )} - - - - setShowPreviewModal(false)} - title="Confirm User Rights" - centered > - - CIF: {CIF} - Savings Account: {savingsAccount} - Mobile Number: {userDetails?.mobile ?? "N/A"} - - - - - - - - - - + + { + const input = e.currentTarget.value.replace(/\D/g, ""); + if (input.length <= 11) setCIF(input); + }} + disabled={isValidated} + withAsterisk + /> + + {isValidated && ( + + + }>User Details + }>Rights + + + + + + + + User ID: + {userDetails?.userId ?? "N/A"} + + + User Name: + {userDetails?.name ?? "N/A"} + + + Mobile Number: + {userDetails?.mobile ?? "N/A"} + + + User Address: + {userDetails?.address ?? "N/A"} + + + Savings Account: + {savingsAccount ?? "N/A"} + + + Active Accounts: + {userDetails?.activeAccount ?? "N/A"} + + + + + + + + + + + Internet Banking + + + { + setIbEnabled(e.currentTarget.checked); + if (!e.currentTarget.checked) setIbAccess(null); + }} + /> + + + setIbAccess("1")} + label="Transaction" + /> + + + setIbAccess("2")} + label="Read" + /> + + + + + + Mobile Banking + + + { + setMbEnabled(e.currentTarget.checked); + if (!e.currentTarget.checked) setMbAccess(null); + }} + /> + + + setMbAccess("1")} + label="Transaction" + /> + + + setMbAccess("2")} + label="Read" + /> + + + + + + )} + + setShowPreviewModal(false)} + title="Confirm User Rights" + centered + > + + + CIF: {CIF} + + + Savings Account: {savingsAccount ?? "N/A"} + + + Mobile Number: {userDetails?.mobile ?? "N/A"} + + + + + + + + + + + + + + + {isValidated && ( + + )} + + ); -} \ No newline at end of file +} diff --git a/src/app/administrator/home/page.tsx b/src/app/administrator/home/page.tsx index 52cde41..46b1bd9 100644 --- a/src/app/administrator/home/page.tsx +++ b/src/app/administrator/home/page.tsx @@ -145,19 +145,19 @@ export default function Login() { handleClick('UserConf')}> - User Configuration + User Configuration handleClick('ViewUser')}> - View User Configuration + View User Configuration - Logout + Logout {/* Main Content Area */} - + Welcome ,{name} @@ -166,14 +166,15 @@ export default function Login() { {view === 'userConf' && } - {view === 'view' && } + {view === 'view' && } {!view && Welcome To The Admin Portal } - © 2025 The Kangra Central Co-Operative Bank + + © 2025 The Kangra Central Co-Operative Bank );