Looped sync works, but is slow. No retry or resiliency built in.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import axios from "axios";
|
||||
import qs from "qs";
|
||||
import dotenv from "dotenv";
|
||||
import PocketBase from "pocketbase";
|
||||
import XLSX from "xlsx";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// -----------------------------
|
||||
// CONFIG
|
||||
// -----------------------------
|
||||
const CONFIG = JSON.parse(fs.readFileSync("config.json", "utf8"));
|
||||
const tenantId = CONFIG.tenantId;
|
||||
const clientId = CONFIG.clientId;
|
||||
const clientSecret = process.env.CLIENT_SECRET;
|
||||
|
||||
|
||||
// -----------------------------
|
||||
// EXCEL HEADER AND DATA ROW PARAMETERS
|
||||
// -----------------------------
|
||||
const HEADER_ROW_INDEX = 1; // headers on row 2 (index 1)
|
||||
const DATA_START_ROW_INDEX = 2; // data starts on row 3 (index 2)
|
||||
|
||||
|
||||
// -----------------------------
|
||||
// GRAPH TOKEN ACQUISITION
|
||||
// -----------------------------
|
||||
async function getGraphToken() {
|
||||
const tokenUrl = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`;
|
||||
const body = qs.stringify({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "https://graph.microsoft.com/.default",
|
||||
grant_type: "client_credentials",
|
||||
});
|
||||
const res = await axios.post(tokenUrl, body, {
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
});
|
||||
log("✅ Graph token acquired");
|
||||
return res.data.access_token;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// GRAPH EXCEL DOWNLOAD HELPER
|
||||
// -----------------------------
|
||||
async function downloadExcelFromGraph(graphToken, driveId, itemId) {
|
||||
const url = `https://graph.microsoft.com/v1.0/drives/${driveId}/items/${itemId}/content`;
|
||||
const res = await axios.get(url, {
|
||||
headers: { Authorization: `Bearer ${graphToken}` },
|
||||
responseType: "arraybuffer",
|
||||
});
|
||||
return Buffer.from(res.data);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// MAIN SYNC LOGIC
|
||||
// -----------------------------
|
||||
async function runSync() {
|
||||
try {
|
||||
const graphToken = await getGraphToken();
|
||||
|
||||
const driveId = "b!9YOqqr2xM0G2DFBwMEJYY4UzQgocFddEtpLYWuS9_AtkCKl2q8yjQJteQ7Ti4QSx";
|
||||
const itemId = "01SPNXLDSQTMG4F6BDANCLJU2FZKEDQLVU";
|
||||
|
||||
const excelBuffer = await downloadExcelFromGraph(graphToken, driveId, itemId);
|
||||
} catch (error) {
|
||||
console.error("❌ Error during sync:", error);
|
||||
}}
|
||||
Reference in New Issue
Block a user