Files
adminapi/apxtri/routes/trackings.js
2025-12-09 21:32:22 +01:00

160 lines
6.4 KiB
JavaScript

const express = require("express");
const glob = require("glob");
const fs = require("fs-extra");
const path = require("path");
const conf = require(`../../../adminapi/objects/tribes/itm/adminapi.json`);
const currentmod = "Trackings";
const log = process.env.ACTIVELOG ? process.env.ACTIVELOG.split(',').includes(currentmod) : false;
const Odmdb = require("../models/Odmdb.js");
// Middlewares
const checkHeaders = require("../middlewares/checkHeaders");
const isAuthenticated = require("../middlewares/isAuthenticated");
const router = express.Router();
/**
* @api {post} adminapi/trackings/newdevice - Post new device
* @apiName adddevice
* @apiGroup Trackings
* @apiDescription
* Create a device from a xuuid wco tracker.js with information about the device. It is suppose to be a new device but if xuuid already exist then it will be update with new information
* @apiBody {object} device see schema/adminapi/schema/device.json
*
* @apiError {json} objectNotfound the file does not exist
* @apiErrorExample {json}
* HTTP/1.1 406 Not Acceptable
{"status":406,"ref":"Trackings","multimsg":{},"data":{body}}
*
* @apiSuccessExample {json} Success-create:
* HTTP/1.1 200 OK
* {"status":200, "ref":"Trackings", "msg":"successfullcreate", "data":{}}
* @apiSuccessExample {json} Success-update:
* HTTP/1.1 200 OK
* {"status":200, "ref":"Trackings", "msg":"successfullupdate", "data":{}}
*/
router.post("/newdevice", checkHeaders, async (req, res) => {
if (log) console.log(currentmod, "post new device with", req.body);
const newdevice = { status: 200, ref: "Trackings", msg: "success", data: {} };
const role = {
xalias: req.session.header.xalias,
xprofils: req.session.header.xprofils,
};
console.log(req.session.header)
if (!fs.pathExistsSync(`../${req.session.header.xtribe}/objects/devices`)) {
fs.mkdirSync(`../${req.session.header.xtribe}/objects/devices/itm/`, {
recursive: true,
});
fs.mkdirSync(`../${req.session.header.xtribe}/objects/devices/idx/`, {
recursive: true,
});
const confdevice = {
name: "devices",
schema: "adminapi/schema/devices.json",
lastupdate: 0,
lastupdatedata: "2025-02-23T09:23:56.266Z",
};
fs.outputJSONSync(
`../${req.session.header.xtribe}/objects/devices/conf.json`,
confdevice
);
}
const devicepath = `../${req.session.header.xtribe}/objects/devices/itm/${req.session.header.xuuid}.json`;
const device = req.body;
device.xuuid = req.session.header.xuuid;
if (req.session.header.xalias != "anonymous") {
device.alias = req.session.header.xalias;
}
if (await fs.pathExists(devicepath)) {
const updatedevice = Odmdb.cud(
`../${req.session.header.xtribe}/objects/devices`,
"U",
device,
role
);
if (updatedevice.status == 200) {
newdevice.msg = "successfullupdate";
} else {
newdevice.status = updatedevice.status;
newdevice.msg = updatedevice.msg;
newdevice.data = updatedevice.data;
if (updatedevice.multimsg) newdevice.multimsg = updatedevice.multimsg;
}
} else {
const createdevice = Odmdb.cud(
`../${req.session.header.xtribe}/objects/devices`,
"C",
device,
role
);
console.log(createdevice);
if (createdevice.status == 200) {
newdevice.msg = "successfullcreate";
} else {
newdevice.status = createdevice.status;
newdevice.msg = createdevice.msg;
newdevice.data = createdevice.data;
if (createdevice.multimsg) newdevice.multimsg = createdevice.multimsg;
}
}
res.status(newdevice.status).json(newdevice);
});
/**
* @api {get} https://dns.xx/trk/pathtofile? - tracking system
* @apiGroup Trackings
* @apiName trackingsystem
* @apiDescription
*
* **WARNING** a cors issue must be fix, currently this tracking work for the same domain.
*
* without header:<br>
* <code>https://dns.xx/trk/pathtofile?alias=anonymous&uuid=1b506f71-1bff-416c-9057-cb8b86296f60&srckey=btnregister&version=1&lg=fr </code>
*
* with header<br>
* <code>https://dns.xx/trk/pathtofile?srckey=btnregister&version=1</code>
*
* where pathtofile is a ressource accessible from https://dns.xx/pathtofile
* For dummy pathtofile for apxtri project, you have:<br>
* /cdn/trkret/1x1.png (a 1pixel image 95 bytes )
* /cdn/trkret/empty.json (an empty jason 2 bytes)
*
* html usage to track a loading page or email when a picture is load
* using apxwebapp in /src/ we got:
* <code> < img src="static/img/photo.jpg" data-trksrckey="loadpage" data-version="1" > </code>
*
* in js action:
*
* <code> <a data-trksrckey="linktoblabla" href='https:..' onclick="apx.tracker.hit({srckey:"key name", anyusefull key:value});actionfct();"></a></code>
*
* To hit an eventlistener<br>
* <code> axios.get("https://dns.xx/trk/cdn/trkret/empty.json?alias=anonymous&uuid=1b506f71-1bff-416c-9057-cb8b86296f60&srckey=btnregister&version=1");</code>
*
* If no js available (example:email or pdf document)<br>
* <code> < img src="https://dns.xx/trk/static/img/photo.jpg?alias=anonymous&uuid=1b506f71-1bff-416c-9057-cb8b86296f60&srckey=loadpage&version=1"</code>
*
* <code><a href="https://dns.xx/trk/redirect?alias=anonymous&uuid=1b506f71-1bff-416c-9057-cb8b86296f60&srckey=loadpage&version=1&url=http://..." </code>
*
* This will hit a tracker then redirect to url></a> *
*
* **if you use apx.js** : in html add in < button >, < img >, < a > tag data-trksrc="srckey"
* <code>
* < img src="https://dns.xx/static/img/photo.jpg" data-trkversion="1" data-trksrckey="registerform">
* < button data-trksrc="https://dns.xx/static/img/photo.jpg" data-trkversion="1" data-trksrckey="registerform">
* </code>
* A lazyloader can also be track.
* <code>< img lazysrc="trk/static/img/photo.jpg data-trksrckey="loadpage" data-version="1" ></code>
*
* Tracking log are store into tribe/logs/nginx/tribe_appname.trk.log<br>
* Src have to be manage in tribe/api/models/lg/src_en.json<br>
* <code>{"srckey":{"app":"presentation|app|apptest","title":"","description":""}}</code>
*
* @apiParam {String} alias=anonymous if authenticated we get from headers
* @apiParam {String} uuid a uuid v4 generate the first time a web page is open on a browser
* @apiParam {String} srckey source action that trig this get
* @apiParam {integer} version=1 can be an int, date or any version of the src
* @apiParam {integer} [tm] a timestamp of action when it is not immediate (for offline app)
*
*/
module.exports = router;