2026-06-13 13:05:28 +05:30

711 lines
18 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require("express");
const router = express.Router();
const pool = require("../config/db");
const upload = require("../middlewares/upload");
const { v4: uuidv4 } = require("uuid");
const generateSlug = (name) => {
return name
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-]/g, "");
};
router.post("/add-partner", async (req, res) => {
try {
const {
name,
open_time,
close_time,
address,
city,
state,
pincode,
screens,
// logo_url, // Assuming logo_url is a URL string, adjust if needed.
} = req.body;
const slug = generateSlug(name);
const uniqueurl = slug;
var id = uuidv4();
const [result] = await pool.query(
"INSERT INTO tbl_partners (transid,name, logo_url, open_time, close_time, address, city, state, pincode, screens) VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?)",
[
id,
name,
uniqueurl,
open_time,
close_time,
address,
city,
state,
pincode,
screens,
]
);
//hsh
var screensnumber = Number(screens);
for (let i = 0; i < screensnumber; i++) {
const [result1] = await pool.query(
"INSERT INTO tbl_screens (transid,partnerid,name) VALUES (UUID(),?, ?)",
[id, "Screen " + Number(i + 1)]
);
}
res.json({
message: "Partner Added Successfully! ",
clientid: id,
uniqueurl,
id,
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/add-screen", async (req, res) => {
try {
const {
partnerid,
// logo_url, // Assuming logo_url is a URL string, adjust if needed.
} = req.body;
console.log(partnerid);
const [countRows] = await pool.query(
`SELECT COUNT(*) AS cnt
FROM tbl_screens
WHERE partnerid = ?`,
[partnerid]
);
const existing = countRows[0].cnt;
console.log(existing);
// 2⃣ prepare the new screen name
const newScreenName = `Screen ${existing + 1}`;
// 3⃣ insert the new screen
const [result] = await pool.query(
`INSERT INTO tbl_screens
(transid, partnerid, name)
VALUES
(UUID(), ?, ?)`,
[partnerid, newScreenName]
);
res.json({
message: "Screen Added Successfully! ",
});
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
router.post("/delete-screen", async (req, res) => {
try {
const {
screenid,
} = req.body;
const [result] = await pool.query(
`delete from tbl_screens where transid = ?
`,
[screenid]
);
res.json({
message: "Screen Deleted Successfully! ",
});
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
router.post("/upload", upload.single("file"), async (req, res) => {
try {
const { filename, path } = req.file;
const clientid = req.body.clientid;
await pool.query(
"insert into tbl_files (transid,client_id,file_name,file_path) values (UUID(),?,?,?)",
[clientid, filename, path]
);
res.json({ message: "File Uploaded Successfully!", filepath: path });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/upload-partner-logo", upload.single("file"), async (req, res) => {
try {
const { filename, path } = req.file;
const partnerid = req.body.partnerid;
await pool.query("update tbl_partners set logo_url = ? where transid = ?", [
path,
partnerid,
]);
res.json({
message: "Partner Logo Uploaded Successfully!",
filepath: path,
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.get("/partners", async (req, res) => {
try {
const [partners] = await pool.query("select * from tbl_partners");
res.json(partners);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/update-partner", async (req, res) => {
try {
const {
name,
open_time,
logo_url,
close_time,
address,
city,
state,
pincode,
screens,
transid,
yt,
scrolltext,
} = req.body;
const [result] = await pool.query(
"update tbl_partners set name =?, logo_url=?, open_time=?, close_time=?, address=?, city=?, state=?, pincode=?, screens=?,yt=?,scrolltext=? where transid = ?",
[
name,
logo_url,
open_time,
close_time,
address,
city,
state,
pincode,
screens,
yt,
scrolltext,
transid,
]
);
res.json({
message: "Partner Updated Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/update-screen-youtube", async (req, res) => {
try {
const { screenid, sts } = req.body;
const [result] = await pool.query(
"update tbl_screens set isyoutube=? where transid = ?",
[sts, screenid]
);
res.json({
message: "Partner Updated Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/update-screen-type", async (req, res) => {
try {
const { screenid, sts } = req.body;
const [result] = await pool.query(
"update tbl_screens set screentype=? where transid = ?",
[sts, screenid]
);
res.json({
message: "Screen Type Updated Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.get("/clients", async (req, res) => {
try {
const [partners] = await pool.query("select * from tbl_clients");
res.json(partners);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/add-client", async (req, res) => {
try {
const { name, emailid, phoneno, address } = req.body;
const [result] = await pool.query(
"INSERT INTO tbl_clients (transid,name,emailid,phoneno,address) VALUES (UUID(),?, ?,?,?)",
[name, emailid, phoneno, address]
);
res.json({
message: "Client Added Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/update-client", async (req, res) => {
try {
const { transid, name, emailid, phoneno, address } = req.body;
const [result] = await pool.query(
"update tbl_clients set name=?,emailid=?,phoneno=?,address=? where transid = ?",
[name, emailid, phoneno, address, transid]
);
res.json({
message: "Client Updated Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.delete("/delete-client/:clientid", async (req, res) => {
try {
const { clientid } = req.params;
const [response] = await pool.query(
"delete from tbl_clients where transid =? ",
[clientid]
);
const [response1] = await pool.query(
"delete from tbl_files where client_id =? ",
[clientid]
);
const [response2] = await pool.query(
"delete from tbl_client_partner_mapping where clientid =? ",
[clientid]
);
res.json(response);
} catch (err) {
res.status(500), json({ error: err.message });
}
});
router.get("/get-partner/:partnreid", async (req, res) => {
try {
const { partnreid } = req.params;
const [partners] = await pool.query(
"select * from tbl_partners where transid=?",
[partnreid]
);
res.json(partners);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.delete("/delete-partner/:partnerid", async (req, res) => {
try {
const { partnerid } = req.params;
const [response] = await pool.query(
"delete from tbl_partners where transid =? ",
[partnerid]
);
const [response1] = await pool.query(
"delete from tbl_screens where partnerid =? ",
[partnerid]
);
const [response2] = await pool.query(
"delete from tbl_client_partner_mapping where partnerid =? ",
[partnerid]
);
res.json(response);
} catch (err) {
res.status(500), json({ error: err.message });
}
});
router.delete(
"/delete-client-partner-mapping/:partnerid/:clientid",
async (req, res) => {
try {
const { partnerid, clientid } = req.params;
const [response] = await pool.query(
"delete from tbl_client_partner_mapping where partnerid =? and clientid =?",
[partnerid, clientid]
);
res.json(response);
} catch (err) {
res.status(500), json({ error: err.message });
}
}
);
router.get("/get-client/:clientid", async (req, res) => {
try {
const { clientid } = req.params;
const [partners] = await pool.query(
"select * from tbl_clients where transid=?",
[clientid]
);
res.json(partners);
} catch (err) {
res.status(500).json({ error: err.massage });
}
});
router.get("/get-screen/:partnerid", async (req, res) => {
try {
const partnerid = req.params.partnerid;
const query = "SELECT * FROM tbl_screens WHERE partnerid = ?";
const [screens] = await pool.query(query, [partnerid]);
res.json(screens);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
//Client - Partner Mapping
router.get("/client-partner-mappings", async (req, res) => {
try {
const { clientid, partnerid } = req.query; // Get clientid and partnerid from query parameters
if (!clientid || !partnerid) {
return res
.status(400)
.json({ error: "clientid and partnerid are required." });
}
const [mappings] = await pool.query(
"SELECT * FROM tbl_client_partner_mapping WHERE clientid = ? AND partnerid = ?",
[clientid, partnerid]
);
res.json(mappings);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// router.post("/add-client-partner-mapping", async (req, res) => {
// try {
// const mappings = req.body; // Assuming req.body is an array of mapping objects
// console.log(mappings);
// if (!Array.isArray(mappings) || mappings.length === 0) {
// return res
// .status(400)
// .json({ error: "Invalid or empty mapping data provided." });
// }
// const clientID = mappings[0].clientid;
// const partnerID = mappings[0].partnerid;
// await pool.query(
// "delete from tbl_client_partner_mapping where clientid =? and partnerid = ?",
// [clientID, partnerID]
// );
// for (const mapping of mappings) {
// const {
// clientid,
// partnerid,
// screenid,
// fileid,
// file_path,
// ismonday,
// istuesday,
// iswednesday,
// isthursday,
// isfriday,
// issaturday,
// issunday,
// imageduration,
// } = mapping;
// // Basic validation (you might want to add more robust validation)
// if (!clientid || !partnerid || !screenid || !fileid || !file_path) {
// return res
// .status(400)
// .json({ error: "Missing required fields in mapping." });
// }
// await pool.query(
// "INSERT INTO tbl_client_partner_mapping (clientid, partnerid, screenid, fileid, file_path, ismonday, istuesday, iswednesday, isthursday, isfriday, issaturday, issunday,imageduration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)",
// [
// clientid,
// partnerid,
// screenid,
// fileid,
// file_path,
// ismonday || 0, // Default to 0 if not provided
// istuesday || 0,
// iswednesday || 0,
// isthursday || 0,
// isfriday || 0,
// issaturday || 0,
// issunday || 0,
// imageduration || 10,
// ]
// );
// }
// res.json({ message: "Client-Partner mappings added successfully!" });
// } catch (err) {
// console.log(err);
// res.status(500).json({ error: err });
// }
// });
router.post("/add-client-partner-mapping", async (req, res) => {
const mappings = req.body;
if (!Array.isArray(mappings) || mappings.length === 0) {
return res
.status(400)
.json({ error: "Invalid or empty mapping data provided." });
}
const clientID = mappings[0].clientid;
const partnerID = mappings[0].partnerid;
const conn = await pool.getConnection();
try {
await conn.beginTransaction();
// 1) fetch old values by fileid
const [oldRows] = await conn.query(
`SELECT fileid, imageduration, isinhousead, iscarousel, ismainad
FROM tbl_client_partner_mapping
WHERE clientid = ? AND partnerid = ?`,
[clientID, partnerID]
);
const oldMap = oldRows.reduce((m, row) => {
m[row.fileid] = row;
return m;
}, {});
// 2) delete existing mappings
await conn.query(
"DELETE FROM tbl_client_partner_mapping WHERE clientid = ? AND partnerid = ?",
[clientID, partnerID]
);
// 3) re-insert, pulling in old flags if the incoming mapping didnt supply them
for (const mapping of mappings) {
const {
clientid,
partnerid,
screenid,
fileid,
file_path,
ismonday = 0,
istuesday = 0,
iswednesday = 0,
isthursday = 0,
isfriday = 0,
issaturday = 0,
issunday = 0,
imageduration,
isinhousead,
iscarousel,
ismainad,
} = mapping;
// fall back to old values (or defaults) if not supplied
const old = oldMap[fileid] || {};
const duration = imageduration != null
? imageduration
: old.imageduration ?? 10;
const inhouse = isinhousead != null
? isinhousead
: old.isinhousead ?? 0;
const carousel = iscarousel != null
? iscarousel
: old.iscarousel ?? 0;
const mainAd = ismainad != null
? ismainad
: old.ismainad ?? 0;
// basic field check
if (!clientid || !partnerid || !screenid || !fileid || !file_path) {
await conn.rollback();
return res
.status(400)
.json({ error: "Missing required fields in mapping." });
}
await conn.query(
`INSERT INTO tbl_client_partner_mapping
(clientid, partnerid, screenid, fileid, file_path,
ismonday, istuesday, iswednesday, isthursday,
isfriday, issaturday, issunday,
imageduration, isinhousead, iscarousel, ismainad)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
clientid,
partnerid,
screenid,
fileid,
file_path,
ismonday,
istuesday,
iswednesday,
isthursday,
isfriday,
issaturday,
issunday,
duration,
inhouse,
carousel,
mainAd,
]
);
}
await conn.commit();
res.json({ message: "Client-Partner mappings updated successfully!" });
} catch (err) {
await conn.rollback();
console.error(err);
res.status(500).json({ error: err.message || err });
} finally {
conn.release();
}
});
router.post("/reorder-partner-ads", async (req, res) => {
try {
const mappings = req.body; // Assuming req.body is an array of mapping objects
if (!Array.isArray(mappings) || mappings.length === 0) {
return res
.status(400)
.json({ error: "Invalid or empty mapping data provided." });
}
const partnerID = mappings[0].partnerid;
await pool.query(
"delete from tbl_client_partner_mapping where partnerid = ?",
[partnerID]
);
for (const mapping of mappings) {
const {
clientid,
partnerid,
screenid,
fileid,
file_path,
ismonday,
istuesday,
iswednesday,
isthursday,
isfriday,
issaturday,
issunday,
imageduration,
isinhousead,
iscarousel,
ismainad,
} = mapping;
// console.log(mapping);
// Basic validation (you might want to add more robust validation)
if (!clientid || !partnerid || !screenid || !fileid || !file_path) {
return res
.status(400)
.json({ error: "Missing required fields in mapping." });
}
await pool.query(
"INSERT INTO tbl_client_partner_mapping (clientid, partnerid, screenid, fileid, file_path, ismonday, istuesday, iswednesday, isthursday, isfriday, issaturday, issunday,imageduration,isinhousead,iscarousel,ismainad) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)",
[
clientid,
partnerid,
screenid,
fileid,
file_path,
ismonday || 0, // Default to 0 if not provided
istuesday || 0,
iswednesday || 0,
isthursday || 0,
isfriday || 0,
issaturday || 0,
issunday || 0,
imageduration || 10,
isinhousead || 0,
iscarousel || 0,
ismainad || 0,
]
);
}
res.json({ message: "Partner Ads Re-ordered successfully!" });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
router.get("/partner-ads", async (req, res) => {
try {
const { partnerid } = req.query; // Get clientid and partnerid from query parameters
if (!partnerid) {
return res.status(400).json({ error: "Partnerid is required." });
}
const [mappings] = await pool.query(
" SELECT *, (select name from tbl_screens where transid = screenid) as 'screenname',(select name from tbl_clients where transid = clientid) as 'clientname' FROM tbl_client_partner_mapping WHERE partnerid = ?",
[partnerid]
);
res.json(mappings);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post("/update-settings", async (req, res) => {
try {
const { transid, value } = req.body;
const [result] = await pool.query(
"update tbl_settings set value =? where transid = ?",
[value, transid]
);
res.json({
message: "Settings Updated Successfully! ",
});
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = router;