diff --git a/controllers/ListenController.js b/controllers/ListenController.js index 95a9972..ae31f0c 100644 --- a/controllers/ListenController.js +++ b/controllers/ListenController.js @@ -556,7 +556,11 @@ class ListenController { /*{ "imei": "866888046130754", "picture_at": 1678888888, - "file": "http://example.com/image.jpg", + "file": { + "filename": "CMD_862798052124449_095111fe_2025_11_20_12_52_18_F_09.jpg", + "url": "http://example.com/image.jpg" + }, + "cam": "F" }*/ let apiRes = {} try { @@ -564,23 +568,36 @@ class ListenController { const now = moment().unix() const vhc_id = (await VhcModels.getVhcByDeviceId(data.imei.padStart(16, "0")))?.[0]?.vid - console.log("vhc_id", vhc_id) - if (!vhc_id) { - apiRes = JSON.parse(JSON.stringify(response[400])) + apiRes = JSON.parse(JSON.stringify(response[422])) apiRes.meta.message = "Device ID not registered to any vehicle" - return res.status(400).json(apiRes) + return res.status(422).json(apiRes) } + // const cekQ = `select * from t_camera where vhc_id = ? and crt_d = ? and cam = ? limit 1` + // const cekD = [vhc_id, data.picture_at, data.cam || 0] + // const cekR = await db.query(cekQ, cekD) + // if (cekR.length > 0) { + // apiRes = JSON.parse(JSON.stringify(response[422])) + // apiRes.meta.message = "Duplicate camera entry" + // return res.status(422).json(apiRes) + // } + const insQ = ` insert into t_camera set vhc_id = ?, crt_d = ?, + cam = ?, + filename = ?, image = ?, crt_s = ? + as new + on duplicate key update + image = new.image, + filename = new.filename ` - const insD = [vhc_id, data.picture_at, data.file.url, now] + const insD = [vhc_id, data.picture_at, data.cam, data.file.filename, data.file.url, now] const insR = await db.query(insQ, insD) apiRes = JSON.parse(JSON.stringify(response[200])) @@ -593,6 +610,36 @@ class ListenController { return res.status(500).json(apiRes) } } + + async tracksolidCheckImg(req, res) { + /*{ + filename: "CMD_862798052124449_095111fe_2025_11_20_12_52_18_F_09.jpg" + }*/ + let apiRes = {} + try { + const data = req.body + if (!data.filename) { + apiRes = JSON.parse(JSON.stringify(response[422])) + // apiRes.meta.message = "filename is required" + return res.status(422).json(apiRes) + } + + const cekQ = `select * from t_camera where filename = ? limit 1` + const cekD = [data.filename] + const cekR = await db.query(cekQ, cekD) + const isExists = cekR.length > 0 + + apiRes = JSON.parse(JSON.stringify(response[200])) + apiRes.meta.message = "success" + apiRes.data = { isExists } + return res.status(200).json(apiRes) + } catch (err) { + console.error("❌ error:", err.message) + apiRes = JSON.parse(JSON.stringify(response[500])) + apiRes.meta.message += Helper.setErrMsg(": " + err.message) + return res.status(500).json(apiRes) + } + } } const object = new ListenController() diff --git a/routes/listen.js b/routes/listen.js index 297c077..e4432c1 100644 --- a/routes/listen.js +++ b/routes/listen.js @@ -6,4 +6,5 @@ exports.use = function (app) { app.post(PATH_URL + "/listen/gps", ListenController.gps) app.post(PATH_URL + "/listen/teltonika", ListenController.teltonika) app.post(PATH_URL + "/listen/tracksolid/camera", ListenController.tracksolidCamera) + app.get(PATH_URL + "/listen/tracksolid/check-img", ListenController.tracksolidCheckImg) }