diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 825afae94..8147c9c4b 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -46,11 +46,13 @@ import { checkOptInCode, activationLink, printTimeDuration } from './UserResolve import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail' import { transactionLinkCode as contributionLinkCode } from './TransactionLinkResolver' import CONFIG from '@/config' +import { getUserCreation } from './util/getUserCreation' +import { isContributionValid } from './util/isContributionValid' // const EMAIL_OPT_IN_REGISTER = 1 // const EMAIL_OPT_UNKNOWN = 3 // elopage? const MAX_CREATION_AMOUNT = new Decimal(1000) -const FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT] +export const FULL_CREATION_AVAILABLE = [MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT, MAX_CREATION_AMOUNT] @Resolver() export class AdminResolver { @@ -650,13 +652,7 @@ interface CreationMap { creations: Decimal[] } -export const getUserCreation = async (id: number, includePending = true): Promise => { - logger.trace('getUserCreation', id, includePending) - const creations = await getUserCreations([id], includePending) - return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE -} - -async function getUserCreations(ids: number[], includePending = true): Promise { +export async function getUserCreations(ids: number[], includePending = true): Promise { logger.trace('getUserCreations:', ids, includePending) const months = getCreationMonths() logger.trace('getUserCreations months', months) @@ -713,27 +709,6 @@ function updateCreations(creations: Decimal[], contribution: Contribution): Deci return creations } -export const isContributionValid = ( - creations: Decimal[], - amount: Decimal, - creationDate: Date, -): boolean => { - logger.trace('isContributionValid', creations, amount, creationDate) - const index = getCreationIndex(creationDate.getMonth()) - - if (index < 0) { - throw new Error('No information for available creations for the given date') - } - - if (amount.greaterThan(creations[index].toString())) { - throw new Error( - `The amount (${amount} GDD) to be created exceeds the amount (${creations[index]} GDD) still available for this month.`, - ) - } - - return true -} - const getCreationMonths = (): number[] => { const now = new Date(Date.now()) return [ @@ -743,6 +718,6 @@ const getCreationMonths = (): number[] => { ].reverse() } -const getCreationIndex = (month: number): number => { +export const getCreationIndex = (month: number): number => { return getCreationMonths().findIndex((el) => el === month + 1) } diff --git a/backend/src/graphql/resolver/util/getUserCreation.ts b/backend/src/graphql/resolver/util/getUserCreation.ts new file mode 100644 index 000000000..445e25b9c --- /dev/null +++ b/backend/src/graphql/resolver/util/getUserCreation.ts @@ -0,0 +1,9 @@ +import { backendLogger as logger } from '@/server/logger' +import Decimal from 'decimal.js-light' +import { getUserCreations, FULL_CREATION_AVAILABLE } from '../AdminResolver' + +export const getUserCreation = async (id: number, includePending = true): Promise => { + logger.trace('getUserCreation', id, includePending) + const creations = await getUserCreations([id], includePending) + return creations[0] ? creations[0].creations : FULL_CREATION_AVAILABLE +} diff --git a/backend/src/graphql/resolver/util/isContributionValid.ts b/backend/src/graphql/resolver/util/isContributionValid.ts new file mode 100644 index 000000000..5ae7755c1 --- /dev/null +++ b/backend/src/graphql/resolver/util/isContributionValid.ts @@ -0,0 +1,24 @@ +import { backendLogger as logger } from '@/server/logger' +import Decimal from 'decimal.js-light' +import { getCreationIndex } from '../AdminResolver' + +export const isContributionValid = ( + creations: Decimal[], + amount: Decimal, + creationDate: Date, +): boolean => { + logger.trace('isContributionValid', creations, amount, creationDate) + const index = getCreationIndex(creationDate.getMonth()) + + if (index < 0) { + throw new Error('No information for available creations for the given date') + } + + if (amount.greaterThan(creations[index].toString())) { + throw new Error( + `The amount (${amount} GDD) to be created exceeds the amount (${creations[index]} GDD) still available for this month.`, + ) + } + + return true +}