I am currently in the process of developing a function that calls two other functions. Function 1 is responsible for saving an object to a database, while function 2 performs an API call.
async createMSCalendarEntry(start: Date, end: Date, name: string, userID: number, email: string) {
const api_url = `https://graph.microsoft.com/v1.0/users/${vacationEmailforCalendar}/calendar/events`
let graphresponse = await msRestNodeAuth.loginWithServicePrincipalSecret(`${microsoftClientIDforCalendar}`, `${microsoftSecretforCalendar}`, `${microsoftTenantIdforCalendar}`, { tokenAudience: "https://graph.microsoft.com/" })
let token = await graphresponse.getToken()
let newEvent = new Event(start.toISOString(), end.toISOString(), name, userID, email)
var options = {
url: api_url,
json: newEvent.toGraphSchema(),
method: "POST",
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + token.accessToken
}
}
let graphResponse = await this.doRequest(options)
return (graphResponse)
}
async doRequest(options) {
return new Promise(function (resolve, reject) {
request(options, function (error, body) {
if (error) {
reject(error);
}
else {
resolve(body);
}
});
});
}
The DB action is as follows:
await this.dataService.TimesRepo.save(vacationDays)
This is just a snippet of the function that calls the other two:
await this.createMSCalendarEntry(dates.start, dates.end, currentUser.FirstName, currentUser.Id, currentUser.Email)
await this.dataService.TimesRepo.save(vacationDays)
let responseMessage: string = (this.createMSCalendarEntry && this.dataService.TimesRepo.save) ? 'Vacation submitted' : 'Vacation could not be submitted'
res.status(200).send({
message: responseMessage,
data: vacationDays
})
return
}
else {
res.status(400).send("The selected vacation days are in an already planned vacation time!")
return
}
I need help creating a better check for ensuring the two functions were executed successfully, rather than just checking if they exist. Thanks, Leonidas