const { success, failure } = require('../../utils/response'); const IntegrationService = require('../../services/integration/integrationService'); async function getData(req, res) { try { const { provider, service, resource, page, limit, filters } = req.query; console.log('query is', req.query); const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const data = await integrationService.getData(provider, service, resource, params); res.json(success(`${provider} ${service} ${resource} data`, data)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getServices(req, res) { try { const { provider } = req.query; const integrationService = new IntegrationService(req.user.uuid); const services = await integrationService.getAvailableServices(provider); res.json(success(`${provider} available services`, services)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getResources(req, res) { try { const { provider, service } = req.query; const integrationService = new IntegrationService(req.user.uuid); const resources = await integrationService.getAvailableResources(provider, service); res.json(success(`${provider} ${service} available resources`, resources)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getPortals(req, res) { try { const { provider } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Portals are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const portals = await integrationService.getPortals(provider); res.json(success('Zoho portals retrieved successfully', portals)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAllProjects(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('All projects are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const projects = await integrationService.getAllProjects(provider, params); res.json(success('All Zoho projects retrieved successfully', projects)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAllProjectTasks(req, res) { try { const { provider, page, limit, filters, portal_id } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('All project tasks are only available for Zoho provider', 'INVALID_PROVIDER')); } if (!portal_id) { return res.status(400).json(failure('portal_id is required in query parameters', 'MISSING_PORTAL_ID')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const tasks = await integrationService.getAllProjectTasks(provider, portal_id, params); res.json(success('All Zoho project tasks retrieved successfully', tasks)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAllProjectTaskLists(req, res) { try { const { provider, page, limit, filters, portal_id } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('All project task lists are only available for Zoho provider', 'INVALID_PROVIDER')); } if (!portal_id) { return res.status(400).json(failure('portal_id is required in query parameters', 'MISSING_PORTAL_ID')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const taskLists = await integrationService.getAllProjectTaskLists(provider, portal_id, params); res.json(success('All Zoho project task lists retrieved successfully', taskLists)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAllProjectIssues(req, res) { try { const { provider, page, limit, filters, portal_id } = req.query; console.log('req query is', req.query) if (provider !== 'zoho') { return res.status(400).json(failure('All project issues are only available for Zoho provider', 'INVALID_PROVIDER')); } if (!portal_id) { return res.status(400).json(failure('portal_id is required in query parameters', 'MISSING_PORTAL_ID')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const issues = await integrationService.getAllProjectIssues(provider, portal_id, params); res.json(success('All Zoho project issues retrieved successfully', issues)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAllProjectPhases(req, res) { try { const { provider, page, limit, filters, portal_id } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('All project phases are only available for Zoho provider', 'INVALID_PROVIDER')); } if (!portal_id) { return res.status(400).json(failure('portal_id is required in query parameters', 'MISSING_PORTAL_ID')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const phases = await integrationService.getAllProjectPhases(provider, portal_id, params); res.json(success('All Zoho project phases retrieved successfully', phases)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getSalesOrders(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Sales Orders are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const salesOrders = await integrationService.getSalesOrders(provider, params); res.json(success('Zoho Sales Orders retrieved successfully', salesOrders)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getPurchaseOrders(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Purchase Orders are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const purchaseOrders = await integrationService.getPurchaseOrders(provider, params); res.json(success('Zoho Purchase Orders retrieved successfully', purchaseOrders)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getInvoices(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Invoices are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const invoices = await integrationService.getInvoices(provider, params); res.json(success('Zoho Invoices retrieved successfully', invoices)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } // Zoho People specific controllers async function getDepartments(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Departments are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const departments = await integrationService.getDepartments(provider, params); res.json(success('Zoho Departments retrieved successfully', departments)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getLeaveRequests(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Leave Requests are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const leaveRequests = await integrationService.getLeaveRequests(provider, params); res.json(success('Zoho Leave Requests retrieved successfully', leaveRequests)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAttendance(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Attendance is only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const attendance = await integrationService.getAttendance(provider, params); res.json(success('Zoho Attendance retrieved successfully', attendance)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } // Zoho Books specific controllers async function getOrganizations(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Organizations are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const organizations = await integrationService.getOrganizations(provider, params); res.json(success('Zoho Organizations retrieved successfully', organizations)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getCustomers(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Customers are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const customers = await integrationService.getCustomers(provider, params); res.json(success('Zoho Customers retrieved successfully', customers)); } catch (error) { console.log('customer response error i got', JSON.stringify(error)) res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getVendors(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Vendors are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const vendors = await integrationService.getVendors(provider, params); res.json(success('Zoho Vendors retrieved successfully', vendors)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getItems(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Items are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const items = await integrationService.getItems(provider, params); res.json(success('Zoho Items retrieved successfully', items)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getEstimates(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Estimates are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const estimates = await integrationService.getEstimates(provider, params); res.json(success('Zoho Estimates retrieved successfully', estimates)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getBills(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Bills are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const bills = await integrationService.getBills(provider, params); res.json(success('Zoho Bills retrieved successfully', bills)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getExpenses(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Expenses are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const expenses = await integrationService.getExpenses(provider, params); res.json(success('Zoho Expenses retrieved successfully', expenses)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getBankAccounts(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Bank Accounts are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const bankAccounts = await integrationService.getBankAccounts(provider, params); res.json(success('Zoho Bank Accounts retrieved successfully', bankAccounts)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getBankTransactions(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Bank Transactions are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const bankTransactions = await integrationService.getBankTransactions(provider, params); res.json(success('Zoho Bank Transactions retrieved successfully', bankTransactions)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getReports(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Reports are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const reports = await integrationService.getReports(provider, params); res.json(success('Zoho Reports retrieved successfully', reports)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getBooksSalesOrders(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Books Sales Orders are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const salesOrders = await integrationService.getBooksSalesOrders(provider, params); res.json(success('Zoho Books Sales Orders retrieved successfully', salesOrders)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getBooksPurchaseOrders(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Books Purchase Orders are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const purchaseOrders = await integrationService.getBooksPurchaseOrders(provider, params); res.json(success('Zoho Books Purchase Orders retrieved successfully', purchaseOrders)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getContacts(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Contacts are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const contacts = await integrationService.getContacts(provider, params); res.json(success('Zoho Books Contacts retrieved successfully', contacts)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } // Zoho People Forms API controllers async function getEmployeeForms(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Employee Forms are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const employeeForms = await integrationService.getEmployeeForms(provider, params); res.json(success('Zoho People Employee Forms retrieved successfully', employeeForms)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getEmployeeById(req, res) { try { const { provider, recordId, formLinkName } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Employee Details are only available for Zoho provider', 'INVALID_PROVIDER')); } if (!recordId) { return res.status(400).json(failure('recordId is required', 'MISSING_RECORD_ID')); } const integrationService = new IntegrationService(req.user.uuid); const employeeDetail = await integrationService.getEmployeeById(provider, recordId, formLinkName); res.json(success('Zoho People Employee Details retrieved successfully', employeeDetail)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getAttendanceEntries(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Attendance Entries are only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const attendanceEntries = await integrationService.getAttendanceEntries(provider, params); res.json(success('Zoho People Attendance Entries retrieved successfully', attendanceEntries)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getShiftConfiguration(req, res) { try { const { provider, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Shift Configuration is only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const shiftConfig = await integrationService.getShiftConfiguration(provider, params); res.json(success('Zoho People Shift Configuration retrieved successfully', shiftConfig)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getLeaveData(req, res) { try { const { provider, formLinkName, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Leave Data is only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const leaveData = await integrationService.getLeaveData(provider, formLinkName, params); res.json(success('Zoho People Leave Data retrieved successfully', leaveData)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getGoalsData(req, res) { try { const { provider, formLinkName, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Goals Data is only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const goalsData = await integrationService.getGoalsData(provider, formLinkName, params); res.json(success('Zoho People Goals Data retrieved successfully', goalsData)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } async function getPerformanceData(req, res) { try { const { provider, formLinkName, page, limit, filters } = req.query; if (provider !== 'zoho') { return res.status(400).json(failure('Performance Data is only available for Zoho provider', 'INVALID_PROVIDER')); } const integrationService = new IntegrationService(req.user.uuid); const params = { page, limit }; if (filters) { try { params.filters = JSON.parse(filters); } catch (e) { return res.status(400).json(failure('Invalid filters format', 'INVALID_FILTERS')); } } const performanceData = await integrationService.getPerformanceData(provider, formLinkName, params); res.json(success('Zoho People Performance Data retrieved successfully', performanceData)); } catch (error) { res.status(400).json(failure(error.message, 'INTEGRATION_ERROR')); } } module.exports = { getData, getServices, getResources, getPortals, getAllProjects, getAllProjectTasks, getAllProjectTaskLists, getAllProjectIssues, getAllProjectPhases, getSalesOrders, getPurchaseOrders, getInvoices, getDepartments, getLeaveRequests, getAttendance, getOrganizations, getCustomers, getVendors, getItems, getEstimates, getBills, getExpenses, getBankAccounts, getBankTransactions, getReports, getBooksSalesOrders, getBooksPurchaseOrders, getContacts, getEmployeeForms, getEmployeeById, getAttendanceEntries, getShiftConfiguration, getLeaveData, getGoalsData, getPerformanceData };