diff --git a/src/database/migrations/20221012064037-create-review-request-views.js b/src/database/migrations/20221012064037-create-review-request-views.js new file mode 100644 index 000000000..18e9c04a7 --- /dev/null +++ b/src/database/migrations/20221012064037-create-review-request-views.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..43b950b2f --- /dev/null +++ b/src/database/models/ReviewRequestView.ts @@ -0,0 +1,56 @@ +import { + ForeignKey, + DataType, + Column, + Model, + Table, + BelongsTo, + 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 ReviewRequestView extends Model { + @ForeignKey(() => ReviewRequest) + @PrimaryKey + @Column + reviewRequestId!: number + + @BelongsTo(() => ReviewRequest, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + reviewRequest!: ReviewRequest + + @ForeignKey(() => Site) + @PrimaryKey + @Column + siteId!: number + + @BelongsTo(() => Site, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + site!: Site + + @ForeignKey(() => User) + @PrimaryKey + @Column + userId!: number + + @BelongsTo(() => User, { + onUpdate: "CASCADE", + onDelete: "CASCADE", + }) + user!: User + + @Column({ + allowNull: true, + type: DataType.DATE, + }) + lastViewedAt!: Date +} diff --git a/src/database/models/index.ts b/src/database/models/index.ts index 42db40b18..89825da63 100644 --- a/src/database/models/index.ts +++ b/src/database/models/index.ts @@ -12,3 +12,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" diff --git a/src/server.js b/src/server.js index f7e4b44aa..4bebd8d8a 100644 --- a/src/server.js +++ b/src/server.js @@ -19,6 +19,7 @@ import { ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, } from "@database/models" import bootstrap from "@root/bootstrap" import { @@ -67,6 +68,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 f5b5093ea..222be294a 100644 --- a/src/tests/database.ts +++ b/src/tests/database.ts @@ -16,6 +16,7 @@ import { ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, } from "@database/models" const sequelize = new Sequelize({ @@ -37,6 +38,7 @@ sequelize.addModels([ ReviewRequest, ReviewMeta, Reviewer, + ReviewRequestView, ]) // eslint-disable-next-line import/prefer-default-export