Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

Commit 17c9cd6

Browse files
authored
remove user chapter, replace with user chapter role (#361)
1 parent 0b28470 commit 17c9cd6

12 files changed

+151
-80
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.snap

server/controllers/userChapterController.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from 'express';
22
import { Chapter, User } from 'server/models';
3-
import { UserChapter } from 'server/models/UserChapter';
3+
import { UserChapterRole } from 'server/models/UserChapterRole';
44

55
// The whole model is a json response, fix that if there's some sensitive data here
66

@@ -12,14 +12,15 @@ export default {
1212
const user = await User.findOne({ id: user_id });
1313

1414
if (chapter && user) {
15-
const userChapter = new UserChapter({
16-
user,
17-
chapter,
15+
const userChapterRole = new UserChapterRole({
16+
userId: user_id,
17+
chapterId: chapter_id,
18+
roleName: 'member',
1819
});
1920

2021
try {
21-
await userChapter.save();
22-
res.status(201).json(userChapter);
22+
await userChapterRole.save();
23+
res.status(201).json(userChapterRole);
2324
} catch (e) {
2425
res.status(500).json({ error: e });
2526
}
@@ -29,13 +30,13 @@ export default {
2930
},
3031
async ban(req: Request, res: Response) {
3132
const { id, user_id } = req.params;
32-
const userChapter = await UserChapter.findOne({
33+
const userChapterRole = await UserChapterRole.findOne({
3334
where: { user_id: parseInt(user_id), chapter_id: parseInt(id) },
3435
});
3536

36-
if (userChapter) {
37+
if (userChapterRole) {
3738
try {
38-
await userChapter.remove();
39+
await userChapterRole.remove();
3940
res.status(201).json({ id, user_id });
4041
} catch (e) {
4142
res.json(500).json({ error: e });

server/factories/user_chapter.factory.ts

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Faker from 'faker';
2+
import { define } from 'typeorm-seeding';
3+
4+
import { Chapter } from 'server/models/Chapter';
5+
import { User } from 'server/models/User';
6+
import { UserChapterRole, ChapterRoles } from 'server/models/UserChapterRole';
7+
8+
define(UserChapterRole, (
9+
_faker: typeof Faker,
10+
params: { user: User; chapter: Chapter; roleName: ChapterRoles },
11+
) => {
12+
const { user, chapter, roleName } = params;
13+
14+
const userChapter = new UserChapterRole({
15+
userId: user.id,
16+
chapterId: chapter.id,
17+
roleName,
18+
});
19+
20+
return userChapter;
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class RemoveChapterUser1595424588635 implements MigrationInterface {
4+
name = 'RemoveChapterUser1595424588635';
5+
6+
public async up(queryRunner: QueryRunner): Promise<any> {
7+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "interested" boolean NOT NULL`, undefined);
8+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined);
9+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b" PRIMARY KEY ("user_id", "chapter_id")`, undefined);
10+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined);
11+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" text NOT NULL`, undefined);
12+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b"`, undefined);
13+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined);
14+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined);
15+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined);
16+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined);
17+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" text NOT NULL`, undefined);
18+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined);
19+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined);
20+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" DROP DEFAULT`, undefined);
21+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" DROP DEFAULT`, undefined);
22+
}
23+
24+
public async down(queryRunner: QueryRunner): Promise<any> {
25+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" SET DEFAULT true`, undefined);
26+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" SET DEFAULT false`, undefined);
27+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined);
28+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined);
29+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined);
30+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" character varying NOT NULL`, undefined);
31+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined);
32+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined);
33+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined);
34+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b" PRIMARY KEY ("user_id", "chapter_id")`, undefined);
35+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined);
36+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" character varying NOT NULL`, undefined);
37+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_e18f99bd3676bd6ff32cc045d2b"`, undefined);
38+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined);
39+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "interested"`, undefined);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class ModifyUCR1595425069646 implements MigrationInterface {
4+
name = 'ModifyUCR1595425069646';
5+
6+
public async up(queryRunner: QueryRunner): Promise<any> {
7+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "id" SERIAL NOT NULL`, undefined);
8+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d"`, undefined);
9+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "role_name", "id")`, undefined);
10+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "created_at" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
11+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "updated_at" TIMESTAMP NOT NULL DEFAULT now()`, undefined);
12+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "interested" boolean NOT NULL`, undefined);
13+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined);
14+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2" PRIMARY KEY ("user_id", "chapter_id", "id")`, undefined);
15+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined);
16+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" text NOT NULL`, undefined);
17+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2"`, undefined);
18+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "id", "role_name")`, undefined);
19+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined);
20+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined);
21+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined);
22+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" text NOT NULL`, undefined);
23+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined);
24+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined);
25+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" DROP DEFAULT`, undefined);
26+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" DROP DEFAULT`, undefined);
27+
}
28+
29+
public async down(queryRunner: QueryRunner): Promise<any> {
30+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "interested" SET DEFAULT true`, undefined);
31+
await queryRunner.query(`ALTER TABLE "rsvps" ALTER COLUMN "canceled" SET DEFAULT false`, undefined);
32+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_a6668338a373b7a8d914a193f3f"`, undefined);
33+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0" PRIMARY KEY ("user_id")`, undefined);
34+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP COLUMN "role_name"`, undefined);
35+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD "role_name" character varying NOT NULL`, undefined);
36+
await queryRunner.query(`ALTER TABLE "user_instance_roles" DROP CONSTRAINT "PK_f6aa9d1d5bffdd18382d18729d0"`, undefined);
37+
await queryRunner.query(`ALTER TABLE "user_instance_roles" ADD CONSTRAINT "PK_a6668338a373b7a8d914a193f3f" PRIMARY KEY ("user_id", "role_name")`, undefined);
38+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined);
39+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2" PRIMARY KEY ("user_id", "chapter_id", "id")`, undefined);
40+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "role_name"`, undefined);
41+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD "role_name" character varying NOT NULL`, undefined);
42+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_9daf4841d7ac950bb3c511ad5f2"`, undefined);
43+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_41033d3ee61995e854787fc7230" PRIMARY KEY ("user_id", "chapter_id", "role_name", "id")`, undefined);
44+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "interested"`, undefined);
45+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "updated_at"`, undefined);
46+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "created_at"`, undefined);
47+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP CONSTRAINT "PK_41033d3ee61995e854787fc7230"`, undefined);
48+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" ADD CONSTRAINT "PK_d4726976f7c0d644f1a01cebe6d" PRIMARY KEY ("user_id", "chapter_id", "role_name")`, undefined);
49+
await queryRunner.query(`ALTER TABLE "user_chapter_roles" DROP COLUMN "id"`, undefined);
50+
}
51+
}

server/models/Chapter.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BaseModel } from './BaseModel';
33
import { Event } from './Event';
44
import { Location } from './Location';
55
import { User } from './User';
6-
import { UserChapter } from './UserChapter';
6+
import { UserChapterRole } from './UserChapterRole';
77
import { UserBan } from './UserBan';
88

99
@Entity({ name: 'chapters' })
@@ -41,10 +41,10 @@ export class Chapter extends BaseModel {
4141
creator!: User;
4242

4343
@OneToMany(
44-
_type => UserChapter,
45-
userChapter => userChapter.chapter,
44+
_type => UserChapterRole,
45+
UserChapterRole => UserChapterRole.chapter,
4646
)
47-
users!: UserChapter[];
47+
users!: UserChapterRole[];
4848

4949
@OneToMany(
5050
_type => UserBan,

server/models/User.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { BaseModel } from './BaseModel';
33
import { SocialProviderUser } from './SocialProviderUser';
44
import { Chapter } from './Chapter';
55
import { Rsvp } from './Rsvp';
6-
import { UserChapter } from './UserChapter';
76
import { UserBan } from './UserBan';
87
import { UserChapterRole } from './UserChapterRole';
98
import { UserInstanceRole } from './UserInstanceRole';
@@ -38,10 +37,10 @@ export class User extends BaseModel {
3837
rsvps!: Rsvp[];
3938

4039
@OneToMany(
41-
_type => UserChapter,
40+
_type => UserChapterRole,
4241
chapter => chapter.user,
4342
)
44-
chapters!: UserChapter[];
43+
chapters!: UserChapterRole[];
4544

4645
@OneToMany(
4746
_type => UserBan,

server/models/UserChapter.ts

-38
This file was deleted.

server/models/UserChapterRole.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Entity, JoinColumn, ManyToOne, PrimaryColumn } from 'typeorm';
1+
import { Entity, JoinColumn, ManyToOne, PrimaryColumn, Column } from 'typeorm';
22
import { User } from './User';
33
import { Chapter } from './Chapter';
4+
import { BaseModel } from './BaseModel';
45

56
export type ChapterRoles = 'organizer' | 'member';
67

78
@Entity({ name: 'user_chapter_roles' })
8-
export class UserChapterRole {
9+
export class UserChapterRole extends BaseModel {
910
@PrimaryColumn()
1011
user_id!: number;
1112

@@ -23,15 +24,25 @@ export class UserChapterRole {
2324
@JoinColumn({ name: 'chapter_id' })
2425
chapter!: Chapter;
2526

27+
/*
28+
This indicates whether he use wants to receive notifications about this chapter.
29+
Defaults to True when the user joins this chapter
30+
*/
31+
@Column({ nullable: false })
32+
interested: boolean;
33+
2634
constructor(params: {
2735
userId: number;
2836
roleName: ChapterRoles;
2937
chapterId: number;
38+
interested?: boolean;
3039
}) {
40+
super();
3141
if (params) {
3242
this.user_id = params.userId;
3343
this.role_name = params.roleName;
3444
this.chapter_id = params.chapterId;
45+
this.interested = params.interested || true;
3546
}
3647
}
3748
}

server/models/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export * from './Sponsor';
1010
export * from './Tag';
1111
export * from './User';
1212
export * from './UserBan';
13-
export * from './UserChapter';
13+
export * from './UserChapterRole';
1414
export * from './Venue';

server/seeds/008_user_chapter.seed.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { Factory, Seeder } from 'typeorm-seeding';
22

33
import { Chapter } from '../models/Chapter';
44
import { User } from '../models/User';
5-
import { UserChapter } from '../models/UserChapter';
5+
import { UserChapterRole } from '../models/UserChapterRole';
66

7-
export default class CreateUserChapter implements Seeder {
7+
export default class CreateUserChapterRole implements Seeder {
88
public async run(factory: Factory): Promise<any> {
99
const user = await User.findOne();
1010
const chapter = await Chapter.findOne();
1111

12-
await factory(UserChapter)({ user, chapter }).seedMany(5);
12+
await factory(UserChapterRole)({
13+
user,
14+
chapter,
15+
roleName: Math.random() > 0.5 ? 'organizer' : 'member',
16+
}).seedMany(5);
1317
}
1418
}

0 commit comments

Comments
 (0)