From 90aa070d3e45e500d18a0bffccbe4de2fccae9fa Mon Sep 17 00:00:00 2001 From: timmyichen Date: Thu, 12 Mar 2020 06:44:07 +0000 Subject: [PATCH 1/6] remove user chapter, replace with user chapter role --- server/factories/user_chapter.factory.ts | 20 ------------- server/factories/user_chapter_role.factory.ts | 21 +++++++++++++ server/models/Chapter.ts | 8 ++--- server/models/User.ts | 5 ++-- server/models/UserChapter.ts | 30 ------------------- server/seeds/008_user_chapter.seed.ts | 10 +++++-- 6 files changed, 34 insertions(+), 60 deletions(-) delete mode 100644 server/factories/user_chapter.factory.ts create mode 100644 server/factories/user_chapter_role.factory.ts delete mode 100644 server/models/UserChapter.ts diff --git a/server/factories/user_chapter.factory.ts b/server/factories/user_chapter.factory.ts deleted file mode 100644 index ff3d9a6284..0000000000 --- a/server/factories/user_chapter.factory.ts +++ /dev/null @@ -1,20 +0,0 @@ -import Faker from 'faker'; -import { define } from 'typeorm-seeding'; - -import { UserChapter } from 'server/models/UserChapter'; -import { Chapter } from 'server/models/Chapter'; -import { User } from 'server/models/User'; - -define(UserChapter, ( - _faker: typeof Faker, - params: { user: User; chapter: Chapter }, -) => { - const { user, chapter } = params; - - const userChapter = new UserChapter({ - user, - chapter, - }); - - return userChapter; -}); diff --git a/server/factories/user_chapter_role.factory.ts b/server/factories/user_chapter_role.factory.ts new file mode 100644 index 0000000000..5fd17ee319 --- /dev/null +++ b/server/factories/user_chapter_role.factory.ts @@ -0,0 +1,21 @@ +import Faker from 'faker'; +import { define } from 'typeorm-seeding'; + +import { Chapter } from 'server/models/Chapter'; +import { User } from 'server/models/User'; +import { UserChapterRole, ChapterRoles } from 'server/models/UserChapterRole'; + +define(UserChapterRole, ( + _faker: typeof Faker, + params: { user: User; chapter: Chapter; roleName: ChapterRoles }, +) => { + const { user, chapter, roleName } = params; + + const userChapter = new UserChapterRole({ + userId: user.id, + chapterId: chapter.id, + roleName, + }); + + return userChapter; +}); diff --git a/server/models/Chapter.ts b/server/models/Chapter.ts index b3634be0b7..ca08d25e45 100644 --- a/server/models/Chapter.ts +++ b/server/models/Chapter.ts @@ -3,7 +3,7 @@ import { BaseModel } from './BaseModel'; import { Event } from './Event'; import { Location } from './Location'; import { User } from './User'; -import { UserChapter } from './UserChapter'; +import { UserChapterRole } from './UserChapterRole'; import { UserBan } from './UserBan'; @Entity({ name: 'chapters' }) @@ -41,10 +41,10 @@ export class Chapter extends BaseModel { creator!: User; @OneToMany( - _type => UserChapter, - userChapter => userChapter.chapter, + _type => UserChapterRole, + UserChapterRole => UserChapterRole.chapter, ) - users!: UserChapter[]; + users!: UserChapterRole[]; @OneToMany( _type => UserBan, diff --git a/server/models/User.ts b/server/models/User.ts index b505ab4e44..6ab4b34b6c 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -3,7 +3,6 @@ import { BaseModel } from './BaseModel'; import { SocialProviderUser } from './SocialProviderUser'; import { Chapter } from './Chapter'; import { Rsvp } from './Rsvp'; -import { UserChapter } from './UserChapter'; import { UserBan } from './UserBan'; import { UserChapterRole } from './UserChapterRole'; import { UserInstanceRole } from './UserInstanceRole'; @@ -38,10 +37,10 @@ export class User extends BaseModel { rsvps!: Rsvp[]; @OneToMany( - _type => UserChapter, + _type => UserChapterRole, chapter => chapter.user, ) - chapters!: UserChapter[]; + chapters!: UserChapterRole[]; @OneToMany( _type => UserBan, diff --git a/server/models/UserChapter.ts b/server/models/UserChapter.ts deleted file mode 100644 index f47f8f4bcf..0000000000 --- a/server/models/UserChapter.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Entity, ManyToOne, JoinColumn } from 'typeorm'; -import { BaseModel } from './BaseModel'; -import { User } from './User'; -import { Chapter } from './Chapter'; - -@Entity({ name: 'user_chapters' }) -export class UserChapter extends BaseModel { - @ManyToOne( - _type => User, - user => user.chapters, - ) - @JoinColumn({ name: 'user_id' }) - user!: User; - - @ManyToOne( - _type => Chapter, - chapter => chapter.users, - ) - @JoinColumn({ name: 'chapter_id' }) - chapter!: Chapter; - - constructor(params: { user: User; chapter: Chapter }) { - super(); - if (params) { - const { user, chapter } = params; - this.user = user; - this.chapter = chapter; - } - } -} diff --git a/server/seeds/008_user_chapter.seed.ts b/server/seeds/008_user_chapter.seed.ts index 092fb91f06..604a01e256 100644 --- a/server/seeds/008_user_chapter.seed.ts +++ b/server/seeds/008_user_chapter.seed.ts @@ -2,13 +2,17 @@ import { Factory, Seeder } from 'typeorm-seeding'; import { Chapter } from '../models/Chapter'; import { User } from '../models/User'; -import { UserChapter } from '../models/UserChapter'; +import { UserChapterRole } from '../models/UserChapterRole'; -export default class CreateUserChapter implements Seeder { +export default class CreateUserChapterRole implements Seeder { public async run(factory: Factory): Promise { const user = await User.findOne(); const chapter = await Chapter.findOne(); - await factory(UserChapter)({ user, chapter }).seedMany(5); + await factory(UserChapterRole)({ + user, + chapter, + roleName: Math.random() > 0.5 ? 'organizer' : 'member', + }).seedMany(5); } } From cc95977c040accc8654090c2595206ce22500ada Mon Sep 17 00:00:00 2001 From: timchen Date: Wed, 22 Jul 2020 06:30:25 -0700 Subject: [PATCH 2/6] fix export --- server/models/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/models/index.ts b/server/models/index.ts index e9656f8d70..aafb90438c 100644 --- a/server/models/index.ts +++ b/server/models/index.ts @@ -10,5 +10,5 @@ export * from './Sponsor'; export * from './Tag'; export * from './User'; export * from './UserBan'; -export * from './UserChapter'; +export * from './UserChapterRole'; export * from './Venue'; From cb59780ad66f48ab6fc7d2fdcd69d6c7dc9d9197 Mon Sep 17 00:00:00 2001 From: timchen Date: Wed, 22 Jul 2020 06:30:41 -0700 Subject: [PATCH 3/6] migration --- .../1595424588635-RemoveChapterUser.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 server/migrations/1595424588635-RemoveChapterUser.ts diff --git a/server/migrations/1595424588635-RemoveChapterUser.ts b/server/migrations/1595424588635-RemoveChapterUser.ts new file mode 100644 index 0000000000..16b2586f4d --- /dev/null +++ b/server/migrations/1595424588635-RemoveChapterUser.ts @@ -0,0 +1,41 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class RemoveChapterUser1595424588635 implements MigrationInterface { + name = 'RemoveChapterUser1595424588635'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "interested" boolean NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b" PRIMARY KEY ("user_id", "chapter_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" text NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" text NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" DROP DEFAULT`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" DROP DEFAULT`, undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" SET DEFAULT true`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" SET DEFAULT false`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" character varying NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b" PRIMARY KEY ("user_id", "chapter_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" character varying NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "interested"`, undefined); + } +} From 12f2313546c87c11080dfe3bf3b39261a39af9ba Mon Sep 17 00:00:00 2001 From: timchen Date: Wed, 22 Jul 2020 06:30:53 -0700 Subject: [PATCH 4/6] ignroe snapshot files --- .prettierignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000000..69c63cc56c --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +*.snap \ No newline at end of file From e04203c539bde95e6dfcbb16daa3137084d4199a Mon Sep 17 00:00:00 2001 From: timchen Date: Wed, 22 Jul 2020 06:39:00 -0700 Subject: [PATCH 5/6] fix class extension --- server/controllers/userChapterController.ts | 19 ++++++++++--------- server/models/UserChapterRole.ts | 4 +++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/server/controllers/userChapterController.ts b/server/controllers/userChapterController.ts index 116bdc81d8..ebc25aa668 100644 --- a/server/controllers/userChapterController.ts +++ b/server/controllers/userChapterController.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; import { Chapter, User } from 'server/models'; -import { UserChapter } from 'server/models/UserChapter'; +import { UserChapterRole } from 'server/models/UserChapterRole'; // The whole model is a json response, fix that if there's some sensitive data here @@ -12,14 +12,15 @@ export default { const user = await User.findOne({ id: user_id }); if (chapter && user) { - const userChapter = new UserChapter({ - user, - chapter, + const userChapterRole = new UserChapterRole({ + userId: user_id, + chapterId: chapter_id, + roleName: 'member', }); try { - await userChapter.save(); - res.status(201).json(userChapter); + await userChapterRole.save(); + res.status(201).json(userChapterRole); } catch (e) { res.status(500).json({ error: e }); } @@ -29,13 +30,13 @@ export default { }, async ban(req: Request, res: Response) { const { id, user_id } = req.params; - const userChapter = await UserChapter.findOne({ + const userChapterRole = await UserChapterRole.findOne({ where: { user_id: parseInt(user_id), chapter_id: parseInt(id) }, }); - if (userChapter) { + if (userChapterRole) { try { - await userChapter.remove(); + await userChapterRole.remove(); res.status(201).json({ id, user_id }); } catch (e) { res.json(500).json({ error: e }); diff --git a/server/models/UserChapterRole.ts b/server/models/UserChapterRole.ts index bd79bd9187..1e542d47f1 100644 --- a/server/models/UserChapterRole.ts +++ b/server/models/UserChapterRole.ts @@ -1,11 +1,12 @@ import { Entity, JoinColumn, ManyToOne, PrimaryColumn, Column } from 'typeorm'; import { User } from './User'; import { Chapter } from './Chapter'; +import { BaseModel } from './BaseModel'; export type ChapterRoles = 'organizer' | 'member'; @Entity({ name: 'user_chapter_roles' }) -export class UserChapterRole { +export class UserChapterRole extends BaseModel { @PrimaryColumn() user_id!: number; @@ -36,6 +37,7 @@ export class UserChapterRole { chapterId: number; interested?: boolean; }) { + super(); if (params) { this.user_id = params.userId; this.role_name = params.roleName; From 935ef101134042ce8235324714974880bb803fa8 Mon Sep 17 00:00:00 2001 From: timchen Date: Wed, 22 Jul 2020 06:39:09 -0700 Subject: [PATCH 6/6] migration --- server/migrations/1595425069646-ModifyUCR.ts | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 server/migrations/1595425069646-ModifyUCR.ts diff --git a/server/migrations/1595425069646-ModifyUCR.ts b/server/migrations/1595425069646-ModifyUCR.ts new file mode 100644 index 0000000000..bb89de4938 --- /dev/null +++ b/server/migrations/1595425069646-ModifyUCR.ts @@ -0,0 +1,51 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class ModifyUCR1595425069646 implements MigrationInterface { + name = 'ModifyUCR1595425069646'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "id" SERIAL NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "role_name", "id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "created_at" TIMESTAMP NOT NULL DEFAULT now()`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "updated_at" TIMESTAMP NOT NULL DEFAULT now()`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "interested" boolean NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2" PRIMARY KEY ("user_id", "chapter_id", "id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" text NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" text NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" DROP DEFAULT`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" DROP DEFAULT`, undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" SET DEFAULT true`, undefined); + await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" SET DEFAULT false`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" character varying NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined); + await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2" PRIMARY KEY ("user_id", "chapter_id", "id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" character varying NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "role_name", "id")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "interested"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "updated_at"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "created_at"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined); + await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "id"`, undefined); + } +}