feat: add Power Automate webhook integration for job notifications with task creation support
This commit is contained in:
@@ -50,6 +50,62 @@ async function logError(context: string, error: any, additionalData?: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// TEAMS WEBHOOK NOTIFICATION (Power Automate)
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
async function notifyTeamsChannel(jobData: any, jobFolderLink: string) {
|
||||||
|
const webhook = process.env.POWER_AUTOMATE_WEBHOOK_URL || process.env.TEAMS_WEBHOOK_URL;
|
||||||
|
|
||||||
|
// Skip if webhook not configured
|
||||||
|
if (!webhook) {
|
||||||
|
console.log('⚠️ Power Automate webhook not configured (POWER_AUTOMATE_WEBHOOK_URL missing)');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const jobName = jobData.Job_Name || '';
|
||||||
|
const jobNumber = jobData.Job_Number || '';
|
||||||
|
const jobAddress = jobData.Job_Address || '';
|
||||||
|
const companyClient = jobData.Company_Client || '';
|
||||||
|
const jobDivision = jobData.Job_Division || '';
|
||||||
|
const jobType = jobData.Job_Type || '';
|
||||||
|
const contactPerson = jobData.Contact_Person || '';
|
||||||
|
const phoneNumber = jobData.Phone_Number || '';
|
||||||
|
|
||||||
|
// Build payload for Power Automate
|
||||||
|
const payload = {
|
||||||
|
jobNumber,
|
||||||
|
jobName,
|
||||||
|
jobAddress,
|
||||||
|
companyClient,
|
||||||
|
jobDivision,
|
||||||
|
jobType,
|
||||||
|
contactPerson,
|
||||||
|
phoneNumber,
|
||||||
|
jobFolderLink
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch(webhook, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const text = await response.text();
|
||||||
|
console.warn(`⚠️ Power Automate notification failed: ${response.status} ${text}`);
|
||||||
|
} else {
|
||||||
|
console.log(`✓ Power Automate notification sent for Job ${jobNumber}`);
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('⚠️ Failed to send Power Automate notification:', error);
|
||||||
|
// Don't fail the job creation if notification fails
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Enable CORS
|
// Enable CORS
|
||||||
app.use('/*', cors());
|
app.use('/*', cors());
|
||||||
|
|
||||||
@@ -600,6 +656,13 @@ app.post('/api/submit', async (c) => {
|
|||||||
await logError('PocketBase Final Update', pbFinalizeErr, { recordId: record.id, jobFolderLink });
|
await logError('PocketBase Final Update', pbFinalizeErr, { recordId: record.id, jobFolderLink });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send Teams notification (fire and forget, don't block response)
|
||||||
|
if (jobFolderLink) {
|
||||||
|
notifyTeamsChannel(record, jobFolderLink).catch(err => {
|
||||||
|
console.error('⚠️ Teams notification error:', err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: `Job created successfully with Job Number: ${newJobNumber} and synced to Excel`,
|
message: `Job created successfully with Job Number: ${newJobNumber} and synced to Excel`,
|
||||||
|
|||||||
Reference in New Issue
Block a user