'use strict';
/**
 *  trigger-mapping controller
 */

const { createCoreController } = require('@strapi/strapi').factories;
const axios = require('axios');
module.exports = createCoreController('api::trigger-mapping.trigger-mapping', ({ strapi }) => ({
    async getTagDetails(ctx) {
        try {
            let { taguid } = ctx.params;
            console.log(`Searching For RFID : ${taguid}`);
            const myResult = await strapi.db.query('api::trigger-mapping.trigger-mapping').findOne({
                where: { rfid: taguid }
            });

            if (!myResult) {
                console.log("Record Could Not Be Found");
                ctx.body = { message: "Record Could Not Be Found" };
                ctx.status = 200;
            }
            if (myResult) {                
                console.log(JSON.stringify(myResult));
                await axios.get(`${process.env.NTB_CONTROLLER_SERVICE}/api/v1/goto/${myResult.triggervalue}`)                
                ctx.body = myResult;
                ctx.status = 200;
            }
        } catch (err) {
            ctx.status = 404;
            ctx.body = err.message;
        }
    }
}));
