diff --git a/backend/src/auth/RIGHTS.ts b/backend/src/auth/RIGHTS.ts index 8b0e82c86..22be48e40 100644 --- a/backend/src/auth/RIGHTS.ts +++ b/backend/src/auth/RIGHTS.ts @@ -54,4 +54,5 @@ export enum RIGHTS { UPDATE_CONTRIBUTION_LINK = 'UPDATE_CONTRIBUTION_LINK', ADMIN_CREATE_CONTRIBUTION_MESSAGE = 'ADMIN_CREATE_CONTRIBUTION_MESSAGE', DENY_CONTRIBUTION = 'DENY_CONTRIBUTION', + ADMIN_OPEN_CREATIONS = 'ADMIN_OPEN_CREATIONS', } diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index 561d5c88c..49ab5fd15 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -27,11 +27,11 @@ import { RIGHTS } from '@/auth/RIGHTS' import { Context, getUser, getClientTimezoneOffset } from '@/server/context' import { backendLogger as logger } from '@/server/logger' import { - getCreationDates, getUserCreation, validateContribution, updateCreations, isValidDateString, + getOpenCreations, } from './util/creations' import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' import { @@ -570,21 +570,17 @@ export class ContributionResolver { @Authorized([RIGHTS.OPEN_CREATIONS]) @Query(() => [OpenCreation]) - async openCreations( - @Arg('userId', () => Int, { nullable: true }) userId: number | null, + async openCreations(@Ctx() context: Context): Promise { + return getOpenCreations(getUser(context).id, getClientTimezoneOffset(context)) + } + + @Authorized([RIGHTS.ADMIN_OPEN_CREATIONS]) + @Query(() => [OpenCreation]) + async adminOpenCreations( + @Arg('userId', () => Int) userId: number, @Ctx() context: Context, ): Promise { - const id = userId || getUser(context).id - 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], - } - }) + return getOpenCreations(userId, getClientTimezoneOffset(context)) } @Authorized([RIGHTS.DENY_CONTRIBUTION]) diff --git a/backend/src/graphql/resolver/util/creations.ts b/backend/src/graphql/resolver/util/creations.ts index b9ba2e69f..8453781ca 100644 --- a/backend/src/graphql/resolver/util/creations.ts +++ b/backend/src/graphql/resolver/util/creations.ts @@ -4,6 +4,7 @@ import { getConnection } from '@dbTools/typeorm' import { Contribution } from '@entity/Contribution' import Decimal from 'decimal.js-light' import { FULL_CREATION_AVAILABLE, MAX_CREATION_AMOUNT } from '../const/const' +import { OpenCreation } from '@model/OpenCreation' interface CreationMap { id: number @@ -100,7 +101,7 @@ const getCreationMonths = (timezoneOffset: number): number[] => { return getCreationDates(timezoneOffset).map((date) => date.getMonth() + 1) } -export const getCreationDates = (timezoneOffset: number): Date[] => { +const getCreationDates = (timezoneOffset: number): Date[] => { const clientNow = new Date() clientNow.setTime(clientNow.getTime() - timezoneOffset * 60 * 1000) logger.info( @@ -152,3 +153,18 @@ export const updateCreations = ( export const isValidDateString = (dateString: string): boolean => { return new Date(dateString).toString() !== 'Invalid Date' } + +export const getOpenCreations = async ( + id: number, + timezoneOffset: number, +): Promise => { + 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], + } + }) +}