mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
have timezoneOffset as optional parameter
This commit is contained in:
parent
2b27e6aaa8
commit
37e9482807
@ -1,15 +1,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import {
|
import { testEnvironment, resetToken, cleanDB, contributionDateFormatter } from '@test/helpers'
|
||||||
testEnvironment,
|
|
||||||
resetToken,
|
|
||||||
cleanDB,
|
|
||||||
contributionDateFormatter,
|
|
||||||
setClientRequestTime,
|
|
||||||
toJSTzone,
|
|
||||||
toPSTzone,
|
|
||||||
} from '@test/helpers'
|
|
||||||
import { logger } from '@test/testSetup'
|
import { logger } from '@test/testSetup'
|
||||||
import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg'
|
import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg'
|
||||||
import { peterLustig } from '@/seeds/users/peter-lustig'
|
import { peterLustig } from '@/seeds/users/peter-lustig'
|
||||||
@ -17,7 +9,7 @@ import { User } from '@entity/User'
|
|||||||
import { Contribution } from '@entity/Contribution'
|
import { Contribution } from '@entity/Contribution'
|
||||||
import { userFactory } from '@/seeds/factory/user'
|
import { userFactory } from '@/seeds/factory/user'
|
||||||
import { login, createContribution, adminCreateContribution } from '@/seeds/graphql/mutations'
|
import { login, createContribution, adminCreateContribution } from '@/seeds/graphql/mutations'
|
||||||
import { getUserCreation } from './creations'
|
import { getUserCreation, validateContribution } from './creations'
|
||||||
|
|
||||||
let mutate: any, query: any, con: any
|
let mutate: any, query: any, con: any
|
||||||
let testEnv: any
|
let testEnv: any
|
||||||
@ -52,7 +44,6 @@ describe('util/creation', () => {
|
|||||||
|
|
||||||
describe('getUserCreations', () => {
|
describe('getUserCreations', () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
setClientRequestTime(now.toString())
|
|
||||||
await mutate({
|
await mutate({
|
||||||
mutation: login,
|
mutation: login,
|
||||||
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
variables: { email: 'peter@lustig.de', password: 'Aa12345_' },
|
||||||
@ -117,7 +108,6 @@ describe('util/creation', () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
contributionDate: setZeroHours(now),
|
contributionDate: setZeroHours(now),
|
||||||
clientRequestTime: now.toString(),
|
|
||||||
amount: expect.decimalEqual(250),
|
amount: expect.decimalEqual(250),
|
||||||
memo: 'Admin contribution for this month',
|
memo: 'Admin contribution for this month',
|
||||||
moderatorId: admin.id,
|
moderatorId: admin.id,
|
||||||
@ -129,7 +119,6 @@ describe('util/creation', () => {
|
|||||||
contributionDate: setZeroHours(
|
contributionDate: setZeroHours(
|
||||||
new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()),
|
new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()),
|
||||||
),
|
),
|
||||||
clientRequestTime: now.toString(),
|
|
||||||
amount: expect.decimalEqual(160),
|
amount: expect.decimalEqual(160),
|
||||||
memo: 'Admin contribution for the last month',
|
memo: 'Admin contribution for the last month',
|
||||||
moderatorId: admin.id,
|
moderatorId: admin.id,
|
||||||
@ -141,7 +130,6 @@ describe('util/creation', () => {
|
|||||||
contributionDate: setZeroHours(
|
contributionDate: setZeroHours(
|
||||||
new Date(now.getFullYear(), now.getMonth() - 2, now.getDate()),
|
new Date(now.getFullYear(), now.getMonth() - 2, now.getDate()),
|
||||||
),
|
),
|
||||||
clientRequestTime: now.toString(),
|
|
||||||
amount: expect.decimalEqual(450),
|
amount: expect.decimalEqual(450),
|
||||||
memo: 'Admin contribution for two months ago',
|
memo: 'Admin contribution for two months ago',
|
||||||
moderatorId: admin.id,
|
moderatorId: admin.id,
|
||||||
@ -151,7 +139,6 @@ describe('util/creation', () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
contributionDate: setZeroHours(now),
|
contributionDate: setZeroHours(now),
|
||||||
clientRequestTime: now.toString(),
|
|
||||||
amount: expect.decimalEqual(400),
|
amount: expect.decimalEqual(400),
|
||||||
memo: 'Contribution for this month',
|
memo: 'Contribution for this month',
|
||||||
moderatorId: null,
|
moderatorId: null,
|
||||||
@ -163,7 +150,6 @@ describe('util/creation', () => {
|
|||||||
contributionDate: setZeroHours(
|
contributionDate: setZeroHours(
|
||||||
new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()),
|
new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()),
|
||||||
),
|
),
|
||||||
clientRequestTime: now.toString(),
|
|
||||||
amount: expect.decimalEqual(500),
|
amount: expect.decimalEqual(500),
|
||||||
memo: 'Contribution for the last month',
|
memo: 'Contribution for the last month',
|
||||||
moderatorId: null,
|
moderatorId: null,
|
||||||
@ -175,7 +161,7 @@ describe('util/creation', () => {
|
|||||||
|
|
||||||
describe('call getUserCreation now', () => {
|
describe('call getUserCreation now', () => {
|
||||||
it('returns the expected open contributions', async () => {
|
it('returns the expected open contributions', async () => {
|
||||||
await expect(getUserCreation(user.id, now.toString())).resolves.toEqual([
|
await expect(getUserCreation(user.id)).resolves.toEqual([
|
||||||
expect.decimalEqual(550),
|
expect.decimalEqual(550),
|
||||||
expect.decimalEqual(340),
|
expect.decimalEqual(340),
|
||||||
expect.decimalEqual(350),
|
expect.decimalEqual(350),
|
||||||
@ -192,6 +178,10 @@ describe('util/creation', () => {
|
|||||||
jest.runAllTimers()
|
jest.runAllTimers()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
jest.useRealTimers()
|
||||||
|
})
|
||||||
|
|
||||||
it('has the clock set correctly', () => {
|
it('has the clock set correctly', () => {
|
||||||
expect(new Date().toISOString()).toContain(
|
expect(new Date().toISOString()).toContain(
|
||||||
`${targetDate.getFullYear()}-${targetDate.getMonth() + 1}-${targetDate.getDate()}T23:`,
|
`${targetDate.getFullYear()}-${targetDate.getMonth() + 1}-${targetDate.getDate()}T23:`,
|
||||||
@ -199,12 +189,8 @@ describe('util/creation', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('call getUserCreation with UTC', () => {
|
describe('call getUserCreation with UTC', () => {
|
||||||
beforeAll(() => {
|
|
||||||
setClientRequestTime(targetDate.toString())
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns the expected open contributions', async () => {
|
it('returns the expected open contributions', async () => {
|
||||||
await expect(getUserCreation(user.id, now.toString())).resolves.toEqual([
|
await expect(getUserCreation(user.id)).resolves.toEqual([
|
||||||
expect.decimalEqual(550),
|
expect.decimalEqual(550),
|
||||||
expect.decimalEqual(340),
|
expect.decimalEqual(340),
|
||||||
expect.decimalEqual(350),
|
expect.decimalEqual(350),
|
||||||
@ -213,14 +199,8 @@ describe('util/creation', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('call getUserCreation with JST (GMT+0900)', () => {
|
describe('call getUserCreation with JST (GMT+0900)', () => {
|
||||||
beforeAll(() => {
|
|
||||||
setClientRequestTime(toJSTzone(targetDate.toString()))
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns the expected open contributions', async () => {
|
it('returns the expected open contributions', async () => {
|
||||||
await expect(
|
await expect(getUserCreation(user.id, true, -540)).resolves.toEqual([
|
||||||
getUserCreation(user.id, toJSTzone(targetDate.toString())),
|
|
||||||
).resolves.toEqual([
|
|
||||||
expect.decimalEqual(340),
|
expect.decimalEqual(340),
|
||||||
expect.decimalEqual(350),
|
expect.decimalEqual(350),
|
||||||
expect.decimalEqual(1000),
|
expect.decimalEqual(1000),
|
||||||
@ -228,8 +208,61 @@ describe('util/creation', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
describe('call getUserCreation with PST (GMT-0800)', () => {
|
||||||
jest.useRealTimers()
|
it('returns the expected open contributions', async () => {
|
||||||
|
await expect(getUserCreation(user.id, true, 450)).resolves.toEqual([
|
||||||
|
expect.decimalEqual(550),
|
||||||
|
expect.decimalEqual(340),
|
||||||
|
expect.decimalEqual(350),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('run two hours forward to be in the next month in UTC', () => {
|
||||||
|
const nextMonthTargetDate = new Date()
|
||||||
|
nextMonthTargetDate.setTime(targetDate.getTime() + 2 * 60 * 60 * 1000)
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
|
||||||
|
setTimeout(() => {}, 2 * 60 * 60 * 1000)
|
||||||
|
jest.runAllTimers()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('has the clock set correctly', () => {
|
||||||
|
expect(new Date().toISOString()).toContain(
|
||||||
|
`${nextMonthTargetDate.getFullYear()}-${nextMonthTargetDate.getMonth() + 1}-01T01:`,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('call getUserCreation with UTC', () => {
|
||||||
|
it('returns the expected open contributions', async () => {
|
||||||
|
await expect(getUserCreation(user.id, true, -540)).resolves.toEqual([
|
||||||
|
expect.decimalEqual(340),
|
||||||
|
expect.decimalEqual(350),
|
||||||
|
expect.decimalEqual(1000),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('call getUserCreation with JST (GMT+0900)', () => {
|
||||||
|
it('returns the expected open contributions', async () => {
|
||||||
|
await expect(getUserCreation(user.id, true, -540)).resolves.toEqual([
|
||||||
|
expect.decimalEqual(340),
|
||||||
|
expect.decimalEqual(350),
|
||||||
|
expect.decimalEqual(1000),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('call getUserCreation with PST (GMT-0800)', () => {
|
||||||
|
it('returns the expected open contributions', async () => {
|
||||||
|
await expect(getUserCreation(user.id, true, 450)).resolves.toEqual([
|
||||||
|
expect.decimalEqual(550),
|
||||||
|
expect.decimalEqual(340),
|
||||||
|
expect.decimalEqual(350),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -13,9 +13,10 @@ export const validateContribution = (
|
|||||||
creations: Decimal[],
|
creations: Decimal[],
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
creationDate: Date,
|
creationDate: Date,
|
||||||
|
timezoneOffset = 0,
|
||||||
): void => {
|
): void => {
|
||||||
logger.trace('isContributionValid: ', creations, amount, creationDate)
|
logger.trace('isContributionValid: ', creations, amount, creationDate)
|
||||||
const index = getCreationIndex(creationDate.getMonth())
|
const index = getCreationIndex(creationDate.getMonth(), timezoneOffset)
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
logger.error(
|
logger.error(
|
||||||
@ -38,9 +39,10 @@ export const validateContribution = (
|
|||||||
export const getUserCreations = async (
|
export const getUserCreations = async (
|
||||||
ids: number[],
|
ids: number[],
|
||||||
includePending = true,
|
includePending = true,
|
||||||
|
timezoneOffset = 0,
|
||||||
): Promise<CreationMap[]> => {
|
): Promise<CreationMap[]> => {
|
||||||
logger.trace('getUserCreations:', ids, includePending)
|
logger.trace('getUserCreations:', ids, includePending)
|
||||||
const months = getCreationMonths()
|
const months = getCreationMonths(timezoneOffset)
|
||||||
logger.trace('getUserCreations months', months)
|
logger.trace('getUserCreations months', months)
|
||||||
|
|
||||||
const queryRunner = getConnection().createQueryRunner()
|
const queryRunner = getConnection().createQueryRunner()
|
||||||
@ -87,15 +89,20 @@ export const getUserCreations = async (
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getUserCreation = async (id: number, includePending = true): Promise<Decimal[]> => {
|
export const getUserCreation = async (
|
||||||
logger.trace('getUserCreation', id, includePending)
|
id: number,
|
||||||
const creations = await getUserCreations([id], includePending)
|
includePending = true,
|
||||||
|
timezoneOffset = 0,
|
||||||
|
): Promise<Decimal[]> => {
|
||||||
|
logger.trace('getUserCreation', id, includePending, timezoneOffset)
|
||||||
|
const creations = await getUserCreations([id], includePending, timezoneOffset)
|
||||||
logger.trace('getUserCreation creations=', creations)
|
logger.trace('getUserCreation creations=', creations)
|
||||||
return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE
|
return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getCreationMonths = (): number[] => {
|
const getCreationMonths = (timezoneOffset: number): number[] => {
|
||||||
const now = new Date(Date.now())
|
const now = new Date()
|
||||||
|
now.setTime(now.getTime() - timezoneOffset * 60 * 1000)
|
||||||
return [
|
return [
|
||||||
now.getMonth() + 1,
|
now.getMonth() + 1,
|
||||||
new Date(now.getFullYear(), now.getMonth() - 1, 1).getMonth() + 1,
|
new Date(now.getFullYear(), now.getMonth() - 1, 1).getMonth() + 1,
|
||||||
@ -103,8 +110,8 @@ export const getCreationMonths = (): number[] => {
|
|||||||
].reverse()
|
].reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getCreationIndex = (month: number): number => {
|
const getCreationIndex = (month: number, timezoneOffset: number): number => {
|
||||||
return getCreationMonths().findIndex((el) => el === month + 1)
|
return getCreationMonths(timezoneOffset).findIndex((el) => el === month + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isStartEndDateValid = (
|
export const isStartEndDateValid = (
|
||||||
@ -128,8 +135,12 @@ export const isStartEndDateValid = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateCreations = (creations: Decimal[], contribution: Contribution): Decimal[] => {
|
export const updateCreations = (
|
||||||
const index = getCreationIndex(contribution.contributionDate.getMonth())
|
creations: Decimal[],
|
||||||
|
contribution: Contribution,
|
||||||
|
timezoneOffset = 0,
|
||||||
|
): Decimal[] => {
|
||||||
|
const index = getCreationIndex(contribution.contributionDate.getMonth(), timezoneOffset)
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
throw new Error('You cannot create GDD for a month older than the last three months.')
|
throw new Error('You cannot create GDD for a month older than the last three months.')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user