diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 282d458ce..ae73fa8ac 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0046-adapt_users_table_for_gradidoid', + DB_VERSION: '0047-messages_tables', 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/graphql/enum/MessageType.ts b/backend/src/graphql/enum/MessageType.ts new file mode 100644 index 000000000..a4606e464 --- /dev/null +++ b/backend/src/graphql/enum/MessageType.ts @@ -0,0 +1,11 @@ +import { registerEnumType } from 'type-graphql' + +export enum ContributionMessageType { + HISTORY = 'HISTORY', + DIALOG = 'DIALOG', +} + +registerEnumType(ContributionMessageType, { + name: 'ContributionMessageType', + description: 'Name of the Type of the ContributionMessage', +}) diff --git a/database/entity/0047-messages_tables/Contribution.ts b/database/entity/0047-messages_tables/Contribution.ts new file mode 100644 index 000000000..1ba31bb88 --- /dev/null +++ b/database/entity/0047-messages_tables/Contribution.ts @@ -0,0 +1,89 @@ +import Decimal from 'decimal.js-light' +import { + BaseEntity, + Column, + Entity, + PrimaryGeneratedColumn, + DeleteDateColumn, + JoinColumn, + ManyToOne, + OneToMany, +} from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' +import { User } from '../User' +import { ContributionMessage } from '../ContributionMessage' + +@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 + + @DeleteDateColumn({ name: 'deleted_at' }) + deletedAt: Date | null + + @OneToMany(() => ContributionMessage, (message) => message.contribution) + @JoinColumn({ name: 'contribution_id' }) + messages?: ContributionMessage[] +} diff --git a/database/entity/0047-messages_tables/ContributionMessage.ts b/database/entity/0047-messages_tables/ContributionMessage.ts new file mode 100644 index 000000000..d9ac124dd --- /dev/null +++ b/database/entity/0047-messages_tables/ContributionMessage.ts @@ -0,0 +1,46 @@ +import { + BaseEntity, + Column, + DeleteDateColumn, + Entity, + JoinColumn, + ManyToOne, + PrimaryGeneratedColumn, +} from 'typeorm' +import { Contribution } from '../Contribution' + +@Entity('contribution_messages', { + engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci', +}) +export class ContributionMessage extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'contribution_id', unsigned: true, nullable: false }) + contributionId: number + + @ManyToOne(() => Contribution, (contribution) => contribution.messages) + @JoinColumn({ name: 'contribution_id' }) + contribution: Contribution + + @Column({ name: 'user_id', unsigned: true, nullable: false }) + userId: number + + @Column({ length: 2000, nullable: false, collation: 'utf8mb4_unicode_ci' }) + message: string + + @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP', name: 'created_at' }) + createdAt: Date + + @Column({ type: 'datetime', default: null, nullable: true, name: 'updated_at' }) + updatedAt: Date + + @DeleteDateColumn({ name: 'deleted_at' }) + deletedAt: Date | null + + @Column({ name: 'deleted_by', default: null, unsigned: true, nullable: true }) + deletedBy: number + + @Column({ length: 12, nullable: false, collation: 'utf8mb4_unicode_ci' }) + type: string +} diff --git a/database/entity/Contribution.ts b/database/entity/Contribution.ts index 800e7f9cd..f6530f00b 100644 --- a/database/entity/Contribution.ts +++ b/database/entity/Contribution.ts @@ -1 +1 @@ -export { Contribution } from './0045-add_denied_type_and_status_to_contributions/Contribution' +export { Contribution } from './0047-messages_tables/Contribution' diff --git a/database/entity/ContributionMessage.ts b/database/entity/ContributionMessage.ts new file mode 100644 index 000000000..9b6450811 --- /dev/null +++ b/database/entity/ContributionMessage.ts @@ -0,0 +1 @@ +export { ContributionMessage } from './0047-messages_tables/ContributionMessage' diff --git a/database/entity/index.ts b/database/entity/index.ts index 733c99a3a..abd31bfb9 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -7,6 +7,7 @@ import { TransactionLink } from './TransactionLink' import { User } from './User' import { Contribution } from './Contribution' import { EventProtocol } from './EventProtocol' +import { ContributionMessage } from './ContributionMessage' export const entities = [ Contribution, @@ -18,4 +19,5 @@ export const entities = [ TransactionLink, User, EventProtocol, + ContributionMessage, ] diff --git a/database/migrations/0047-messages_tables.ts b/database/migrations/0047-messages_tables.ts new file mode 100644 index 000000000..36fa42a8e --- /dev/null +++ b/database/migrations/0047-messages_tables.ts @@ -0,0 +1,30 @@ +/** + * MIGRATION TO CREATE THE MESSAGES TABLES + * + * This migration creates the `messages` tables in the `community_server` database (`gradido_community`). + * This is done to keep all data in the same place and is to be understood in conjunction with the next migration + * `0046-messages_tables` which will fill the tables with the existing data + */ +/* 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(` + CREATE TABLE IF NOT EXISTS \`contribution_messages\` ( + \`id\` int(10) unsigned NOT NULL AUTO_INCREMENT, + \`contribution_id\` int(10) unsigned NOT NULL, + \`user_id\` int(10) unsigned NOT NULL, + \`message\` varchar(2000) COLLATE utf8mb4_unicode_ci NOT NULL, + \`created_at\` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + \`updated_at\` datetime DEFAULT NULL, + \`deleted_at\` datetime DEFAULT NULL, + \`deleted_by\` int(10) unsigned DEFAULT NULL, + \`type\` varchar(12) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT "DIALOG", + PRIMARY KEY (\`id\`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + `) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn(`DROP TABLE IF EXISTS \`contribution_messages\`;`) +}