diff --git a/modules/worker/back/methods/worker-time-control/sendMail.js b/modules/worker/back/methods/worker-time-control/sendMail.js index 229e4d323..5fd5f65ff 100644 --- a/modules/worker/back/methods/worker-time-control/sendMail.js +++ b/modules/worker/back/methods/worker-time-control/sendMail.js @@ -41,21 +41,10 @@ module.exports = Self => { const stmts = []; let stmt; - if (!args.week || !args.year) { - const from = Date.vnNew(); - const to = Date.vnNew(); + const {date: started, week, year} = Self.getMondayWeekYear(args.week, args.year); + args.week = week; + args.year = year; - const [time] = await models.Time.find({ - where: { - dated: {between: [from.setDate(from.getDate() - 10), to.setDate(to.getDate() - 4)]} - }, - order: 'week ASC' - }, myOptions); - - args.week = time.week; - args.year = time.year; - } - const started = Self.getStartDateOfWeekNumber(args.week, args.year); started.setHours(0, 0, 0, 0); const ended = new Date(started); diff --git a/modules/worker/back/methods/worker-time-control/weeklyHourRecordEmail.js b/modules/worker/back/methods/worker-time-control/weeklyHourRecordEmail.js index 51573c1d6..eadbecb00 100644 --- a/modules/worker/back/methods/worker-time-control/weeklyHourRecordEmail.js +++ b/modules/worker/back/methods/worker-time-control/weeklyHourRecordEmail.js @@ -55,8 +55,8 @@ module.exports = Self => { } }, myOptions); - const dated = Self.getStartDateOfWeekNumber(args.week, args.year); - const timestamp = dated.getTime() / 1000; + const {date} = Self.getMondayWeekYear(args.week, args.year); + const timestamp = date.getTime() / 1000; const url = `${salix.url}worker/${args.workerId}/time-control?timestamp=${timestamp}`; ctx.args.url = url; diff --git a/modules/worker/back/models/worker-time-control.js b/modules/worker/back/models/worker-time-control.js index eab061b0e..cdf51b712 100644 --- a/modules/worker/back/models/worker-time-control.js +++ b/modules/worker/back/models/worker-time-control.js @@ -1,4 +1,5 @@ const UserError = require('vn-loopback/util/user-error'); +const moment = require('moment'); module.exports = Self => { require('../methods/worker-time-control/filter')(Self); @@ -20,14 +21,14 @@ module.exports = Self => { return err; }); - Self.getStartDateOfWeekNumber = (week, year) => { - const simple = new Date(year, 0, 1 + (week - 1) * 7); - const dow = simple.getDay(); - const weekStart = simple; - if (dow <= 4) - weekStart.setDate(simple.getDate() - simple.getDay() + 1); - else - weekStart.setDate(simple.getDate() + 8 - simple.getDay()); - return weekStart; + Self.getMondayWeekYear = (week, year) => { + if (!week || !year) { + const today = Date.vnNew(); + today.setDate(today.getDate() - 7); + week = moment(today).isoWeek(); + year = moment(today).isoWeekYear(); + } + const date = moment(year, 'YYYY').week(week).startOf('isoweek').toDate(); + return {date, year, week}; }; };