1+ import { getOrCreateCalendar } from "./calendar.js" ;
2+
13export function getCurrentYearMonth ( ) {
24 const today = new Date ( ) ;
35 return {
@@ -78,16 +80,18 @@ function normalizeBatchText(raw) {
7880 return raw . replace ( / \r ? \n / g, "\r\n" ) ;
7981}
8082
81- function generateBatchRequestBody ( events , boundary = "batch_boundary" ) {
83+ function generateBatchRequestBody ( events , boundary = "batch_boundary" , calendarId ) {
8284 const CRLF = "\r\n" ;
8385 let body = "" ;
86+ console . log ( calendarId ) ;
8487
8588 events . forEach ( ( event , i ) => {
8689 body += `--${ boundary } ${ CRLF } ` ;
8790 body += `Content-Type: application/http${ CRLF } ` ;
8891 body += `Content-ID: <item-${ i + 1 } >${ CRLF } ${ CRLF } ` ;
8992
90- body += `POST /calendar/v3/calendars/primary/events HTTP/1.1${ CRLF } ` ;
93+ body += `POST /calendar/v3/calendars/${ calendarId } /events HTTP/1.1${ CRLF } ` ;
94+ body += `Host: www.googleapis.com${ CRLF } ` ;
9195 body += `Content-Type: application/json${ CRLF } ${ CRLF } ` ;
9296
9397 const eventPayload = {
@@ -104,6 +108,7 @@ function generateBatchRequestBody(events, boundary = "batch_boundary") {
104108 } ;
105109
106110 body += JSON . stringify ( eventPayload ) + CRLF + CRLF ;
111+ console . log ( body ) ;
107112 } ) ;
108113
109114 body += `--${ boundary } --${ CRLF } ` ;
@@ -148,7 +153,8 @@ function parseGoogleBatchResponse(responseText) {
148153// Google Calendar API batch add Event
149154export async function addBatchEventsToCalendar ( token , events ) {
150155 const boundary = "batch_boundary_" + Date . now ( ) ;
151- const body = generateBatchRequestBody ( events , boundary ) ;
156+ const calendarId = await getOrCreateCalendar ( token ) ;
157+ const body = generateBatchRequestBody ( events , boundary , calendarId ) ;
152158
153159 const res = await fetch ( "https://www.googleapis.com/batch/calendar/v3" , {
154160 method : "POST" ,
0 commit comments