Dine360-Ads-Backend/routes/clientRoutes.js
2026-06-13 13:05:28 +05:30

276 lines
7.6 KiB
JavaScript

const express = require("express");
const router = express.Router();
const pool = require("../config/db");
router.get("/files/:clientId", async (req, res) => {
try {
const { clientId } = req.params;
const [files] = await pool.query(
"select * from tbl_files where client_id =? ",
[clientId]
);
res.json(files);
} catch (err) {
res.status(500), json({ error: err.message });
}
});
router.get("/ads/:partnerId/:screenName", async (req, res) => {
try {
const { partnerId, screenName } = req.params;
console.log(req.params);
// Find the screenId based on partnerId and screenName
const [screenResults] = await pool.query(
"SELECT transid FROM tbl_screens WHERE REPLACE(name, ' ', '') = ? AND SUBSTRING(partnerid, 1, 5) = SUBSTRING(?, 1, 5)",
[screenName, partnerId]
);
if (screenResults.length === 0) {
return res.status(404).json({ error: "Screen not found." });
}
const screenId = screenResults[0].transid;
const [result] = await pool.query(
"update tbl_screens set isactive=1 where transid = ?",
[screenId]
);
// Find the files associated with the screenId
const [files] = await pool.query(
"SELECT file_path,screenid,partnerid,transid,imageduration,iscarousel,isinhousead,ismainad,(select screentype from tbl_screens where transid = screenid)as screentype,(select isyoutube from tbl_screens where transid = screenid)as yt FROM tbl_client_partner_mapping WHERE screenid = ?",
[screenId]
);
res.json(files);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// router.get("/ads/:partnerId/:screenName", async (req, res) => {
// try {
// const { partnerId, screenName } = req.params;
// console.log(req.params);
// // Find the screenId based on partnerId and screenName
// const [screenResults] = await pool.query(
// "SELECT file_path,screenid,partnerid,transid,imageduration,isinhousead,iscarousel,ismainad FROM tbl_client_partner_mapping WHERE screenid = ?",
// [screenId]
// );
// if (screenResults.length === 0) {
// return res.status(404).json({ error: "Screen not found." });
// }
// const screenId = screenResults[0].transid;
// const [result] = await pool.query(
// "update tbl_screens set isactive=1 where transid = ?",
// [screenId]
// );
// // Find the files associated with the screenId
// const [files] = await pool.query(
// "SELECT * FROM tbl_client_partner_mapping WHERE screenid = ?",
// [screenId]
// );
// res.json(files);
// } catch (err) {
// res.status(500).json({ error: err.message });
// }
// });
router.post("/add-screen-log", async (req, res) => {
try {
const { screen_id, screen_name, partner_id } = req.body;
// Insert data into the `screen_logs` table
const [result1] = await pool.query(
"update tbl_screens set isactive=1 where transid = ?",
[screen_id]
);
// const [result] = await pool.query(
// "INSERT INTO tbl_screen_logs (screen_id, screen_name, partner_id) VALUES (?, ?, ?)",
// [screen_id, screen_name, partner_id]
// );
res.json({
message: "Screen log added successfully!",
});
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
router.post("/idle-screen-log", async (req, res) => {
try {
const { screen_id, screen_name, partner_id } = req.body;
// Insert data into the `screen_logs` table
const [result1] = await pool.query(
"update tbl_screens set isactive=2 where transid = ?",
[screen_id]
);
res.json({
message: "IDLE log added successfully!",
});
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
router.get("/get-settings", async (req, res) => {
try {
const [result] = await pool.query("select * from tbl_settings ", []);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/validate-code", async (req, res) => {
try {
const { code } = req.body;
if (!code || code.length !== 4) {
return res
.status(400)
.json({ error: "Invalid code format. It should be 4 digits." });
} // Split the code into two parts
const firstTwo = code.substring(0, 2);
const lastTwo = code.substring(3, 4); // Query to check if there is a matching record
const [rows] = await pool.query(
"SELECT partnerid, name FROM tbl_screens WHERE LEFT(partnerid, 2) = ? AND right(name, 1) = ?",
[firstTwo, lastTwo]
);
if (rows.length === 0) {
return res.status(404).json({ error: "No matching record found" });
}
const { partnerid, name } = rows[0]; // Generate the response value: first 5 letters of transid/name (without spaces)
const responseValue = `${partnerid.substring(0, 5)}/${name.replace(
/\s+/g,
""
)}`;
res.json({ success: true, value: responseValue });
// res.json([
// {
// success: true,
// value: {
// screenPath: responseValue,
// raw: responseValue
// }
// }
// ]);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
router.post("/validate-code-new", async (req, res) => {
try {
const { code } = req.body;
if (!code || code.length !== 4) {
return res
.status(400)
.json({ error: "Invalid code format. It should be 4 digits." });
} // Split the code into two parts
const firstTwo = code.substring(0, 2);
const lastTwo = code.substring(3, 4); // Query to check if there is a matching record
const [rows] = await pool.query(
"SELECT partnerid, name,transid FROM tbl_screens WHERE LEFT(partnerid, 2) = ? AND right(name, 1) = ?",
[firstTwo, lastTwo]
);
if (rows.length === 0) {
return res.status(404).json({ error: "No matching record found" });
}
const { partnerid, name, transid } = rows[0]; // Generate the response value: first 5 letters of transid/name (without spaces)
const responseValue = `${partnerid.substring(0, 5)}/${name.replace(
/\s+/g,
""
)}`;
//res.json({ success: true, value: responseValue });
res.json([
{
success: true,
value: {
screenPath: responseValue,
raw: responseValue,
id: transid,
},
},
]);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
router.post("/get-yt", async (req, res) => {
try {
const { code } = req.body;
if (!code || code.length !== 4) {
return res
.status(400)
.json({ error: "Invalid code format. It should be 4 digits." });
} // Split the code into two parts
const firstTwo = code.substring(0, 2);
const lastTwo = code.substring(3, 4); // Query to check if there is a matching record
const [rows] = await pool.query(
"SELECT * FROM tbl_partners WHERE LEFT(transid, 2) = ? ",
[firstTwo]
);
res.json(rows);
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
});
router.post("/add-screen-status", async (req, res) => {
try {
const { id, sts } = req.body;
// Insert data into the `screen_logs` table
const [result1] = await pool.query(
"update tbl_screens set status=?,stsupdat = CURRENT_TIMESTAMP where transid = ?",
[sts, id]
);
res.json({
message: "Screen Status Updated successfully!",
});
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
module.exports = router;