From 235773c9b9fb53e14f8292826b7f2ab7f66e2896 Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Fri, 6 Oct 2023 00:42:07 +0200 Subject: [PATCH] introduce foreign user in users table --- backend/src/config/index.ts | 2 +- backend/src/util/communityUser.ts | 10 +- .../User.ts | 133 ++++++++++++++++++ database/entity/User.ts | 2 +- ...3-introduce_foreign_user_in_users_table.ts | 21 +++ dht-node/src/config/index.ts | 2 +- federation/src/config/index.ts | 2 +- 7 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 database/entity/0073-introduce_foreign_user_in_users_table/User.ts create mode 100644 database/migrations/0073-introduce_foreign_user_in_users_table.ts diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index a77840738..25e901491 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0072-add_communityuuid_to_transactions_table', + DB_VERSION: '0073-introduce_foreign_user_in_users_table', 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/util/communityUser.ts b/backend/src/util/communityUser.ts index 422c836df..de41ea310 100644 --- a/backend/src/util/communityUser.ts +++ b/backend/src/util/communityUser.ts @@ -33,21 +33,23 @@ const communityDbUser: dbUser = { hasId: function (): boolean { throw new Error('Function not implemented.') }, - save: function (options?: SaveOptions): Promise { + save: function (_options?: SaveOptions): Promise { throw new Error('Function not implemented.') }, - remove: function (options?: RemoveOptions): Promise { + remove: function (_options?: RemoveOptions): Promise { throw new Error('Function not implemented.') }, - softRemove: function (options?: SaveOptions): Promise { + softRemove: function (_options?: SaveOptions): Promise { throw new Error('Function not implemented.') }, - recover: function (options?: SaveOptions): Promise { + recover: function (_options?: SaveOptions): Promise { throw new Error('Function not implemented.') }, reload: function (): Promise { throw new Error('Function not implemented.') }, + foreign: false, + communityUuid: null, } const communityUser = new User(communityDbUser) diff --git a/database/entity/0073-introduce_foreign_user_in_users_table/User.ts b/database/entity/0073-introduce_foreign_user_in_users_table/User.ts new file mode 100644 index 000000000..423039679 --- /dev/null +++ b/database/entity/0073-introduce_foreign_user_in_users_table/User.ts @@ -0,0 +1,133 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + DeleteDateColumn, + OneToMany, + JoinColumn, + OneToOne, +} from 'typeorm' +import { Community } from '../Community' +import { Contribution } from '../Contribution' +import { ContributionMessage } from '../ContributionMessage' +import { UserContact } from '../UserContact' +import { UserRole } from '../UserRole' + +@Entity('users', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' }) +export class User extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ type: 'bool', default: false }) + foreign: boolean + + @Column({ + name: 'gradido_id', + length: 36, + nullable: false, + collation: 'utf8mb4_unicode_ci', + }) + gradidoID: string + + @Column({ + name: 'community_uuid', + type: 'char', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + communityUuid: string | null + + @Column({ + name: 'alias', + length: 20, + nullable: true, + default: null, + collation: 'utf8mb4_unicode_ci', + }) + alias: string + + @OneToOne(() => UserContact, (emailContact: UserContact) => emailContact.user) + @JoinColumn({ name: 'email_id' }) + emailContact: UserContact + + @Column({ name: 'email_id', type: 'int', unsigned: true, nullable: true, default: null }) + emailId: number | null + + @Column({ + name: 'first_name', + length: 255, + nullable: true, + default: null, + collation: 'utf8mb4_unicode_ci', + }) + firstName: string + + @Column({ + name: 'last_name', + length: 255, + nullable: true, + default: null, + collation: 'utf8mb4_unicode_ci', + }) + lastName: string + + @Column({ name: 'created_at', default: () => 'CURRENT_TIMESTAMP(3)', nullable: false }) + createdAt: Date + + @DeleteDateColumn({ name: 'deleted_at', nullable: true }) + deletedAt: Date | null + + @Column({ type: 'bigint', default: 0, unsigned: true }) + password: BigInt + + @Column({ + name: 'password_encryption_type', + type: 'int', + unsigned: true, + nullable: false, + default: 0, + }) + passwordEncryptionType: number + + @Column({ length: 4, default: 'de', collation: 'utf8mb4_unicode_ci', nullable: false }) + language: string + + @Column({ type: 'bool', default: false }) + hideAmountGDD: boolean + + @Column({ type: 'bool', default: false }) + hideAmountGDT: boolean + + @OneToMany(() => UserRole, (userRole) => userRole.user) + @JoinColumn({ name: 'user_id' }) + userRoles: UserRole[] + + @Column({ name: 'referrer_id', type: 'int', unsigned: true, nullable: true, default: null }) + referrerId?: number | null + + @Column({ + name: 'contribution_link_id', + type: 'int', + unsigned: true, + nullable: true, + default: null, + }) + contributionLinkId?: number | null + + @Column({ name: 'publisher_id', default: 0 }) + publisherId: number + + @OneToMany(() => Contribution, (contribution) => contribution.user) + @JoinColumn({ name: 'user_id' }) + contributions?: Contribution[] + + @OneToMany(() => ContributionMessage, (message) => message.user) + @JoinColumn({ name: 'user_id' }) + messages?: ContributionMessage[] + + @OneToMany(() => UserContact, (userContact: UserContact) => userContact.user) + @JoinColumn({ name: 'user_id' }) + userContacts?: UserContact[] +} diff --git a/database/entity/User.ts b/database/entity/User.ts index eb66bdee9..21785ee9c 100644 --- a/database/entity/User.ts +++ b/database/entity/User.ts @@ -1 +1 @@ -export { User } from './0069-add_user_roles_table/User' +export { User } from './0073-introduce_foreign_user_in_users_table/User' diff --git a/database/migrations/0073-introduce_foreign_user_in_users_table.ts b/database/migrations/0073-introduce_foreign_user_in_users_table.ts new file mode 100644 index 000000000..f04c2f004 --- /dev/null +++ b/database/migrations/0073-introduce_foreign_user_in_users_table.ts @@ -0,0 +1,21 @@ +/* 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 `users` ADD COLUMN IF NOT EXISTS `foreign` tinyint(4) NOT NULL DEFAULT 0 AFTER `id`;', + ) + + await queryFn( + 'ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `community_uuid` char(36) DEFAULT NULL NULL AFTER `gradido_id`;', + ) + await queryFn( + 'ALTER TABLE `users` ADD CONSTRAINT uuid_key UNIQUE KEY (`gradido_id`, `community_uuid`);', + ) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn('ALTER TABLE `users` DROP CONSTRAINT IF EXISTS `uuid_key`;') + await queryFn('ALTER TABLE `users` DROP COLUMN IF EXISTS `foreign`;') + await queryFn('ALTER TABLE `users` DROP COLUMN IF EXISTS `community_uuid`;') +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 78755f280..2d88e0f92 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -4,7 +4,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0072-add_communityuuid_to_transactions_table', + DB_VERSION: '0073-introduce_foreign_user_in_users_table', 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 e88267589..7cc9ef37e 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0072-add_communityuuid_to_transactions_table', + DB_VERSION: '0073-introduce_foreign_user_in_users_table', 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