// Post Outlook Calendar Event Attachment via Microsoft Graph API v1.0 (ver. 202209)
// (c) 2022, Questetra, Inc. (the MIT License)
//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2 = configs.get( "conf_OAuth2" ) + "";
const eventId = configs.get( "conf_eventId" ) + "";
const files = engine.findDataByNumber( configs.get( "conf_attachment" ) );
const dataId_attachmentId = configs.get( "conf_attachmentId" );
const dataId_log = configs.get( "conf_log" );
//// == 演算 / Calculating ==
if( eventId == "" ){
throw `Event ID none.`;
}
if( !files ){
throw `File none.`;
}
const file = files.get(0);
//ファイルサイズ確認
// API仕様的には3MBまで、ファイルだけでなくペイロード全体での制限、余裕をみて2.9MBまでとする
// https://learn.microsoft.com/ja-jp/graph/api/event-post-attachments?view=graph-rest-1.0&tabs=javascript
if (file.getLength() > 3040871) {
throw `File size over.`;
}
let fileData = "";
fileRepository.readFile(file, 3145728, function(bytes) {
fileData = base64.encodeToString(bytes);
});
let requestObj = {};
requestObj['@odata.type'] = '#microsoft.graph.fileAttachment';
requestObj.name = file.getName();
requestObj.contentBytes = fileData;
//engine.log("json:" + JSON.stringify( requestObj ));
let accessLog = "";
let uri = "https://graph.microsoft.com/v1.0/me/events/" + eventId + "/attachments";
let response = httpClient.begin()
.authSetting( oauth2 )
.body( JSON.stringify( requestObj ), "application/json" )
.post( uri );
accessLog += "---POST request--- " + response.getStatusCode() + "\n";
accessLog += response.getResponseAsString() + "\n";
//// == ワークフローデータへの代入 / Data Updating ==
const status = response.getStatusCode();
if( status >= 300 ){
engine.log(accessLog);
throw `Failed in POST request. status: ${status}`;
} else {
const jsonObj = JSON.parse( response.getResponseAsString() );
const attachmentId = jsonObj.id + "";
if( dataId_attachmentId !== "" ){
engine.setDataByNumber( dataId_attachmentId, attachmentId );
}
}
if( dataId_log !== "" ){
engine.setDataByNumber( dataId_log, accessLog );
}