fix redeem event, new events for contribution links, refactor contribution links resolver

This commit is contained in:
Ulf Gebhardt 2023-03-07 00:04:08 +01:00
parent 4a9f3e666d
commit e3377652d4
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
17 changed files with 156 additions and 18 deletions

View File

@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_CONFIRM = async (
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_CREATE = async (
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_DELETE = async (
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_DENY = async (
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -0,0 +1,23 @@
import Decimal from 'decimal.js-light'
import { User as DbUser } from '@entity/User'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_ADMIN_CONTRIBUTION_LINK_CREATE = async (
moderator: DbUser,
contributionLink: DbContributionLink,
amount: Decimal,
): Promise<DbEvent> =>
Event(
EventType.ADMIN_CONTRIBUTION_LINK_CREATE,
{ id: 0 } as DbUser,
moderator,
null,
null,
null,
null,
null,
contributionLink,
amount,
).save()

View File

@ -0,0 +1,20 @@
import { User as DbUser } from '@entity/User'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_ADMIN_CONTRIBUTION_LINK_DELETE = async (
moderator: DbUser,
contributionLink: DbContributionLink,
): Promise<DbEvent> =>
Event(
EventType.ADMIN_CONTRIBUTION_LINK_DELETE,
{ id: 0 } as DbUser,
moderator,
null,
null,
null,
null,
null,
contributionLink,
).save()

View File

@ -0,0 +1,23 @@
import Decimal from 'decimal.js-light'
import { User as DbUser } from '@entity/User'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import { Event as DbEvent } from '@entity/Event'
import { Event, EventType } from './Event'
export const EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE = async (
moderator: DbUser,
contributionLink: DbContributionLink,
amount: Decimal,
): Promise<DbEvent> =>
Event(
EventType.ADMIN_CONTRIBUTION_LINK_UPDATE,
{ id: 0 } as DbUser,
moderator,
null,
null,
null,
null,
null,
contributionLink,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_UPDATE = async (
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_CREATE = async (
contribution: DbContribution,
amount: Decimal,
): Promise<DbEvent> =>
Event(EventType.CONTRIBUTION_CREATE, user, user, null, null, contribution, null, amount).save()
Event(
EventType.CONTRIBUTION_CREATE,
user,
user,
null,
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_DELETE = async (
contribution: DbContribution,
amount: Decimal,
): Promise<DbEvent> =>
Event(EventType.CONTRIBUTION_DELETE, user, user, null, null, contribution, null, amount).save()
Event(
EventType.CONTRIBUTION_DELETE,
user,
user,
null,
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_UPDATE = async (
contribution: DbContribution,
amount: Decimal,
): Promise<DbEvent> =>
Event(EventType.CONTRIBUTION_UPDATE, user, user, null, null, contribution, null, amount).save()
Event(
EventType.CONTRIBUTION_UPDATE,
user,
user,
null,
null,
contribution,
null,
null,
null,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_TRANSACTION_RECEIVE = async (
transaction,
null,
null,
null,
null,
amount,
).save()

View File

@ -18,5 +18,7 @@ export const EVENT_TRANSACTION_SEND = async (
transaction,
null,
null,
null,
null,
amount,
).save()

View File

@ -1,8 +1,10 @@
import { Event as DbEvent } from '@entity/Event'
import { User as DbUser } from '@entity/User'
import { ContributionMessage as DbContributionMessage } from '@entity/ContributionMessage'
import { Contribution as DbContribution } from '@entity/Contribution'
import { Transaction as DbTransaction } from '@entity/Transaction'
import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink'
import { Contribution as DbContribution } from '@entity/Contribution'
import { ContributionMessage as DbContributionMessage } from '@entity/ContributionMessage'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import Decimal from 'decimal.js-light'
import { EventType } from './Event'
@ -14,6 +16,8 @@ export const Event = (
involvedTransaction: DbTransaction | null = null,
involvedContribution: DbContribution | null = null,
involvedContributionMessage: DbContributionMessage | null = null,
involvedTransactionLink: DbTransactionLink | null = null,
involvedContributionLink: DbContributionLink | null = null,
amount: Decimal | null = null,
): DbEvent => {
const event = new DbEvent()
@ -24,6 +28,8 @@ export const Event = (
event.involvedTransaction = involvedTransaction
event.involvedContribution = involvedContribution
event.involvedContributionMessage = involvedContributionMessage
event.involvedTransactionLink = involvedTransactionLink
event.involvedContributionLink = involvedContributionLink
event.amount = amount
return event
}
@ -36,6 +42,9 @@ export { EVENT_ADMIN_CONTRIBUTION_CREATE } from './EVENT_ADMIN_CONTRIBUTION_CREA
export { EVENT_ADMIN_CONTRIBUTION_DELETE } from './EVENT_ADMIN_CONTRIBUTION_DELETE'
export { EVENT_ADMIN_CONTRIBUTION_DENY } from './EVENT_ADMIN_CONTRIBUTION_DENY'
export { EVENT_ADMIN_CONTRIBUTION_UPDATE } from './EVENT_ADMIN_CONTRIBUTION_UPDATE'
export { EVENT_ADMIN_CONTRIBUTION_LINK_CREATE } from './EVENT_ADMIN_CONTRIBUTION_LINK_CREATE'
export { EVENT_ADMIN_CONTRIBUTION_LINK_DELETE } from './EVENT_ADMIN_CONTRIBUTION_LINK_DELETE'
export { EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE } from './EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE'
export { EVENT_ADMIN_SEND_CONFIRMATION_EMAIL } from './EVENT_ADMIN_SEND_CONFIRMATION_EMAIL'
export { EVENT_CONTRIBUTION_CREATE } from './EVENT_CONTRIBUTION_CREATE'
export { EVENT_CONTRIBUTION_DELETE } from './EVENT_CONTRIBUTION_DELETE'

View File

@ -11,6 +11,9 @@ export enum EventType {
ADMIN_CONTRIBUTION_DELETE = 'ADMIN_CONTRIBUTION_DELETE',
ADMIN_CONTRIBUTION_DENY = 'ADMIN_CONTRIBUTION_DENY',
ADMIN_CONTRIBUTION_UPDATE = 'ADMIN_CONTRIBUTION_UPDATE',
ADMIN_CONTRIBUTION_LINK_CREATE = 'ADMIN_CONTRIBUTION_LINK_CREATE',
ADMIN_CONTRIBUTION_LINK_DELETE = 'ADMIN_CONTRIBUTION_LINK_DELETE',
ADMIN_CONTRIBUTION_LINK_UPDATE = 'ADMIN_CONTRIBUTION_LINK_UPDATE',
ADMIN_SEND_CONFIRMATION_EMAIL = 'ADMIN_SEND_CONFIRMATION_EMAIL',
CONTRIBUTION_CREATE = 'CONTRIBUTION_CREATE',
CONTRIBUTION_DELETE = 'CONTRIBUTION_DELETE',
@ -40,8 +43,6 @@ export enum EventType {
// SEND_TRANSACTION_LINK_REDEEM_EMAIL = 'SEND_TRANSACTION_LINK_REDEEM_EMAIL',
// SEND_ADDED_CONTRIBUTION_EMAIL = 'SEND_ADDED_CONTRIBUTION_EMAIL',
// SEND_CONTRIBUTION_CONFIRM_EMAIL = 'SEND_CONTRIBUTION_CONFIRM_EMAIL',
// CONTRIBUTION_DENY = 'CONTRIBUTION_DENY',
// CONTRIBUTION_LINK_DEFINE = 'CONTRIBUTION_LINK_DEFINE',
// CONTRIBUTION_LINK_ACTIVATE_REDEEM = 'CONTRIBUTION_LINK_ACTIVATE_REDEEM',
// USER_CREATE_CONTRIBUTION_MESSAGE = 'USER_CREATE_CONTRIBUTION_MESSAGE',
// ADMIN_CREATE_CONTRIBUTION_MESSAGE = 'ADMIN_CREATE_CONTRIBUTION_MESSAGE',

View File

@ -1,5 +1,5 @@
import Decimal from 'decimal.js-light'
import { Resolver, Args, Arg, Authorized, Mutation, Query, Int } from 'type-graphql'
import { Resolver, Args, Arg, Authorized, Mutation, Query, Int, Ctx } from 'type-graphql'
import { MoreThan, IsNull } from '@dbTools/typeorm'
import {
@ -21,6 +21,12 @@ import Paginated from '@arg/Paginated'
// TODO: this is a strange construct
import { transactionLinkCode as contributionLinkCode } from './TransactionLinkResolver'
import LogError from '@/server/LogError'
import { Context, getUser } from '@/server/context'
import {
EVENT_ADMIN_CONTRIBUTION_LINK_CREATE,
EVENT_ADMIN_CONTRIBUTION_LINK_DELETE,
EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE,
} from '@/event/Event'
@Resolver()
export class ContributionLinkResolver {
@ -38,6 +44,7 @@ export class ContributionLinkResolver {
maxAmountPerMonth,
maxPerCycle,
}: ContributionLinkArgs,
@Ctx() context: Context,
): Promise<ContributionLink> {
isStartEndDateValid(validFrom, validTo)
if (name.length < CONTRIBUTIONLINK_NAME_MIN_CHARS) {
@ -68,7 +75,8 @@ export class ContributionLinkResolver {
dbContributionLink.maxAmountPerMonth = maxAmountPerMonth
dbContributionLink.maxPerCycle = maxPerCycle
await dbContributionLink.save()
logger.debug(`createContributionLink successful!`)
await EVENT_ADMIN_CONTRIBUTION_LINK_CREATE(getUser(context), dbContributionLink, amount)
return new ContributionLink(dbContributionLink)
}
@ -92,15 +100,18 @@ export class ContributionLinkResolver {
@Authorized([RIGHTS.ADMIN_DELETE_CONTRIBUTION_LINK])
@Mutation(() => Date, { nullable: true })
async deleteContributionLink(@Arg('id', () => Int) id: number): Promise<Date | null> {
const contributionLink = await DbContributionLink.findOne(id)
if (!contributionLink) {
async deleteContributionLink(
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<boolean> {
const dbContributionLink = await DbContributionLink.findOne(id)
if (!dbContributionLink) {
throw new LogError('Contribution Link not found', id)
}
await contributionLink.softRemove()
logger.debug(`deleteContributionLink successful!`)
const newContributionLink = await DbContributionLink.findOne({ id }, { withDeleted: true })
return newContributionLink ? newContributionLink.deletedAt : null
await dbContributionLink.softRemove()
await EVENT_ADMIN_CONTRIBUTION_LINK_DELETE(getUser(context), dbContributionLink)
return true
}
@Authorized([RIGHTS.ADMIN_UPDATE_CONTRIBUTION_LINK])
@ -118,6 +129,7 @@ export class ContributionLinkResolver {
maxPerCycle,
}: ContributionLinkArgs,
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<ContributionLink> {
const dbContributionLink = await DbContributionLink.findOne(id)
if (!dbContributionLink) {
@ -133,6 +145,9 @@ export class ContributionLinkResolver {
dbContributionLink.maxPerCycle = maxPerCycle
await dbContributionLink.save()
logger.debug(`updateContributionLink successful!`)
await EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE(getUser(context), dbContributionLink, amount)
return new ContributionLink(dbContributionLink)
}
}

View File

@ -290,7 +290,7 @@ export class UserResolver {
if (contributionLink) {
dbUser.contributionLinkId = contributionLink.id
// TODO this is so wrong
eventRegisterRedeem.involvedContribution = { id: contributionLink.id } as DbContribution
eventRegisterRedeem.involvedContributionLink = contributionLink
}
} else {
const transactionLink = await DbTransactionLink.findOne({ code: redeemCode })
@ -298,7 +298,7 @@ export class UserResolver {
if (transactionLink) {
dbUser.referrerId = transactionLink.userId
// TODO this is so wrong
eventRegisterRedeem.involvedTransaction = { id: transactionLink.id } as DbTransaction
eventRegisterRedeem.involvedTransactionLink = transactionLink
}
}
}