From fdab3fa6873235d2f414ebc074be6ccc96c73f13 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sun, 26 May 2024 00:34:28 +0300 Subject: [PATCH] Add device and user notification modules --- .../device.notification.module.ts | 10 ++++++ .../dtos/device.notification.dto.ts | 15 +++++++++ .../modules/device-notification/dtos/index.ts | 1 + .../entities/device.notification.entity.ts | 33 +++++++++++++++++++ .../device-notification/entities/index.ts | 1 + .../device.notification.repository.ts | 10 ++++++ .../device-notification/repositories/index.ts | 1 + .../modules/device/entities/device.entity.ts | 10 +++++- .../modules/user-notification/dtos/index.ts | 1 + .../dtos/user.notification.dto.ts | 19 +++++++++++ .../user-notification/entities/index.ts | 1 + .../entities/user.notification.entity.ts | 27 +++++++++++++++ .../user-notification/repositories/index.ts | 1 + .../user.notification.repository.ts | 10 ++++++ .../user.notification.repository.module.ts | 10 ++++++ .../src/modules/user/entities/user.entity.ts | 13 +++++++- 16 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 libs/common/src/modules/device-notification/device.notification.module.ts create mode 100644 libs/common/src/modules/device-notification/dtos/device.notification.dto.ts create mode 100644 libs/common/src/modules/device-notification/dtos/index.ts create mode 100644 libs/common/src/modules/device-notification/entities/device.notification.entity.ts create mode 100644 libs/common/src/modules/device-notification/entities/index.ts create mode 100644 libs/common/src/modules/device-notification/repositories/device.notification.repository.ts create mode 100644 libs/common/src/modules/device-notification/repositories/index.ts create mode 100644 libs/common/src/modules/user-notification/dtos/index.ts create mode 100644 libs/common/src/modules/user-notification/dtos/user.notification.dto.ts create mode 100644 libs/common/src/modules/user-notification/entities/index.ts create mode 100644 libs/common/src/modules/user-notification/entities/user.notification.entity.ts create mode 100644 libs/common/src/modules/user-notification/repositories/index.ts create mode 100644 libs/common/src/modules/user-notification/repositories/user.notification.repository.ts create mode 100644 libs/common/src/modules/user-notification/user.notification.repository.module.ts diff --git a/libs/common/src/modules/device-notification/device.notification.module.ts b/libs/common/src/modules/device-notification/device.notification.module.ts new file mode 100644 index 0000000..73acce3 --- /dev/null +++ b/libs/common/src/modules/device-notification/device.notification.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { DeviceNotificationEntity } from './entities'; +@Module({ + providers: [], + exports: [], + controllers: [], + imports: [TypeOrmModule.forFeature([DeviceNotificationEntity])], +}) +export class DeviceNotificationRepositoryModule {} diff --git a/libs/common/src/modules/device-notification/dtos/device.notification.dto.ts b/libs/common/src/modules/device-notification/dtos/device.notification.dto.ts new file mode 100644 index 0000000..0746c14 --- /dev/null +++ b/libs/common/src/modules/device-notification/dtos/device.notification.dto.ts @@ -0,0 +1,15 @@ +import { IsNotEmpty, IsString } from 'class-validator'; + +export class DeviceNotificationDto { + @IsString() + @IsNotEmpty() + public uuid: string; + + @IsString() + @IsNotEmpty() + public userUuid: string; + + @IsString() + @IsNotEmpty() + public deviceUuid: string; +} diff --git a/libs/common/src/modules/device-notification/dtos/index.ts b/libs/common/src/modules/device-notification/dtos/index.ts new file mode 100644 index 0000000..d205031 --- /dev/null +++ b/libs/common/src/modules/device-notification/dtos/index.ts @@ -0,0 +1 @@ +export * from './device.notification.dto'; diff --git a/libs/common/src/modules/device-notification/entities/device.notification.entity.ts b/libs/common/src/modules/device-notification/entities/device.notification.entity.ts new file mode 100644 index 0000000..84e592a --- /dev/null +++ b/libs/common/src/modules/device-notification/entities/device.notification.entity.ts @@ -0,0 +1,33 @@ +import { Column, Entity, ManyToOne, Unique } from 'typeorm'; +import { AbstractEntity } from '../../abstract/entities/abstract.entity'; +import { DeviceNotificationDto } from '../dtos'; +import { DeviceEntity } from '../../device/entities'; +import { UserEntity } from '../../user/entities'; + +@Entity({ name: 'device-notification' }) +@Unique(['userUuid', 'deviceUuid']) +export class DeviceNotificationEntity extends AbstractEntity { + @Column({ + nullable: false, + }) + public userUuid: string; + + @Column({ + nullable: false, + }) + deviceUuid: string; + + @ManyToOne(() => DeviceEntity, (device) => device.permission, { + nullable: false, + }) + device: DeviceEntity; + + @ManyToOne(() => UserEntity, (user) => user.userPermission, { + nullable: false, + }) + user: UserEntity; + constructor(partial: Partial) { + super(); + Object.assign(this, partial); + } +} diff --git a/libs/common/src/modules/device-notification/entities/index.ts b/libs/common/src/modules/device-notification/entities/index.ts new file mode 100644 index 0000000..fedfc80 --- /dev/null +++ b/libs/common/src/modules/device-notification/entities/index.ts @@ -0,0 +1 @@ +export * from './device.notification.entity'; diff --git a/libs/common/src/modules/device-notification/repositories/device.notification.repository.ts b/libs/common/src/modules/device-notification/repositories/device.notification.repository.ts new file mode 100644 index 0000000..b226791 --- /dev/null +++ b/libs/common/src/modules/device-notification/repositories/device.notification.repository.ts @@ -0,0 +1,10 @@ +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; +import { DeviceNotificationEntity } from '../entities'; + +@Injectable() +export class DeviceNotificationRepository extends Repository { + constructor(private dataSource: DataSource) { + super(DeviceNotificationEntity, dataSource.createEntityManager()); + } +} diff --git a/libs/common/src/modules/device-notification/repositories/index.ts b/libs/common/src/modules/device-notification/repositories/index.ts new file mode 100644 index 0000000..1f3da6f --- /dev/null +++ b/libs/common/src/modules/device-notification/repositories/index.ts @@ -0,0 +1 @@ +export * from './device.notification.repository'; diff --git a/libs/common/src/modules/device/entities/device.entity.ts b/libs/common/src/modules/device/entities/device.entity.ts index 53dbbb3..fee939a 100644 --- a/libs/common/src/modules/device/entities/device.entity.ts +++ b/libs/common/src/modules/device/entities/device.entity.ts @@ -5,6 +5,7 @@ import { GroupDeviceEntity } from '../../group-device/entities'; import { SpaceEntity } from '../../space/entities'; import { ProductEntity } from '../../product/entities'; import { DeviceUserPermissionEntity } from '../../device-user-permission/entities'; +import { DeviceNotificationEntity } from '../../device-notification/entities'; @Entity({ name: 'device' }) @Unique(['spaceDevice', 'deviceTuyaUuid']) @@ -28,7 +29,14 @@ export class DeviceEntity extends AbstractEntity { }, ) permission: DeviceUserPermissionEntity[]; - + @OneToMany( + () => DeviceNotificationEntity, + (deviceUserNotification) => deviceUserNotification.device, + { + nullable: true, + }, + ) + deviceUserNotification: DeviceNotificationEntity[]; @OneToMany( () => GroupDeviceEntity, (userGroupDevices) => userGroupDevices.device, diff --git a/libs/common/src/modules/user-notification/dtos/index.ts b/libs/common/src/modules/user-notification/dtos/index.ts new file mode 100644 index 0000000..307e6f3 --- /dev/null +++ b/libs/common/src/modules/user-notification/dtos/index.ts @@ -0,0 +1 @@ +export * from './user.notification.dto'; diff --git a/libs/common/src/modules/user-notification/dtos/user.notification.dto.ts b/libs/common/src/modules/user-notification/dtos/user.notification.dto.ts new file mode 100644 index 0000000..4e9f72f --- /dev/null +++ b/libs/common/src/modules/user-notification/dtos/user.notification.dto.ts @@ -0,0 +1,19 @@ +import { IsBoolean, IsNotEmpty, IsString } from 'class-validator'; + +export class UserNotificationDto { + @IsString() + @IsNotEmpty() + public uuid: string; + + @IsString() + @IsNotEmpty() + public userUuid: string; + + @IsString() + @IsNotEmpty() + public subscriptionUuid: string; + + @IsBoolean() + @IsNotEmpty() + public active: boolean; +} diff --git a/libs/common/src/modules/user-notification/entities/index.ts b/libs/common/src/modules/user-notification/entities/index.ts new file mode 100644 index 0000000..1acf5c0 --- /dev/null +++ b/libs/common/src/modules/user-notification/entities/index.ts @@ -0,0 +1 @@ +export * from './user.notification.entity'; diff --git a/libs/common/src/modules/user-notification/entities/user.notification.entity.ts b/libs/common/src/modules/user-notification/entities/user.notification.entity.ts new file mode 100644 index 0000000..aa6ec6b --- /dev/null +++ b/libs/common/src/modules/user-notification/entities/user.notification.entity.ts @@ -0,0 +1,27 @@ +import { Column, Entity, ManyToOne, Unique } from 'typeorm'; +import { AbstractEntity } from '../../abstract/entities/abstract.entity'; +import { UserNotificationDto } from '../dtos'; +import { UserEntity } from '../../user/entities'; + +@Entity({ name: 'user-notification' }) +@Unique(['user', 'subscriptionUuid']) +export class UserNotificationEntity extends AbstractEntity { + @ManyToOne(() => UserEntity, (user) => user.roles, { + nullable: false, + }) + user: UserEntity; + @Column({ + nullable: false, + }) + subscriptionUuid: string; + + @Column({ + nullable: false, + default: true, + }) + active: boolean; + constructor(partial: Partial) { + super(); + Object.assign(this, partial); + } +} diff --git a/libs/common/src/modules/user-notification/repositories/index.ts b/libs/common/src/modules/user-notification/repositories/index.ts new file mode 100644 index 0000000..76d3ac2 --- /dev/null +++ b/libs/common/src/modules/user-notification/repositories/index.ts @@ -0,0 +1 @@ +export * from './user.notification.repository'; diff --git a/libs/common/src/modules/user-notification/repositories/user.notification.repository.ts b/libs/common/src/modules/user-notification/repositories/user.notification.repository.ts new file mode 100644 index 0000000..6862e88 --- /dev/null +++ b/libs/common/src/modules/user-notification/repositories/user.notification.repository.ts @@ -0,0 +1,10 @@ +import { DataSource, Repository } from 'typeorm'; +import { Injectable } from '@nestjs/common'; +import { UserNotificationEntity } from '../entities'; + +@Injectable() +export class UserNotificationRepository extends Repository { + constructor(private dataSource: DataSource) { + super(UserNotificationEntity, dataSource.createEntityManager()); + } +} diff --git a/libs/common/src/modules/user-notification/user.notification.repository.module.ts b/libs/common/src/modules/user-notification/user.notification.repository.module.ts new file mode 100644 index 0000000..73997be --- /dev/null +++ b/libs/common/src/modules/user-notification/user.notification.repository.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { UserNotificationEntity } from './entities'; +@Module({ + providers: [], + exports: [], + controllers: [], + imports: [TypeOrmModule.forFeature([UserNotificationEntity])], +}) +export class UserNotificationRepositoryModule {} diff --git a/libs/common/src/modules/user/entities/user.entity.ts b/libs/common/src/modules/user/entities/user.entity.ts index 17a9268..06c7c91 100644 --- a/libs/common/src/modules/user/entities/user.entity.ts +++ b/libs/common/src/modules/user/entities/user.entity.ts @@ -4,6 +4,8 @@ import { UserDto } from '../dtos'; import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { UserSpaceEntity } from '../../user-space/entities'; import { UserRoleEntity } from '../../user-role/entities'; +import { DeviceNotificationEntity } from '../../device-notification/entities'; +import { UserNotificationEntity } from '../../user-notification/entities'; @Entity({ name: 'user' }) export class UserEntity extends AbstractEntity { @@ -53,12 +55,21 @@ export class UserEntity extends AbstractEntity { @OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user) userSpaces: UserSpaceEntity[]; + @OneToMany( + () => UserNotificationEntity, + (userNotification) => userNotification.user, + ) + userNotification: UserNotificationEntity[]; @OneToMany( () => DeviceUserPermissionEntity, (userPermission) => userPermission.user, ) userPermission: DeviceUserPermissionEntity[]; - + @OneToMany( + () => DeviceNotificationEntity, + (deviceUserNotification) => deviceUserNotification.user, + ) + deviceUserNotification: DeviceNotificationEntity[]; @OneToMany(() => UserRoleEntity, (role) => role.user, { nullable: true, })