utils#getUnixBeforeDays JavaScript Examples

The following examples show how to use utils#getUnixBeforeDays. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: workflows.js    From holo-schedule with MIT License 6 votes vote down vote up
syncEndedLives = async () => {
  const cashedLives = getCachedEndedLives() ?? []
  const startBefore = cashedLives.length ? Math.min(
    ...cashedLives.map(({ start_at: startAt }) => getUnix(startAt)),
  ) + 1 : getUnix()

  const lives = filterLives(await getEndedLives({
    membersMask: getMembersMask(getSubscriptionByMember()),
    startAfter: getUnixBeforeDays(3),
    startBefore,
    limit: 25,
  }))

  await Promise.resolve({
    [ENDED_LIVES]: limitRight(uniqRightBy([...reverse(lives), ...cashedLives], 'id'), MAX_LIVES_LENGTH),
  }).then(data => store.set(data))
    .catch(data => store.set(data, { local: false }))

  return getCachedEndedLives()
}
Example #2
Source File: workflows.test.js    From holo-schedule with MIT License 5 votes vote down vote up
test('should sync ended lives', async () => {
  Date.now = jest.fn(() => unixTime * 1000)
  // First run
  const endedLivesOne = [
    { id: 10, start_at: dayjs().subtract(3, 'hour').toISOString() },
    { id: 9, start_at: dayjs().subtract(4, 'hour').toISOString() },
  ]
  const returnValueExpectedOne = [endedLivesOne[1], endedLivesOne[0]]

  getEndedLives.mockResolvedValueOnce(endedLivesOne)

  const returnValueOne = await workflows.syncEndedLives()

  expect(getEndedLives).toHaveBeenCalledWith({
    startAfter: getUnixBeforeDays(3),
    startBefore: getUnix(),
    limit: 25,
  })
  expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedOne)
  expect(returnValueOne).toEqual(returnValueExpectedOne)
  // Second run
  getEndedLives.mockClear()
  const endedLivesTwo = [
    { id: 9, start_at: dayjs().subtract(4, 'hour').toISOString() },
    { id: 8, start_at: dayjs().subtract(5, 'hour').toISOString() },
  ]
  const returnValueExpectedTwo = [endedLivesTwo[1], ...returnValueExpectedOne]

  getEndedLives.mockResolvedValueOnce(endedLivesTwo)

  const returnValueTwo = await workflows.syncEndedLives()

  expect(getEndedLives).toHaveBeenCalledWith({
    startAfter: getUnixBeforeDays(3),
    startBefore: getUnix(returnValueExpectedOne[0].start_at) + 1,
    limit: 25,
  })
  expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedTwo)
  expect(returnValueTwo).toEqual(returnValueExpectedTwo)
  // Third run
  getEndedLives.mockClear()
  const endedLivesThree = []
  const returnValueExpectedThree = returnValueExpectedTwo

  getEndedLives.mockResolvedValueOnce(endedLivesThree)

  const returnValueThree = await workflows.syncEndedLives()

  expect(getEndedLives).toHaveBeenCalledWith({
    startAfter: getUnixBeforeDays(3),
    startBefore: getUnix(returnValueExpectedTwo[0].start_at) + 1,
    limit: 25,
  })
  expect(store.data[ENDED_LIVES]).toEqual(returnValueExpectedThree)
  expect(returnValueThree).toEqual(returnValueExpectedThree)
})