// Post-Record Integration Module // Handles all post-PocketBase record creation tasks: // 1. Captures form data and Job_Number from the created PB record // 2. Creates folder structure in OneDrive using the same Graph token // 3. Captures share link from created folder // 4. Returns the share link to be added to Excel row const { createJobFolderAndGetLink } = require('./job-folder-integration'); /** * Processes a newly created PocketBase record by: * - Creating corresponding folder structure in OneDrive * - Capturing the share link for the folder * * @param {Object} record - The PocketBase record that was just created * @param {string} accessToken - Graph API access token (from server) * @returns {Promise} Result with share link and folder info */ async function processNewJobRecord(record, accessToken) { try { console.log('[Post-Record Integration] Processing new job record:', { jobNumber: record.Job_Number, jobName: record.Job_Name, recordId: record.id }); // Extract required fields from the record const jobNumber = record.Job_Number || record.job_number; const jobName = record.Job_Name || record.job_name || ''; const jobAddress = record.Job_Address || record.job_address || ''; const companyClient = record.Company_Client || record.company_client || ''; // Calculate Job_Full_Name (same logic as in server.ts) const parts = [jobName, jobAddress, companyClient].filter(p => p && String(p).trim()); const jobFullName = parts.join(' - '); console.log('[Post-Record Integration] Calculated job full name:', jobFullName); // Validate required fields if (!jobNumber) { throw new Error('Job_Number is required but was not found in record'); } if (!jobFullName || jobFullName.trim() === '') { throw new Error('Job_Full_Name could not be calculated from record data'); } // Create folder and get share link const folderResult = await createJobFolderAndGetLink( accessToken, jobNumber, jobFullName, record ); console.log('[Post-Record Integration] ✓ Folder creation successful'); console.log('[Post-Record Integration] Share link:', folderResult.shareLink); return { success: true, jobNumber, jobFullName, shareLink: folderResult.shareLink, mainFolderId: folderResult.mainFolderId, mainFolderName: folderResult.mainFolderName, subFolderId: folderResult.subFolderId, folderPath: folderResult.folderPath }; } catch (error) { console.error('[Post-Record Integration] Error processing new job record:', error); return { success: false, error: error.message, jobNumber: record.Job_Number || record.job_number, shareLink: null }; } } module.exports = { processNewJobRecord };