diff --git a/src/api/timesheet/controllers/timesheet.js b/src/api/timesheet/controllers/timesheet.js index 7d82ef4..86482b2 100644 --- a/src/api/timesheet/controllers/timesheet.js +++ b/src/api/timesheet/controllers/timesheet.js @@ -1,61 +1,61 @@ -"use strict"; +'use strict' /** - * A set of functions called "actions" for `timesheet` + * A set of functions called 'actions' for `timesheet` */ -const service = require("../services/timesheet"); +const service = require('../services/timesheet') module.exports = { find: async (ctx, next) => { try { - ctx.body = await service.find(); + ctx.body = await service.find(ctx.request.query) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, findOne: async (ctx, next) => { try { - ctx.body = await service.findOne(ctx.params.id); + ctx.body = await service.findOne(ctx.params.id, ctx.request.query) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, project: async (ctx, next) => { try { - let period = ctx.request.headers["period"]; - ctx.body = await service.findProject(ctx.params.id, period); + let period = ctx.request.headers['period'] + ctx.body = await service.findProject(ctx.params.id, period) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, allocated: async (ctx, next) => { try { - let period = ctx.request.headers["period"]; - ctx.body = await service.findAllocatedTime(period); + let period = ctx.request.headers['period'] + ctx.body = await service.findAllocatedTime(period) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, leave: async (ctx, next) => { try { - ctx.body = await service.findLeave(); + ctx.body = await service.findLeave(ctx.request.query) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, user: async (ctx, next) => { try { - let period = ctx.request.headers["period"]; - ctx.body = await service.findUser(ctx.params.id, period); + let period = ctx.request.headers['period'] + ctx.body = await service.findUser(ctx.params.id, period) } catch (err) { - ctx.body = err; - console.error(err); + ctx.body = err + console.error(err) } }, -}; +} diff --git a/src/api/timesheet/services/timesheet.js b/src/api/timesheet/services/timesheet.js index b04d101..dc0ad0c 100644 --- a/src/api/timesheet/services/timesheet.js +++ b/src/api/timesheet/services/timesheet.js @@ -1,6 +1,6 @@ "use strict"; -const { DateTime } = require("luxon"); +const { DateTime, Interval } = require("luxon"); /** * timesheet service. @@ -302,15 +302,27 @@ const dateHelper = (month) => { module.exports = { async find(...args) { + const query = args[0] + const currentDate = DateTime.utc() - let startDate = DateTime.utc(currentDate.year, 8), - endDate = startDate.plus({ year: 1 }) + let startDate, + endDate - if(currentDate.month < 8) { - startDate = startDate.minus({ year: 1 }), - endDate = endDate.minus({ year: 1 }) + // Load timesheets of provided year + if(query.filters.year.$eq) { + startDate = DateTime.utc(Number(query.filters.year.$eq), 8) + } + // Is after december of the current financial year + else if(currentDate.month < 8) { + startDate = DateTime.utc(currentDate.year - 1, 8) } + // Is before december of the current financial year + else { + startDate = DateTime.utc(currentDate.year, 8) + } + + endDate = startDate.plus({ year: 1 }) const payload = { dateRangeStart: startDate.toISO(), @@ -378,18 +390,30 @@ module.exports = { } }, - async findOne(userID) { + async findOne(userID, ...args) { + + const query = args[0] const currentDate = DateTime.utc() - let startDate = DateTime.utc(currentDate.year, 8), - endDate = startDate.plus({ year: 1 }) + let startDate, + endDate - if(currentDate.month < 8) { - startDate = startDate.minus({ year: 1 }), - endDate = endDate.minus({ year: 1 }) + // Load timesheets of provided year + if(query.filters.year.$eq) { + startDate = DateTime.utc(Number(query.filters.year.$eq), 8) + } + // Is after december of the current financial year + else if(currentDate.month < 8) { + startDate = DateTime.utc(currentDate.year - 1, 8) + } + // Is before december of the current financial year + else { + startDate = DateTime.utc(currentDate.year, 8) } + endDate = startDate.plus({ year: 1 }) + const payload = { dateRangeStart: startDate.toISO(), dateRangeEnd: endDate.toISO(), @@ -563,10 +587,54 @@ module.exports = { } }, - async findLeave() { + async findLeave(...args) { + + const query = args[0] + + const currentDate = DateTime.utc() + + let startDate, + endDate + + // Load leave of provided year + if(query.filters.year.$eq) { + startDate = DateTime.utc(Number(query.filters.year.$eq), 8) + } + // Is after december of the current financial year + else if(currentDate.month < 8) { + startDate = DateTime.utc(currentDate.year - 1, 8) + } + // Is before december of the current financial year + else { + startDate = DateTime.utc(currentDate.year, 8) + } + + endDate = startDate.plus({ year: 1 }) + + const period = Interval.fromDateTimes(startDate.startOf('day'), endDate.endOf('day')) + try { - let response = await axios.get(`/turner`, leaveConfig) - return { data: response.data } + + // Due to the FY not being the same as the leave year, get the previous year too and combine the two + const [response1, response2] = await Promise.all([ + axios.get(`/turner?YEAR=${startDate.year}-${endDate.year}`, leaveConfig), + axios.get(`/turner?YEAR=${(startDate.year-1)}-${(endDate.year -1)}`, leaveConfig) + ]) + + const response = [...response1.data, ...response2.data] + + const FYleave = [] + + // Include the leave that is within the FY period + response.forEach(leave => { + if (period.contains(DateTime.fromISO(leave.DATE))) { + FYleave.push(leave) + } + }) + + return { + data: FYleave + } } catch(ex) { console.error(ex)