diff --git a/backend/src/graphql/enum/ContributionCycleType.ts b/backend/src/graphql/enum/ContributionCycleType.ts index 5fe494a02..a3c55aa68 100644 --- a/backend/src/graphql/enum/ContributionCycleType.ts +++ b/backend/src/graphql/enum/ContributionCycleType.ts @@ -1,13 +1,14 @@ import { registerEnumType } from 'type-graphql' +// lowercase values are not implemented yet export enum ContributionCycleType { - ONCE = 'once', + ONCE = 'ONCE', HOUR = 'hour', TWO_HOURS = 'two_hours', FOUR_HOURS = 'four_hours', EIGHT_HOURS = 'eight_hours', HALF_DAY = 'half_day', - DAY = 'day', + DAILY = 'DAILY', TWO_DAYS = 'two_days', THREE_DAYS = 'three_days', FOUR_DAYS = 'four_days', diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index c9acbace3..e6b28a0f8 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -1,6 +1,6 @@ import { backendLogger as logger } from '@/server/logger' import { Context, getUser } from '@/server/context' -import { getConnection } from '@dbTools/typeorm' +import { getConnection, Between } from '@dbTools/typeorm' import { Resolver, Args, @@ -34,6 +34,7 @@ import { getUserCreation, validateContribution } from './util/creations' import { Decay } from '@model/Decay' import Decimal from 'decimal.js-light' import { TransactionTypeId } from '@enum/TransactionTypeId' +import { ContributionCycleType } from '@enum/ContributionCycleType' const QueryLinkResult = createUnionType({ name: 'QueryLinkResult', // the name of the GraphQL union @@ -204,23 +205,55 @@ export class TransactionLinkResolver { throw new Error('Contribution link is depricated') } } - if (contributionLink.cycle !== 'ONCE') { - logger.error('contribution link has unknown cycle', contributionLink.cycle) - throw new Error('Contribution link has unknown cycle') - } - // Test ONCE rule - const alreadyRedeemed = await queryRunner.manager - .createQueryBuilder() - .select('contribution') - .from(DbContribution, 'contribution') - .where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', { - linkId: contributionLink.id, - id: user.id, - }) - .getOne() - if (alreadyRedeemed) { - logger.error('contribution link with rule ONCE already redeemed by user with id', user.id) - throw new Error('Contribution link already redeemed') + let alreadyRedeemed: DbContribution | undefined + switch (contributionLink.cycle) { + case ContributionCycleType.ONCE: { + alreadyRedeemed = await queryRunner.manager + .createQueryBuilder() + .select('contribution') + .from(DbContribution, 'contribution') + .where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', { + linkId: contributionLink.id, + id: user.id, + }) + .getOne() + if (alreadyRedeemed) { + logger.error( + 'contribution link with rule ONCE already redeemed by user with id', + user.id, + ) + throw new Error('Contribution link already redeemed') + } + break + } + case ContributionCycleType.DAILY: { + const start = new Date() + start.setHours(0, 0, 0, 0) + const end = new Date() + end.setHours(23, 59, 59, 999) + alreadyRedeemed = await queryRunner.manager + .createQueryBuilder() + .select('contribution') + .from(DbContribution, 'contribution') + .where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', { + linkId: contributionLink.id, + id: user.id, + contributionDate: Between(start, end), + }) + .getOne() + if (alreadyRedeemed) { + logger.error( + 'contribution link with rule DAILY already redeemed by user with id', + user.id, + ) + throw new Error('Contribution link already redeemed today') + } + break + } + default: { + logger.error('contribution link has unknown cycle', contributionLink.cycle) + throw new Error('Contribution link has unknown cycle') + } } const creations = await getUserCreation(user.id, false)