From 38a52c59855c1f8bb03f128c460ad28aa9ee3df7 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Wed, 12 Oct 2022 14:58:52 +0800 Subject: [PATCH 1/6] feat: add model and migration for review_request_views table --- ...221012064037-create-review-request-view.js | 59 +++++++++++++++++++ src/database/models/ReviewRequestView.ts | 52 ++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/database/migrations/20221012064037-create-review-request-view.js create mode 100644 src/database/models/ReviewRequestView.ts diff --git a/src/database/migrations/20221012064037-create-review-request-view.js b/src/database/migrations/20221012064037-create-review-request-view.js new file mode 100644 index 000000000..18e9c04a7 --- /dev/null +++ b/src/database/migrations/20221012064037-create-review-request-view.js @@ -0,0 +1,59 @@ +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("review_request_views", { + review_request_id: { + allowNull: false, + primaryKey: true, + type: Sequelize.BIGINT, + references: { + model: "review_requests", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + site_id: { + type: Sequelize.BIGINT, + allowNull: false, + primaryKey: true, + references: { + model: "sites", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + user_id: { + type: Sequelize.BIGINT, + allowNull: false, + primaryKey: true, + references: { + model: "users", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + last_viewed_at: { + type: Sequelize.DATE, + allowNull: true, + defaultValue: null, + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + updated_at: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.fn("NOW"), + }, + }) + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("review_request_views") + }, +} diff --git a/src/database/models/ReviewRequestView.ts b/src/database/models/ReviewRequestView.ts new file mode 100644 index 000000000..8f943672c --- /dev/null +++ b/src/database/models/ReviewRequestView.ts @@ -0,0 +1,52 @@ +import { + ForeignKey, + DataType, + Column, + Model, + Table, + BelongsTo, + PrimaryKey, +} from "sequelize-typescript" + +import { Site } from "@database/models/Site" +import { User } from "@database/models/User" + +@Table({ tableName: "review_request_views" }) +// eslint-disable-next-line import/prefer-default-export +export class ReviewRequest extends Model { + @ForeignKey(() => ReviewRequest) + @PrimaryKey + reviewRequestId!: number + + @BelongsTo(() => ReviewRequest, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + reviewRequest!: ReviewRequest + + @ForeignKey(() => Site) + @PrimaryKey + siteId!: number + + @BelongsTo(() => Site, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + site!: Site + + @ForeignKey(() => User) + @PrimaryKey + userId!: number + + @BelongsTo(() => User, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + user!: User + + @Column({ + allowNull: true, + type: DataType.DATE, + }) + lastViewedAt!: Date +} From 3e08a0bb6a1a24ad3231b103d40c0e9bdf0f503b Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Wed, 12 Oct 2022 15:06:01 +0800 Subject: [PATCH 2/6] chore: rename migration --- ...uest-view.js => 20221012064037-create-review-request-views.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/database/migrations/{20221012064037-create-review-request-view.js => 20221012064037-create-review-request-views.js} (100%) diff --git a/src/database/migrations/20221012064037-create-review-request-view.js b/src/database/migrations/20221012064037-create-review-request-views.js similarity index 100% rename from src/database/migrations/20221012064037-create-review-request-view.js rename to src/database/migrations/20221012064037-create-review-request-views.js From 38580bdc2b2ce12b38bb887cbab4c663c9278c6b Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Fri, 21 Oct 2022 15:33:26 +0800 Subject: [PATCH 3/6] fix: add missing association with ReviewRequest model --- src/database/models/ReviewRequest.ts | 5 +++++ src/database/models/ReviewRequestView.ts | 3 ++- src/database/models/index.ts | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/database/models/ReviewRequest.ts b/src/database/models/ReviewRequest.ts index 22bd5b737..9f78bdcbb 100644 --- a/src/database/models/ReviewRequest.ts +++ b/src/database/models/ReviewRequest.ts @@ -7,6 +7,7 @@ import { BelongsTo, BelongsToMany, HasOne, + HasMany, } from "sequelize-typescript" import { Site } from "@database/models/Site" @@ -15,6 +16,7 @@ import { ReviewRequestStatus } from "@root/constants" import { Reviewer } from "./Reviewers" import { ReviewMeta } from "./ReviewMeta" +import { ReviewRequestView } from "./ReviewRequestView" @Table({ tableName: "review_requests" }) // eslint-disable-next-line import/prefer-default-export @@ -66,4 +68,7 @@ export class ReviewRequest extends Model { as: "reviewers", }) reviewers!: User[] + + @HasMany(() => ReviewRequestView) + reviewRequestViews!: ReviewRequestView[] } diff --git a/src/database/models/ReviewRequestView.ts b/src/database/models/ReviewRequestView.ts index 8f943672c..d4a3e74a4 100644 --- a/src/database/models/ReviewRequestView.ts +++ b/src/database/models/ReviewRequestView.ts @@ -8,12 +8,13 @@ import { PrimaryKey, } from "sequelize-typescript" +import { ReviewRequest } from "@database/models/ReviewRequest" import { Site } from "@database/models/Site" import { User } from "@database/models/User" @Table({ tableName: "review_request_views" }) // eslint-disable-next-line import/prefer-default-export -export class ReviewRequest extends Model { +export class ReviewRequestView extends Model { @ForeignKey(() => ReviewRequest) @PrimaryKey reviewRequestId!: number diff --git a/src/database/models/index.ts b/src/database/models/index.ts index 1d6ceaedb..7f409089f 100644 --- a/src/database/models/index.ts +++ b/src/database/models/index.ts @@ -10,3 +10,4 @@ export * from "@database/models/ReviewMeta" export * from "@database/models/ReviewRequest" export * from "@database/models/Reviewers" export * from "@database/models/Notification" +export * from "@database/models/ReviewRequestView" From d9299dfe04f472d9c32e01318ba2119d0cfddb00 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Wed, 12 Oct 2022 15:42:24 +0800 Subject: [PATCH 4/6] fix: revert adding association in ReviewRequest model --- src/database/models/ReviewRequest.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/database/models/ReviewRequest.ts b/src/database/models/ReviewRequest.ts index 9f78bdcbb..22bd5b737 100644 --- a/src/database/models/ReviewRequest.ts +++ b/src/database/models/ReviewRequest.ts @@ -7,7 +7,6 @@ import { BelongsTo, BelongsToMany, HasOne, - HasMany, } from "sequelize-typescript" import { Site } from "@database/models/Site" @@ -16,7 +15,6 @@ import { ReviewRequestStatus } from "@root/constants" import { Reviewer } from "./Reviewers" import { ReviewMeta } from "./ReviewMeta" -import { ReviewRequestView } from "./ReviewRequestView" @Table({ tableName: "review_requests" }) // eslint-disable-next-line import/prefer-default-export @@ -68,7 +66,4 @@ export class ReviewRequest extends Model { as: "reviewers", }) reviewers!: User[] - - @HasMany(() => ReviewRequestView) - reviewRequestViews!: ReviewRequestView[] } From 05fd6a73441b7c411ddd69f68a39defdc0ff16ae Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Thu, 13 Oct 2022 14:40:52 +0800 Subject: [PATCH 5/6] fix: missing @Column for primary and foreign key --- src/database/models/ReviewRequestView.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/database/models/ReviewRequestView.ts b/src/database/models/ReviewRequestView.ts index d4a3e74a4..43b950b2f 100644 --- a/src/database/models/ReviewRequestView.ts +++ b/src/database/models/ReviewRequestView.ts @@ -17,6 +17,7 @@ import { User } from "@database/models/User" export class ReviewRequestView extends Model { @ForeignKey(() => ReviewRequest) @PrimaryKey + @Column reviewRequestId!: number @BelongsTo(() => ReviewRequest, { @@ -27,6 +28,7 @@ export class ReviewRequestView extends Model { @ForeignKey(() => Site) @PrimaryKey + @Column siteId!: number @BelongsTo(() => Site, { @@ -37,6 +39,7 @@ export class ReviewRequestView extends Model { @ForeignKey(() => User) @PrimaryKey + @Column userId!: number @BelongsTo(() => User, { From 2bf7798294c35f357d2dd257e4d39fc7ac1f0634 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Fri, 21 Oct 2022 11:28:00 +0800 Subject: [PATCH 6/6] fix: add new database table to sequelize --- src/server.js | 3 +++ src/tests/database.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/server.js b/src/server.js index 4f2a91cc8..2fdfa1df1 100644 --- a/src/server.js +++ b/src/server.js @@ -17,6 +17,7 @@ import { ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, } from "@database/models" import bootstrap from "@root/bootstrap" import { @@ -58,6 +59,8 @@ const sequelize = initSequelize([ ReviewRequest, ReviewMeta, Reviewer, + ReviewRequest, + ReviewRequestView, ]) const usersService = getUsersService(sequelize) diff --git a/src/tests/database.ts b/src/tests/database.ts index 6628fd24f..87521eae2 100644 --- a/src/tests/database.ts +++ b/src/tests/database.ts @@ -14,6 +14,7 @@ import { ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, } from "@database/models" const sequelize = new Sequelize({ @@ -33,6 +34,7 @@ sequelize.addModels([ ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, ]) // eslint-disable-next-line import/prefer-default-export