feat: Daily Rule for Contribution Links

This commit is contained in:
Moriz Wahl 2022-10-12 14:46:22 +02:00
parent b291c56d7b
commit 7e66e299d9
2 changed files with 54 additions and 20 deletions

View File

@ -1,13 +1,14 @@
import { registerEnumType } from 'type-graphql' import { registerEnumType } from 'type-graphql'
// lowercase values are not implemented yet
export enum ContributionCycleType { export enum ContributionCycleType {
ONCE = 'once', ONCE = 'ONCE',
HOUR = 'hour', HOUR = 'hour',
TWO_HOURS = 'two_hours', TWO_HOURS = 'two_hours',
FOUR_HOURS = 'four_hours', FOUR_HOURS = 'four_hours',
EIGHT_HOURS = 'eight_hours', EIGHT_HOURS = 'eight_hours',
HALF_DAY = 'half_day', HALF_DAY = 'half_day',
DAY = 'day', DAILY = 'DAILY',
TWO_DAYS = 'two_days', TWO_DAYS = 'two_days',
THREE_DAYS = 'three_days', THREE_DAYS = 'three_days',
FOUR_DAYS = 'four_days', FOUR_DAYS = 'four_days',

View File

@ -1,6 +1,6 @@
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
import { Context, getUser } from '@/server/context' import { Context, getUser } from '@/server/context'
import { getConnection } from '@dbTools/typeorm' import { getConnection, Between } from '@dbTools/typeorm'
import { import {
Resolver, Resolver,
Args, Args,
@ -34,6 +34,7 @@ import { getUserCreation, validateContribution } from './util/creations'
import { Decay } from '@model/Decay' import { Decay } from '@model/Decay'
import Decimal from 'decimal.js-light' import Decimal from 'decimal.js-light'
import { TransactionTypeId } from '@enum/TransactionTypeId' import { TransactionTypeId } from '@enum/TransactionTypeId'
import { ContributionCycleType } from '@enum/ContributionCycleType'
const QueryLinkResult = createUnionType({ const QueryLinkResult = createUnionType({
name: 'QueryLinkResult', // the name of the GraphQL union name: 'QueryLinkResult', // the name of the GraphQL union
@ -204,23 +205,55 @@ export class TransactionLinkResolver {
throw new Error('Contribution link is depricated') throw new Error('Contribution link is depricated')
} }
} }
if (contributionLink.cycle !== 'ONCE') { let alreadyRedeemed: DbContribution | undefined
logger.error('contribution link has unknown cycle', contributionLink.cycle) switch (contributionLink.cycle) {
throw new Error('Contribution link has unknown cycle') case ContributionCycleType.ONCE: {
} alreadyRedeemed = await queryRunner.manager
// Test ONCE rule .createQueryBuilder()
const alreadyRedeemed = await queryRunner.manager .select('contribution')
.createQueryBuilder() .from(DbContribution, 'contribution')
.select('contribution') .where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', {
.from(DbContribution, 'contribution') linkId: contributionLink.id,
.where('contribution.contributionLinkId = :linkId AND contribution.userId = :id', { id: user.id,
linkId: contributionLink.id, })
id: user.id, .getOne()
}) if (alreadyRedeemed) {
.getOne() logger.error(
if (alreadyRedeemed) { 'contribution link with rule ONCE already redeemed by user with id',
logger.error('contribution link with rule ONCE already redeemed by user with id', user.id) user.id,
throw new Error('Contribution link already redeemed') )
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) const creations = await getUserCreation(user.id, false)