11require ( "dotenv" ) . config ( ) ;
22const axios = require ( "axios" ) ;
33const getBearerToken = require ( "./get-access-token" ) ;
4-
54const baseURL = process . env . BASE_URL ;
65
76/**
87 * Get detailed export schedule information including recipients
9- * @param {string } scheduleId - The schedule notification ID
8+ * @param {string } workbookId - The workbook ID
9+ * @param {string } scheduleId - The schedule notification ID (optional - used to filter results)
1010 * @returns {Promise<Object> } The detailed schedule data including recipients
1111 */
12- async function getExportScheduleDetails ( scheduleId ) {
13- if ( ! scheduleId ) {
14- throw new Error ( 'Schedule ID is required' ) ;
12+ async function getExportScheduleDetails ( workbookId , scheduleId = null ) {
13+ if ( ! workbookId ) {
14+ throw new Error ( 'Workbook ID is required' ) ;
1515 }
1616
1717 if ( process . env . DEBUG === "true" ) {
18- console . log ( `Fetching detailed schedule information for ID : ${ scheduleId } ` ) ;
18+ console . log ( `Fetching schedule information for workbook : ${ workbookId } ` ) ;
1919 }
2020
2121 const bearerToken = await getBearerToken ( ) ;
@@ -25,7 +25,7 @@ async function getExportScheduleDetails(scheduleId) {
2525
2626 try {
2727 const response = await axios . get (
28- `${ baseURL } /schedules/ ${ scheduleId } ` ,
28+ `${ baseURL } /v2/workbooks/ ${ workbookId } /schedules ` ,
2929 {
3030 headers : {
3131 "Authorization" : `Bearer ${ bearerToken } ` ,
@@ -38,16 +38,30 @@ async function getExportScheduleDetails(scheduleId) {
3838 throw new Error ( 'No schedule data returned from API' ) ;
3939 }
4040
41- const schedule = response . data ;
41+ let schedule = response . data ;
42+
43+ // If scheduleId is provided, filter to find the specific schedule
44+ if ( scheduleId ) {
45+ if ( Array . isArray ( schedule ) ) {
46+ schedule = schedule . find ( s => s . scheduleId === scheduleId || s . id === scheduleId ) ;
47+ if ( ! schedule ) {
48+ throw new Error ( `Schedule with ID ${ scheduleId } not found in workbook ${ workbookId } ` ) ;
49+ }
50+ }
51+ }
4252
4353 if ( process . env . DEBUG === "true" ) {
44- console . log ( `Successfully fetched schedule details for: ${ schedule . configV2 ?. title || scheduleId } ` ) ;
45-
46- // Log recipient information if available
47- if ( schedule . target && schedule . target . length > 0 ) {
48- console . log ( `Found ${ schedule . target . length } recipients:` , schedule . target . map ( t => t . email ) ) ;
54+ if ( Array . isArray ( schedule ) ) {
55+ console . log ( `Successfully fetched ${ schedule . length } schedules for workbook` ) ;
4956 } else {
50- console . log ( 'No recipients found in schedule details' ) ;
57+ console . log ( `Successfully fetched schedule details for: ${ schedule . configV2 ?. title || scheduleId } ` ) ;
58+
59+ // Log recipient information if available
60+ if ( schedule . target && schedule . target . length > 0 ) {
61+ console . log ( `Found ${ schedule . target . length } recipients:` , schedule . target . map ( t => t . email ) ) ;
62+ } else {
63+ console . log ( 'No recipients found in schedule details' ) ;
64+ }
5165 }
5266 }
5367
@@ -56,13 +70,11 @@ async function getExportScheduleDetails(scheduleId) {
5670 const errorMsg = error . response ?. data ?. message ||
5771 error . response ?. data ?. error ||
5872 error . message ;
59-
6073 if ( process . env . DEBUG === "true" ) {
61- console . error ( `Error fetching schedule details for ${ scheduleId } :` , errorMsg ) ;
74+ console . error ( `Error fetching schedule details for workbook ${ workbookId } :` , errorMsg ) ;
6275 }
63-
6476 throw new Error ( `Failed to get schedule details: ${ errorMsg } ` ) ;
6577 }
6678}
6779
68- module . exports = getExportScheduleDetails ;
80+ module . exports = getExportScheduleDetails ;
0 commit comments