From 62913898f7341e1bf1d183f7799051c14d56d582 Mon Sep 17 00:00:00 2001 From: Roland Bernard <rolbernard@unibz.it> Date: Mon, 24 May 2021 16:21:13 +0200 Subject: [PATCH] Fixed some of the time handling --- server/src/setupTests.ts | 48 ++++++++++++++++++++-------------------- server/src/v1/comment.ts | 6 ++--- server/src/v1/project.ts | 27 +++++++++++----------- server/src/v1/task.ts | 12 +++++----- server/src/v1/team.ts | 24 +++++++++++--------- server/src/v1/user.ts | 28 +++++++++++------------ server/src/v1/work.ts | 6 ++--- 7 files changed, 77 insertions(+), 74 deletions(-) diff --git a/server/src/setupTests.ts b/server/src/setupTests.ts index 8667666..52ee95a 100644 --- a/server/src/setupTests.ts +++ b/server/src/setupTests.ts @@ -83,7 +83,7 @@ async function loadTestData() { text: 'Project0 Text', color: '#00f', status: 'open', - deadline: new Date('2020-10-10'), + deadline: Date.parse('2020-10-10'), }, { id: '00000000-0000-4000-8000-000000000001', name: 'Project1', @@ -126,8 +126,8 @@ async function loadTestData() { icon: '0', status: 'open', priority: 'medium', - created: new Date('2020-10-05'), - edited: new Date('2020-10-10'), + created: Date.parse('2020-10-05'), + edited: Date.parse('2020-10-10'), }, { id: '00000000-0000-4000-8000-000000000001', project_id: '00000000-0000-4000-8000-000000000001', @@ -136,8 +136,8 @@ async function loadTestData() { icon: '1', status: 'closed', priority: 'high', - created: new Date('2020-10-10'), - edited: new Date('2020-10-15'), + created: Date.parse('2020-10-10'), + edited: Date.parse('2020-10-15'), }, { id: '00000000-0000-4000-8000-000000000002', project_id: '00000000-0000-4000-8000-000000000002', @@ -146,8 +146,8 @@ async function loadTestData() { icon: '2', status: 'open', priority: 'low', - created: new Date('2020-10-15'), - edited: new Date('2020-10-20'), + created: Date.parse('2020-10-15'), + edited: Date.parse('2020-10-20'), }, { id: '00000000-0000-4000-8000-000000000003', project_id: '00000000-0000-4000-8000-000000000002', @@ -156,8 +156,8 @@ async function loadTestData() { icon: '3', status: 'closed', priority: 'urgent', - created: new Date('2020-10-15'), - edited: new Date('2020-10-20'), + created: Date.parse('2020-10-15'), + edited: Date.parse('2020-10-20'), }, { id: '00000000-0000-4000-8000-000000000004', project_id: '00000000-0000-4000-8000-000000000002', @@ -166,8 +166,8 @@ async function loadTestData() { icon: '4', status: 'suspended', priority: 'urgent', - created: new Date('2020-10-15'), - edited: new Date('2020-10-20'), + created: Date.parse('2020-10-15'), + edited: Date.parse('2020-10-20'), }, { id: '00000000-0000-4000-8000-000000000005', project_id: '00000000-0000-4000-8000-000000000002', @@ -176,8 +176,8 @@ async function loadTestData() { icon: '5', status: 'open', priority: 'urgent', - created: new Date('2020-10-15'), - edited: new Date('2020-10-20'), + created: Date.parse('2020-10-15'), + edited: Date.parse('2020-10-20'), } ]); await database('task_requirements') @@ -222,19 +222,19 @@ async function loadTestData() { id: '00000000-0000-4000-8000-000000000000', task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000000', - started: new Date('2020-10-10T00:00:00'), - finished: new Date('2020-10-10T01:00:00'), + started: Date.parse('2020-10-10T12:00:00'), + finished: Date.parse('2020-10-10T13:00:00'), }, { id: '00000000-0000-4000-8000-000000000001', task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000000', - started: new Date('2020-10-10T03:00:00'), - finished: new Date('2020-10-10T04:00:00'), + started: Date.parse('2020-10-10T13:00:00'), + finished: Date.parse('2020-10-10T14:00:00'), }, { id: '00000000-0000-4000-8000-000000000002', task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000000', - started: new Date('2020-10-11T05:00:00'), + started: Date.parse('2020-10-11T12:00:00'), finished: null, } ]); @@ -245,22 +245,22 @@ async function loadTestData() { task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000000', text: 'Comment0', - created: new Date('2020-10-10T00:00:00'), - edited: new Date('2020-10-10T01:00:00'), + created: Date.parse('2020-10-10T00:00:00'), + edited: Date.parse('2020-10-10T01:00:00'), }, { id: '00000000-0000-4000-8000-000000000001', task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000001', text: 'Comment1', - created: new Date('2020-10-10T03:00:00'), - edited: new Date('2020-10-10T04:00:00'), + created: Date.parse('2020-10-10T03:00:00'), + edited: Date.parse('2020-10-10T04:00:00'), }, { id: '00000000-0000-4000-8000-000000000002', task_id: '00000000-0000-4000-8000-000000000005', user_id: '00000000-0000-4000-8000-000000000001', text: 'Comment2', - created: new Date('2020-10-10T05:00:00'), - edited: new Date('2020-10-10T05:00:00'), + created: Date.parse('2020-10-10T05:00:00'), + edited: Date.parse('2020-10-10T05:00:00'), } ]); } diff --git a/server/src/v1/comment.ts b/server/src/v1/comment.ts index 66fef84..4d691c9 100644 --- a/server/src/v1/comment.ts +++ b/server/src/v1/comment.ts @@ -36,8 +36,8 @@ comment.post('/', async (req, res) => { task_id: task_id, user_id: req.body.token.id, text: req.body.text, - created: new Date(), - edited: new Date(), + created: Date.now(), + edited: Date.now(), }); res.status(200).json({ status: 'success', @@ -82,7 +82,7 @@ comment.put('/:uuid', async (req, res) => { const comment = await database('comments') .update({ text: req.body.text, - edited: new Date(), + edited: Date.now(), }) .where({ 'comments.user_id': req.body.token.id, diff --git a/server/src/v1/project.ts b/server/src/v1/project.ts index 4605e66..6a33e99 100644 --- a/server/src/v1/project.ts +++ b/server/src/v1/project.ts @@ -187,7 +187,8 @@ project.get('/:uuid/work', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const work = await database({ ut: 'team_members' }) .innerJoin('team_projects', 'ut.team_id', 'team_projects.team_id') .innerJoin('tasks', 'team_projects.project_id', 'tasks.project_id') @@ -203,7 +204,8 @@ project.get('/:uuid/work', async (req, res) => { 'ut.user_id': req.body.token.id, 'team_projects.project_id': id, }) - .andWhere('workhours.started', '>=', since) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('workhours.id'); res.status(200).json({ status: 'success', @@ -227,8 +229,8 @@ project.get('/:uuid/activity', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; - const to = (req.query.to ?? Date.now()) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const activity = await database( database('team_members') .innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id') @@ -243,12 +245,12 @@ project.get('/:uuid/activity', async (req, res) => { 'team_projects.project_id': id, }) .andWhereNot({ 'workhours.finished': null }) - .andWhere('workhours.started', '>=', since) - .andWhere('workhours.started', '<=', to) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('workhours.id') ) .select({ - day: database.raw('Date(`started` / 1000, \'unixepoch\')'), + day: database.raw('Date(`started` / 1000, \'unixepoch\')'), // TODO: does not work in prostgres }) .sum({ time: database.raw('`finished` - `started`') }) .groupBy('day'); @@ -263,7 +265,6 @@ project.get('/:uuid/activity', async (req, res) => { }); } } catch (e) { - console.error(e); res.status(400).json({ status: 'error', message: 'failed get activity', @@ -275,8 +276,8 @@ project.get('/:uuid/completion', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; - const to = (req.query.to ?? Date.now()) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const completion = await database( database('team_members') .innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id') @@ -294,8 +295,8 @@ project.get('/:uuid/completion', async (req, res) => { 'team_members.user_id': req.body.token.id, 'team_projects.project_id': id, }) - .andWhere('tasks.edited', '>=', since) - .andWhere('tasks.created', '<=', to) + .andWhere('tasks.edited', '>=', since.getTime()) + .andWhere('tasks.created', '<=', to.getTime()) .groupBy('tasks.id') ) .select({ @@ -360,7 +361,7 @@ project.post('/', async (req, res) => { name: req.body.name, text: req.body.text, color: req.body.color, - deadline: req.body.deadline ? new Date(req.body.deadline) : null, + deadline: req.body.deadline ? Date.parse(req.body.deadline) : null, status: 'open', }) await transaction('team_projects').insert( diff --git a/server/src/v1/task.ts b/server/src/v1/task.ts index 6dc5bc6..8510369 100644 --- a/server/src/v1/task.ts +++ b/server/src/v1/task.ts @@ -265,7 +265,8 @@ task.get('/:uuid/work', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const work = await database({ ut: 'team_members' }) .innerJoin('team_projects', 'ut.team_id', 'team_projects.team_id') .innerJoin('tasks', 'team_projects.project_id', 'tasks.project_id') @@ -281,7 +282,8 @@ task.get('/:uuid/work', async (req, res) => { 'ut.user_id': req.body.token.id, 'tasks.id': id, }) - .andWhere('workhours.started', '>=', since) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('workhours.id'); res.status(200).json({ status: 'success', @@ -475,8 +477,8 @@ task.post('/', async (req, res) => { icon: req.body.icon, priority: req.body.priority, status: 'open', - created: new Date(), - edited: new Date(), + created: Date.now(), + edited: Date.now(), }); if (requirements.length !== 0) { await transaction('task_requirements').insert( @@ -593,7 +595,7 @@ task.put('/:uuid', async (req, res) => { icon: req.body.icon, priority: req.body.priority, status: req.body.status, - edited: new Date(), + edited: Date.now(), }) .where({ 'tasks.id': task_id, diff --git a/server/src/v1/team.ts b/server/src/v1/team.ts index c428069..a106fe7 100644 --- a/server/src/v1/team.ts +++ b/server/src/v1/team.ts @@ -314,7 +314,8 @@ team.get('/:uuid/work', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const work = await database({ ut: 'team_members' }) .innerJoin('team_members', 'ut.team_id', 'team_members.team_id') .innerJoin('workhours', 'team_members.user_id', 'workhours.user_id') @@ -329,7 +330,8 @@ team.get('/:uuid/work', async (req, res) => { 'ut.user_id': req.body.token.id, 'ut.team_id': id, }) - .andWhere('workhours.started', '>=', since) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('workhours.id'); res.status(200).json({ status: 'success', @@ -353,13 +355,13 @@ team.get('/:uuid/activity', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; - const to = (req.query.to ?? Date.now()) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const activity = await database({ ut: 'team_members' }) .innerJoin('team_members', 'ut.team_id', 'team_members.team_id') .innerJoin('workhours', 'team_members.user_id', 'workhours.user_id') .select({ - day: database.raw('Date(`workhours`.`started` / 1000, \'unixepoch\')'), + day: database.raw('Date(`workhours`.`started` / 1000, \'unixepoch\')'), // TODO: does not work in postgres }) .sum({ time: database.raw('`workhours`.`finished` - `workhours`.`started`') }) .where({ @@ -367,8 +369,8 @@ team.get('/:uuid/activity', async (req, res) => { 'ut.team_id': id, }) .andWhereNot({ 'workhours.finished': null }) - .andWhere('workhours.started', '>=', since) - .andWhere('workhours.started', '<=', to) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('day'); res.status(200).json({ status: 'success', @@ -392,8 +394,8 @@ team.get('/:uuid/completion', async (req, res) => { try { const id = req.params.uuid; if (validate(id)) { - const since = (req.query.since ?? 0) as number; - const to = (req.query.to ?? Date.now()) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const completion = await database( database('team_members') .innerJoin('team_projects', 'team_members.team_id', 'team_projects.team_id') @@ -411,8 +413,8 @@ team.get('/:uuid/completion', async (req, res) => { 'team_members.user_id': req.body.token.id, 'team_members.team_id': id, }) - .andWhere('tasks.edited', '>=', since) - .andWhere('tasks.created', '<=', to) + .andWhere('tasks.edited', '>=', since.getTime()) + .andWhere('tasks.created', '<=', to.getTime()) .groupBy('tasks.id') ) .select({ diff --git a/server/src/v1/user.ts b/server/src/v1/user.ts index 5fe655e..43e9244 100644 --- a/server/src/v1/user.ts +++ b/server/src/v1/user.ts @@ -32,7 +32,6 @@ user.get('/name/:username', async (req, res) => { }); } } catch (e) { - console.log(e); res.status(400).json({ status: 'error', message: 'failed get user', @@ -160,8 +159,8 @@ user.get('/tasks', async (req, res) => { user.get('/work', async (req, res) => { try { - const since = parseInt(req.query.since as string ?? 0); - const to = parseInt(req.query.to as string ?? Date.now()); + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const work = await database('workhours') .select({ id: 'workhours.id', @@ -173,8 +172,8 @@ user.get('/work', async (req, res) => { .where({ 'workhours.user_id': req.body.token.id, }) - .andWhere('workhours.started', '>=', since) - .andWhere('workhours.started', '<=', to) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('workhours.id'); res.status(200).json({ status: 'success', @@ -190,26 +189,25 @@ user.get('/work', async (req, res) => { user.get('/activity', async (req, res) => { try { - const since = parseInt(req.query.since as string ?? 0); - const to = parseInt(req.query.to as string ?? Date.now()); + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const activity = await database('workhours') .select({ - day: database.raw('Date(`started` / 1000, \'unixepoch\')'), + day: database.raw('Date(`started` / 1000, \'unixepoch\')'), // TODO: This does not work in postgres }) .sum({ time: database.raw('`finished` - `started`') }) .where({ 'workhours.user_id': req.body.token.id, }) .andWhereNot({ 'workhours.finished': null }) - .andWhere('workhours.started', '>=', since) - .andWhere('workhours.started', '<=', to) + .andWhere('workhours.started', '>=', since.getTime()) + .andWhere('workhours.started', '<=', to.getTime()) .groupBy('day'); res.status(200).json({ status: 'success', activity: activity, }); } catch (e) { - console.error(e); res.status(400).json({ status: 'error', message: 'failed get activity', @@ -219,8 +217,8 @@ user.get('/activity', async (req, res) => { user.get('/completion', async (req, res) => { try { - const since = (req.query.since ?? 0) as number; - const to = (req.query.to ?? Date.now()) as number; + const since = new Date(parseInt(req.query.since as string ?? 0)); + const to = new Date(parseInt(req.query.to as string ?? Date.now())); const completion = await database( database('task_assignees') .leftJoin('tasks', 'task_assignees.task_id', 'tasks.id') @@ -236,8 +234,8 @@ user.get('/completion', async (req, res) => { .where({ 'task_assignees.user_id': req.body.token.id, }) - .andWhere('tasks.edited', '>=', since) - .andWhere('tasks.created', '<=', to) + .andWhere('tasks.edited', '>=', since.getTime()) + .andWhere('tasks.created', '<=', to.getTime()) .groupBy('tasks.id') ) .select({ diff --git a/server/src/v1/work.ts b/server/src/v1/work.ts index 65e3223..aae5946 100644 --- a/server/src/v1/work.ts +++ b/server/src/v1/work.ts @@ -33,7 +33,7 @@ work.post('/start', async (req, res) => { await database.transaction(async transaction => { await transaction('workhours') .update({ - finished: new Date(), + finished: Date.now(), }) .where({ user_id: req.body.token.id, @@ -44,7 +44,7 @@ work.post('/start', async (req, res) => { id: work_id, task_id: task_id, user_id: req.body.token.id, - started: new Date(), + started: Date.now(), finished: null, }); }); @@ -82,7 +82,7 @@ work.put('/finish', async (req, res) => { try { const work = await database('workhours') .update({ - finished: new Date(), + finished: Date.now(), }) .where({ user_id: req.body.token.id, -- GitLab