From 5c7ffe7018b5815bc1294e97aa3e21b010e70c84 Mon Sep 17 00:00:00 2001 From: einhorn_b Date: Fri, 3 Nov 2023 14:09:11 +0100 Subject: [PATCH] add updatedBy field to Contributions in db --- .../Contribution.ts | 104 ++++++++++++++++++ database/entity/Contribution.ts | 2 +- .../0074-add_updated_by_contribution.ts | 9 ++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 database/entity/0074-add_updated_by_contribution/Contribution.ts create mode 100644 database/migrations/0074-add_updated_by_contribution.ts diff --git a/database/entity/0074-add_updated_by_contribution/Contribution.ts b/database/entity/0074-add_updated_by_contribution/Contribution.ts new file mode 100644 index 000000000..f2b6987f1 --- /dev/null +++ b/database/entity/0074-add_updated_by_contribution/Contribution.ts @@ -0,0 +1,104 @@ +import { Decimal } from 'decimal.js-light' +import { + BaseEntity, + Column, + Entity, + PrimaryGeneratedColumn, + DeleteDateColumn, + JoinColumn, + ManyToOne, + OneToMany, + OneToOne, +} from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { User } from '../User' +import { ContributionMessage } from '../ContributionMessage' +import { Transaction } from '../Transaction' + +@Entity('contributions') +export class Contribution extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ unsigned: true, nullable: false, name: 'user_id' }) + userId: number + + @ManyToOne(() => User, (user) => user.contributions) + @JoinColumn({ name: 'user_id' }) + user: User + + @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' }) + createdAt: Date + + @Column({ type: 'datetime', nullable: false, name: 'contribution_date' }) + contributionDate: Date + + @Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' }) + memo: string + + @Column({ + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + amount: Decimal + + @Column({ unsigned: true, nullable: true, name: 'moderator_id' }) + moderatorId: number + + @Column({ unsigned: true, nullable: true, name: 'contribution_link_id' }) + contributionLinkId: number + + @Column({ unsigned: true, nullable: true, name: 'confirmed_by' }) + confirmedBy: number + + @Column({ nullable: true, name: 'confirmed_at' }) + confirmedAt: Date + + @Column({ unsigned: true, nullable: true, name: 'denied_by' }) + deniedBy: number + + @Column({ nullable: true, name: 'denied_at' }) + deniedAt: Date + + @Column({ + name: 'contribution_type', + length: 12, + nullable: false, + collation: 'utf8mb4_unicode_ci', + }) + contributionType: string + + @Column({ + name: 'contribution_status', + length: 12, + nullable: false, + collation: 'utf8mb4_unicode_ci', + }) + contributionStatus: string + + @Column({ unsigned: true, nullable: true, name: 'transaction_id' }) + transactionId: number + + @Column({ nullable: true, name: 'updated_at' }) + updatedAt: Date + + @Column({ nullable: true, unsigned: true, name: 'updated_by' }) + updatedBy?: number + + @DeleteDateColumn({ name: 'deleted_at' }) + deletedAt: Date | null + + @DeleteDateColumn({ unsigned: true, nullable: true, name: 'deleted_by' }) + deletedBy: number + + @OneToMany(() => ContributionMessage, (message) => message.contribution) + @JoinColumn({ name: 'contribution_id' }) + messages?: ContributionMessage[] + + @OneToOne(() => Transaction, (transaction) => transaction.contribution) + @JoinColumn({ name: 'transaction_id' }) + transaction?: Transaction | null +} diff --git a/database/entity/Contribution.ts b/database/entity/Contribution.ts index 0441e7a1f..ef43b88df 100644 --- a/database/entity/Contribution.ts +++ b/database/entity/Contribution.ts @@ -1 +1 @@ -export { Contribution } from './0052-add_updated_at_to_contributions/Contribution' +export { Contribution } from './0074-add_updated_by_contribution/Contribution' diff --git a/database/migrations/0074-add_updated_by_contribution.ts b/database/migrations/0074-add_updated_by_contribution.ts new file mode 100644 index 000000000..6403326ca --- /dev/null +++ b/database/migrations/0074-add_updated_by_contribution.ts @@ -0,0 +1,9 @@ +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + `ALTER TABLE \`contributions\` ADD COLUMN \`updated_by\` boolean NULL DEFAULT false AFTER \`updated_at\`;`, + ) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn(`ALTER TABLE \`contributions\` DROP COLUMN \`updated_by\`;`) +}