Skip to content

Commit

Permalink
Feat/notifications (#508)
Browse files Browse the repository at this point in the history
* Feat: add notification database model and update related models

* Chore: add migrations

Also adds id to site_members table for easier reference

* Feat: add notificationService

* Feat: add notificationUtils

* Feat: add notifications router

* Chore: initialise Notifications table and services

* Fix: remove unused imports

* Fix: change behaviour of quick retrieval

Always returns only new notifications now, unless there are none, in which case it returns most recent 6

* Chore: remove unused imports

* Refactor: findAll method

* Chore: add notificationResponse type

* Feat: add created_at sorting criteria

* Fix: notification sorting order

* Chore: add documentation for sort criteria

* Fix: rebase errors

* Fix: rebase errors for tests
  • Loading branch information
alexanderleegs authored Oct 21, 2022
1 parent 90eb48e commit a78d7e4
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
Promise.all([
queryInterface.changeColumn("site_members", "user_id", {
allowNull: false,
primaryKey: false,
type: Sequelize.BIGINT,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction,
}),
queryInterface.changeColumn("site_members", "site_id", {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: false,
references: {
model: "sites",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction,
}),
queryInterface.addColumn(
"site_members", // name of Source model
"id", // name of column we're adding
{
unique: true,
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.BIGINT,
transaction,
}
),
])
})
},

async down(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
Promise.all([
queryInterface.removeColumn(
"site_members", // name of Source Model
"id", // name of column we want to remove
{ transaction }
),
queryInterface.changeColumn("site_members", "user_id", {
allowNull: false,
primaryKey: true,
type: Sequelize.BIGINT,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction,
}),
queryInterface.changeColumn("site_members", "site_id", {
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true,
references: {
model: "sites",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction,
}),
])
})
},
}
80 changes: 80 additions & 0 deletions src/database/migrations/20220926081632-create-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("notifications", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.BIGINT,
},
site_member_id: {
allowNull: false,
type: Sequelize.BIGINT,
references: {
model: "site_members",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
site_id: {
allowNull: false,
type: Sequelize.BIGINT,
references: {
model: "sites",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
user_id: {
allowNull: false,
type: Sequelize.BIGINT,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
message: {
allowNull: true,
type: Sequelize.STRING,
},
link: {
allowNull: true,
type: Sequelize.STRING,
},
source_username: {
allowNull: false,
type: Sequelize.STRING,
},
type: {
allowNull: false,
type: Sequelize.STRING,
},
first_read_time: {
allowNull: true,
type: Sequelize.DATE,
},
priority: {
allowNull: false,
type: Sequelize.BIGINT,
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn("NOW"),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn("NOW"),
},
})
},

down: async (queryInterface) => {
await queryInterface.dropTable("notifications")
},
}
88 changes: 88 additions & 0 deletions src/database/models/Notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
DataType,
Column,
Model,
Table,
CreatedAt,
UpdatedAt,
DeletedAt,
BelongsToMany,
HasOne,
BelongsTo,
ForeignKey,
} from "sequelize-typescript"

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

@Table({ tableName: "notifications" })
export class Notification extends Model {
@Column({
autoIncrement: true,
primaryKey: true,
allowNull: false,
type: DataType.BIGINT,
})
id!: number

@ForeignKey(() => SiteMember)
siteMemberId!: number

@BelongsTo(() => SiteMember)
siteMember!: SiteMember

@ForeignKey(() => Site)
siteId!: number

@BelongsTo(() => Site)
site!: Site

@ForeignKey(() => User)
userId!: number

@BelongsTo(() => User)
user!: Site

@Column({
allowNull: true,
type: DataType.TEXT,
})
message!: string

@Column({
allowNull: true,
type: DataType.TEXT,
})
link!: string

@Column({
allowNull: true,
type: DataType.TEXT,
})
sourceUsername!: string

@Column({
allowNull: false,
type: DataType.TEXT,
})
type!: string

@Column({
allowNull: true,
type: DataType.DATE,
})
firstReadTime!: Date | null

@Column({
allowNull: false,
type: DataType.BIGINT,
})
priority!: number

@CreatedAt
createdAt!: Date

@UpdatedAt
updatedAt!: Date
}
20 changes: 20 additions & 0 deletions src/database/models/SiteMember.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import {
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey,
HasMany,
Model,
Table,
UpdatedAt,
} from "sequelize-typescript"

import { CollaboratorRoles } from "@constants/index"

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

@Table({ tableName: "site_members" })
export class SiteMember extends Model {
@Column({
autoIncrement: true,
primaryKey: true,
allowNull: false,
type: DataType.BIGINT,
})
id!: number

@ForeignKey(() => User)
@Column
userId!: number
Expand All @@ -34,4 +45,13 @@ export class SiteMember extends Model {

@UpdatedAt
updatedAt!: Date

@BelongsTo(() => Site)
site!: Site

@BelongsTo(() => User)
user!: User

@HasMany(() => Notification)
notifications?: Notification[]
}
1 change: 1 addition & 0 deletions src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "@database/models/IsomerAdmin"
export * from "@database/models/ReviewMeta"
export * from "@database/models/ReviewRequest"
export * from "@database/models/Reviewers"
export * from "@database/models/Notification"
8 changes: 8 additions & 0 deletions src/routes/v2/authenticatedSites/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { attachSiteHandler } from "@root/middleware"

import { NotificationsRouter } from "./notifications"

const express = require("express")

const {
Expand Down Expand Up @@ -88,6 +90,7 @@ const getAuthenticatedSitesSubrouter = ({
authorizationMiddleware,
gitHubService,
configYmlService,
notificationsService,
}) => {
const collectionYmlService = new CollectionYmlService({ gitHubService })
const homepagePageService = new HomepagePageService({ gitHubService })
Expand Down Expand Up @@ -184,6 +187,7 @@ const getAuthenticatedSitesSubrouter = ({
const navigationV2Router = new NavigationRouter({
navigationYmlService: navYmlService,
})
const notificationsRouter = new NotificationsRouter({ notificationsService })

const authenticatedSitesSubrouter = express.Router({ mergeParams: true })

Expand Down Expand Up @@ -221,6 +225,10 @@ const getAuthenticatedSitesSubrouter = ({
authenticatedSitesSubrouter.use("/contactUs", contactUsV2Router.getRouter())
authenticatedSitesSubrouter.use("/homepage", homepageV2Router.getRouter())
authenticatedSitesSubrouter.use("/settings", settingsV2Router.getRouter())
authenticatedSitesSubrouter.use(
"/notifications",
notificationsRouter.getRouter()
)

return authenticatedSitesSubrouter
}
Expand Down
Loading

0 comments on commit a78d7e4

Please sign in to comment.