do what we can
This commit is contained in:
@@ -507,7 +507,7 @@ async function createShareLink(accessToken, driveId, itemId) {
|
||||
return json;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
async function run(incomingRecords) {
|
||||
console.log('minimal-uploader starting', dryRun ? '(dry-run)' : '');
|
||||
const token = await getAppToken();
|
||||
console.log('acquired app token (truncated):', token ? token.substring(0, 30) + '...' : '(none)');
|
||||
@@ -522,25 +522,34 @@ async function run() {
|
||||
}
|
||||
const safeMainFolderName = sanitizeName(mainFolderName);
|
||||
const subFolderName = 'Managers Info';
|
||||
// Prefer generating from an Excel workbook if EXCEL_DRIVE_ID and EXCEL_ITEM_ID are present
|
||||
let csvBase64 = await pdfFromCsvToBase64(pathToCsv);
|
||||
if (process.env.EXCEL_DRIVE_ID && process.env.EXCEL_ITEM_ID) {
|
||||
console.log('EXCEL_DRIVE_ID/EXCEL_ITEM_ID present, downloading workbook from Graph to build PDF');
|
||||
try {
|
||||
const token1 = token; // app token used for Graph
|
||||
const workbookBuffer = await downloadExcelFromGraph(token1, process.env.EXCEL_DRIVE_ID, process.env.EXCEL_ITEM_ID);
|
||||
// Build a PDF directly from the workbook and its formatting/regions if possible
|
||||
const wbPdf = await pdfFromWorkbookBufferToBase64(workbookBuffer);
|
||||
if (wbPdf) {
|
||||
csvBase64 = wbPdf;
|
||||
// count rows if we can
|
||||
const recs = parseManagersSheetFromWorkbookBuffer(workbookBuffer) || [];
|
||||
console.log('Built PDF from workbook Managers Info with', recs.length, 'rows');
|
||||
} else {
|
||||
console.log('No Managers Info sheet / no rows found in workbook — falling back to managers.csv or static PDF');
|
||||
|
||||
// Priority: use incoming records from add-in, else try CSV, else try workbook from Graph
|
||||
let csvBase64 = null;
|
||||
|
||||
if (incomingRecords && incomingRecords.length > 0) {
|
||||
console.log('Using records from Excel add-in:', incomingRecords.length, 'rows');
|
||||
csvBase64 = await pdfFromRecordsToBase64(incomingRecords, 'Managers Info');
|
||||
} else {
|
||||
// fallback to CSV file or Graph workbook
|
||||
csvBase64 = await pdfFromCsvToBase64(pathToCsv);
|
||||
if (process.env.EXCEL_DRIVE_ID && process.env.EXCEL_ITEM_ID) {
|
||||
console.log('EXCEL_DRIVE_ID/EXCEL_ITEM_ID present, downloading workbook from Graph to build PDF');
|
||||
try {
|
||||
const token1 = token; // app token used for Graph
|
||||
const workbookBuffer = await downloadExcelFromGraph(token1, process.env.EXCEL_DRIVE_ID, process.env.EXCEL_ITEM_ID);
|
||||
// Build a PDF directly from the workbook and its formatting/regions if possible
|
||||
const wbPdf = await pdfFromWorkbookBufferToBase64(workbookBuffer);
|
||||
if (wbPdf) {
|
||||
csvBase64 = wbPdf;
|
||||
// count rows if we can
|
||||
const recs = parseManagersSheetFromWorkbookBuffer(workbookBuffer) || [];
|
||||
console.log('Built PDF from workbook Managers Info with', recs.length, 'rows');
|
||||
} else {
|
||||
console.log('No Managers Info sheet / no rows found in workbook — falling back to managers.csv or static PDF');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to build PDF from workbook', e && e.message ? e.message : e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to build PDF from workbook', e && e.message ? e.message : e);
|
||||
}
|
||||
}
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
|
||||
@@ -19,7 +19,9 @@ app.get('/api/health', (req, res) => res.json({ ok: true, ts: new Date().toISOSt
|
||||
// main endpoint — run the uploader flow (download workbook if env vars exist, create folders and upload PDF)
|
||||
app.post('/api/upload-managers', async (req, res) => {
|
||||
try {
|
||||
const result = await run();
|
||||
// Accept records from request body (sent from Excel add-in)
|
||||
const records = req.body && req.body.records ? req.body.records : null;
|
||||
const result = await run(records);
|
||||
res.json({ ok: true, result });
|
||||
} catch (err) {
|
||||
console.error('Error in /api/upload-managers', err);
|
||||
|
||||
Reference in New Issue
Block a user