From 76b0f45862497011a29442eea48f4938c052b3cd Mon Sep 17 00:00:00 2001 From: sandrvvu Date: Tue, 24 Sep 2024 17:42:21 +0300 Subject: [PATCH] feat: add notification enum status instead boolean is read gf-460 --- ...0637_add_status_to_notifications_table.ts} | 9 ++++--- .../modules/notifications/libs/enums/enums.ts | 2 +- .../modules/notifications/libs/types/types.ts | 1 + .../notifications/notification.entity.ts | 26 +++++++++++-------- .../notifications/notification.model.ts | 5 ++-- .../notifications/notification.repository.ts | 5 ++-- .../notification-item/notification-item.tsx | 8 +++--- .../notifications-popover/libs/enums/enums.ts | 1 + .../notifications-popover/libs/types/types.ts | 1 + .../notifications-popover.tsx | 2 +- packages/shared/src/index.ts | 2 ++ .../modules/notifications/libs/enums/enums.ts | 1 + .../libs/enums/notification-status.enum.ts | 6 +++++ ...fication-get-all-item-response-dto.type.ts | 4 ++- .../types/notification-status-value.type.ts | 6 +++++ .../modules/notifications/libs/types/types.ts | 1 + .../modules/notifications/notifications.ts | 6 ++++- readme.md | 2 +- 18 files changed, 62 insertions(+), 26 deletions(-) rename apps/backend/src/db/migrations/{20240923175910_add_is_read_to_notifications_table.ts => 20240924130637_add_status_to_notifications_table.ts} (68%) create mode 100644 apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/enums/enums.ts create mode 100644 apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/types/types.ts create mode 100644 packages/shared/src/modules/notifications/libs/enums/notification-status.enum.ts create mode 100644 packages/shared/src/modules/notifications/libs/types/notification-status-value.type.ts diff --git a/apps/backend/src/db/migrations/20240923175910_add_is_read_to_notifications_table.ts b/apps/backend/src/db/migrations/20240924130637_add_status_to_notifications_table.ts similarity index 68% rename from apps/backend/src/db/migrations/20240923175910_add_is_read_to_notifications_table.ts rename to apps/backend/src/db/migrations/20240924130637_add_status_to_notifications_table.ts index 567a6fb99..700465a79 100644 --- a/apps/backend/src/db/migrations/20240923175910_add_is_read_to_notifications_table.ts +++ b/apps/backend/src/db/migrations/20240924130637_add_status_to_notifications_table.ts @@ -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 { 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 { return knex.schema.alterTable(TABLE_NAME, (table) => { - table.dropColumn(ColumnName.IS_READ); + table.dropColumn(ColumnName.STATUS); }); } diff --git a/apps/backend/src/modules/notifications/libs/enums/enums.ts b/apps/backend/src/modules/notifications/libs/enums/enums.ts index cd709b268..1db05b602 100644 --- a/apps/backend/src/modules/notifications/libs/enums/enums.ts +++ b/apps/backend/src/modules/notifications/libs/enums/enums.ts @@ -1 +1 @@ -export { NotificationsApiPath } from "@git-fit/shared"; +export { NotificationsApiPath, NotificationStatus } from "@git-fit/shared"; diff --git a/apps/backend/src/modules/notifications/libs/types/types.ts b/apps/backend/src/modules/notifications/libs/types/types.ts index 04c89ec63..084b4e65e 100644 --- a/apps/backend/src/modules/notifications/libs/types/types.ts +++ b/apps/backend/src/modules/notifications/libs/types/types.ts @@ -5,5 +5,6 @@ export { type NotificationGetAllItemResponseDto, type NotificationGetAllRequestDto, type NotificationGetAllResponseDto, + type NotificationStatusValue, type UserAuthResponseDto, } from "@git-fit/shared"; diff --git a/apps/backend/src/modules/notifications/notification.entity.ts b/apps/backend/src/modules/notifications/notification.entity.ts index a7756be29..696c60e7a 100644 --- a/apps/backend/src/modules/notifications/notification.entity.ts +++ b/apps/backend/src/modules/notifications/notification.entity.ts @@ -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, }); } @@ -65,9 +69,9 @@ class NotificationEntity implements Entity { return new NotificationEntity({ createdAt: null, id: null, - isRead: false, payload, receiverUserId, + status: NotificationStatus.UNREAD, }); } @@ -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, }; } } diff --git a/apps/backend/src/modules/notifications/notification.model.ts b/apps/backend/src/modules/notifications/notification.model.ts index 023082b94..6ef6650b9 100644 --- a/apps/backend/src/modules/notifications/notification.model.ts +++ b/apps/backend/src/modules/notifications/notification.model.ts @@ -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: { diff --git a/apps/backend/src/modules/notifications/notification.repository.ts b/apps/backend/src/modules/notifications/notification.repository.ts index c80859398..d29cda710 100644 --- a/apps/backend/src/modules/notifications/notification.repository.ts +++ b/apps/backend/src/modules/notifications/notification.repository.ts @@ -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"; @@ -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(); @@ -103,7 +104,7 @@ class NotificationRepository implements Repository { const readNotification = await this.notificationModel .query() .patchAndFetchById(id, { - isRead: true, + status: NotificationStatus.READ, }) .execute(); diff --git a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/components/notification-item/notification-item.tsx b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/components/notification-item/notification-item.tsx index 2391c2f32..bb630225f 100644 --- a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/components/notification-item/notification-item.tsx +++ b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/components/notification-item/notification-item.tsx @@ -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 ( diff --git a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/enums/enums.ts b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/enums/enums.ts new file mode 100644 index 000000000..9c2391570 --- /dev/null +++ b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/enums/enums.ts @@ -0,0 +1 @@ +export { NotificationStatus } from "@git-fit/shared"; diff --git a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/types/types.ts b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/types/types.ts new file mode 100644 index 000000000..b8c9ea578 --- /dev/null +++ b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/libs/types/types.ts @@ -0,0 +1 @@ +export { type NotificationStatusValue } from "@git-fit/shared"; diff --git a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/notifications-popover.tsx b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/notifications-popover.tsx index 9ac7a05ba..4127785ac 100644 --- a/apps/frontend/src/libs/components/header/libs/components/notifications-popover/notifications-popover.tsx +++ b/apps/frontend/src/libs/components/header/libs/components/notifications-popover/notifications-popover.tsx @@ -70,9 +70,9 @@ const NotificationsPopover = ({ {hasNotifications ? ( notifications.map((notification) => ( )) diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index ecf183c35..2d07f5b5e 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -109,6 +109,8 @@ export { type NotificationGetAllRequestDto, type NotificationGetAllResponseDto, NotificationsApiPath, + NotificationStatus, + type NotificationStatusValue, } from "./modules/notifications/notifications.js"; export { type PermissionGetAllItemResponseDto, diff --git a/packages/shared/src/modules/notifications/libs/enums/enums.ts b/packages/shared/src/modules/notifications/libs/enums/enums.ts index 9c89113eb..ca5902b86 100644 --- a/packages/shared/src/modules/notifications/libs/enums/enums.ts +++ b/packages/shared/src/modules/notifications/libs/enums/enums.ts @@ -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"; diff --git a/packages/shared/src/modules/notifications/libs/enums/notification-status.enum.ts b/packages/shared/src/modules/notifications/libs/enums/notification-status.enum.ts new file mode 100644 index 000000000..2684f3a53 --- /dev/null +++ b/packages/shared/src/modules/notifications/libs/enums/notification-status.enum.ts @@ -0,0 +1,6 @@ +const NotificationStatus = { + READ: "read", + UNREAD: "unread", +} as const; + +export { NotificationStatus }; diff --git a/packages/shared/src/modules/notifications/libs/types/notification-get-all-item-response-dto.type.ts b/packages/shared/src/modules/notifications/libs/types/notification-get-all-item-response-dto.type.ts index fe0454a6e..5d91c0cd3 100644 --- a/packages/shared/src/modules/notifications/libs/types/notification-get-all-item-response-dto.type.ts +++ b/packages/shared/src/modules/notifications/libs/types/notification-get-all-item-response-dto.type.ts @@ -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 }; diff --git a/packages/shared/src/modules/notifications/libs/types/notification-status-value.type.ts b/packages/shared/src/modules/notifications/libs/types/notification-status-value.type.ts new file mode 100644 index 000000000..9fe4f7225 --- /dev/null +++ b/packages/shared/src/modules/notifications/libs/types/notification-status-value.type.ts @@ -0,0 +1,6 @@ +import { type NotificationStatus } from "../enums/enums.js"; + +type NotificationStatusValue = + (typeof NotificationStatus)[keyof typeof NotificationStatus]; + +export { type NotificationStatusValue }; diff --git a/packages/shared/src/modules/notifications/libs/types/types.ts b/packages/shared/src/modules/notifications/libs/types/types.ts index 3b126d875..dee10b8ec 100644 --- a/packages/shared/src/modules/notifications/libs/types/types.ts +++ b/packages/shared/src/modules/notifications/libs/types/types.ts @@ -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"; diff --git a/packages/shared/src/modules/notifications/notifications.ts b/packages/shared/src/modules/notifications/notifications.ts index 40d470cbf..7e77af56d 100644 --- a/packages/shared/src/modules/notifications/notifications.ts +++ b/packages/shared/src/modules/notifications/notifications.ts @@ -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, @@ -7,4 +10,5 @@ export { type NotificationGetAllItemResponseDto, type NotificationGetAllRequestDto, type NotificationGetAllResponseDto, + type NotificationStatusValue, } from "./libs/types/types.js"; diff --git a/readme.md b/readme.md index 98a1c4460..65b11c444 100644 --- a/readme.md +++ b/readme.md @@ -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 }