admin open creations query

This commit is contained in:
Moriz Wahl 2023-03-10 11:54:28 +01:00
parent ac2ce5e6b1
commit 5ac70867df
3 changed files with 28 additions and 15 deletions

View File

@ -54,4 +54,5 @@ export enum RIGHTS {
UPDATE_CONTRIBUTION_LINK = 'UPDATE_CONTRIBUTION_LINK', UPDATE_CONTRIBUTION_LINK = 'UPDATE_CONTRIBUTION_LINK',
ADMIN_CREATE_CONTRIBUTION_MESSAGE = 'ADMIN_CREATE_CONTRIBUTION_MESSAGE', ADMIN_CREATE_CONTRIBUTION_MESSAGE = 'ADMIN_CREATE_CONTRIBUTION_MESSAGE',
DENY_CONTRIBUTION = 'DENY_CONTRIBUTION', DENY_CONTRIBUTION = 'DENY_CONTRIBUTION',
ADMIN_OPEN_CREATIONS = 'ADMIN_OPEN_CREATIONS',
} }

View File

@ -27,11 +27,11 @@ import { RIGHTS } from '@/auth/RIGHTS'
import { Context, getUser, getClientTimezoneOffset } from '@/server/context' import { Context, getUser, getClientTimezoneOffset } from '@/server/context'
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
import { import {
getCreationDates,
getUserCreation, getUserCreation,
validateContribution, validateContribution,
updateCreations, updateCreations,
isValidDateString, isValidDateString,
getOpenCreations,
} from './util/creations' } from './util/creations'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { import {
@ -570,21 +570,17 @@ export class ContributionResolver {
@Authorized([RIGHTS.OPEN_CREATIONS]) @Authorized([RIGHTS.OPEN_CREATIONS])
@Query(() => [OpenCreation]) @Query(() => [OpenCreation])
async openCreations( async openCreations(@Ctx() context: Context): Promise<OpenCreation[]> {
@Arg('userId', () => Int, { nullable: true }) userId: number | null, return getOpenCreations(getUser(context).id, getClientTimezoneOffset(context))
}
@Authorized([RIGHTS.ADMIN_OPEN_CREATIONS])
@Query(() => [OpenCreation])
async adminOpenCreations(
@Arg('userId', () => Int) userId: number,
@Ctx() context: Context, @Ctx() context: Context,
): Promise<OpenCreation[]> { ): Promise<OpenCreation[]> {
const id = userId || getUser(context).id return getOpenCreations(userId, getClientTimezoneOffset(context))
const clientTimezoneOffset = getClientTimezoneOffset(context)
const creationDates = getCreationDates(clientTimezoneOffset)
const creations = await getUserCreation(id, clientTimezoneOffset)
return creationDates.map((date, index) => {
return {
month: date.getMonth(),
year: date.getFullYear(),
amount: creations[index],
}
})
} }
@Authorized([RIGHTS.DENY_CONTRIBUTION]) @Authorized([RIGHTS.DENY_CONTRIBUTION])

View File

@ -4,6 +4,7 @@ import { getConnection } from '@dbTools/typeorm'
import { Contribution } from '@entity/Contribution' import { Contribution } from '@entity/Contribution'
import Decimal from 'decimal.js-light' import Decimal from 'decimal.js-light'
import { FULL_CREATION_AVAILABLE, MAX_CREATION_AMOUNT } from '../const/const' import { FULL_CREATION_AVAILABLE, MAX_CREATION_AMOUNT } from '../const/const'
import { OpenCreation } from '@model/OpenCreation'
interface CreationMap { interface CreationMap {
id: number id: number
@ -100,7 +101,7 @@ const getCreationMonths = (timezoneOffset: number): number[] => {
return getCreationDates(timezoneOffset).map((date) => date.getMonth() + 1) return getCreationDates(timezoneOffset).map((date) => date.getMonth() + 1)
} }
export const getCreationDates = (timezoneOffset: number): Date[] => { const getCreationDates = (timezoneOffset: number): Date[] => {
const clientNow = new Date() const clientNow = new Date()
clientNow.setTime(clientNow.getTime() - timezoneOffset * 60 * 1000) clientNow.setTime(clientNow.getTime() - timezoneOffset * 60 * 1000)
logger.info( logger.info(
@ -152,3 +153,18 @@ export const updateCreations = (
export const isValidDateString = (dateString: string): boolean => { export const isValidDateString = (dateString: string): boolean => {
return new Date(dateString).toString() !== 'Invalid Date' return new Date(dateString).toString() !== 'Invalid Date'
} }
export const getOpenCreations = async (
id: number,
timezoneOffset: number,
): Promise<OpenCreation[]> => {
const creations = await getUserCreation(id, timezoneOffset)
const creationDates = getCreationDates(timezoneOffset)
return creationDates.map((date, index) => {
return {
month: date.getMonth(),
year: date.getFullYear(),
amount: creations[index],
}
})
}