Skip to content

Commit

Permalink
error handling for time tracking request
Browse files Browse the repository at this point in the history
  • Loading branch information
f.szwedo committed Aug 11, 2023
1 parent be32d7e commit 8d016d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ logger.saveLog({
message: 'App started at ' + new Date().toUTCString()
});

const job = new cron.CronJob('1/10 * 6-22 * * *', async function () {
const job = new cron.CronJob('1/30 * 6-22 * * *', async function () {
assignNewTickets(logger);
});

Expand Down
9 changes: 5 additions & 4 deletions src/controllers/timeTrackingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class TimeTrackingController {

//get the newest audit logs - with search date if it was passed to saveNewTimeTrackingEvents (so we can backfill older tickets in case of app failure)
const auditLogs = await this.getAuditLogs(eventSearchStartDate);
if (!auditLogs) return "No data to be saved!";

//filter audit logs to get the ones from the newest tracking event from mongo (or all events, if theres no data in mongo)
//we filter IF theres at least one event in db AND we dont have eventSearchStartDate (as otherwise we want to resave everything that's possible)
Expand All @@ -37,7 +38,7 @@ export default class TimeTrackingController {
if (newAuditLogs.length > 0) {
newAuditLogs.forEach(log => {
const timeSpentEvent = log.events.find(ev => ev.field_name == TICKET_CUSTOM_FIELDS.TIME_SPENT.toString());
const totalTimeSpentEvent = log.events.find(ev => ev.field_name == TICKET_CUSTOM_FIELDS.TOTAL_TIME_SPENT.toString())
const totalTimeSpentEvent = log.events.find(ev => ev.field_name == TICKET_CUSTOM_FIELDS.TOTAL_TIME_SPENT.toString());

if (timeSpentEvent && totalTimeSpentEvent) {
const timeTrackingEvent: TimeTrackingEvent = {
Expand All @@ -49,7 +50,7 @@ export default class TimeTrackingController {
ticketId: log.ticket_id,
timeSpent: parseInt(timeSpentEvent.value),
totalTimeSpent: parseInt(totalTimeSpentEvent.value)
}
};
timeTrackingEventsToSave.push(timeTrackingEvent);
}
})
Expand All @@ -63,10 +64,10 @@ export default class TimeTrackingController {
numberOfNewEventsSaved: savedEvents.success.length
};
} catch (error) {
console.log(error)
console.log(error);
}
}
return "No data to be saved!"
return "No data to be saved!";
}

refreshTimeTrackingSince = async (
Expand Down
19 changes: 12 additions & 7 deletions src/services/zendesk/getTicketAuditLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import makeZendeskRequest from "./authenticationService";
import { auditLog } from "../../models/zendeskAuditLogModel";

const getTicketAuditLogs = async (untilDate?: Date): Promise<auditLog[]> => {
let ticketAuditsResponse = await makeZendeskRequest('/api/v2/ticket_audits', 'GET');
const audits = ticketAuditsResponse.audits;
try {
let ticketAuditsResponse = await makeZendeskRequest('/api/v2/ticket_audits', 'GET');
const audits = ticketAuditsResponse.audits;

if (untilDate) {
while (untilDate < new Date(ticketAuditsResponse.audits.at(-1).created_at)) {
ticketAuditsResponse = await makeZendeskRequest(`/api/v2/ticket_audits.json?cursor=${ticketAuditsResponse.before_cursor}`, 'GET');
audits.push(...ticketAuditsResponse.audits);
if (untilDate) {
while (untilDate < new Date(ticketAuditsResponse.audits.at(-1).created_at)) {
ticketAuditsResponse = await makeZendeskRequest(`/api/v2/ticket_audits.json?cursor=${ticketAuditsResponse.before_cursor}`, 'GET');
audits.push(...ticketAuditsResponse.audits);
}
}
return audits;
} catch (error) {
console.log('Cannot get the ticket audit logs from Zendesk!');
return null;
}
return audits;
}

export default getTicketAuditLogs;
Expand Down

0 comments on commit 8d016d9

Please sign in to comment.