const db = require('../config/db'); const { comparePassword, hashPassword } = require('../util/hash'); const axios = require('axios'); async function findAdminByUserName(customerNo) { const result = await db.query('SELECT * FROM admin WHERE username = $1', [ customerNo, ]); return result.rows[0]; } async function validateAdmin(customerNo, password) { const user = await findAdminByUserName(customerNo); if (!user) return null; const isMatch = await comparePassword(password, user.password); return isMatch ? user : null; } async function getCustomerDetails(customerNo) { try { const response = await axios.get( 'http://localhost:8686/kccb/cbs/custInfo/details', { params: { stcustno: customerNo } } ); const details = response.data; const processedDetails = details.map((acc) => ({ ...acc, activeAccounts: details.length, cifNumber: customerNo, })); return processedDetails; } catch (error) { logger.error('while fetching customer details', error); throw new Error( 'API call failed: ' + (error.response?.data?.message || error.message) ); } } async function getCustomerDetailsFromDB(customerNo) { const result = await db.query( 'SELECT customer_no,created_at,last_login,is_first_login,ib_access_level,mb_access_level,inb_limit_amount,mobile_limit_amount,locked FROM users WHERE customer_no = $1 or preferred_name= $1', [ customerNo, ]); return result.rows[0]; } async function updateUserLockStatus(customerNo ,locked ,adminUserName) { const result = await db.query( 'Update users set locked =$2 ,unlocked_by=$3 WHERE customer_no = $1 or preferred_name= $1', [ customerNo, locked ,adminUserName ]); return result.rows[0]; } module.exports = { validateAdmin, findAdminByUserName, getCustomerDetails, getCustomerDetailsFromDB ,updateUserLockStatus };