Skip to content

Commit

Permalink
feat: add notification enum status instead boolean is read gf-460
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrvvu committed Sep 24, 2024
1 parent bf4a3f1 commit 76b0f45
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import { type Knex } from "knex";
const TABLE_NAME = "notifications";

const ColumnName = {
IS_READ: "is_read",
STATUS: "status",
} as const;

function up(knex: Knex): Promise<void> {
return knex.schema.alterTable(TABLE_NAME, (table) => {
table.boolean(ColumnName.IS_READ).notNullable().defaultTo(false);
table
.enu(ColumnName.STATUS, ["unread", "read"])
.notNullable()
.defaultTo("unread");
});
}

function down(knex: Knex): Promise<void> {
return knex.schema.alterTable(TABLE_NAME, (table) => {
table.dropColumn(ColumnName.IS_READ);
table.dropColumn(ColumnName.STATUS);
});
}

Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/modules/notifications/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { NotificationsApiPath } from "@git-fit/shared";
export { NotificationsApiPath, NotificationStatus } from "@git-fit/shared";
1 change: 1 addition & 0 deletions apps/backend/src/modules/notifications/libs/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export {
type NotificationGetAllItemResponseDto,
type NotificationGetAllRequestDto,
type NotificationGetAllResponseDto,
type NotificationStatusValue,
type UserAuthResponseDto,
} from "@git-fit/shared";
26 changes: 15 additions & 11 deletions apps/backend/src/modules/notifications/notification.entity.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,61 @@
import { type Entity } from "~/libs/types/types.js";

import { type NotificationGetAllItemResponseDto } from "./libs/types/types.js";
import { NotificationStatus } from "./libs/enums/enums.js";
import {
type NotificationGetAllItemResponseDto,
type NotificationStatusValue,
} from "./libs/types/types.js";

class NotificationEntity implements Entity {
private createdAt: null | string;

private id: null | number;

private isRead: boolean;

private payload!: string;

private receiverUserId!: number;

private status!: NotificationStatusValue;

private constructor({
createdAt,
id,
isRead,
payload,
receiverUserId,
status,
}: {
createdAt: null | string;
id: null | number;
isRead: boolean;
payload: string;
receiverUserId: number;
status: NotificationStatusValue;
}) {
this.createdAt = createdAt;
this.id = id;
this.isRead = isRead;
this.payload = payload;
this.receiverUserId = receiverUserId;
this.status = status;
}

public static initialize({
createdAt,
id,
isRead,
payload,
receiverUserId,
status,
}: {
createdAt: string;
id: null | number;
isRead: boolean;
payload: string;
receiverUserId: number;
status: NotificationStatusValue;
}): NotificationEntity {
return new NotificationEntity({
createdAt,
id,
isRead,
payload,
receiverUserId,
status,
});
}

Expand All @@ -65,9 +69,9 @@ class NotificationEntity implements Entity {
return new NotificationEntity({
createdAt: null,
id: null,
isRead: false,
payload,
receiverUserId,
status: NotificationStatus.UNREAD,
});
}

Expand All @@ -85,8 +89,8 @@ class NotificationEntity implements Entity {
return {
createdAt: this.createdAt as string,
id: this.id as number,
isRead: this.isRead,
payload: this.payload,
status: this.status,
};
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/backend/src/modules/notifications/notification.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
} from "~/libs/modules/database/database.js";

import { UserModel } from "../users/users.js";
import { type NotificationStatusValue } from "./libs/types/types.js";

class NotificationModel extends AbstractModel {
public isRead!: boolean;

public payload!: string;

public receiverUserId!: number;

public status!: NotificationStatusValue;

public static override get relationMappings(): RelationMappings {
return {
receiverUser: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type Repository,
} from "~/libs/types/types.js";

import { NotificationStatus } from "./libs/enums/enums.js";
import { type NotificationGetAllRequestDto } from "./libs/types/types.js";
import { NotificationEntity } from "./notification.entity.js";
import { type NotificationModel } from "./notification.model.js";
Expand Down Expand Up @@ -88,7 +89,7 @@ class NotificationRepository implements Repository {
const notifications = await this.notificationModel
.query()
.where("receiverUserId", userId)
.where("isRead", false)
.where("status", NotificationStatus.UNREAD)
.orderBy("created_at", SortType.DESCENDING)
.execute();

Expand All @@ -103,7 +104,7 @@ class NotificationRepository implements Repository {
const readNotification = await this.notificationModel
.query()
.patchAndFetchById(id, {
isRead: true,
status: NotificationStatus.READ,
})
.execute();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { getValidClassNames } from "~/libs/helpers/helpers.js";

import { NotificationStatus } from "../../enums/enums.js";
import { type NotificationStatusValue } from "../../types/types.js";
import styles from "./styles.module.css";

type Properties = {
isRead: boolean;
message: string;
status: NotificationStatusValue;
timestamp: string;
};

const NotificationItem = ({
isRead,
message,
status,
timestamp,
}: Properties): JSX.Element => {
const itemClassName = getValidClassNames(
styles["notification-item"],
!isRead && styles["notification-item-unread"],
status === NotificationStatus.UNREAD && styles["notification-item-unread"],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { NotificationStatus } from "@git-fit/shared";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { type NotificationStatusValue } from "@git-fit/shared";
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const NotificationsPopover = ({
{hasNotifications ? (
notifications.map((notification) => (
<NotificationItem
isRead={notification.isRead}
key={notification.id}
message={notification.payload}
status={notification.status}
timestamp={formatRelativeTime(notification.createdAt)}
/>
))
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export {
type NotificationGetAllRequestDto,
type NotificationGetAllResponseDto,
NotificationsApiPath,
NotificationStatus,
type NotificationStatusValue,
} from "./modules/notifications/notifications.js";
export {
type PermissionGetAllItemResponseDto,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { NotificationStatus } from "./notification-status.enum.js";
export { NotificationValidationMessage } from "./notification-validation-message.enum.js";
export { NotificationValidationRule } from "./notification-validation-rule.enum.js";
export { NotificationsApiPath } from "./notifications-api-path.enum.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const NotificationStatus = {
READ: "read",
UNREAD: "unread",
} as const;

export { NotificationStatus };
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { type NotificationStatusValue } from "./notification-status-value.type.js";

type NotificationGetAllItemResponseDto = {
createdAt: string;
id: number;
isRead: boolean;
payload: string;
status: NotificationStatusValue;
};

export { type NotificationGetAllItemResponseDto };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { type NotificationStatus } from "../enums/enums.js";

type NotificationStatusValue =
(typeof NotificationStatus)[keyof typeof NotificationStatus];

export { type NotificationStatusValue };
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { type NotificationCreateRequestDto } from "./notification-create-request
export { type NotificationGetAllItemResponseDto } from "./notification-get-all-item-response-dto.type.js";
export { type NotificationGetAllRequestDto } from "./notification-get-all-request-dto.type.js";
export { type NotificationGetAllResponseDto } from "./notification-get-all-response-dto.type.js";
export { type NotificationStatusValue } from "./notification-status-value.type.js";
6 changes: 5 additions & 1 deletion packages/shared/src/modules/notifications/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { NotificationsApiPath } from "./libs/enums/enums.js";
export {
NotificationsApiPath,
NotificationStatus,
} from "./libs/enums/enums.js";
export { NotificationError } from "./libs/exceptions/exceptions.js";
export {
type NotificationBulkCreateRequestDto,
Expand All @@ -7,4 +10,5 @@ export {
type NotificationGetAllItemResponseDto,
type NotificationGetAllRequestDto,
type NotificationGetAllResponseDto,
type NotificationStatusValue,
} from "./libs/types/types.js";
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ erDiagram
notifications {
int id PK
boolean is_read
dateTime created_at
dateTime updated_at
int receiver_user_id FK
enum status
varchar payload
}
Expand Down

0 comments on commit 76b0f45

Please sign in to comment.