feat: implemented routes for getting and setting customer tnc flag

This commit is contained in:
2025-11-08 18:39:39 +05:30
parent 3f86697f6b
commit c39492edde
3 changed files with 62 additions and 8 deletions

View File

@@ -359,6 +359,28 @@ async function setUserName(req, res) {
}
}
async function getTncAcceptanceFlag(req, res) {
try {
const flag = await authService.getTncFlag(req.user, req.client);
res.json({ tnc_accepted: flag });
} catch (error) {
logger.error(error, 'error occured while getting tnc flag');
res.status(500).json({ error: 'INTERNAL SERVER ERROR' });
}
}
async function setTncAcceptanceFlag(req, res) {
try {
const { flag } = req.body;
if (typeof flag !== 'boolean')
res.staus(400).json({ error: 'invalid value for flag' });
await authService.setTncFlag(req.user, req.client, flag);
} catch (error) {
logger.error(error, 'error occured while updating tnc flag');
res.status(500).json({ error: 'INTERNAL SERVER ERROR' });
}
}
module.exports = {
login,
tpin,
@@ -371,4 +393,6 @@ module.exports = {
changeTransPassword,
isUserNameExits,
setUserName,
getTncAcceptanceFlag,
setTncAcceptanceFlag,
};

View File

@@ -28,4 +28,7 @@ router.post(
router.get('/user_name', authenticate, authController.isUserNameExits);
router.post('/user_name', authenticate, authController.setUserName);
router.get('/tnc', authenticate, authController.getTncAcceptanceFlag);
router.post('/tnc', authenticate, authController.setTncAcceptanceFlag);
module.exports = router;

View File

@@ -127,16 +127,16 @@ async function changeTransPassword(customerNo, trans_psw) {
async function CheckUserName(customerNo) {
try {
const result = await db.query('SELECT preferred_name from users WHERE customer_no = $1',
const result = await db.query(
'SELECT preferred_name from users WHERE customer_no = $1',
[customerNo]
);
if (result.rows.length > 0) {
return result.rows[0].preferred_name;;
return result.rows[0].preferred_name;
} else {
return null;
}
}
catch (error) {
} catch (error) {
throw new Error(
`error occurred while fetch the preferred name ${error.message}`
);
@@ -150,12 +150,12 @@ async function setUserName(customerNo, username) {
'UPDATE users SET preferred_name = $1 ,updated_at = $2 WHERE customer_no = $3',
[username, currentTime, customerNo]
);
logger.info("user table updated");
logger.info('user table updated');
await db.query(
"INSERT INTO preferred_name_history (customer_no, preferred_name) VALUES ($1, $2)",
'INSERT INTO preferred_name_history (customer_no, preferred_name) VALUES ($1, $2)',
[customerNo, username]
);
logger.info("preferred_name_history table updated");
logger.info('preferred_name_history table updated');
} catch (error) {
if (error.code === '23505') {
throw new Error('PREFERRED_NAME_ALREADY_EXISTS');
@@ -166,6 +166,32 @@ async function setUserName(customerNo, username) {
}
}
async function getTncFlag(customerNo, clientType) {
let query = '';
if (clientType === 'MB') {
query = 'SELECT tnc_mobile AS tnc_flag FROM users WHERE customer_no = $1';
} else if (clientType === 'IB') {
query = 'SELECT tnc_inb AS tnc_flag FROM users WHERE customer_no = $1';
} else {
throw new Error('UNKNOWN_CLIENT_TYPE. ONLY IB AND MB ALLOWED');
}
const result = await db.query(query, [customerNo]);
return result.rows[0]['tnc_flag'];
}
async function setTncFlag(customerNo, clientType, flag) {
let query = '';
if (clientType === 'MB') {
query = 'UPDATE users SET tnc_mobile = $1 WHERE customer_no = $2';
} else if (clientType === 'IB') {
query = 'UPDATE users SET tnc_inb = $1 WHERE customer_no = $2';
} else {
throw new Error('UNKNOWN_CLIENT_TYPE. ONLY IB AND MB ALLOWED');
}
await db.query(query, [flag, customerNo]);
}
module.exports = {
validateUser,
findUserByCustomerNo,
@@ -180,5 +206,6 @@ module.exports = {
isMigratedUser,
CheckUserName,
setUserName,
getTncFlag,
setTncFlag,
};