import { VAModel, TipsModel, ClientModel } from "../../../models/vaModel.js"; // Approve or reject leave async function va_tips_manage_function(req, res) { const { clientId, amount, description, vaId } = req.body; try { const vaUser = await VAModel.findById(vaId); if (!vaUser) { return res.status(404).json({ success: false, message: "Virtual Assistant not found" }); } const clUser = await ClientModel.findById(clientId); if (!clUser) { return res.status(404).json({ success: false, message: "Client not found" }); } // Create new tip entry const newTip = { clientId, status: "Unbilled", amount: parseFloat(amount).toFixed(2), description: description || "", vaName: vaUser.userDetails.firstName ? vaUser.userDetails.firstName : "" + " " + vaUser.userDetails.lastName ? vaUser.userDetails.lastName : "", vaId, }; const createdTip = await TipsModel.create(newTip); const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/va-tips`; const sfData = { "id": createdTip._id.toString(), "vaId": createdTip.vaId.toString(), "status": "Unbilled", "clientId": createdTip.clientId.toString(), "comment": createdTip.description || "", "tipAmount": Number(parseFloat(createdTip.amount).toFixed(2)), // Convert to cents } const response = await fetch(salesforceUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', // Add any required headers for Salesforce authentication // 'Authorization': 'Bearer ' + process.env.SF_ACCESS_TOKEN }, body: JSON.stringify({ ...sfData }) }); if (response.ok) { console.log("✅ Successfully synced Tip data to Salesforce"); return res.status(201).json({ success: true, data: createdTip }); } else { TipsModel.findByIdAndDelete(createdTip._id); const errorText = await response.text(); throw new Error(`Salesforce API responded with status ${response.status}: ${errorText}`); } } catch (error) { console.log(error); res.status(500).json({ success: false, message: "Internal server error" }); } } async function va_tips_get(req, res) { const { clientId } = req.body; try { const clUser = await ClientModel.findById(clientId); if (!clUser) { return res.status(404).json({ success: false, message: "Client not found" }); } const data = await TipsModel.find({ clientId: clientId, status: "Unbilled" }); return res.status(200).json({ success: true, data: data }); } catch (error) { console.log(error); res.status(500).json({ success: false, message: "Internal server error" }); } } async function va_tips_update(req, res) { const { tipId } = req.params; const { update } = req.body; try { const tipData = await TipsModel.findById(tipId); if (!tipData) { return res.status(404).json({ success: false, message: "Tip not found" }); } await TipsModel.updateOne({ _id: tipId }, update); return res.status(200).json({ success: true, message: "Tip updated successfully" }); } catch (error) { console.log(error); res.status(500).json({ success: false, message: "Internal server error" }); } } async function va_tips_delete(req, res) { const { tipId } = req.params; try { const salesforceUrl = `${process.env.SF_DOCTRACKER_API_URL}services/apexrest/docva/api/va-tips`; // const sfData = { // "id": createdTip._id.toString(), // "vaId": createdTip.vaId.toString(), // "status": "Unbilled", // "clientId": createdTip.clientId.toString(), // "comment": createdTip.description, // "tipAmount": Number(parseFloat(createdTip.amount).toFixed(2)), // Convert to cents // } const response = await fetch(salesforceUrl, { method: 'DELETE', headers: { 'Content-Type': 'application/json', // Add any required headers for Salesforce authentication // 'Authorization': 'Bearer ' + process.env.SF_ACCESS_TOKEN }, body: JSON.stringify({ "id": tipId}) }); if (response.ok) { await TipsModel.findOneAndDelete({ _id: tipId }); return res.status(200).json({ success: true, message: "Tip updated successfully" }); } else { const errorText = await response.text(); throw new Error(`Salesforce API responded with status ${response.status}: ${errorText}`); } } catch (error) { console.log(error); res.status(500).json({ success: false, message: "Internal server error" }); } } export { va_tips_manage_function, va_tips_get, va_tips_update, va_tips_delete };