Skip to content

Commit

Permalink
feat: collaborators (#510)
Browse files Browse the repository at this point in the history
* build(devdeps): add lodash types

* fix(model): rectify db model definitions

* refactor: add message param to ForbiddenError

* feat: add CollaboratorsService

* test: add tests for CollaboratorsService

* feat: use CollaboratorService in authorization middleware

* test: add tests for authorization middleware

* feat: add CollaboratorsRouter

* test: add tests for CollaboratorsRouter

* feat(db-migration): change site_members role enum in the database

* feat: modify authzMiddlewareService tests

* fix: error in mock collaborators fixture
  • Loading branch information
prestonlimlianjie authored Oct 4, 2022
1 parent ab1f0a3 commit 398d70f
Show file tree
Hide file tree
Showing 22 changed files with 1,522 additions and 73 deletions.
46 changes: 27 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@tsconfig/recommended": "^1.0.1",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.186",
"@types/node": "^17.0.21",
"@types/supertest": "^2.0.11",
"@types/validator": "^13.7.1",
Expand Down
5 changes: 5 additions & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export enum SiteStatus {
Launched = "LAUNCHED",
}

export enum CollaboratorRoles {
Admin = "ADMIN",
Contributor = "CONTRIBUTOR",
}

export const E2E_ISOMER_ID = "-1"
export const E2E_TEST_EMAIL = "test@e2e"
export const E2E_TEST_CONTACT = "12345678"
59 changes: 59 additions & 0 deletions src/database/migrations/20220811070630-change-role-enum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
async up(queryInterface, Sequelize) {
// Change the role enum values in the site_members table
await queryInterface.sequelize.transaction(async (transaction) => {
// 1. Change column type to TEXT
await queryInterface.changeColumn(
"site_members", // name of Source model
"role", // name of column we're modifying
{
type: Sequelize.TEXT,
},
{ transaction }
)
// 2. Discard enum type
await queryInterface.sequelize.query(
"drop type enum_site_members_role;",
{ transaction }
)
// 3. Change column type to new enum type (fails if inconsistent with existing data)
await queryInterface.changeColumn(
"site_members", // name of Source model
"role", // name of column we're modifying
{
type: Sequelize.ENUM("ADMIN", "CONTRIBUTOR"),
},
{ transaction }
)
})
},

async down(queryInterface, Sequelize) {
// Change the role enum values in the site_members table
await queryInterface.sequelize.transaction(async (transaction) => {
// 1. Change column type to TEXT
await queryInterface.changeColumn(
"site_members", // name of Source model
"role", // name of column we're modifying
{
type: Sequelize.TEXT,
},
{ transaction }
)
// 2. Discard enum type
await queryInterface.sequelize.query(
"drop type enum_site_members_role;",
{ transaction }
)
// 3. Change column type to new enum type (fails if inconsistent with existing data)
await queryInterface.changeColumn(
"site_members", // name of Source model
"role", // name of column we're modifying
{
type: Sequelize.ENUM("ADMIN", "USER"),
},
{ transaction }
)
})
},
}
3 changes: 2 additions & 1 deletion src/database/models/Site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Site extends Model {
@Column({
allowNull: false,
type: DataType.TEXT,
unique: true,
})
name!: string

Expand Down Expand Up @@ -70,7 +71,7 @@ export class Site extends Model {
through: () => SiteMember,
as: "site_members",
})
users!: User[]
site_members!: Array<User & { SiteMember: SiteMember }>

@HasOne(() => Repo)
repo?: Repo
Expand Down
6 changes: 4 additions & 2 deletions src/database/models/SiteMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
UpdatedAt,
} from "sequelize-typescript"

import { CollaboratorRoles } from "@constants/index"

import { Site } from "@database/models/Site"
import { User } from "@database/models/User"

Expand All @@ -23,9 +25,9 @@ export class SiteMember extends Model {

@Column({
allowNull: false,
type: DataType.ENUM("ADMIN", "USER"),
type: DataType.ENUM("ADMIN", "CONTRIBUTOR"),
})
role!: boolean
role!: CollaboratorRoles

@CreatedAt
createdAt!: Date
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class User extends Model {
through: () => SiteMember,
as: "site_members",
})
sites!: Site[]
sites!: Array<Site & { SiteMember: SiteMember }>

@HasMany(() => Site, {
as: "sites_created",
Expand Down
4 changes: 2 additions & 2 deletions src/errors/ForbiddenError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const { BaseIsomerError } = require("@errors/BaseError")

class ForbiddenError extends BaseIsomerError {
constructor() {
super(403, "Access forbidden")
constructor(message) {
super(403, message || "Access forbidden")
}
}

Expand Down
Loading

0 comments on commit 398d70f

Please sign in to comment.