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/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..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', @@ -11,6 +6,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,17 +38,10 @@ 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', // 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', } diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts index 4563d2a6a..77d64ad8a 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.test.ts @@ -19,6 +19,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 @@ -249,6 +251,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( @@ -531,6 +545,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), + }), + ) + }) }) }) @@ -558,18 +584,29 @@ 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( expect.objectContaining({ data: { - deleteContributionLink: expect.any(String), + deleteContributionLink: true, }, }), ) }) + 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({ diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index cccf47399..d2d56e853 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 { @@ -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' @@ -21,6 +20,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 +43,7 @@ export class ContributionLinkResolver { maxAmountPerMonth = null, maxPerCycle, }: ContributionLinkArgs, + @Ctx() context: Context, ): Promise { isStartEndDateValid(validFrom, validTo) if (name.length < CONTRIBUTIONLINK_NAME_MIN_CHARS) { @@ -68,7 +74,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) } @@ -91,16 +98,19 @@ export class ContributionLinkResolver { } @Authorized([RIGHTS.DELETE_CONTRIBUTION_LINK]) - @Mutation(() => Date, { nullable: true }) - async deleteContributionLink(@Arg('id', () => Int) id: number): Promise { - const contributionLink = await DbContributionLink.findOne(id) - if (!contributionLink) { + @Mutation(() => Boolean) + 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.UPDATE_CONTRIBUTION_LINK]) @@ -118,6 +128,7 @@ export class ContributionLinkResolver { maxPerCycle, }: ContributionLinkArgs, @Arg('id', () => Int) id: number, + @Ctx() context: Context, ): Promise { const dbContributionLink = await DbContributionLink.findOne(id) if (!dbContributionLink) { @@ -132,7 +143,8 @@ 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) } } diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index da2dd8cdf..3c01089dc 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -379,7 +379,7 @@ describe('UserResolver', () => { type: EventType.REDEEM_REGISTER, affectedUserId: result.data.createUser.id, actingUserId: result.data.createUser.id, - involvedContributionId: link.id, + involvedContributionLinkId: link.id, }), ) }) @@ -464,7 +464,7 @@ describe('UserResolver', () => { type: EventType.REDEEM_REGISTER, affectedUserId: newUser.data.createUser.id, actingUserId: newUser.data.createUser.id, - involvedTransactionId: transactionLink.id, + involvedTransactionLinkId: transactionLink.id, }), ) }) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index cedea81b1..46c93fcc3 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -20,9 +20,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' @@ -293,16 +291,14 @@ export class UserResolver { logger.info('redeemCode found contributionLink', contributionLink) 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 }) logger.info('redeemCode found transactionLink', transactionLink) if (transactionLink) { dbUser.referrerId = transactionLink.userId - // TODO this is so wrong - eventRegisterRedeem.involvedTransaction = { id: transactionLink.id } as DbTransaction + eventRegisterRedeem.involvedTransactionLink = transactionLink } } } 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..dc7fa776c --- /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(3)', + 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 36fec7a7a..f149b70b7 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