const db = require('../../schemas'); // const constants = require('../../config/constants/constants.json'); const Op = db.Sequelize.Op; const Sequelize = db.Sequelize; // import { CommonHelper } from '../helpers/common'; const path = require("path"), rootPath = process.env.LAMBDA_TASK_ROOT ? process.env.LAMBDA_TASK_ROOT : path.dirname(require.main.filename), load = require(`${rootPath}/config/config.json`), constantsRootPath: any = load.lambda ? `${rootPath}/vk-constants` : `${rootPath}/routes/vk-constants`, // constants = require(`${constantsRootPath}/index.js`), integrationAccountTypes = require(`${constantsRootPath}/constants/integrationAccountType.json`); /** * @desc Class exposing database model operations based on certain requirements. * @class SmartSensorModel */ export class SmartSensorModel { /** * @desc Generic method to get Smart Sensors by filters provided in param * @param {{ hostId: number, loggedInUserId: number, page: number, perPage: number, filters: object }} payload - Request Payload * @returns {{count: number, rows: Array}} - Response Returned * @author Vineet */ async getSmartSensors(payload: any) { try { console.log(">>>>>>>>>>> SmartSensorModel:getSmartSensors, Function started with payload : " + JSON.stringify(payload) + " <<<<<<<<<<<"); let whereCon = { isDeleted: 0 }; if (typeof payload.filters === 'object' && Object.keys(payload.filters).length) { whereCon = Object.assign({}, whereCon, payload.filters); } let accountCon: any = {}; if (payload.hostId) { accountCon.userId = payload.hostId; } let includeModels: any = [{ model: db.Accounts, as: 'Accounts', attributes: [ 'accountName', ['user_id', 'accountUserId'] ], required: true }]; if (payload.accountTypeId && payload.accountTypeId == integrationAccountTypes.SMARTTHING) { includeModels.push({ model: db.SmartthingApi, as: 'SmartthingApi', attributes: ['id'], where: { isDeleted: 0 }, required: true }); } else { includeModels.push({ model: db.SmartthingApi, as: 'SmartthingApi', attributes: ['id'], where: { isDeleted: 0 }, required: false }); } const queryObj: any = { where: whereCon, attributes: [ "id", "accountId", "accountTypeId", "uniqueIdentifier", "deviceSerialNumber", "deivceName" ], include: includeModels }; if (payload.perPage && payload.page) { let limit = payload.perPage ? payload.perPage : 10; let skip = payload.page ? (payload.page - 1) * limit : 0; queryObj.limit = limit; queryObj.offset = skip; } let res = await db.vkSensors.findAndCountAll(queryObj); res = JSON.parse(JSON.stringify(res)); res.rows = res.rows.map((s: any) => { if (s.SmartthingApi) { s.smartthingApiId = s.SmartthingApi.id || ""; delete s.SmartthingApi; } if (s.Accounts) { s.accountName = s.Accounts.accountName || ""; s.accountUserId = s.Accounts.accountUserId || ""; delete s.Accounts; } return s; }); console.log(">>>>>>>>>>> SmartSensorModel:getSmartSensors, Function Response: " + JSON.stringify(res) + " <<<<<<<<<<<"); return res; } catch (err) { console.log(">>>>>>>>>>> SmartSensorModel:getSmartSensors >>> catch, error occured : " + JSON.stringify(err) + " <<<<<<<<<<<"); throw err; } } /** * @desc Function to link Smart Sensor to property * @param {{ hostId: number, loggedInUserId: number, propertyId: number, accountId: number, uniqueIdentifier: string }} payload - Request Payload * @returns {Object} - Response Returned * @author - Vineet */ async linkSmartSensorToProperty(payload: any) { console.log('>>>>>>>>>>> SmartSensorModel:linkSmartSensorToProperty, Method started with payload ' + JSON.stringify(payload)); let res = await db.vkSensorMapping.create(payload); console.log('>>>>>>>>>>> SmartSensorModel:linkSmartSensorToProperty, Response ' + JSON.stringify(res)); return res; } /** * @desc Generic method to get Smart Sensor mappings by filters provided in param * @param {{ hostId: number, loggedInUserId: number, propertyId: number, filters: object, page: number, perPage: number }} payload - Request Payload * @returns {{ count: number, rows: Array<{ smartSensorMappingId: number, accountId: number, smartthingApiId: number, hubListId: number, deviceSerialNumber: string }>}} - Response Returned * @author Vineet */ async getSmartSensorMapping(payload: any) { try { console.log(">>>>>>>>>>> SmartSensorModel:getSmartSensorMapping, Function started with payload : " + JSON.stringify(payload) + " <<<<<<<<<<<"); let whereCon = { isDeleted: 0 }; let smartthingApiIdCon: any = { isDeleted: 0 }; if (payload.hostId) { smartthingApiIdCon.userId = payload.hostId; } if (typeof payload.filters === 'object' && Object.keys(payload.filters).length) { whereCon = Object.assign({}, whereCon, payload.filters); } let queryObj: any = { where: whereCon, attributes: [ ["id", "smartSensorMappingId"], "accountId", "propertyId", "uniqueIdentifier", [Sequelize.fn('IFNULL', Sequelize.col("hubListId"), ""), "hubListId"], [Sequelize.fn('IFNULL', Sequelize.col("smartthingApiId"), ""), "smartthingApiId"], [Sequelize.fn('IFNULL', Sequelize.col("hubListId"), ''), "hubListId"], [Sequelize.fn('IFNULL', Sequelize.col("installedAt"), ''), "installedAt"], [Sequelize.fn('IFNULL', Sequelize.col("sensorLocation"), ''), "sensorLocation"] ], include: [ { model: db.Accounts, as: 'accounts', attributes: ['accountTypeId', 'accountName'], required: true }, { model: db.vkSensors, as: 'vkSensors', where: { isDeleted: 0 }, attributes: [ "deivceName", "deviceSerialNumber", [Sequelize.fn('IFNULL', Sequelize.col("sensorType"), ''), "sensorType"] ], required: true }, { model: db.SmartthingApi, as: 'SmartthingApi', where: smartthingApiIdCon, attributes: [['location_name', 'hubLocation']], required: true } ] }; if (payload.perPage && payload.page) { let limit = payload.perPage ? payload.perPage : 10; let skip = payload.page ? (payload.page - 1) * limit : 0; queryObj.limit = limit; queryObj.offset = skip; } let res = await db.vkSensorMapping.findAndCountAll(queryObj); res = JSON.parse(JSON.stringify(res)); console.log('>>>>>>>>>>> SmartSensorModel:getSmartSensorMapping >>> Response ' + JSON.stringify(res)); return res; } catch (err) { console.log(">>>>>>>>>>> SmartSensorModel:getSmartSensorMapping >>> Error: " + JSON.stringify(err) + " <<<<<<<<<<<"); throw err; } } }