From 4a9f3e666d67f3b166f786fc5412dbe0b1794d46 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 00:02:31 +0100 Subject: [PATCH 01/14] database update, include contribution & transaction link ids --- backend/src/config/index.ts | 2 +- .../entity/0063-event_link_fields/Event.ts | 99 +++++++++++++++++++ database/entity/Event.ts | 2 +- database/migrations/0063-event_link_fields.ts | 29 ++++++ dht-node/src/config/index.ts | 2 +- federation/src/config/index.ts | 2 +- 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 database/entity/0063-event_link_fields/Event.ts create mode 100644 database/migrations/0063-event_link_fields.ts diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 9aabaf4ad..ef537d804 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/database/entity/0063-event_link_fields/Event.ts b/database/entity/0063-event_link_fields/Event.ts new file mode 100644 index 000000000..bd3616cf8 --- /dev/null +++ b/database/entity/0063-event_link_fields/Event.ts @@ -0,0 +1,99 @@ +import { Contribution } from '../Contribution' +import { ContributionMessage } from '../ContributionMessage' +import { User } from '../User' +import { Transaction } from '../Transaction' +import Decimal from 'decimal.js-light' +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + ManyToOne, + JoinColumn, +} from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { TransactionLink } from '../TransactionLink' +import { ContributionLink } from '../ContributionLink' + +@Entity('events') +export class Event extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ length: 100, nullable: false, collation: 'utf8mb4_unicode_ci' }) + type: string + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP()', + nullable: false, + }) + createdAt: Date + + @Column({ name: 'affected_user_id', unsigned: true, nullable: false }) + affectedUserId: number + + @ManyToOne(() => User) + @JoinColumn({ name: 'affected_user_id', referencedColumnName: 'id' }) + affectedUser: User + + @Column({ name: 'acting_user_id', unsigned: true, nullable: false }) + actingUserId: number + + @ManyToOne(() => User) + @JoinColumn({ name: 'acting_user_id', referencedColumnName: 'id' }) + actingUser: User + + @Column({ name: 'involved_user_id', type: 'int', unsigned: true, nullable: true }) + involvedUserId: number | null + + @ManyToOne(() => User) + @JoinColumn({ name: 'involved_user_id', referencedColumnName: 'id' }) + involvedUser: User | null + + @Column({ name: 'involved_transaction_id', type: 'int', unsigned: true, nullable: true }) + involvedTransactionId: number | null + + @ManyToOne(() => Transaction) + @JoinColumn({ name: 'involved_transaction_id', referencedColumnName: 'id' }) + involvedTransaction: Transaction | null + + @Column({ name: 'involved_contribution_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionId: number | null + + @ManyToOne(() => Contribution) + @JoinColumn({ name: 'involved_contribution_id', referencedColumnName: 'id' }) + involvedContribution: Contribution | null + + @Column({ name: 'involved_contribution_message_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionMessageId: number | null + + @ManyToOne(() => ContributionMessage) + @JoinColumn({ name: 'involved_contribution_message_id', referencedColumnName: 'id' }) + involvedContributionMessage: ContributionMessage | null + + @Column({ name: 'involved_transaction_link_id', type: 'int', unsigned: true, nullable: true }) + involvedTransactionLinkId: number | null + + @ManyToOne(() => TransactionLink) + @JoinColumn({ name: 'involved_transaction_link_id', referencedColumnName: 'id' }) + involvedTransactionLink: TransactionLink | null + + @Column({ name: 'involved_contribution_link_id', type: 'int', unsigned: true, nullable: true }) + involvedContributionLinkId: number | null + + @ManyToOne(() => ContributionLink) + @JoinColumn({ name: 'involved_contribution_link_id', referencedColumnName: 'id' }) + involvedContributionLink: ContributionLink | null + + @Column({ + type: 'decimal', + precision: 40, + scale: 20, + nullable: true, + transformer: DecimalTransformer, + }) + amount: Decimal | null +} diff --git a/database/entity/Event.ts b/database/entity/Event.ts index e53085f2f..04cbaf458 100644 --- a/database/entity/Event.ts +++ b/database/entity/Event.ts @@ -1 +1 @@ -export { Event } from './0061-event_refactoring/Event' +export { Event } from './0063-event_link_fields/Event' diff --git a/database/migrations/0063-event_link_fields.ts b/database/migrations/0063-event_link_fields.ts new file mode 100644 index 000000000..3b383ad07 --- /dev/null +++ b/database/migrations/0063-event_link_fields.ts @@ -0,0 +1,29 @@ +/* MIGRATION TO ADD LINK ID FIELDS TO EVENT TABLE + * + * This migration add two fields to store a TransactionLinkId and a ContributionLinkId + * in the event table. Furthermore the event `REDEEM_REGISTER` is rewritten to use the + * new fields. + */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `events` ADD COLUMN `involved_transaction_link_id` int(10) unsigned DEFAULT NULL AFTER `involved_contribution_message_id`;', + ) + await queryFn( + 'ALTER TABLE `events` ADD COLUMN `involved_contribution_link_id` int(10) unsigned DEFAULT NULL AFTER `involved_transaction_link_id`;', + ) + await queryFn( + 'UPDATE `events` SET `involved_transaction_link_id` = `involved_transaction_id`, `involved_transaction_id` = NULL, `involved_contribution_link_id` = `involved_contribution_id`, `involved_contribution_id` = NULL WHERE `type` = "REDEEM_REGISTER";', + ) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'UPDATE `events` SET `involved_transaction_id` = `involved_transaction_link_id`, `involved_contribution_id` = `involved_contribution_link_id` WHERE `type` = "REDEEM_REGISTER";', + ) + await queryFn('ALTER TABLE `events` DROP COLUMN `involved_contribution_link_id`;') + await queryFn('ALTER TABLE `events` DROP COLUMN `involved_transaction_link_id`;') +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 7ca44e52c..78f2b162c 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -3,7 +3,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/federation/src/config/index.ts b/federation/src/config/index.ts index b3b529e20..61204ca37 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0062-event_contribution_confirm', + DB_VERSION: '0063-event_link_fields', // DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info From e3377652d4aea0302ce00ab5fbb79f42d9d1b2b9 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 00:04:08 +0100 Subject: [PATCH 02/14] fix redeem event, new events for contribution links, refactor contribution links resolver --- .../event/EVENT_ADMIN_CONTRIBUTION_CONFIRM.ts | 2 ++ .../event/EVENT_ADMIN_CONTRIBUTION_CREATE.ts | 2 ++ .../event/EVENT_ADMIN_CONTRIBUTION_DELETE.ts | 2 ++ .../event/EVENT_ADMIN_CONTRIBUTION_DENY.ts | 2 ++ .../EVENT_ADMIN_CONTRIBUTION_LINK_CREATE.ts | 23 +++++++++++++ .../EVENT_ADMIN_CONTRIBUTION_LINK_DELETE.ts | 20 +++++++++++ .../EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE.ts | 23 +++++++++++++ .../event/EVENT_ADMIN_CONTRIBUTION_UPDATE.ts | 2 ++ .../src/event/EVENT_CONTRIBUTION_CREATE.ts | 13 +++++++- .../src/event/EVENT_CONTRIBUTION_DELETE.ts | 13 +++++++- .../src/event/EVENT_CONTRIBUTION_UPDATE.ts | 13 +++++++- .../src/event/EVENT_TRANSACTION_RECEIVE.ts | 2 ++ backend/src/event/EVENT_TRANSACTION_SEND.ts | 2 ++ backend/src/event/Event.ts | 13 ++++++-- backend/src/event/EventType.ts | 5 +-- .../resolver/ContributionLinkResolver.ts | 33 ++++++++++++++----- backend/src/graphql/resolver/UserResolver.ts | 4 +-- 17 files changed, 156 insertions(+), 18 deletions(-) create mode 100644 backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_CREATE.ts create mode 100644 backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_DELETE.ts create mode 100644 backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE.ts diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CONFIRM.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CONFIRM.ts index 140ba8497..c85a7a12c 100644 --- a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CONFIRM.ts +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CONFIRM.ts @@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_CONFIRM = async ( null, contribution, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CREATE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CREATE.ts index c95ba5b9b..a10ff1a35 100644 --- a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CREATE.ts +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_CREATE.ts @@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_CREATE = async ( null, contribution, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DELETE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DELETE.ts index 30cfd8b93..d3dd5508e 100644 --- a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DELETE.ts +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DELETE.ts @@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_DELETE = async ( null, contribution, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DENY.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DENY.ts index d84467f4f..5f1203766 100644 --- a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DENY.ts +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_DENY.ts @@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_DENY = async ( null, contribution, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_CREATE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_CREATE.ts new file mode 100644 index 000000000..2ead791ed --- /dev/null +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_CREATE.ts @@ -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 => + Event( + EventType.ADMIN_CONTRIBUTION_LINK_CREATE, + { id: 0 } as DbUser, + moderator, + null, + null, + null, + null, + null, + contributionLink, + amount, + ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_DELETE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_DELETE.ts new file mode 100644 index 000000000..b5816e45d --- /dev/null +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_DELETE.ts @@ -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 => + Event( + EventType.ADMIN_CONTRIBUTION_LINK_DELETE, + { id: 0 } as DbUser, + moderator, + null, + null, + null, + null, + null, + contributionLink, + ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE.ts new file mode 100644 index 000000000..6824833b8 --- /dev/null +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE.ts @@ -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 => + Event( + EventType.ADMIN_CONTRIBUTION_LINK_UPDATE, + { id: 0 } as DbUser, + moderator, + null, + null, + null, + null, + null, + contributionLink, + amount, + ).save() diff --git a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_UPDATE.ts b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_UPDATE.ts index 3fc17f7d2..60315249a 100644 --- a/backend/src/event/EVENT_ADMIN_CONTRIBUTION_UPDATE.ts +++ b/backend/src/event/EVENT_ADMIN_CONTRIBUTION_UPDATE.ts @@ -18,5 +18,7 @@ export const EVENT_ADMIN_CONTRIBUTION_UPDATE = async ( null, contribution, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_CONTRIBUTION_CREATE.ts b/backend/src/event/EVENT_CONTRIBUTION_CREATE.ts index cbc514dc8..50cdbcd18 100644 --- a/backend/src/event/EVENT_CONTRIBUTION_CREATE.ts +++ b/backend/src/event/EVENT_CONTRIBUTION_CREATE.ts @@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_CREATE = async ( contribution: DbContribution, amount: Decimal, ): Promise => - 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() diff --git a/backend/src/event/EVENT_CONTRIBUTION_DELETE.ts b/backend/src/event/EVENT_CONTRIBUTION_DELETE.ts index b15e57ea8..eab04bf47 100644 --- a/backend/src/event/EVENT_CONTRIBUTION_DELETE.ts +++ b/backend/src/event/EVENT_CONTRIBUTION_DELETE.ts @@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_DELETE = async ( contribution: DbContribution, amount: Decimal, ): Promise => - 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() diff --git a/backend/src/event/EVENT_CONTRIBUTION_UPDATE.ts b/backend/src/event/EVENT_CONTRIBUTION_UPDATE.ts index 2fb56dc77..82f14edd6 100644 --- a/backend/src/event/EVENT_CONTRIBUTION_UPDATE.ts +++ b/backend/src/event/EVENT_CONTRIBUTION_UPDATE.ts @@ -9,4 +9,15 @@ export const EVENT_CONTRIBUTION_UPDATE = async ( contribution: DbContribution, amount: Decimal, ): Promise => - 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() diff --git a/backend/src/event/EVENT_TRANSACTION_RECEIVE.ts b/backend/src/event/EVENT_TRANSACTION_RECEIVE.ts index 78f561148..acb2f5881 100644 --- a/backend/src/event/EVENT_TRANSACTION_RECEIVE.ts +++ b/backend/src/event/EVENT_TRANSACTION_RECEIVE.ts @@ -18,5 +18,7 @@ export const EVENT_TRANSACTION_RECEIVE = async ( transaction, null, null, + null, + null, amount, ).save() diff --git a/backend/src/event/EVENT_TRANSACTION_SEND.ts b/backend/src/event/EVENT_TRANSACTION_SEND.ts index e281b0d30..a342cb0aa 100644 --- a/backend/src/event/EVENT_TRANSACTION_SEND.ts +++ b/backend/src/event/EVENT_TRANSACTION_SEND.ts @@ -18,5 +18,7 @@ export const EVENT_TRANSACTION_SEND = async ( transaction, null, null, + null, + null, amount, ).save() diff --git a/backend/src/event/Event.ts b/backend/src/event/Event.ts index 163b0b646..2e7cca6af 100644 --- a/backend/src/event/Event.ts +++ b/backend/src/event/Event.ts @@ -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' diff --git a/backend/src/event/EventType.ts b/backend/src/event/EventType.ts index 241923ffd..e40aa91ab 100644 --- a/backend/src/event/EventType.ts +++ b/backend/src/event/EventType.ts @@ -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', diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index d3c77cb82..850117084 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.ts @@ -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 { 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 { - const contributionLink = await DbContributionLink.findOne(id) - if (!contributionLink) { + async deleteContributionLink( + @Arg('id', () => Int) id: number, + @Ctx() context: Context, + ): Promise { + 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 { 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) } } diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 1ddcc942a..d89e1c498 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -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 } } } From 49612871a310a76d040b7546ebca3ed84ca558cc Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 00:05:14 +0100 Subject: [PATCH 03/14] removed fixed todos --- backend/src/graphql/resolver/UserResolver.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index d89e1c498..f7d6be9fb 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -289,7 +289,6 @@ export class UserResolver { logger.info('redeemCode found contributionLink=' + contributionLink) if (contributionLink) { dbUser.contributionLinkId = contributionLink.id - // TODO this is so wrong eventRegisterRedeem.involvedContributionLink = contributionLink } } else { @@ -297,7 +296,6 @@ export class UserResolver { logger.info('redeemCode found transactionLink=' + transactionLink) if (transactionLink) { dbUser.referrerId = transactionLink.userId - // TODO this is so wrong eventRegisterRedeem.involvedTransactionLink = transactionLink } } From 8399363f05393c9a4604794b60890f5699a3d210 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 00:11:56 +0100 Subject: [PATCH 04/14] removed comments of already implemented event types --- backend/src/event/EventType.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/backend/src/event/EventType.ts b/backend/src/event/EventType.ts index e40aa91ab..5d69eba2d 100644 --- a/backend/src/event/EventType.ts +++ b/backend/src/event/EventType.ts @@ -49,9 +49,4 @@ export enum EventType { // DELETE_USER = 'DELETE_USER', // UNDELETE_USER = 'UNDELETE_USER', // CHANGE_USER_ROLE = 'CHANGE_USER_ROLE', - // ADMIN_UPDATE_CONTRIBUTION = 'ADMIN_UPDATE_CONTRIBUTION', - // ADMIN_DELETE_CONTRIBUTION = 'ADMIN_DELETE_CONTRIBUTION', - // CREATE_CONTRIBUTION_LINK = 'CREATE_CONTRIBUTION_LINK', - // DELETE_CONTRIBUTION_LINK = 'DELETE_CONTRIBUTION_LINK', - // UPDATE_CONTRIBUTION_LINK = 'UPDATE_CONTRIBUTION_LINK', } From 3979e84a8c590c73973c74d06064fec15f2151ee Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 00:14:52 +0100 Subject: [PATCH 05/14] fixed lint --- backend/src/graphql/resolver/UserResolver.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index f7d6be9fb..2cd40938f 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -16,9 +16,7 @@ import { getConnection, getCustomRepository, IsNull, Not } from '@dbTools/typeor import { User as DbUser } from '@entity/User' import { UserContact as DbUserContact } from '@entity/UserContact' import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink' -import { Transaction as DbTransaction } from '@entity/Transaction' import { ContributionLink as DbContributionLink } from '@entity/ContributionLink' -import { Contribution as DbContribution } from '@entity/Contribution' import { UserRepository } from '@repository/User' import { User } from '@model/User' From dac2cf7bba779ec800a4272d63b569a9fed2bfc0 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 11:27:47 +0100 Subject: [PATCH 06/14] fixed tests --- backend/src/graphql/resolver/ContributionLinkResolver.test.ts | 2 +- backend/src/graphql/resolver/ContributionLinkResolver.ts | 2 +- backend/src/graphql/resolver/UserResolver.test.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index 62f273829..42966fa82 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -560,7 +560,7 @@ describe('Contribution Links', () => { ).resolves.toEqual( expect.objectContaining({ data: { - deleteContributionLink: expect.any(String), + deleteContributionLink: true, }, }), ) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index 850117084..3a8c6c85b 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.ts @@ -99,7 +99,7 @@ export class ContributionLinkResolver { } @Authorized([RIGHTS.ADMIN_DELETE_CONTRIBUTION_LINK]) - @Mutation(() => Date, { nullable: true }) + @Mutation(() => Boolean, { nullable: true }) async deleteContributionLink( @Arg('id', () => Int) id: number, @Ctx() context: Context, diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 3bcc229b1..b382b2627 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -374,7 +374,7 @@ describe('UserResolver', () => { type: EventType.REDEEM_REGISTER, affectedUserId: result.data.createUser.id, actingUserId: result.data.createUser.id, - involvedContributionId: link.id, + involvedContributionLinkId: link.id, }), ) }) @@ -459,7 +459,7 @@ describe('UserResolver', () => { type: EventType.REDEEM_REGISTER, affectedUserId: newUser.data.createUser.id, actingUserId: newUser.data.createUser.id, - involvedTransactionId: transactionLink.id, + involvedTransactionLinkId: transactionLink.id, }), ) }) From ccccf75fa5dd53afa3be60f867a681ea3d2d5ddb Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 11:32:39 +0100 Subject: [PATCH 07/14] removed automatically included files --- backend/src/event/EventType.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/backend/src/event/EventType.ts b/backend/src/event/EventType.ts index 5d69eba2d..b219a49ba 100644 --- a/backend/src/event/EventType.ts +++ b/backend/src/event/EventType.ts @@ -1,8 +1,3 @@ -export { EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL } from './EVENT_SEND_ACCOUNT_MULTIREGISTRATION_EMAIL' -export { EVENT_SEND_CONFIRMATION_EMAIL } from './EVENT_SEND_CONFIRMATION_EMAIL' -export { EVENT_TRANSACTION_SEND } from './EVENT_TRANSACTION_SEND' -export { EVENT_TRANSACTION_RECEIVE } from './EVENT_TRANSACTION_RECEIVE' - export enum EventType { ACTIVATE_ACCOUNT = 'ACTIVATE_ACCOUNT', // TODO CONTRIBUTION_CONFIRM = 'CONTRIBUTION_CONFIRM', From ff9df408e30d76705bf494fe9099c9c7150564da Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 11:34:38 +0100 Subject: [PATCH 08/14] corrected test name --- backend/src/graphql/resolver/ContributionLinkResolver.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index 42966fa82..3c95db047 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -554,7 +554,7 @@ describe('Contribution Links', () => { linkId = links.data.listContributionLinks.links[0].id }) - it('returns a date string', async () => { + it('returns true', async () => { await expect( mutate({ mutation: deleteContributionLink, variables: { id: linkId } }), ).resolves.toEqual( From fa2916ef4df08ab62fae31b7e3069cc6c0de3cf8 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 11:41:59 +0100 Subject: [PATCH 09/14] test for first event in ContributionLinkResolver --- .../resolver/ContributionLinkResolver.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index 3c95db047..ed56fd5af 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -15,6 +15,8 @@ import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' import { peterLustig } from '@/seeds/users/peter-lustig' import { userFactory } from '@/seeds/factory/user' import { ContributionLink as DbContributionLink } from '@entity/ContributionLink' +import { EventType } from '@/event/Event' +import { Event as DbEvent } from '@entity/Event' let mutate: any, query: any, con: any let testEnv: any @@ -566,6 +568,17 @@ describe('Contribution Links', () => { ) }) + it('stores the ADMIN_CONTRIBUTION_LINK_DELETE event in the database', async () => { + await expect(DbEvent.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventType.ADMIN_CONTRIBUTION_LINK_DELETE, + affectedUserId: 0, + actingUserId: expect.any(Number), + involvedContributionLinkId: linkId, + }), + ) + }) + it('does not list this contribution link anymore', async () => { await expect(query({ query: listContributionLinks })).resolves.toEqual( expect.objectContaining({ From 7583619058fff4eb3baf2f26c612bb4b32ef41c7 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 11:49:04 +0100 Subject: [PATCH 10/14] test for the other two events --- .../resolver/ContributionLinkResolver.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index ed56fd5af..3462a05a9 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -247,6 +247,18 @@ describe('Contribution Links', () => { ) }) + it('stores the ADMIN_CONTRIBUTION_LINK_CREATE event in the database', async () => { + await expect(DbEvent.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventType.ADMIN_CONTRIBUTION_LINK_CREATE, + affectedUserId: 0, + actingUserId: expect.any(Number), + involvedContributionLinkId: expect.any(Number), + amount: expect.decimalEqual(200), + }), + ) + }) + it('returns an error if missing startDate', async () => { jest.clearAllMocks() await expect( @@ -529,6 +541,18 @@ describe('Contribution Links', () => { }), ) }) + + it('stores the ADMIN_CONTRIBUTION_LINK_UPDATE event in the database', async () => { + await expect(DbEvent.find()).resolves.toContainEqual( + expect.objectContaining({ + type: EventType.ADMIN_CONTRIBUTION_LINK_UPDATE, + affectedUserId: 0, + actingUserId: expect.any(Number), + involvedContributionLinkId: expect.any(Number), + amount: expect.decimalEqual(400), + }), + ) + }) }) }) From d1f72891e03dec94a15b10ec18c3347204d26746 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 12:00:09 +0100 Subject: [PATCH 11/14] increase coverage requirement to 81% in backend --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5eadf1e94..a02aa4b5b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -427,7 +427,7 @@ jobs: report_name: Coverage Backend type: lcov result_path: ./backend/coverage/lcov.info - min_coverage: 80 + min_coverage: 81 token: ${{ github.token }} ########################################################################## From cf0af5dc856e4725c4b546b7e74e0f6830a7421c Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 12:35:40 +0100 Subject: [PATCH 12/14] missing change for changed event database model --- database/entity/0063-event_link_fields/Event.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/entity/0063-event_link_fields/Event.ts b/database/entity/0063-event_link_fields/Event.ts index bd3616cf8..dc7fa776c 100644 --- a/database/entity/0063-event_link_fields/Event.ts +++ b/database/entity/0063-event_link_fields/Event.ts @@ -27,7 +27,7 @@ export class Event extends BaseEntity { @CreateDateColumn({ name: 'created_at', type: 'datetime', - default: () => 'CURRENT_TIMESTAMP()', + default: () => 'CURRENT_TIMESTAMP(3)', nullable: false, }) createdAt: Date From d65948ed81738801d38e5540218ff4224fd436b9 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 12:47:44 +0100 Subject: [PATCH 13/14] cleanup --- backend/src/graphql/resolver/ContributionLinkResolver.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index 3a8c6c85b..b5f423bda 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.ts @@ -144,8 +144,6 @@ export class ContributionLinkResolver { dbContributionLink.maxAmountPerMonth = maxAmountPerMonth dbContributionLink.maxPerCycle = maxPerCycle await dbContributionLink.save() - logger.debug(`updateContributionLink successful!`) - await EVENT_ADMIN_CONTRIBUTION_LINK_UPDATE(getUser(context), dbContributionLink, amount) return new ContributionLink(dbContributionLink) From cdb7fe32c8f3ab0f303f5f5370f1d617c878a173 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Tue, 7 Mar 2023 13:16:27 +0100 Subject: [PATCH 14/14] lint fixes --- backend/src/graphql/resolver/ContributionLinkResolver.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index b5f423bda..6c396e73d 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.ts @@ -12,7 +12,6 @@ import { isStartEndDateValid } from './util/creations' import { ContributionLinkList } from '@model/ContributionLinkList' import { ContributionLink } from '@model/ContributionLink' import ContributionLinkArgs from '@arg/ContributionLinkArgs' -import { backendLogger as logger } from '@/server/logger' import { RIGHTS } from '@/auth/RIGHTS' import { ContributionLink as DbContributionLink } from '@entity/ContributionLink' import { Order } from '@enum/Order'