-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct raids endpoint to use the weekly reset instead of the daily r…
…eset
- Loading branch information
1 parent
022b545
commit 8688f60
Showing
6 changed files
with
137 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const DAY_MS = 24 * 60 * 60 * 1000 | ||
|
||
function getDateAtTime (date, time) { | ||
return new Date(date.toISOString().replace(/T.*Z/, `T${time}.000Z`)) | ||
} | ||
|
||
function getDailyReset (date) { | ||
date = date ? new Date(date) : new Date() | ||
|
||
date = new Date(date.getTime() + DAY_MS) | ||
return getDateAtTime(date, '00:00:00') | ||
} | ||
|
||
function getLastDailyReset (date) { | ||
return new Date(getDailyReset(date).getTime() - DAY_MS) | ||
} | ||
|
||
function getWeeklyReset (date) { | ||
date = date ? new Date(date) : new Date() | ||
|
||
const weekday = date.getUTCDay() | ||
const hours = date.getUTCHours() | ||
const minutes = date.getUTCMinutes() | ||
let dayDiff = 0 | ||
|
||
switch (weekday) { | ||
case 0: | ||
// 0 -> 1 sunday | ||
dayDiff = 1 | ||
break | ||
case 1: | ||
// 1 -> 0 monday (if before reset) | ||
// 1 -> 7 monday (if after reset) | ||
const pastReset = hours > 7 || (hours === 7 && minutes >= 30) | ||
dayDiff = pastReset ? 7 : 0 | ||
break | ||
default: | ||
// 2 -> 6 tuesday | ||
// 3 -> 5 wednesday | ||
// 4 -> 4 thursday | ||
// 5 -> 3 friday | ||
// 6 -> 2 saturday | ||
dayDiff = 8 - weekday | ||
break | ||
} | ||
|
||
date = new Date(date.getTime() + dayDiff * DAY_MS) | ||
return getDateAtTime(date, '07:30:00') | ||
} | ||
|
||
function getLastWeeklyReset (date) { | ||
return new Date(getWeeklyReset(date).getTime() - 7 * DAY_MS) | ||
} | ||
|
||
module.exports = { | ||
getDateAtTime, | ||
getDailyReset, | ||
getLastDailyReset, | ||
getWeeklyReset, | ||
getLastWeeklyReset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-env jest */ | ||
const mockdate = require('mockdate') | ||
const resetTime = require('../../src/helpers/resetTime') | ||
|
||
describe('helpers > resetTime', () => { | ||
it('gets a day at a specific time', () => { | ||
expect(resetTime.getDateAtTime(new Date('2017-08-13T02:04:52.278Z'), '00:01:00')) | ||
.toEqual(new Date('2017-08-13T00:01:00.000Z')) | ||
}) | ||
|
||
it('gets the next daily reset', () => { | ||
const dailyReset = (date) => { | ||
return resetTime.getDailyReset(date).toISOString() | ||
} | ||
|
||
expect(dailyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-14T00:00:00.000Z') | ||
expect(dailyReset('2017-08-11T02:04:52.278Z')).toEqual('2017-08-12T00:00:00.000Z') | ||
expect(dailyReset('2017-07-31T12:04:52.278Z')).toEqual('2017-08-01T00:00:00.000Z') | ||
|
||
expect(dailyReset('2017-08-11T23:59:59.000Z')).toEqual('2017-08-12T00:00:00.000Z') | ||
expect(dailyReset('2017-08-12T00:00:00.000Z')).toEqual('2017-08-13T00:00:00.000Z') | ||
expect(dailyReset('2017-08-12T00:00:01.000Z')).toEqual('2017-08-13T00:00:00.000Z') | ||
}) | ||
|
||
it('gets the last daily reset', () => { | ||
const lastDailyReset = (date) => { | ||
mockdate.set(date) | ||
return resetTime.getLastDailyReset().toISOString() | ||
} | ||
|
||
expect(lastDailyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-13T00:00:00.000Z') | ||
expect(lastDailyReset('2017-08-11T02:04:52.278Z')).toEqual('2017-08-11T00:00:00.000Z') | ||
expect(lastDailyReset('2017-07-31T12:04:52.278Z')).toEqual('2017-07-31T00:00:00.000Z') | ||
|
||
expect(lastDailyReset('2017-08-11T23:59:59.000Z')).toEqual('2017-08-11T00:00:00.000Z') | ||
expect(lastDailyReset('2017-08-12T00:00:00.000Z')).toEqual('2017-08-12T00:00:00.000Z') | ||
expect(lastDailyReset('2017-08-12T00:00:01.000Z')).toEqual('2017-08-12T00:00:00.000Z') | ||
}) | ||
|
||
it('gets the next weekly reset', () => { | ||
const weeklyReset = (date) => { | ||
return resetTime.getWeeklyReset(date).toISOString() | ||
} | ||
|
||
expect(weeklyReset('2017-08-10T02:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(weeklyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(weeklyReset('2017-08-14T06:04:52.278Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(weeklyReset('2017-07-31T09:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z') | ||
|
||
expect(weeklyReset('2017-08-14T07:29:59.000Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(weeklyReset('2017-08-14T07:30:00.000Z')).toEqual('2017-08-21T07:30:00.000Z') | ||
expect(weeklyReset('2017-08-14T07:30:01.000Z')).toEqual('2017-08-21T07:30:00.000Z') | ||
expect(weeklyReset('2017-08-14T07:35:00.000Z')).toEqual('2017-08-21T07:30:00.000Z') | ||
}) | ||
|
||
it('gets the last weekly reset', () => { | ||
const lastWeeklyReset = (date) => { | ||
mockdate.set(date) | ||
return resetTime.getLastWeeklyReset().toISOString() | ||
} | ||
|
||
expect(lastWeeklyReset('2017-08-10T02:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-08-13T02:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-08-14T06:04:52.278Z')).toEqual('2017-08-07T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-07-31T09:04:52.278Z')).toEqual('2017-07-31T07:30:00.000Z') | ||
|
||
expect(lastWeeklyReset('2017-08-14T07:29:59.000Z')).toEqual('2017-08-07T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-08-14T07:30:00.000Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-08-14T07:30:01.000Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
expect(lastWeeklyReset('2017-08-14T07:35:00.000Z')).toEqual('2017-08-14T07:30:00.000Z') | ||
}) | ||
}) |