Skip to content

Commit

Permalink
add support for year query parameter to timesheets and leave
Browse files Browse the repository at this point in the history
  • Loading branch information
markdturner committed Aug 21, 2024
1 parent 6c607c8 commit c09bffe
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 40 deletions.
50 changes: 25 additions & 25 deletions src/api/timesheet/controllers/timesheet.js
Original file line number Diff line number Diff line change
@@ -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)
}
},
};
}
98 changes: 83 additions & 15 deletions src/api/timesheet/services/timesheet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const { DateTime } = require("luxon");
const { DateTime, Interval } = require("luxon");

/**
* timesheet service.
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c09bffe

Please sign in to comment.