Forked from ivansnag/import_incidents_from_CSV_template.js
Created
August 16, 2024 21:59
-
-
Save Sdy603/ca428660cd3ac9734e065eee657ae0f5 to your computer and use it in GitHub Desktop.
import_incidents_from_CSV_template.js - CSV Template (https://docs.google.com/spreadsheets/d/1lC7dOhtyWMaLGXmm1pQsdfwhEBu7NulIov5K_pvv7BE/edit#gid=0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const fs = require('fs'); | |
const csv = require('csv-parser'); | |
const csvFilePath = 'CSV TEMPLATE'; | |
const apiEndpoint = 'CUSTOMER SPECIFIC INCIDENTS SYNC ENDPOINT'; | |
const apiKey = 'API KEY'; | |
fs.createReadStream(csvFilePath) | |
.pipe(csv()) | |
.on('data', (row) => { | |
const incidentData = { | |
reference_id: row.reference_id, | |
name: row.name, | |
started_at: row.started_at, | |
resolved_at: row.resolved_at, | |
source_url: row.source_url || '', | |
services: row.services ? row.services.split(',') : [] | |
}; | |
console.log('Request Body:', JSON.stringify(incidentData, null, 2)); // Print formatted JSON | |
fetch(apiEndpoint, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${apiKey}` | |
}, | |
body: JSON.stringify(incidentData) | |
}) | |
.then(response => response.json()) | |
.then(data => console.log('Success:', data)) | |
.catch(error => console.error('Error:', error)); | |
}) | |
.on('end', () => { | |
console.log('CSV file successfully processed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment