Merge branch 'dev' into SP-197-be-retrieve-devices-in-the-gateway

This commit is contained in:
faris Aljohari
2024-06-11 12:03:30 +03:00
85 changed files with 2151 additions and 839 deletions

View File

@ -13,6 +13,7 @@ import { EmailService } from './util/email.service';
imports: [ imports: [
ConfigModule.forRoot({ ConfigModule.forRoot({
load: config, load: config,
isGlobal: true,
}), }),
DatabaseModule, DatabaseModule,
HelperModule, HelperModule,

View File

@ -1,3 +1,6 @@
import emailConfig from './email.config'; import emailConfig from './email.config';
import superAdminConfig from './super.admin.config'; import superAdminConfig from './super.admin.config';
export default [emailConfig, superAdminConfig]; import tuyaConfig from './tuya.config';
import oneSignalConfig from './onesignal.config';
export default [emailConfig, superAdminConfig, tuyaConfig, oneSignalConfig];

View File

@ -0,0 +1,9 @@
import { registerAs } from '@nestjs/config';
export default registerAs(
'onesignal-config',
(): Record<string, any> => ({
ONESIGNAL_APP_ID: process.env.ONESIGNAL_APP_ID,
ONESIGNAL_API_KEY: process.env.ONESIGNAL_API_KEY,
}),
);

View File

@ -0,0 +1,30 @@
export enum TuyaRegionConfigEnum {
CN = 'wss://mqe.tuyacn.com:8285/',
US = 'wss://mqe.tuyaus.com:8285/',
EU = 'wss://mqe.tuyaeu.com:8285/',
IN = 'wss://mqe.tuyain.com:8285/',
}
export enum TUYA_PASULAR_ENV {
PROD = 'prod',
TEST = 'test',
}
export const TuyaEnvConfig = Object.freeze({
[TUYA_PASULAR_ENV.PROD]: {
name: TUYA_PASULAR_ENV.PROD,
value: 'event',
desc: 'online environment',
},
[TUYA_PASULAR_ENV.TEST]: {
name: TUYA_PASULAR_ENV.TEST,
value: 'event-test',
desc: 'test environment',
},
});
type IEnvConfig = typeof TuyaEnvConfig;
export function getTuyaEnvConfig<K extends keyof IEnvConfig>(
env: TUYA_PASULAR_ENV,
): IEnvConfig[K] {
return TuyaEnvConfig[env];
}

View File

@ -0,0 +1,212 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import {
TUYA_PASULAR_ENV,
getTuyaEnvConfig,
TuyaRegionConfigEnum,
} from './config';
import { getTopicUrl, buildQuery, buildPassword, decrypt } from './utils';
type LoggerLevel = 'INFO' | 'ERROR';
interface IConfig {
accessId: string;
accessKey: string;
env: TUYA_PASULAR_ENV;
url: TuyaRegionConfigEnum;
timeout?: number;
maxRetryTimes?: number;
retryTimeout?: number;
logger?: (level: LoggerLevel, ...args: any) => void;
}
class TuyaMessageSubscribeWebsocket {
static URL = TuyaRegionConfigEnum;
static env = TUYA_PASULAR_ENV;
static data = 'TUTA_DATA';
static error = 'TUYA_ERROR';
static open = 'TUYA_OPEN';
static close = 'TUYA_CLOSE';
static reconnect = 'TUYA_RECONNECT';
static ping = 'TUYA_PING';
static pong = 'TUYA_PONG';
private config: IConfig;
private server?: WebSocket;
private timer: any;
private retryTimes: number;
private event: EventEmitter;
constructor(config: IConfig) {
this.config = Object.assign(
{
ackTimeoutMillis: 3000,
subscriptionType: 'Failover',
retryTimeout: 1000,
maxRetryTimes: 100,
timeout: 30000,
logger: console.log,
},
config,
);
this.event = new EventEmitter();
this.retryTimes = 0;
}
public start() {
this.server = this._connect();
}
public open(cb: (ws: WebSocket) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.open, cb);
}
public message(cb: (ws: WebSocket, message: any) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.data, cb);
}
public ping(cb: (ws: WebSocket) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.ping, cb);
}
public pong(cb: (ws: WebSocket) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.pong, cb);
}
public reconnect(cb: (ws: WebSocket) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.reconnect, cb);
}
public ackMessage(messageId: string) {
this.server && this.server.send(JSON.stringify({ messageId }));
}
public error(cb: (ws: WebSocket, error: any) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.error, cb);
}
public close(cb: (ws: WebSocket) => void) {
this.event.on(TuyaMessageSubscribeWebsocket.close, cb);
}
private _reconnect() {
if (
this.config.maxRetryTimes &&
this.retryTimes < this.config.maxRetryTimes
) {
const timer = setTimeout(() => {
clearTimeout(timer);
this.retryTimes++;
this._connect(false);
}, this.config.retryTimeout);
}
}
private _connect(isInit = true) {
const { accessId, accessKey, env, url } = this.config;
const topicUrl = getTopicUrl(
url,
accessId,
getTuyaEnvConfig(env).value,
`?${buildQuery({ subscriptionType: 'Failover', ackTimeoutMillis: 30000 })}`,
);
const password = buildPassword(accessId, accessKey);
this.server = new WebSocket(topicUrl, {
rejectUnauthorized: false,
headers: { username: accessId, password },
});
this.subOpen(this.server, isInit);
this.subMessage(this.server);
this.subPing(this.server);
this.subPong(this.server);
this.subError(this.server);
this.subClose(this.server);
return this.server;
}
private subOpen(server: WebSocket, isInit = true) {
server.on('open', () => {
if (server.readyState === server.OPEN) {
this.retryTimes = 0;
}
this.keepAlive(server);
this.event.emit(
isInit
? TuyaMessageSubscribeWebsocket.open
: TuyaMessageSubscribeWebsocket.reconnect,
this.server,
);
});
}
private subPing(server: WebSocket) {
server.on('ping', () => {
this.event.emit(TuyaMessageSubscribeWebsocket.ping, this.server);
this.keepAlive(server);
server.pong(this.config.accessId);
});
}
private subPong(server: WebSocket) {
server.on('pong', () => {
this.keepAlive(server);
this.event.emit(TuyaMessageSubscribeWebsocket.pong, this.server);
});
}
private subMessage(server: WebSocket) {
server.on('message', (data: any) => {
try {
this.keepAlive(server);
const obj = this.handleMessage(data);
this.event.emit(TuyaMessageSubscribeWebsocket.data, this.server, obj);
} catch (e) {
this.logger('ERROR', e);
this.event.emit(TuyaMessageSubscribeWebsocket.error, e);
}
});
}
private subClose(server: WebSocket) {
server.on('close', (...data) => {
this._reconnect();
this.clearKeepAlive();
this.event.emit(TuyaMessageSubscribeWebsocket.close, ...data);
});
}
private subError(server: WebSocket) {
server.on('error', (e) => {
this.event.emit(TuyaMessageSubscribeWebsocket.error, this.server, e);
});
}
private clearKeepAlive() {
clearTimeout(this.timer);
}
private keepAlive(server: WebSocket) {
this.clearKeepAlive();
this.timer = setTimeout(() => {
server.ping(this.config.accessId);
}, this.config.timeout);
}
private handleMessage(data: string) {
const { payload, ...others } = JSON.parse(data);
const pStr = Buffer.from(payload, 'base64').toString('utf-8');
const pJson = JSON.parse(pStr);
pJson.data = decrypt(pJson.data, this.config.accessKey);
return { payload: pJson, ...others };
}
private logger(level: LoggerLevel, ...info: any) {
const realInfo = `${Date.now()} `;
this.config.logger && this.config.logger(level, realInfo, ...info);
}
}
export default TuyaMessageSubscribeWebsocket;

View File

@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { MD5, AES, enc, mode, pad } from 'crypto-js';
export function getTopicUrl(
websocketUrl: string,
accessId: string,
env: string,
query: string,
) {
return `${websocketUrl}ws/v2/consumer/persistent/${accessId}/out/${env}/${accessId}-sub${query}`;
}
export function buildQuery(query: { [key: string]: number | string }) {
return Object.keys(query)
.map((key) => `${key}=${encodeURIComponent(query[key])}`)
.join('&');
}
export function buildPassword(accessId: string, accessKey: string) {
const key = MD5(accessKey).toString();
return MD5(`${accessId}${key}`).toString().substr(8, 16);
}
export function decrypt(data: string, accessKey: string) {
try {
const realKey = enc.Utf8.parse(accessKey.substring(8, 24));
const json = AES.decrypt(data, realKey, {
mode: mode.ECB,
padding: pad.Pkcs7,
});
const dataStr = enc.Utf8.stringify(json).toString();
return JSON.parse(dataStr);
} catch (e) {
return '';
}
}
export function encrypt(data: any, accessKey: string) {
try {
const realKey = enc.Utf8.parse(accessKey.substring(8, 24));
const realData = JSON.stringify(data);
const retData = AES.encrypt(realData, realKey, {
mode: mode.ECB,
padding: pad.Pkcs7,
}).toString();
return retData;
} catch (e) {
return '';
}
}

View File

@ -0,0 +1,11 @@
import { registerAs } from '@nestjs/config';
export default registerAs(
'tuya-config',
(): Record<string, any> => ({
TUYA_ACCESS_ID: process.env.TUYA_ACCESS_ID,
TUYA_ACCESS_KEY: process.env.TUYA_ACCESS_KEY,
TRUN_ON_TUYA_SOCKET:
process.env.TRUN_ON_TUYA_SOCKET === 'true' ? true : false,
}),
);

View File

@ -11,11 +11,11 @@ import { PermissionTypeEntity } from '../modules/permission/entities';
import { SpaceEntity } from '../modules/space/entities'; import { SpaceEntity } from '../modules/space/entities';
import { SpaceTypeEntity } from '../modules/space-type/entities'; import { SpaceTypeEntity } from '../modules/space-type/entities';
import { UserSpaceEntity } from '../modules/user-space/entities'; import { UserSpaceEntity } from '../modules/user-space/entities';
import { GroupEntity } from '../modules/group/entities';
import { GroupDeviceEntity } from '../modules/group-device/entities';
import { DeviceUserPermissionEntity } from '../modules/device-user-permission/entities'; import { DeviceUserPermissionEntity } from '../modules/device-user-permission/entities';
import { UserRoleEntity } from '../modules/user-role/entities'; import { UserRoleEntity } from '../modules/user-role/entities';
import { RoleTypeEntity } from '../modules/role-type/entities'; import { RoleTypeEntity } from '../modules/role-type/entities';
import { UserNotificationEntity } from '../modules/user-notification/entities';
import { DeviceNotificationEntity } from '../modules/device-notification/entities';
@Module({ @Module({
imports: [ imports: [
@ -41,11 +41,11 @@ import { RoleTypeEntity } from '../modules/role-type/entities';
SpaceEntity, SpaceEntity,
SpaceTypeEntity, SpaceTypeEntity,
UserSpaceEntity, UserSpaceEntity,
GroupEntity,
GroupDeviceEntity,
DeviceUserPermissionEntity, DeviceUserPermissionEntity,
UserRoleEntity, UserRoleEntity,
RoleTypeEntity, RoleTypeEntity,
UserNotificationEntity,
DeviceNotificationEntity,
], ],
namingStrategy: new SnakeNamingStrategy(), namingStrategy: new SnakeNamingStrategy(),
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))), synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),

View File

@ -3,12 +3,26 @@ import { HelperHashService } from './services';
import { SpacePermissionService } from './services/space.permission.service'; import { SpacePermissionService } from './services/space.permission.service';
import { SpaceRepository } from '../modules/space/repositories'; import { SpaceRepository } from '../modules/space/repositories';
import { SpaceRepositoryModule } from '../modules/space/space.repository.module'; import { SpaceRepositoryModule } from '../modules/space/space.repository.module';
import { TuyaWebSocketService } from './services/tuya.web.socket.service';
import { OneSignalService } from './services/onesignal.service';
import { DeviceMessagesService } from './services/device.messages.service';
import { DeviceNotificationRepositoryModule } from '../modules/device-notification/device.notification.module';
import { DeviceNotificationRepository } from '../modules/device-notification/repositories';
@Global() @Global()
@Module({ @Module({
providers: [HelperHashService, SpacePermissionService, SpaceRepository], providers: [
HelperHashService,
SpacePermissionService,
SpaceRepository,
TuyaWebSocketService,
OneSignalService,
DeviceMessagesService,
DeviceNotificationRepository,
],
exports: [HelperHashService, SpacePermissionService], exports: [HelperHashService, SpacePermissionService],
controllers: [], controllers: [],
imports: [SpaceRepositoryModule], imports: [SpaceRepositoryModule, DeviceNotificationRepositoryModule],
}) })
export class HelperModule {} export class HelperModule {}

View File

@ -0,0 +1,10 @@
export function generateRandomString(length: number): string {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let randomString = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomString += characters.charAt(randomIndex);
}
return randomString;
}

View File

@ -0,0 +1,53 @@
import { Injectable } from '@nestjs/common';
import { DeviceNotificationRepository } from '@app/common/modules/device-notification/repositories';
import { OneSignalService } from './onesignal.service';
@Injectable()
export class DeviceMessagesService {
constructor(
private readonly deviceNotificationRepository: DeviceNotificationRepository,
private readonly oneSignalService: OneSignalService,
) {}
async getDevicesUserNotifications(deviceTuyaUuid: string, bizData: any) {
try {
// Retrieve notifications for the specified device
const notifications = await this.deviceNotificationRepository.find({
where: {
device: {
deviceTuyaUuid,
},
},
relations: ['user', 'user.userNotification'],
});
// If notifications are found, send them
if (notifications) {
await this.sendNotifications(notifications, bizData);
}
} catch (error) {
console.error('Error fetching device notifications:', error);
}
}
private async sendNotifications(notifications: any[], bizData: any) {
const notificationPromises = [];
// Iterate over each notification and its associated user notifications
notifications.forEach((notification) => {
notification.user.userNotification.forEach((userNotification) => {
// Queue notification sending without awaiting
notificationPromises.push(
this.oneSignalService.sendNotification(
JSON.stringify(bizData),
'device-status',
[userNotification.subscriptionUuid],
),
);
});
});
// Wait for all notification sending operations to complete
await Promise.all(notificationPromises);
}
}

View File

@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as OneSignal from 'onesignal-node';
@Injectable()
export class OneSignalService {
private client: any;
constructor(private readonly configService: ConfigService) {
// Initialize OneSignal client here
this.client = new OneSignal.Client(
this.configService.get<string>('onesignal-config.ONESIGNAL_APP_ID'),
this.configService.get<string>('onesignal-config.ONESIGNAL_API_KEY'),
);
}
async sendNotification(
content: string,
title: string,
subscriptionIds: string[],
): Promise<any> {
const notification = {
contents: {
en: content,
},
headings: {
en: title,
},
include_subscription_ids: subscriptionIds,
};
try {
const response = await this.client.createNotification(notification);
return response;
} catch (err) {
console.error('Error:', err);
throw new Error('Error sending notification');
}
}
}

View File

@ -0,0 +1,72 @@
import { Injectable } from '@nestjs/common';
import TuyaWebsocket from '../../config/tuya-web-socket-config';
import { ConfigService } from '@nestjs/config';
import { OneSignalService } from './onesignal.service';
import { DeviceMessagesService } from './device.messages.service';
@Injectable()
export class TuyaWebSocketService {
private client: any; // Adjust type according to your TuyaWebsocket client
constructor(
private readonly configService: ConfigService,
private readonly oneSignalService: OneSignalService,
private readonly deviceMessagesService: DeviceMessagesService,
) {
// Initialize the TuyaWebsocket client
this.client = new TuyaWebsocket({
accessId: this.configService.get<string>('tuya-config.TUYA_ACCESS_ID'),
accessKey: this.configService.get<string>('tuya-config.TUYA_ACCESS_KEY'),
url: TuyaWebsocket.URL.EU,
env: TuyaWebsocket.env.TEST,
maxRetryTimes: 100,
});
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
// Set up event handlers
this.setupEventHandlers();
// Start receiving messages
this.client.start();
}
}
private setupEventHandlers() {
// Event handlers
this.client.open(() => {
console.log('open');
});
this.client.message(async (ws: WebSocket, message: any) => {
try {
await this.deviceMessagesService.getDevicesUserNotifications(
message.payload.data.bizData.devId,
message.payload.data.bizData,
);
this.client.ackMessage(message.messageId);
} catch (error) {
console.error('Error processing message:', error);
}
});
this.client.reconnect(() => {
console.log('reconnect');
});
this.client.ping(() => {
console.log('ping');
});
this.client.pong(() => {
console.log('pong');
});
this.client.close((ws: WebSocket, ...args: any[]) => {
console.log('close', ...args);
});
this.client.error((ws: WebSocket, error: any) => {
console.error('WebSocket error:', error);
});
}
}

View File

@ -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 {}

View File

@ -1,15 +1,15 @@
import { IsNotEmpty, IsString } from 'class-validator'; import { IsNotEmpty, IsString } from 'class-validator';
export class GroupDeviceDto { export class DeviceNotificationDto {
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public uuid: string; public uuid: string;
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public deviceUuid: string; public userUuid: string;
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public groupUuid: string; public deviceUuid: string;
} }

View File

@ -0,0 +1 @@
export * from './device.notification.dto';

View File

@ -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<DeviceNotificationDto> {
@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<DeviceNotificationEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -0,0 +1 @@
export * from './device.notification.entity';

View File

@ -0,0 +1,10 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { DeviceNotificationEntity } from '../entities';
@Injectable()
export class DeviceNotificationRepository extends Repository<DeviceNotificationEntity> {
constructor(private dataSource: DataSource) {
super(DeviceNotificationEntity, dataSource.createEntityManager());
}
}

View File

@ -0,0 +1 @@
export * from './device.notification.repository';

View File

@ -9,6 +9,10 @@ export class DeviceDto {
@IsNotEmpty() @IsNotEmpty()
spaceUuid: string; spaceUuid: string;
@IsString()
@IsNotEmpty()
userUuid: string;
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
deviceTuyaUuid: string; deviceTuyaUuid: string;

View File

@ -1,13 +1,14 @@
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm'; import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { DeviceDto } from '../dtos/device.dto'; import { DeviceDto } from '../dtos/device.dto';
import { GroupDeviceEntity } from '../../group-device/entities';
import { SpaceEntity } from '../../space/entities'; import { SpaceEntity } from '../../space/entities';
import { ProductEntity } from '../../product/entities'; import { ProductEntity } from '../../product/entities';
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities'; import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
import { DeviceNotificationEntity } from '../../device-notification/entities';
import { UserEntity } from '../../user/entities';
@Entity({ name: 'device' }) @Entity({ name: 'device' })
@Unique(['spaceDevice', 'deviceTuyaUuid']) @Unique(['deviceTuyaUuid'])
export class DeviceEntity extends AbstractEntity<DeviceDto> { export class DeviceEntity extends AbstractEntity<DeviceDto> {
@Column({ @Column({
nullable: false, nullable: false,
@ -20,6 +21,9 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
}) })
isActive: true; isActive: true;
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
user: UserEntity;
@OneToMany( @OneToMany(
() => DeviceUserPermissionEntity, () => DeviceUserPermissionEntity,
(permission) => permission.device, (permission) => permission.device,
@ -28,15 +32,17 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
}, },
) )
permission: DeviceUserPermissionEntity[]; permission: DeviceUserPermissionEntity[];
@OneToMany( @OneToMany(
() => GroupDeviceEntity, () => DeviceNotificationEntity,
(userGroupDevices) => userGroupDevices.device, (deviceUserNotification) => deviceUserNotification.device,
{
nullable: true,
},
) )
userGroupDevices: GroupDeviceEntity[]; deviceUserNotification: DeviceNotificationEntity[];
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, { @ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
nullable: false, nullable: true,
}) })
spaceDevice: SpaceEntity; spaceDevice: SpaceEntity;

View File

@ -1 +0,0 @@
export * from './group.device.dto';

View File

@ -1,48 +0,0 @@
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
import { GroupDeviceDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { DeviceEntity } from '../../device/entities';
import { GroupEntity } from '../../group/entities';
@Entity({ name: 'group-device' })
@Unique(['device', 'group'])
export class GroupDeviceEntity extends AbstractEntity<GroupDeviceDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
nullable: false,
})
public uuid: string;
@Column({
type: 'string',
nullable: false,
})
deviceUuid: string;
@Column({
type: 'string',
nullable: false,
})
groupUuid: string;
@ManyToOne(() => DeviceEntity, (device) => device.userGroupDevices, {
nullable: false,
})
device: DeviceEntity;
@ManyToOne(() => GroupEntity, (group) => group.groupDevices, {
nullable: false,
})
group: GroupEntity;
@Column({
nullable: true,
default: true,
})
public isActive: boolean;
constructor(partial: Partial<GroupDeviceEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -1 +0,0 @@
export * from './group.device.entity';

View File

@ -1,11 +0,0 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GroupDeviceEntity } from './entities/group.device.entity';
@Module({
providers: [],
exports: [],
controllers: [],
imports: [TypeOrmModule.forFeature([GroupDeviceEntity])],
})
export class GroupDeviceRepositoryModule {}

View File

@ -1,10 +0,0 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { GroupDeviceEntity } from '../entities/group.device.entity';
@Injectable()
export class GroupDeviceRepository extends Repository<GroupDeviceEntity> {
constructor(private dataSource: DataSource) {
super(GroupDeviceEntity, dataSource.createEntityManager());
}
}

View File

@ -1 +0,0 @@
export * from './group.device.repository';

View File

@ -1,11 +0,0 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class GroupDto {
@IsString()
@IsNotEmpty()
public uuid: string;
@IsString()
@IsNotEmpty()
public groupName: string;
}

View File

@ -1 +0,0 @@
export * from './group.dto';

View File

@ -1,34 +0,0 @@
import { Column, Entity, OneToMany } from 'typeorm';
import { GroupDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { GroupDeviceEntity } from '../../group-device/entities';
@Entity({ name: 'group' })
export class GroupEntity extends AbstractEntity<GroupDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()', // Use gen_random_uuid() for default value
nullable: false,
})
public uuid: string;
@Column({
nullable: false,
})
public groupName: string;
@OneToMany(() => GroupDeviceEntity, (groupDevice) => groupDevice.group, {
cascade: true,
})
groupDevices: GroupDeviceEntity[];
@Column({
nullable: true,
default: true,
})
public isActive: boolean;
constructor(partial: Partial<GroupEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -1 +0,0 @@
export * from './group.entity';

View File

@ -1,11 +0,0 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GroupEntity } from './entities/group.entity';
@Module({
providers: [],
exports: [],
controllers: [],
imports: [TypeOrmModule.forFeature([GroupEntity])],
})
export class GroupRepositoryModule {}

View File

@ -1,10 +0,0 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { GroupEntity } from '../entities/group.entity';
@Injectable()
export class GroupRepository extends Repository<GroupEntity> {
constructor(private dataSource: DataSource) {
super(GroupEntity, dataSource.createEntityManager());
}
}

View File

@ -1 +0,0 @@
export * from './group.repository';

View File

@ -16,4 +16,8 @@ export class SpaceDto {
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public spaceTypeUuid: string; public spaceTypeUuid: string;
@IsString()
@IsNotEmpty()
public invitationCode: string;
} }

View File

@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm'; import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { SpaceDto } from '../dtos'; import { SpaceDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceTypeEntity } from '../../space-type/entities'; import { SpaceTypeEntity } from '../../space-type/entities';
@ -6,6 +6,7 @@ import { UserSpaceEntity } from '../../user-space/entities';
import { DeviceEntity } from '../../device/entities'; import { DeviceEntity } from '../../device/entities';
@Entity({ name: 'space' }) @Entity({ name: 'space' })
@Unique(['invitationCode'])
export class SpaceEntity extends AbstractEntity<SpaceDto> { export class SpaceEntity extends AbstractEntity<SpaceDto> {
@Column({ @Column({
type: 'uuid', type: 'uuid',
@ -18,6 +19,11 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
nullable: false, nullable: false,
}) })
public spaceName: string; public spaceName: string;
@Column({
nullable: true,
})
public invitationCode: string;
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true }) @ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
parent: SpaceEntity; parent: SpaceEntity;

View File

@ -0,0 +1 @@
export * from './user.notification.dto';

View File

@ -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;
}

View File

@ -0,0 +1 @@
export * from './user.notification.entity';

View File

@ -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<UserNotificationDto> {
@ManyToOne(() => UserEntity, (user) => user.roles, {
nullable: false,
})
user: UserEntity;
@Column({
nullable: false,
})
subscriptionUuid: string;
@Column({
nullable: false,
default: true,
})
active: boolean;
constructor(partial: Partial<UserNotificationEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -0,0 +1 @@
export * from './user.notification.repository';

View File

@ -0,0 +1,10 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { UserNotificationEntity } from '../entities';
@Injectable()
export class UserNotificationRepository extends Repository<UserNotificationEntity> {
constructor(private dataSource: DataSource) {
super(UserNotificationEntity, dataSource.createEntityManager());
}
}

View File

@ -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 {}

View File

@ -4,6 +4,9 @@ import { UserDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { UserSpaceEntity } from '../../user-space/entities'; import { UserSpaceEntity } from '../../user-space/entities';
import { UserRoleEntity } from '../../user-role/entities'; import { UserRoleEntity } from '../../user-role/entities';
import { DeviceNotificationEntity } from '../../device-notification/entities';
import { UserNotificationEntity } from '../../user-notification/entities';
import { DeviceEntity } from '../../device/entities';
@Entity({ name: 'user' }) @Entity({ name: 'user' })
export class UserEntity extends AbstractEntity<UserDto> { export class UserEntity extends AbstractEntity<UserDto> {
@ -53,12 +56,24 @@ export class UserEntity extends AbstractEntity<UserDto> {
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user) @OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
userSpaces: UserSpaceEntity[]; userSpaces: UserSpaceEntity[];
@OneToMany(() => DeviceEntity, (userDevice) => userDevice.user)
userDevice: DeviceEntity[];
@OneToMany(
() => UserNotificationEntity,
(userNotification) => userNotification.user,
)
userNotification: UserNotificationEntity[];
@OneToMany( @OneToMany(
() => DeviceUserPermissionEntity, () => DeviceUserPermissionEntity,
(userPermission) => userPermission.user, (userPermission) => userPermission.user,
) )
userPermission: DeviceUserPermissionEntity[]; userPermission: DeviceUserPermissionEntity[];
@OneToMany(
() => DeviceNotificationEntity,
(deviceUserNotification) => deviceUserNotification.user,
)
deviceUserNotification: DeviceNotificationEntity[];
@OneToMany(() => UserRoleEntity, (role) => role.user, { @OneToMany(() => UserRoleEntity, (role) => role.user, {
nullable: true, nullable: true,
}) })

468
package-lock.json generated
View File

@ -17,6 +17,7 @@
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.0", "@nestjs/swagger": "^7.3.0",
"@nestjs/typeorm": "^10.0.2", "@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.8",
"@tuya/tuya-connector-nodejs": "^2.1.2", "@tuya/tuya-connector-nodejs": "^2.1.2",
"argon2": "^0.40.1", "argon2": "^0.40.1",
"axios": "^1.6.7", "axios": "^1.6.7",
@ -29,11 +30,13 @@
"ioredis": "^5.3.2", "ioredis": "^5.3.2",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"nodemailer": "^6.9.10", "nodemailer": "^6.9.10",
"onesignal-node": "^3.4.0",
"passport-jwt": "^4.0.1", "passport-jwt": "^4.0.1",
"pg": "^8.11.3", "pg": "^8.11.3",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"typeorm": "^0.3.20" "typeorm": "^0.3.20",
"ws": "^8.17.0"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.3.2", "@nestjs/cli": "^10.3.2",
@ -2060,6 +2063,28 @@
"typeorm": "^0.3.0" "typeorm": "^0.3.0"
} }
}, },
"node_modules/@nestjs/websockets": {
"version": "10.3.8",
"resolved": "https://registry.npmjs.org/@nestjs/websockets/-/websockets-10.3.8.tgz",
"integrity": "sha512-DTSCK+FYtSTljT6XjVUUZhf1cPxKEJf1AG1y2n+ERnd0vzMpnYpMFgGkDlXqa3uC+LAMcOcx1EyTCcHsSHrOVg==",
"dependencies": {
"iterare": "1.2.1",
"object-hash": "3.0.0",
"tslib": "2.6.2"
},
"peerDependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-socket.io": "^10.0.0",
"reflect-metadata": "^0.1.12 || ^0.2.0",
"rxjs": "^7.1.0"
},
"peerDependenciesMeta": {
"@nestjs/platform-socket.io": {
"optional": true
}
}
},
"node_modules/@nodelib/fs.scandir": { "node_modules/@nodelib/fs.scandir": {
"version": "2.1.5", "version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@ -3086,11 +3111,40 @@
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==", "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==",
"dev": true "dev": true
}, },
"node_modules/asn1": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz",
"integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==",
"dependencies": {
"safer-buffer": "~2.1.0"
}
},
"node_modules/assert-plus": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha512-NfJ4UzBCcQGLDlQq7nHxH+tv3kyZ0hHQqF5BO6J7tNJeP5do1llPr8dZ8zHonfhAu0PHAdMkSo+8o0wxg9lZWw==",
"engines": {
"node": ">=0.8"
}
},
"node_modules/asynckit": { "node_modules/asynckit": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
}, },
"node_modules/aws-sign2": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz",
"integrity": "sha512-08kcGqnYf/YmjoRhfxyu+CLxBjUtHLXLXX/vUfx9l2LYzG3c1m61nrpyFUZI6zeS+Li/wWMMidD9KgrqtGq3mA==",
"engines": {
"node": "*"
}
},
"node_modules/aws4": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.13.0.tgz",
"integrity": "sha512-3AungXC4I8kKsS9PuS4JH2nc+0bVY/mjgrephHTIi8fpEeGsTHBUJeosp0Wc1myYMElmD0B3Oc4XL/HVJ4PV2g=="
},
"node_modules/axios": { "node_modules/axios": {
"version": "1.6.7", "version": "1.6.7",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz",
@ -3257,6 +3311,14 @@
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
}, },
"node_modules/bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
"integrity": "sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==",
"dependencies": {
"tweetnacl": "^0.14.3"
}
},
"node_modules/bcryptjs": { "node_modules/bcryptjs": {
"version": "2.4.3", "version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
@ -3296,6 +3358,11 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/bluebird": {
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
"integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg=="
},
"node_modules/body-parser": { "node_modules/body-parser": {
"version": "1.20.2", "version": "1.20.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
@ -3536,6 +3603,11 @@
} }
] ]
}, },
"node_modules/caseless": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha512-4tYFyifaFfGacoiObjJegolkwSU4xQNGbVgUiNYVUxbQ2x2lUsFvY4hVgVzGiIe6WLOPqycWXA40l+PWsxthUw=="
},
"node_modules/chalk": { "node_modules/chalk": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
@ -4076,6 +4148,17 @@
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz", "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.2.0.tgz",
"integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==" "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q=="
}, },
"node_modules/dashdash": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
"integrity": "sha512-jRFi8UDGo6j+odZiEpjazZaWqEal3w/basFjQHQEwVtZJGDpxbH1MeYluwCS8Xq5wmLJooDlMgvVarmWfGM44g==",
"dependencies": {
"assert-plus": "^1.0.0"
},
"engines": {
"node": ">=0.10"
}
},
"node_modules/date-fns": { "node_modules/date-fns": {
"version": "2.30.0", "version": "2.30.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz",
@ -4288,6 +4371,15 @@
"resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz",
"integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==" "integrity": "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA=="
}, },
"node_modules/ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
"integrity": "sha512-eh9O+hwRHNbG4BLTjEl3nw044CkGm5X6LoaCf7LPp7UU8Qrt47JYNi6nPX8xjW97TKGKm1ouctg0QSpZe9qrnw==",
"dependencies": {
"jsbn": "~0.1.0",
"safer-buffer": "^2.1.0"
}
},
"node_modules/ecdsa-sig-formatter": { "node_modules/ecdsa-sig-formatter": {
"version": "1.0.11", "version": "1.0.11",
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
@ -4852,6 +4944,11 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
"integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="
},
"node_modules/external-editor": { "node_modules/external-editor": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz",
@ -4866,11 +4963,18 @@
"node": ">=4" "node": ">=4"
} }
}, },
"node_modules/extsprintf": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
"integrity": "sha512-11Ndz7Nv+mvAC1j0ktTa7fAb0vLyGGX+rMHNBYQviQDGU0Hw7lhctJANqbPhu9nV9/izT/IntTgZ7Im/9LJs9g==",
"engines": [
"node >=0.6.0"
]
},
"node_modules/fast-deep-equal": { "node_modules/fast-deep-equal": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
"dev": true
}, },
"node_modules/fast-diff": { "node_modules/fast-diff": {
"version": "1.3.0", "version": "1.3.0",
@ -4897,8 +5001,7 @@
"node_modules/fast-json-stable-stringify": { "node_modules/fast-json-stable-stringify": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="
"dev": true
}, },
"node_modules/fast-levenshtein": { "node_modules/fast-levenshtein": {
"version": "2.0.6", "version": "2.0.6",
@ -5134,6 +5237,14 @@
"url": "https://github.com/sponsors/isaacs" "url": "https://github.com/sponsors/isaacs"
} }
}, },
"node_modules/forever-agent": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
"integrity": "sha512-j0KLYPhm6zeac4lz3oJ3o65qvgQCcPubiyotZrXqEaG4hNagNYO8qdlUrX5vwqv9ohqeT/Z3j6+yW067yWWdUw==",
"engines": {
"node": "*"
}
},
"node_modules/fork-ts-checker-webpack-plugin": { "node_modules/fork-ts-checker-webpack-plugin": {
"version": "9.0.2", "version": "9.0.2",
"resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-9.0.2.tgz", "resolved": "https://registry.npmjs.org/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-9.0.2.tgz",
@ -5254,20 +5365,6 @@
"integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
"dev": true "dev": true
}, },
"node_modules/fsevents": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
"dev": true,
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/function-bind": { "node_modules/function-bind": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
@ -5332,6 +5429,14 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/getpass": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
"integrity": "sha512-0fzj9JxOLfJ+XGLhR8ze3unN0KZCgZwiSSDz168VERjK8Wl8kVSdcu2kspd4s4wtAa1y/qrVRiAA0WclVsu0ng==",
"dependencies": {
"assert-plus": "^1.0.0"
}
},
"node_modules/glob": { "node_modules/glob": {
"version": "10.3.10", "version": "10.3.10",
"resolved": "https://registry.npmjs.org/glob/-/glob-10.3.10.tgz", "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.10.tgz",
@ -5429,6 +5534,47 @@
"integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==", "integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==",
"dev": true "dev": true
}, },
"node_modules/har-schema": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
"integrity": "sha512-Oqluz6zhGX8cyRaTQlFMPw80bSJVG2x/cFb8ZPhUILGgHka9SsokCCOQgpveePerqidZOrT14ipqfJb7ILcW5Q==",
"engines": {
"node": ">=4"
}
},
"node_modules/har-validator": {
"version": "5.1.5",
"resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.5.tgz",
"integrity": "sha512-nmT2T0lljbxdQZfspsno9hgrG3Uir6Ks5afism62poxqBM6sDnMEuPmzTq8XN0OEwqKLLdh1jQI3qyE66Nzb3w==",
"deprecated": "this library is no longer supported",
"dependencies": {
"ajv": "^6.12.3",
"har-schema": "^2.0.0"
},
"engines": {
"node": ">=6"
}
},
"node_modules/har-validator/node_modules/ajv": {
"version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
"dependencies": {
"fast-deep-equal": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0",
"json-schema-traverse": "^0.4.1",
"uri-js": "^4.2.2"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/epoberezkin"
}
},
"node_modules/har-validator/node_modules/json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
},
"node_modules/has-flag": { "node_modules/has-flag": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
@ -5536,6 +5682,20 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/http-signature": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
"integrity": "sha512-CAbnr6Rz4CYQkLYUtSNXxQPUH2gK8f3iWexVlsnMeD+GjlsQ0Xsy1cOX+mN3dtxYomRy21CiOzU8Uhw6OwncEQ==",
"dependencies": {
"assert-plus": "^1.0.0",
"jsprim": "^1.2.2",
"sshpk": "^1.7.0"
},
"engines": {
"node": ">=0.8",
"npm": ">=1.3.7"
}
},
"node_modules/human-signals": { "node_modules/human-signals": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz", "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz",
@ -5816,6 +5976,11 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/is-typedarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
"integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA=="
},
"node_modules/is-unicode-supported": { "node_modules/is-unicode-supported": {
"version": "0.1.0", "version": "0.1.0",
"resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz",
@ -5838,6 +6003,11 @@
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="
}, },
"node_modules/isstream": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha512-Yljz7ffyPbrLpLngrMtZ7NduUgVvi6wG9RJ9IUcyCd59YQ911PBJphODUcbOVbqYfxe1wuYf/LJ8PauMRwsM/g=="
},
"node_modules/istanbul-lib-coverage": { "node_modules/istanbul-lib-coverage": {
"version": "3.2.2", "version": "3.2.2",
"resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz",
@ -6625,6 +6795,11 @@
"js-yaml": "bin/js-yaml.js" "js-yaml": "bin/js-yaml.js"
} }
}, },
"node_modules/jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
"integrity": "sha512-UVU9dibq2JcFWxQPA6KCqj5O42VOmAY3zQUfEKxU0KpTGXwNoCjkX1e13eHNvw/xPynt6pU0rZ1htjWTNTSXsg=="
},
"node_modules/jsesc": { "node_modules/jsesc": {
"version": "2.5.2", "version": "2.5.2",
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz",
@ -6649,6 +6824,11 @@
"integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==", "integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==",
"dev": true "dev": true
}, },
"node_modules/json-schema": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz",
"integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="
},
"node_modules/json-schema-traverse": { "node_modules/json-schema-traverse": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
@ -6661,6 +6841,11 @@
"integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==",
"dev": true "dev": true
}, },
"node_modules/json-stringify-safe": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
"integrity": "sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA=="
},
"node_modules/json5": { "node_modules/json5": {
"version": "2.2.3", "version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
@ -6712,6 +6897,20 @@
"npm": ">=6" "npm": ">=6"
} }
}, },
"node_modules/jsprim": {
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.2.tgz",
"integrity": "sha512-P2bSOMAc/ciLz6DzgjVlGJP9+BrJWu5UDGK70C2iweC5QBIeFf0ZXRvGjEj2uYgrY2MkAAhsSWHDWlFtEroZWw==",
"dependencies": {
"assert-plus": "1.0.0",
"extsprintf": "1.3.0",
"json-schema": "0.4.0",
"verror": "1.10.0"
},
"engines": {
"node": ">=0.6.0"
}
},
"node_modules/jwa": { "node_modules/jwa": {
"version": "1.4.1", "version": "1.4.1",
"resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
@ -7278,6 +7477,14 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/oauth-sign": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
"integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==",
"engines": {
"node": "*"
}
},
"node_modules/object-assign": { "node_modules/object-assign": {
"version": "4.1.1", "version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@ -7286,6 +7493,14 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/object-hash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz",
"integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==",
"engines": {
"node": ">= 6"
}
},
"node_modules/object-inspect": { "node_modules/object-inspect": {
"version": "1.13.1", "version": "1.13.1",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz",
@ -7322,6 +7537,18 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"node_modules/onesignal-node": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/onesignal-node/-/onesignal-node-3.4.0.tgz",
"integrity": "sha512-9dNpfU5Xp6VhJLkdZT4kVqmOaU36RJOgp+6REQHyv+hLOcgqqa4/FRXxuHbjRCE51x9BK4jIC/gn2Mnw0gQgFQ==",
"dependencies": {
"request": "^2.88.2",
"request-promise": "^4.2.6"
},
"engines": {
"node": ">=8.13.0"
}
},
"node_modules/onetime": { "node_modules/onetime": {
"version": "5.1.2", "version": "5.1.2",
"resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
@ -7596,6 +7823,11 @@
"integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg==", "integrity": "sha512-KG8UEiEVkR3wGEb4m5yZkVCzigAD+cVEJck2CzYZO37ZGJfctvVptVO192MwrtPhzONn6go8ylnOdMhKqi4nfg==",
"peer": true "peer": true
}, },
"node_modules/performance-now": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow=="
},
"node_modules/pg": { "node_modules/pg": {
"version": "8.11.3", "version": "8.11.3",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.11.3.tgz", "resolved": "https://registry.npmjs.org/pg/-/pg-8.11.3.tgz",
@ -7911,11 +8143,15 @@
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
}, },
"node_modules/psl": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",
"integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag=="
},
"node_modules/punycode": { "node_modules/punycode": {
"version": "2.3.1", "version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
"dev": true,
"engines": { "engines": {
"node": ">=6" "node": ">=6"
} }
@ -8101,6 +8337,99 @@
"node": ">=0.10" "node": ">=0.10"
} }
}, },
"node_modules/request": {
"version": "2.88.2",
"resolved": "https://registry.npmjs.org/request/-/request-2.88.2.tgz",
"integrity": "sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==",
"deprecated": "request has been deprecated, see https://github.com/request/request/issues/3142",
"dependencies": {
"aws-sign2": "~0.7.0",
"aws4": "^1.8.0",
"caseless": "~0.12.0",
"combined-stream": "~1.0.6",
"extend": "~3.0.2",
"forever-agent": "~0.6.1",
"form-data": "~2.3.2",
"har-validator": "~5.1.3",
"http-signature": "~1.2.0",
"is-typedarray": "~1.0.0",
"isstream": "~0.1.2",
"json-stringify-safe": "~5.0.1",
"mime-types": "~2.1.19",
"oauth-sign": "~0.9.0",
"performance-now": "^2.1.0",
"qs": "~6.5.2",
"safe-buffer": "^5.1.2",
"tough-cookie": "~2.5.0",
"tunnel-agent": "^0.6.0",
"uuid": "^3.3.2"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/request-promise": {
"version": "4.2.6",
"resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.6.tgz",
"integrity": "sha512-HCHI3DJJUakkOr8fNoCc73E5nU5bqITjOYFMDrKHYOXWXrgD/SBaC7LjwuPymUprRyuF06UK7hd/lMHkmUXglQ==",
"deprecated": "request-promise has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142",
"dependencies": {
"bluebird": "^3.5.0",
"request-promise-core": "1.1.4",
"stealthy-require": "^1.1.1",
"tough-cookie": "^2.3.3"
},
"engines": {
"node": ">=0.10.0"
},
"peerDependencies": {
"request": "^2.34"
}
},
"node_modules/request-promise-core": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.4.tgz",
"integrity": "sha512-TTbAfBBRdWD7aNNOoVOBH4pN/KigV6LyapYNNlAPA8JwbovRti1E88m3sYAwsLi5ryhPKsE9APwnjFTgdUjTpw==",
"dependencies": {
"lodash": "^4.17.19"
},
"engines": {
"node": ">=0.10.0"
},
"peerDependencies": {
"request": "^2.34"
}
},
"node_modules/request/node_modules/form-data": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz",
"integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.6",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 0.12"
}
},
"node_modules/request/node_modules/qs": {
"version": "6.5.3",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.3.tgz",
"integrity": "sha512-qxXIEh4pCGfHICj1mAJQ2/2XVZkjCDTcEgfoSQxc/fYivUZxTkk7L3bDBJSoNrEzXI17oUO5Dp07ktqE5KzczA==",
"engines": {
"node": ">=0.6"
}
},
"node_modules/request/node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
"deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.",
"bin": {
"uuid": "bin/uuid"
}
},
"node_modules/require-directory": { "node_modules/require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
@ -8681,6 +9010,30 @@
"integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==", "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==",
"dev": true "dev": true
}, },
"node_modules/sshpk": {
"version": "1.18.0",
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.18.0.tgz",
"integrity": "sha512-2p2KJZTSqQ/I3+HX42EpYOa2l3f8Erv8MWKsy2I9uf4wA7yFIkXRffYdsx86y6z4vHtV8u7g+pPlr8/4ouAxsQ==",
"dependencies": {
"asn1": "~0.2.3",
"assert-plus": "^1.0.0",
"bcrypt-pbkdf": "^1.0.0",
"dashdash": "^1.12.0",
"ecc-jsbn": "~0.1.1",
"getpass": "^0.1.1",
"jsbn": "~0.1.0",
"safer-buffer": "^2.0.2",
"tweetnacl": "~0.14.0"
},
"bin": {
"sshpk-conv": "bin/sshpk-conv",
"sshpk-sign": "bin/sshpk-sign",
"sshpk-verify": "bin/sshpk-verify"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/stack-utils": { "node_modules/stack-utils": {
"version": "2.0.6", "version": "2.0.6",
"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz", "resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.6.tgz",
@ -8715,6 +9068,14 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/stealthy-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz",
"integrity": "sha512-ZnWpYnYugiOVEY5GkcuJK1io5V8QmNYChG62gSit9pQVGErXtrKuPC55ITaVSukmMta5qpMU7vqLt2Lnni4f/g==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/streamsearch": { "node_modules/streamsearch": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
@ -9158,6 +9519,18 @@
"node": ">=0.6" "node": ">=0.6"
} }
}, },
"node_modules/tough-cookie": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
"dependencies": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
},
"engines": {
"node": ">=0.8"
}
},
"node_modules/tr46": { "node_modules/tr46": {
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
@ -9332,6 +9705,22 @@
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz",
"integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="
}, },
"node_modules/tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
"integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==",
"dependencies": {
"safe-buffer": "^5.0.1"
},
"engines": {
"node": "*"
}
},
"node_modules/tweetnacl": {
"version": "0.14.5",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
"integrity": "sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA=="
},
"node_modules/type-check": { "node_modules/type-check": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
@ -9604,7 +9993,6 @@
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
"integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
"dev": true,
"dependencies": { "dependencies": {
"punycode": "^2.1.0" "punycode": "^2.1.0"
} }
@ -9670,6 +10058,24 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/verror": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
"integrity": "sha512-ZZKSmDAEFOijERBLkmYfJ+vmk3w+7hOLYDNkRCuRuMJGEmqYNCNLyBBFwWKVMhfwaEF3WOd0Zlw86U/WC/+nYw==",
"engines": [
"node >=0.6.0"
],
"dependencies": {
"assert-plus": "^1.0.0",
"core-util-is": "1.0.2",
"extsprintf": "^1.2.0"
}
},
"node_modules/verror/node_modules/core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ=="
},
"node_modules/walker": { "node_modules/walker": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz",
@ -9875,6 +10281,26 @@
"integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==",
"dev": true "dev": true
}, },
"node_modules/ws": {
"version": "8.17.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz",
"integrity": "sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/xtend": { "node_modules/xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",

View File

@ -28,6 +28,7 @@
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.0", "@nestjs/swagger": "^7.3.0",
"@nestjs/typeorm": "^10.0.2", "@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.8",
"@tuya/tuya-connector-nodejs": "^2.1.2", "@tuya/tuya-connector-nodejs": "^2.1.2",
"argon2": "^0.40.1", "argon2": "^0.40.1",
"axios": "^1.6.7", "axios": "^1.6.7",
@ -40,11 +41,13 @@
"ioredis": "^5.3.2", "ioredis": "^5.3.2",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"nodemailer": "^6.9.10", "nodemailer": "^6.9.10",
"onesignal-node": "^3.4.0",
"passport-jwt": "^4.0.1", "passport-jwt": "^4.0.1",
"pg": "^8.11.3", "pg": "^8.11.3",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"typeorm": "^0.3.20" "typeorm": "^0.3.20",
"ws": "^8.17.0"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.3.2", "@nestjs/cli": "^10.3.2",

View File

@ -14,6 +14,8 @@ import { FloorModule } from './floor/floor.module';
import { UnitModule } from './unit/unit.module'; import { UnitModule } from './unit/unit.module';
import { RoleModule } from './role/role.module'; import { RoleModule } from './role/role.module';
import { SeederModule } from '@app/common/seed/seeder.module'; import { SeederModule } from '@app/common/seed/seeder.module';
import { UserNotificationModule } from './user-notification/user-notification.module';
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot({ ConfigModule.forRoot({
@ -30,7 +32,9 @@ import { SeederModule } from '@app/common/seed/seeder.module';
RoomModule, RoomModule,
GroupModule, GroupModule,
DeviceModule, DeviceModule,
DeviceMessagesSubscriptionModule,
UserDevicePermissionModule, UserDevicePermissionModule,
UserNotificationModule,
SeederModule, SeederModule,
], ],
controllers: [AuthenticationController], controllers: [AuthenticationController],

View File

@ -30,7 +30,7 @@ export class BuildingController {
constructor(private readonly buildingService: BuildingService) {} constructor(private readonly buildingService: BuildingService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckCommunityTypeGuard) @UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
@Post() @Post()
async addBuilding(@Body() addBuildingDto: AddBuildingDto) { async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try { try {

View File

@ -32,7 +32,7 @@ export class CommunityController {
constructor(private readonly communityService: CommunityService) {} constructor(private readonly communityService: CommunityService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async addCommunity(@Body() addCommunityDto: AddCommunityDto) { async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
try { try {

View File

@ -0,0 +1,98 @@
import {
Body,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { DeviceMessagesSubscriptionService } from '../services/device-messages.service';
import { DeviceMessagesAddDto } from '../dtos/device-messages.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('Device Messages Status Module')
@Controller({
version: '1',
path: 'device-messages/subscription',
})
export class DeviceMessagesSubscriptionController {
constructor(
private readonly deviceMessagesSubscriptionService: DeviceMessagesSubscriptionService,
) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post()
async addDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
const addDetails =
await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription(
deviceMessagesAddDto,
);
return {
statusCode: HttpStatus.CREATED,
message: 'Device Messages Subscription Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':deviceUuid/user/:userUuid')
async getDeviceMessagesSubscription(
@Param('deviceUuid') deviceUuid: string,
@Param('userUuid') userUuid: string,
) {
try {
const deviceDetails =
await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription(
userUuid,
deviceUuid,
);
return {
statusCode: HttpStatus.OK,
message: 'User Device Subscription fetched Successfully',
data: deviceDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete()
async deleteDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription(
deviceMessagesAddDto,
);
return {
statusCode: HttpStatus.OK,
message: 'User subscription deleted Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './device-messages.controller';

View File

@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { DeviceMessagesSubscriptionController } from './controllers';
import { DeviceMessagesSubscriptionService } from './services';
import { DeviceNotificationRepositoryModule } from '@app/common/modules/device-notification/device.notification.module';
import { DeviceNotificationRepository } from '@app/common/modules/device-notification/repositories';
@Module({
imports: [ConfigModule, DeviceNotificationRepositoryModule],
controllers: [DeviceMessagesSubscriptionController],
providers: [DeviceNotificationRepository, DeviceMessagesSubscriptionService],
exports: [DeviceMessagesSubscriptionService],
})
export class DeviceMessagesSubscriptionModule {}

View File

@ -1,26 +1,20 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator'; import { IsNotEmpty, IsString } from 'class-validator';
export class ControlGroupDto { export class DeviceMessagesAddDto {
@ApiProperty({ @ApiProperty({
description: 'groupUuid', description: 'user uuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public groupUuid: string; userUuid: string;
@ApiProperty({ @ApiProperty({
description: 'code', description: 'device uuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public code: string; deviceUuid: string;
@ApiProperty({
description: 'value',
required: true,
})
@IsNotEmpty()
public value: any;
} }

View File

@ -0,0 +1 @@
export * from './device-messages.dto';

View File

@ -0,0 +1,75 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { DeviceMessagesAddDto } from '../dtos/device-messages.dto';
import { DeviceNotificationRepository } from '@app/common/modules/device-notification/repositories';
@Injectable()
export class DeviceMessagesSubscriptionService {
constructor(
private readonly deviceNotificationRepository: DeviceNotificationRepository,
) {}
async addDeviceMessagesSubscription(
deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
return await this.deviceNotificationRepository.save({
user: {
uuid: deviceMessagesAddDto.userUuid,
},
device: {
uuid: deviceMessagesAddDto.deviceUuid,
},
});
} catch (error) {
if (error.code === '23505') {
throw new HttpException(
'This User already belongs to this device',
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
error.message || 'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceMessagesSubscription(userUuid: string, deviceUuid: string) {
try {
const deviceUserSubscription =
await this.deviceNotificationRepository.findOne({
where: {
user: { uuid: userUuid },
device: { uuid: deviceUuid },
},
});
return {
uuid: deviceUserSubscription.uuid,
deviceUuid: deviceUserSubscription.deviceUuid,
userUuid: deviceUserSubscription.userUuid,
};
} catch (error) {
throw new HttpException(
'User device subscription not found',
HttpStatus.NOT_FOUND,
);
}
}
async deleteDeviceMessagesSubscription(
deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
const result = await this.deviceNotificationRepository.delete({
user: { uuid: deviceMessagesAddDto.userUuid },
device: { uuid: deviceMessagesAddDto.deviceUuid },
});
return result;
} catch (error) {
throw new HttpException(
error.message || 'device not found',
HttpStatus.NOT_FOUND,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './device-messages.service';

View File

@ -10,22 +10,18 @@ import {
HttpStatus, HttpStatus,
UseGuards, UseGuards,
Req, Req,
Put,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
AddDeviceInGroupDto, import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
AddDeviceInRoomDto,
} from '../dtos/add.device.dto';
import {
GetDeviceByGroupIdDto,
GetDeviceByRoomUuidDto,
} from '../dtos/get.device.dto';
import { ControlDeviceDto } from '../dtos/control.device.dto'; import { ControlDeviceDto } from '../dtos/control.device.dto';
import { CheckRoomGuard } from 'src/guards/room.guard'; import { CheckRoomGuard } from 'src/guards/room.guard';
import { CheckGroupGuard } from 'src/guards/group.guard';
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard'; import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard'; import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { CheckDeviceGuard } from 'src/guards/device.guard';
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
@ApiTags('Device Module') @ApiTags('Device Module')
@Controller({ @Controller({
@ -34,7 +30,39 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
}) })
export class DeviceController { export class DeviceController {
constructor(private readonly deviceService: DeviceService) {} constructor(private readonly deviceService: DeviceService) {}
@ApiBearerAuth()
@UseGuards(SuperAdminRoleGuard, CheckDeviceGuard)
@Post()
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
try {
const device = await this.deviceService.addDeviceUser(addDeviceDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'device added successfully',
data: device,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('user/:userUuid')
async getDevicesByUser(@Param('userUuid') userUuid: string) {
try {
return await this.deviceService.getDevicesByUser(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard) @UseGuards(JwtAuthGuard, CheckRoomGuard)
@Get('room') @Get('room')
@ -58,16 +86,19 @@ export class DeviceController {
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard) @UseGuards(JwtAuthGuard, CheckRoomGuard)
@Post('room') @Put('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) { async updateDeviceInRoom(
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
) {
try { try {
const device = const device = await this.deviceService.updateDeviceInRoom(
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto); updateDeviceInRoomDto,
);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'device added in room successfully', message: 'device updated in room successfully',
data: device, data: device,
}; };
} catch (error) { } catch (error) {
@ -77,39 +108,7 @@ export class DeviceController {
); );
} }
} }
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckGroupGuard)
@Get('group')
async getDevicesByGroupId(
@Query() getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
return await this.deviceService.getDevicesByGroupId(
getDeviceByGroupIdDto,
userUuid,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckGroupGuard)
@Post('group')
async addDeviceInGroup(@Body() addDeviceInGroupDto: AddDeviceInGroupDto) {
try {
return await this.deviceService.addDeviceInGroup(addDeviceInGroupDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckUserHavePermission) @UseGuards(JwtAuthGuard, CheckUserHavePermission)
@Get(':deviceUuid') @Get(':deviceUuid')

View File

@ -8,18 +8,10 @@ import { DeviceRepositoryModule } from '@app/common/modules/device';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { DeviceRepository } from '@app/common/modules/device/repositories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories'; import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import { SpaceRepository } from '@app/common/modules/space/repositories'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { GroupRepository } from '@app/common/modules/group/repositories';
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories'; import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
import { UserRepository } from '@app/common/modules/user/repositories'; import { UserRepository } from '@app/common/modules/user/repositories';
@Module({ @Module({
imports: [ imports: [ConfigModule, ProductRepositoryModule, DeviceRepositoryModule],
ConfigModule,
ProductRepositoryModule,
DeviceRepositoryModule,
GroupRepositoryModule,
],
controllers: [DeviceController], controllers: [DeviceController],
providers: [ providers: [
DeviceService, DeviceService,
@ -28,8 +20,6 @@ import { UserRepository } from '@app/common/modules/user/repositories';
PermissionTypeRepository, PermissionTypeRepository,
SpaceRepository, SpaceRepository,
DeviceRepository, DeviceRepository,
GroupDeviceRepository,
GroupRepository,
UserRepository, UserRepository,
], ],
exports: [DeviceService], exports: [DeviceService],

View File

@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator'; import { IsNotEmpty, IsString } from 'class-validator';
export class AddDeviceInRoomDto { export class AddDeviceDto {
@ApiProperty({ @ApiProperty({
description: 'deviceTuyaUuid', description: 'deviceTuyaUuid',
required: true, required: true,
@ -11,14 +11,14 @@ export class AddDeviceInRoomDto {
public deviceTuyaUuid: string; public deviceTuyaUuid: string;
@ApiProperty({ @ApiProperty({
description: 'roomUuid', description: 'userUuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public roomUuid: string; public userUuid: string;
} }
export class AddDeviceInGroupDto { export class UpdateDeviceInRoomDto {
@ApiProperty({ @ApiProperty({
description: 'deviceUuid', description: 'deviceUuid',
required: true, required: true,
@ -28,10 +28,10 @@ export class AddDeviceInGroupDto {
public deviceUuid: string; public deviceUuid: string;
@ApiProperty({ @ApiProperty({
description: 'groupUuid', description: 'roomUuid',
required: true, required: true,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
public groupUuid: string; public roomUuid: string;
} }

View File

@ -10,12 +10,3 @@ export class GetDeviceByRoomUuidDto {
@IsNotEmpty() @IsNotEmpty()
public roomUuid: string; public roomUuid: string;
} }
export class GetDeviceByGroupIdDto {
@ApiProperty({
description: 'groupUuid',
required: true,
})
@IsString()
@IsNotEmpty()
public groupUuid: string;
}

View File

@ -8,10 +8,7 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto';
AddDeviceInGroupDto,
AddDeviceInRoomDto,
} from '../dtos/add.device.dto';
import { import {
DeviceInstructionResponse, DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface, GetDeviceDetailsFunctionsInterface,
@ -20,14 +17,10 @@ import {
controlDeviceInterface, controlDeviceInterface,
updateDeviceFirmwareInterface, updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface'; } from '../interfaces/get.device.interface';
import { import { GetDeviceByRoomUuidDto } from '../dtos/get.device.dto';
GetDeviceByGroupIdDto,
GetDeviceByRoomUuidDto,
} from '../dtos/get.device.dto';
import { ControlDeviceDto } from '../dtos/control.device.dto'; import { ControlDeviceDto } from '../dtos/control.device.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { DeviceRepository } from '@app/common/modules/device/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { PermissionType } from '@app/common/constants/permission-type.enum'; import { PermissionType } from '@app/common/constants/permission-type.enum';
import { In } from 'typeorm'; import { In } from 'typeorm';
import { ProductType } from '@app/common/constants/product-type.enum'; import { ProductType } from '@app/common/constants/product-type.enum';
@ -38,7 +31,6 @@ export class DeviceService {
constructor( constructor(
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly deviceRepository: DeviceRepository, private readonly deviceRepository: DeviceRepository,
private readonly groupDeviceRepository: GroupDeviceRepository,
private readonly productRepository: ProductRepository, private readonly productRepository: ProductRepository,
) { ) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY'); const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
@ -60,6 +52,82 @@ export class DeviceService {
...(withProductDevice && { relations: ['productDevice'] }), ...(withProductDevice && { relations: ['productDevice'] }),
}); });
} }
async addDeviceUser(addDeviceDto: AddDeviceDto) {
try {
const device = await this.getDeviceDetailsByDeviceIdTuya(
addDeviceDto.deviceTuyaUuid,
);
if (!device.productUuid) {
throw new Error('Product UUID is missing for the device.');
}
return await this.deviceRepository.save({
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
productDevice: { uuid: device.productUuid },
user: {
uuid: addDeviceDto.userUuid,
},
});
} catch (error) {
if (error.code === '23505') {
throw new HttpException(
'Device already exists',
HttpStatus.BAD_REQUEST,
);
} else {
throw new HttpException(
error.message || 'Failed to add device in room',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
async getDevicesByUser(
userUuid: string,
): Promise<GetDeviceDetailsInterface[]> {
try {
const devices = await this.deviceRepository.find({
where: {
user: { uuid: userUuid },
permission: {
userUuid,
permissionType: {
type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
},
},
},
relations: [
'spaceDevice',
'productDevice',
'permission',
'permission.permissionType',
],
});
const devicesData = await Promise.all(
devices.map(async (device) => {
return {
haveRoom: device.spaceDevice ? true : false,
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type,
...(await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
)),
uuid: device.uuid,
} as GetDeviceDetailsInterface;
}),
);
return devicesData;
} catch (error) {
// Handle the error here
throw new HttpException(
'User does not have any devices',
HttpStatus.NOT_FOUND,
);
}
}
async getDevicesByRoomId( async getDevicesByRoomId(
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
userUuid: string, userUuid: string,
@ -85,13 +153,14 @@ export class DeviceService {
const devicesData = await Promise.all( const devicesData = await Promise.all(
devices.map(async (device) => { devices.map(async (device) => {
return { return {
haveRoom: device.spaceDevice ? true : false,
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type,
...(await this.getDeviceDetailsByDeviceIdTuya( ...(await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid, device.deviceTuyaUuid,
)), )),
uuid: device.uuid, uuid: device.uuid,
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type,
} as GetDeviceDetailsInterface; } as GetDeviceDetailsInterface;
}), }),
); );
@ -105,104 +174,31 @@ export class DeviceService {
); );
} }
} }
async updateDeviceInRoom(updateDeviceInRoomDto: UpdateDeviceInRoomDto) {
async getDevicesByGroupId(
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
userUuid: string,
) {
try { try {
const groupDevices = await this.groupDeviceRepository.find({ await this.deviceRepository.update(
where: { { uuid: updateDeviceInRoomDto.deviceUuid },
group: { uuid: getDeviceByGroupIdDto.groupUuid }, {
device: { spaceDevice: { uuid: updateDeviceInRoomDto.roomUuid },
permission: {
userUuid,
permissionType: {
type: PermissionType.READ || PermissionType.CONTROLLABLE,
},
},
},
}, },
relations: [
'device',
'device.productDevice',
'device.permission',
'device.permission.permissionType',
],
});
const devicesData = await Promise.all(
groupDevices.map(async (device) => {
return {
...(await this.getDeviceDetailsByDeviceIdTuya(
device.device.deviceTuyaUuid,
)),
uuid: device.device.uuid,
productUuid: device.device.productDevice.uuid,
productType: device.device.productDevice.prodType,
permissionType: device.device.permission[0].permissionType.type,
} as GetDeviceDetailsInterface;
}),
); );
const device = await this.deviceRepository.findOne({
return devicesData; where: {
uuid: updateDeviceInRoomDto.deviceUuid,
},
relations: ['spaceDevice'],
});
return {
uuid: device.uuid,
roomUuid: device.spaceDevice.uuid,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
'Error fetching devices by group', 'Failed to add device in room',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
async addDeviceInRoom(addDeviceInRoomDto: AddDeviceInRoomDto) {
try {
const device = await this.getDeviceDetailsByDeviceIdTuya(
addDeviceInRoomDto.deviceTuyaUuid,
);
if (!device.productUuid) {
throw new Error('Product UUID is missing for the device.');
}
return await this.deviceRepository.save({
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
productDevice: { uuid: device.productUuid },
});
} catch (error) {
if (error.code === '23505') {
throw new HttpException(
'Device already exists in the room',
HttpStatus.BAD_REQUEST,
);
} else {
throw new HttpException(
'Failed to add device in room',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
try {
await this.groupDeviceRepository.save({
device: { uuid: addDeviceInGroupDto.deviceUuid },
group: { uuid: addDeviceInGroupDto.groupUuid },
});
return { message: 'device added in group successfully' };
} catch (error) {
if (error.code === '23505') {
throw new HttpException(
'Device already exists in the group',
HttpStatus.BAD_REQUEST,
);
} else {
throw new HttpException(
'Failed to add device in group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) { async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
try { try {

View File

@ -30,7 +30,7 @@ export class FloorController {
constructor(private readonly floorService: FloorService) {} constructor(private readonly floorService: FloorService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckBuildingTypeGuard) @UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
@Post() @Post()
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { try {

View File

@ -1,22 +1,16 @@
import { GroupService } from '../services/group.service'; import { GroupService } from '../services/group.service';
import { import {
Body,
Controller, Controller,
Get, Get,
Post,
UseGuards, UseGuards,
Param, Param,
Put,
Delete,
HttpException, HttpException,
HttpStatus, HttpStatus,
Req,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddGroupDto } from '../dtos/add.group.dto';
import { ControlGroupDto } from '../dtos/control.group.dto';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
@ApiTags('Group Module') @ApiTags('Group Module')
@Controller({ @Controller({
@ -27,11 +21,11 @@ export class GroupController {
constructor(private readonly groupService: GroupService) {} constructor(private readonly groupService: GroupService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get('space/:spaceUuid') @Get(':unitUuid')
async getGroupsBySpaceUuid(@Param('spaceUuid') spaceUuid: string) { async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) {
try { try {
return await this.groupService.getGroupsBySpaceUuid(spaceUuid); return await this.groupService.getGroupsByUnitUuid(unitUuid);
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -40,72 +34,21 @@ export class GroupController {
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':groupUuid') @Get(':unitUuid/devices/:groupName')
async getGroupsByGroupId(@Param('groupUuid') groupUuid: string) { async getUnitDevicesByGroupName(
try { @Param('unitUuid') unitUuid: string,
return await this.groupService.getGroupsByGroupUuid(groupUuid); @Param('groupName') groupName: string,
} catch (error) { @Req() req: any,
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckProductUuidForAllDevicesGuard)
@Post()
async addGroup(@Body() addGroupDto: AddGroupDto) {
try {
return await this.groupService.addGroup(addGroupDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post('control')
async controlGroup(@Body() controlGroupDto: ControlGroupDto) {
try {
return await this.groupService.controlGroup(controlGroupDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('rename/:groupUuid')
async renameGroupByUuid(
@Param('groupUuid') groupUuid: string,
@Body() renameGroupDto: RenameGroupDto,
) { ) {
try { try {
return await this.groupService.renameGroupByUuid( const userUuid = req.user.uuid;
groupUuid,
renameGroupDto,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth() return await this.groupService.getUnitDevicesByGroupName(
@UseGuards(JwtAuthGuard) unitUuid,
@Delete(':groupUuid') groupName,
async deleteGroup(@Param('groupUuid') groupUuid: string) { userUuid,
try { );
return await this.groupService.deleteGroup(groupUuid);
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -1,20 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsArray } from 'class-validator';
export class AddGroupDto {
@ApiProperty({
description: 'groupName',
required: true,
})
@IsString()
@IsNotEmpty()
public groupName: string;
@ApiProperty({
description: 'deviceUuids',
required: true,
})
@IsArray()
@IsNotEmpty()
public deviceUuids: [string];
}

View File

@ -1 +0,0 @@
export * from './add.group.dto';

View File

@ -1,12 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class RenameGroupDto {
@ApiProperty({
description: 'groupName',
required: true,
})
@IsString()
@IsNotEmpty()
public groupName: string;
}

View File

@ -1,26 +1,20 @@
import { DeviceRepository } from './../../libs/common/src/modules/device/repositories/device.repository';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { GroupService } from './services/group.service'; import { GroupService } from './services/group.service';
import { GroupController } from './controllers/group.controller'; import { GroupController } from './controllers/group.controller';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { GroupRepositoryModule } from '@app/common/modules/group/group.repository.module';
import { GroupRepository } from '@app/common/modules/group/repositories';
import { GroupDeviceRepositoryModule } from '@app/common/modules/group-device/group.device.repository.module';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { DeviceRepositoryModule } from '@app/common/modules/device'; import { DeviceRepositoryModule } from '@app/common/modules/device';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
@Module({ @Module({
imports: [ imports: [ConfigModule, DeviceRepositoryModule],
ConfigModule,
GroupRepositoryModule,
GroupDeviceRepositoryModule,
DeviceRepositoryModule,
],
controllers: [GroupController], controllers: [GroupController],
providers: [ providers: [
GroupService, GroupService,
GroupRepository,
GroupDeviceRepository,
DeviceRepository, DeviceRepository,
SpaceRepository,
DeviceRepository,
ProductRepository,
], ],
exports: [GroupService], exports: [GroupService],
}) })

View File

@ -1,16 +0,0 @@
export interface GetGroupDetailsInterface {
groupUuid: string;
groupName: string;
createdAt: Date;
updatedAt: Date;
}
export interface GetGroupsBySpaceUuidInterface {
groupUuid: string;
groupName: string;
}
export interface controlGroupInterface {
success: boolean;
result: boolean;
msg: string;
}

View File

@ -1,33 +1,23 @@
import { import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
Injectable,
HttpException,
HttpStatus,
BadRequestException,
} from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { AddGroupDto } from '../dtos/add.group.dto'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
GetGroupDetailsInterface, import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
GetGroupsBySpaceUuidInterface, import { ProductRepository } from '@app/common/modules/product/repositories';
} from '../interfaces/get.group.interface'; import { PermissionType } from '@app/common/constants/permission-type.enum';
import { ControlGroupDto } from '../dtos/control.group.dto'; import { In } from 'typeorm';
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
import { GroupRepository } from '@app/common/modules/group/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { controlDeviceInterface } from 'src/device/interfaces/get.device.interface';
@Injectable() @Injectable()
export class GroupService { export class GroupService {
private tuya: TuyaContext; private tuya: TuyaContext;
constructor( constructor(
private readonly configService: ConfigService, private readonly configService: ConfigService,
private readonly groupRepository: GroupRepository, private readonly productRepository: ProductRepository,
private readonly groupDeviceRepository: GroupDeviceRepository, private readonly spaceRepository: SpaceRepository,
) { ) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY'); const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY'); const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
this.tuya = new TuyaContext({ this.tuya = new TuyaContext({
baseUrl: 'https://openapi.tuyaeu.com', baseUrl: 'https://openapi.tuyaeu.com',
accessKey, accessKey,
@ -35,230 +25,126 @@ export class GroupService {
}); });
} }
async getGroupsBySpaceUuid( async getGroupsByUnitUuid(unitUuid: string) {
spaceUuid: string,
): Promise<GetGroupsBySpaceUuidInterface[]> {
try { try {
const groupDevices = await this.groupDeviceRepository.find({ const spaces = await this.spaceRepository.find({
relations: ['group', 'device'],
where: { where: {
device: { spaceDevice: { uuid: spaceUuid } }, parent: {
isActive: true, uuid: unitUuid,
},
});
// Extract and return only the group entities
const groups = groupDevices.map((groupDevice) => {
return {
groupUuid: groupDevice.uuid,
groupName: groupDevice.group.groupName,
};
});
if (groups.length > 0) {
return groups;
} else {
throw new HttpException(
'this space has no groups',
HttpStatus.NOT_FOUND,
);
}
} catch (error) {
throw new HttpException(
error.message || 'Error fetching groups',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async addGroup(addGroupDto: AddGroupDto) {
try {
const group = await this.groupRepository.save({
groupName: addGroupDto.groupName,
});
const groupDevicePromises = addGroupDto.deviceUuids.map(
async (deviceUuid) => {
await this.saveGroupDevice(group.uuid, deviceUuid);
},
);
await Promise.all(groupDevicePromises);
return { message: 'Group added successfully' };
} catch (err) {
if (err.code === '23505') {
throw new HttpException(
'User already belongs to this group',
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
err.message || 'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async saveGroupDevice(groupUuid: string, deviceUuid: string) {
try {
await this.groupDeviceRepository.save({
group: {
uuid: groupUuid,
},
device: {
uuid: deviceUuid,
},
});
} catch (error) {
throw error;
}
}
async getDevicesByGroupUuid(groupUuid: string) {
try {
const devices = await this.groupDeviceRepository.find({
relations: ['device'],
where: {
group: {
uuid: groupUuid,
}, },
isActive: true,
}, },
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'],
}); });
return devices;
} catch (error) {
throw error;
}
}
async controlDevice(deviceUuid: string, code: string, value: any) {
try {
const response = await this.controlDeviceTuya(deviceUuid, code, value);
if (response.success) { const groupNames = spaces.flatMap((space) => {
return response; return space.devicesSpaceEntity.map(
} else { (device) => device.productDevice.prodType,
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
); );
}
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async controlDeviceTuya(
deviceUuid: string,
code: string,
value: any,
): Promise<controlDeviceInterface> {
try {
const path = `/v1.0/iot-03/devices/${deviceUuid}/commands`;
const response = await this.tuya.request({
method: 'POST',
path,
body: {
commands: [{ code, value: value }],
},
}); });
return response as controlDeviceInterface; const uniqueGroupNames = [...new Set(groupNames)];
return uniqueGroupNames.map((groupName) => ({ groupName }));
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
'Error control device from Tuya', 'This unit does not have any groups',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.NOT_FOUND,
); );
} }
} }
async controlGroup(controlGroupDto: ControlGroupDto) {
const devices = await this.getDevicesByGroupUuid(controlGroupDto.groupUuid);
async getUnitDevicesByGroupName(
unitUuid: string,
groupName: string,
userUuid: string,
) {
try { try {
await Promise.all( const spaces = await this.spaceRepository.find({
devices.map(async (device) => { where: {
return this.controlDevice( parent: {
device.device.deviceTuyaUuid, uuid: unitUuid,
controlGroupDto.code, },
controlGroupDto.value, devicesSpaceEntity: {
productDevice: {
prodType: groupName,
},
permission: {
userUuid,
permissionType: {
type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
},
},
},
},
relations: [
'devicesSpaceEntity',
'devicesSpaceEntity.productDevice',
'devicesSpaceEntity.spaceDevice',
'devicesSpaceEntity.permission',
'devicesSpaceEntity.permission.permissionType',
],
});
const devices = await Promise.all(
spaces.flatMap(async (space) => {
return await Promise.all(
space.devicesSpaceEntity.map(async (device) => {
const deviceDetails = await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
);
return {
haveRoom: !!device.spaceDevice,
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0]?.permissionType?.type,
...deviceDetails,
uuid: device.uuid,
};
}),
); );
}), }),
); );
return { message: 'Group controlled successfully', success: true }; if (devices.length === 0)
throw new HttpException('No devices found', HttpStatus.NOT_FOUND);
return devices.flat(); // Flatten the array since flatMap was used
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
'Error controlling devices', 'This unit does not have any devices for the specified group name',
HttpStatus.NOT_FOUND,
);
}
}
async getDeviceDetailsByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {
try {
const path = `/v1.1/iot-03/devices/${deviceId}`;
const response = await this.tuya.request({
method: 'GET',
path,
});
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(response);
const product = await this.productRepository.findOne({
where: {
prodId: camelCaseResponse.result.productId,
},
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { productName, productId, ...rest } = camelCaseResponse.result;
return {
...rest,
productUuid: product.uuid,
} as GetDeviceDetailsInterface;
} catch (error) {
throw new HttpException(
'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
async renameGroupByUuid(
groupUuid: string,
renameGroupDto: RenameGroupDto,
): Promise<GetGroupsBySpaceUuidInterface> {
try {
await this.groupRepository.update(
{ uuid: groupUuid },
{ groupName: renameGroupDto.groupName },
);
// Fetch the updated floor
const updatedGroup = await this.groupRepository.findOneOrFail({
where: { uuid: groupUuid },
});
return {
groupUuid: updatedGroup.uuid,
groupName: updatedGroup.groupName,
};
} catch (error) {
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
}
}
async deleteGroup(groupUuid: string) {
try {
const group = await this.getGroupsByGroupUuid(groupUuid);
if (!group) {
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
}
await this.groupRepository.update(
{ uuid: groupUuid },
{ isActive: false },
);
return { message: 'Group deleted successfully' };
} catch (error) {
throw new HttpException(
error.message || 'Error deleting group',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getGroupsByGroupUuid(
groupUuid: string,
): Promise<GetGroupDetailsInterface> {
try {
const group = await this.groupRepository.findOne({
where: {
uuid: groupUuid,
isActive: true,
},
});
if (!group) {
throw new BadRequestException('Invalid group UUID');
}
return {
groupUuid: group.uuid,
groupName: group.groupName,
createdAt: group.createdAt,
updatedAt: group.updatedAt,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Group not found', HttpStatus.NOT_FOUND);
}
}
}
} }

View File

@ -0,0 +1,89 @@
import {
CanActivate,
ExecutionContext,
Injectable,
HttpStatus,
} from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ConfigService } from '@nestjs/config';
import { UserRepository } from '@app/common/modules/user/repositories';
@Injectable()
export class CheckDeviceGuard implements CanActivate {
private tuya: TuyaContext;
constructor(
private readonly configService: ConfigService,
private readonly userRepository: UserRepository,
private readonly deviceRepository: DeviceRepository,
) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
this.tuya = new TuyaContext({
baseUrl: 'https://openapi.tuyaeu.com',
accessKey,
secretKey,
});
}
async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest();
try {
if (req.body && req.body.userUuid && req.body.deviceTuyaUuid) {
const { userUuid, deviceTuyaUuid } = req.body;
await this.checkUserIsFound(userUuid);
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
} else {
throw new BadRequestException('Invalid request parameters');
}
return true;
} catch (error) {
this.handleGuardError(error, context);
return false;
}
}
private async checkUserIsFound(userUuid: string) {
const user = await this.userRepository.findOne({
where: {
uuid: userUuid,
},
});
if (!user) {
throw new NotFoundException('User not found');
}
}
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
const response = await this.tuya.request({
method: 'GET',
path,
});
if (!response.success) {
throw new NotFoundException('Device not found from Tuya');
}
}
private handleGuardError(error: Error, context: ExecutionContext) {
const response = context.switchToHttp().getResponse();
if (error instanceof NotFoundException) {
response
.status(HttpStatus.NOT_FOUND)
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
} else if (error instanceof BadRequestException) {
response
.status(HttpStatus.BAD_REQUEST)
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
} else {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: error.message || 'Invalid UUID',
});
}
}
}

View File

@ -1,81 +0,0 @@
import {
CanActivate,
ExecutionContext,
Injectable,
HttpStatus,
} from '@nestjs/common';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { GroupRepository } from '@app/common/modules/group/repositories';
@Injectable()
export class CheckGroupGuard implements CanActivate {
constructor(
private readonly groupRepository: GroupRepository,
private readonly deviceRepository: DeviceRepository,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest();
try {
if (req.query && req.query.groupUuid) {
const { groupUuid } = req.query;
await this.checkGroupIsFound(groupUuid);
} else if (req.body && req.body.groupUuid && req.body.deviceUuid) {
const { groupUuid, deviceUuid } = req.body;
await this.checkGroupIsFound(groupUuid);
await this.checkDeviceIsFound(deviceUuid);
} else {
throw new BadRequestException('Invalid request parameters');
}
return true;
} catch (error) {
this.handleGuardError(error, context);
return false;
}
}
private async checkGroupIsFound(groupUuid: string) {
const group = await this.groupRepository.findOne({
where: {
uuid: groupUuid,
},
});
if (!group) {
throw new NotFoundException('Group not found');
}
}
async checkDeviceIsFound(deviceUuid: string) {
const device = await this.deviceRepository.findOne({
where: {
uuid: deviceUuid,
},
});
if (!device) {
throw new NotFoundException('Device not found');
}
}
private handleGuardError(error: Error, context: ExecutionContext) {
const response = context.switchToHttp().getResponse();
if (error instanceof NotFoundException) {
response
.status(HttpStatus.NOT_FOUND)
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
} else if (error instanceof BadRequestException) {
response
.status(HttpStatus.BAD_REQUEST)
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
} else {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid UUID',
});
}
}
}

View File

@ -4,29 +4,17 @@ import {
Injectable, Injectable,
HttpStatus, HttpStatus,
} from '@nestjs/common'; } from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { SpaceRepository } from '@app/common/modules/space/repositories'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { BadRequestException, NotFoundException } from '@nestjs/common'; import { BadRequestException, NotFoundException } from '@nestjs/common';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class CheckRoomGuard implements CanActivate { export class CheckRoomGuard implements CanActivate {
private tuya: TuyaContext;
constructor( constructor(
private readonly configService: ConfigService,
private readonly spaceRepository: SpaceRepository, private readonly spaceRepository: SpaceRepository,
private readonly deviceRepository: DeviceRepository, private readonly deviceRepository: DeviceRepository,
) { ) {}
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
this.tuya = new TuyaContext({
baseUrl: 'https://openapi.tuyaeu.com',
accessKey,
secretKey,
});
}
async canActivate(context: ExecutionContext): Promise<boolean> { async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest(); const req = context.switchToHttp().getRequest();
@ -35,10 +23,10 @@ export class CheckRoomGuard implements CanActivate {
if (req.query && req.query.roomUuid) { if (req.query && req.query.roomUuid) {
const { roomUuid } = req.query; const { roomUuid } = req.query;
await this.checkRoomIsFound(roomUuid); await this.checkRoomIsFound(roomUuid);
} else if (req.body && req.body.roomUuid && req.body.deviceTuyaUuid) { } else if (req.body && req.body.roomUuid && req.body.deviceUuid) {
const { roomUuid, deviceTuyaUuid } = req.body; const { roomUuid, deviceUuid } = req.body;
await this.checkRoomIsFound(roomUuid); await this.checkRoomIsFound(roomUuid);
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid); await this.checkDeviceIsFound(deviceUuid);
} else { } else {
throw new BadRequestException('Invalid request parameters'); throw new BadRequestException('Invalid request parameters');
} }
@ -63,14 +51,14 @@ export class CheckRoomGuard implements CanActivate {
throw new NotFoundException('Room not found'); throw new NotFoundException('Room not found');
} }
} }
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) { async checkDeviceIsFound(deviceUuid: string) {
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`; const response = await this.deviceRepository.findOne({
const response = await this.tuya.request({ where: {
method: 'GET', uuid: deviceUuid,
path, },
}); });
if (!response.success) { if (!response.uuid) {
throw new NotFoundException('Device not found'); throw new NotFoundException('Device not found');
} }
} }
@ -88,7 +76,7 @@ export class CheckRoomGuard implements CanActivate {
} else { } else {
response.status(HttpStatus.BAD_REQUEST).json({ response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST, statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid UUID', message: error.message || 'Invalid UUID',
}); });
} }
} }

View File

@ -28,7 +28,7 @@ export class RoomController {
constructor(private readonly roomService: RoomService) {} constructor(private readonly roomService: RoomService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckUnitTypeGuard) @UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
@Post() @Post()
async addRoom(@Body() addRoomDto: AddRoomDto) { async addRoom(@Body() addRoomDto: AddRoomDto) {
try { try {

View File

@ -12,12 +12,15 @@ import {
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddUnitDto, AddUserUnitDto } from '../dtos/add.unit.dto'; import {
AddUnitDto,
AddUserUnitDto,
AddUserUnitUsingCodeDto,
} from '../dtos/add.unit.dto';
import { GetUnitChildDto } from '../dtos/get.unit.dto'; import { GetUnitChildDto } from '../dtos/get.unit.dto';
import { UpdateUnitNameDto } from '../dtos/update.unit.dto'; import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard'; import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard'; import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard'; import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
@ -30,7 +33,7 @@ export class UnitController {
constructor(private readonly unitService: UnitService) {} constructor(private readonly unitService: UnitService) {}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckFloorTypeGuard) @UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
@Post() @Post()
async addUnit(@Body() addUnitDto: AddUnitDto) { async addUnit(@Body() addUnitDto: AddUnitDto) {
try { try {
@ -96,7 +99,7 @@ export class UnitController {
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckUserUnitGuard) @UseGuards(JwtAuthGuard, CheckUserUnitGuard)
@Post('user') @Post('user')
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) { async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
try { try {
@ -147,4 +150,39 @@ export class UnitController {
); );
} }
} }
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid/invitation-code')
async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) {
try {
const unit = await this.unitService.getUnitInvitationCode(unitUuid);
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post('user/verify-code')
async verifyCodeAndAddUserUnit(
@Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
) {
try {
await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user unit added successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
} }

View File

@ -40,3 +40,22 @@ export class AddUserUnitDto {
Object.assign(this, dto); Object.assign(this, dto);
} }
} }
export class AddUserUnitUsingCodeDto {
@ApiProperty({
description: 'userUuid',
required: true,
})
@IsString()
@IsNotEmpty()
public userUuid: string;
@ApiProperty({
description: 'inviteCode',
required: true,
})
@IsString()
@IsNotEmpty()
public inviteCode: string;
constructor(dto: Partial<AddUserUnitDto>) {
Object.assign(this, dto);
}
}

View File

@ -7,7 +7,7 @@ import {
BadRequestException, BadRequestException,
} from '@nestjs/common'; } from '@nestjs/common';
import { SpaceRepository } from '@app/common/modules/space/repositories'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { AddUnitDto, AddUserUnitDto } from '../dtos'; import { AddUnitDto, AddUserUnitDto, AddUserUnitUsingCodeDto } from '../dtos';
import { import {
UnitChildInterface, UnitChildInterface,
UnitParentInterface, UnitParentInterface,
@ -18,6 +18,9 @@ import {
import { SpaceEntity } from '@app/common/modules/space/entities'; import { SpaceEntity } from '@app/common/modules/space/entities';
import { UpdateUnitNameDto } from '../dtos/update.unit.dto'; import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories'; import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
import { generateRandomString } from '@app/common/helper/randomString';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { PermissionType } from '@app/common/constants/permission-type.enum';
@Injectable() @Injectable()
export class UnitService { export class UnitService {
@ -25,6 +28,7 @@ export class UnitService {
private readonly spaceRepository: SpaceRepository, private readonly spaceRepository: SpaceRepository,
private readonly spaceTypeRepository: SpaceTypeRepository, private readonly spaceTypeRepository: SpaceTypeRepository,
private readonly userSpaceRepository: UserSpaceRepository, private readonly userSpaceRepository: UserSpaceRepository,
private readonly userDevicePermissionService: UserDevicePermissionService,
) {} ) {}
async addUnit(addUnitDto: AddUnitDto) { async addUnit(addUnitDto: AddUnitDto) {
@ -227,7 +231,7 @@ export class UnitService {
async addUserUnit(addUserUnitDto: AddUserUnitDto) { async addUserUnit(addUserUnitDto: AddUserUnitDto) {
try { try {
await this.userSpaceRepository.save({ return await this.userSpaceRepository.save({
user: { uuid: addUserUnitDto.userUuid }, user: { uuid: addUserUnitDto.userUuid },
space: { uuid: addUserUnitDto.unitUuid }, space: { uuid: addUserUnitDto.unitUuid },
}); });
@ -282,4 +286,122 @@ export class UnitService {
} }
} }
} }
async getUnitInvitationCode(unitUuid: string): Promise<any> {
try {
// Generate a 6-character random invitation code
const invitationCode = generateRandomString(6);
// Update the unit with the new invitation code
await this.spaceRepository.update({ uuid: unitUuid }, { invitationCode });
// Fetch the updated unit
const updatedUnit = await this.spaceRepository.findOneOrFail({
where: { uuid: unitUuid },
relations: ['spaceType'],
});
return {
uuid: updatedUnit.uuid,
invitationCode: updatedUnit.invitationCode,
type: updatedUnit.spaceType.type,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err;
} else {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
}
}
async verifyCodeAndAddUserUnit(
addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
) {
try {
const unit = await this.findUnitByInviteCode(
addUserUnitUsingCodeDto.inviteCode,
);
await this.addUserToUnit(addUserUnitUsingCodeDto.userUuid, unit.uuid);
await this.clearUnitInvitationCode(unit.uuid);
const deviceUUIDs = await this.getDeviceUUIDsForUnit(unit.uuid);
await this.addUserPermissionsToDevices(
addUserUnitUsingCodeDto.userUuid,
deviceUUIDs,
);
} catch (err) {
throw new HttpException(
'Invalid invitation code',
HttpStatus.BAD_REQUEST,
);
}
}
private async findUnitByInviteCode(inviteCode: string): Promise<SpaceEntity> {
const unit = await this.spaceRepository.findOneOrFail({
where: {
invitationCode: inviteCode,
spaceType: { type: 'unit' },
},
relations: ['spaceType'],
});
return unit;
}
private async addUserToUnit(userUuid: string, unitUuid: string) {
const user = await this.addUserUnit({ userUuid, unitUuid });
if (user.uuid) {
return user;
} else {
throw new HttpException(
'Failed to add user to unit',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async clearUnitInvitationCode(unitUuid: string) {
await this.spaceRepository.update(
{ uuid: unitUuid },
{ invitationCode: null },
);
}
private async getDeviceUUIDsForUnit(
unitUuid: string,
): Promise<{ uuid: string }[]> {
const devices = await this.spaceRepository.find({
where: { parent: { uuid: unitUuid } },
relations: ['devicesSpaceEntity', 'devicesSpaceEntity.productDevice'],
});
const allDevices = devices.flatMap((space) => space.devicesSpaceEntity);
return allDevices.map((device) => ({ uuid: device.uuid }));
}
private async addUserPermissionsToDevices(
userUuid: string,
deviceUUIDs: { uuid: string }[],
): Promise<void> {
const permissionPromises = deviceUUIDs.map(async (device) => {
try {
await this.userDevicePermissionService.addUserPermission({
userUuid,
deviceUuid: device.uuid,
permissionType: PermissionType.CONTROLLABLE,
});
} catch (error) {
console.error(
`Failed to add permission for device ${device.uuid}: ${error.message}`,
);
}
});
await Promise.all(permissionPromises);
}
} }

View File

@ -10,6 +10,9 @@ import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.s
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories'; import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module'; import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
import { UserRepository } from '@app/common/modules/user/repositories'; import { UserRepository } from '@app/common/modules/user/repositories';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
@Module({ @Module({
imports: [ imports: [
@ -26,6 +29,9 @@ import { UserRepository } from '@app/common/modules/user/repositories';
SpaceTypeRepository, SpaceTypeRepository,
UserSpaceRepository, UserSpaceRepository,
UserRepository, UserRepository,
UserDevicePermissionService,
DeviceUserPermissionRepository,
PermissionTypeRepository,
], ],
exports: [UnitService], exports: [UnitService],
}) })

View File

@ -0,0 +1 @@
export * from './user-notification.controller';

View File

@ -0,0 +1,94 @@
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Put,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { UserNotificationService } from '../services/user-notification.service';
import {
UserNotificationAddDto,
UserNotificationUpdateDto,
} from '../dtos/user-notification.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('User Notification Module')
@Controller({
version: '1',
path: 'user-notification/subscription',
})
export class UserNotificationController {
constructor(
private readonly userNotificationService: UserNotificationService,
) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post()
async addUserSubscription(
@Body() userNotificationAddDto: UserNotificationAddDto,
) {
try {
const addDetails = await this.userNotificationService.addUserSubscription(
userNotificationAddDto,
);
return {
statusCode: HttpStatus.CREATED,
message: 'User Notification Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':userUuid')
async fetchUserSubscriptions(@Param('userUuid') userUuid: string) {
try {
const userDetails =
await this.userNotificationService.fetchUserSubscriptions(userUuid);
return {
statusCode: HttpStatus.OK,
message: 'User Notification fetched Successfully',
data: { ...userDetails },
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put()
async updateUserSubscription(
@Body() userNotificationUpdateDto: UserNotificationUpdateDto,
) {
try {
await this.userNotificationService.updateUserSubscription(
userNotificationUpdateDto,
);
return {
statusCode: HttpStatus.OK,
message: 'User subscription updated Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './user-notification.dto';

View File

@ -0,0 +1,44 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
export class UserNotificationAddDto {
@ApiProperty({
description: 'user uuid',
required: true,
})
@IsString()
@IsNotEmpty()
userUuid: string;
@ApiProperty({
description: 'subscription uuid',
required: true,
})
@IsString()
@IsNotEmpty()
subscriptionUuid: string;
}
export class UserNotificationUpdateDto {
@ApiProperty({
description: 'user uuid',
required: true,
})
@IsString()
@IsNotEmpty()
userUuid: string;
@ApiProperty({
description: 'subscription uuid',
required: true,
})
@IsString()
@IsNotEmpty()
subscriptionUuid: string;
@ApiProperty({
description: 'active',
required: true,
})
@IsBoolean()
@IsNotEmpty()
active: boolean;
}

View File

@ -0,0 +1 @@
export * from './user-notification.service';

View File

@ -0,0 +1,83 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
UserNotificationAddDto,
UserNotificationUpdateDto,
} from '../dtos/user-notification.dto';
import { UserNotificationRepository } from '@app/common/modules/user-notification/repositories';
@Injectable()
export class UserNotificationService {
constructor(
private readonly userNotificationRepository: UserNotificationRepository,
) {}
async addUserSubscription(userNotificationAddDto: UserNotificationAddDto) {
try {
return await this.userNotificationRepository.save({
user: {
uuid: userNotificationAddDto.userUuid,
},
subscriptionUuid: userNotificationAddDto.subscriptionUuid,
});
} catch (error) {
if (error.code === '23505') {
throw new HttpException(
'This User already has this subscription uuid',
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
error.message || 'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async fetchUserSubscriptions(userUuid: string) {
try {
const userNotifications = await this.userNotificationRepository.find({
where: {
user: { uuid: userUuid },
active: true,
},
});
return {
userUuid,
subscriptionUuids: [
...userNotifications.map((sub) => sub.subscriptionUuid),
],
};
} catch (error) {
throw new HttpException(
'User subscription not found',
HttpStatus.NOT_FOUND,
);
}
}
async updateUserSubscription(
userNotificationUpdateDto: UserNotificationUpdateDto,
) {
try {
const result = await this.userNotificationRepository.update(
{
user: { uuid: userNotificationUpdateDto.userUuid },
subscriptionUuid: userNotificationUpdateDto.subscriptionUuid,
},
{ active: userNotificationUpdateDto.active },
);
if (result.affected === 0) {
throw new HttpException(
'Subscription uuid not found',
HttpStatus.NOT_FOUND,
);
}
return result;
} catch (error) {
throw new HttpException(
error.message || 'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { UserNotificationRepositoryModule } from '@app/common/modules/user-notification/user.notification.repository.module';
import { UserNotificationRepository } from '@app/common/modules/user-notification/repositories';
import { UserNotificationService } from 'src/user-notification/services';
import { UserNotificationController } from 'src/user-notification/controllers';
@Module({
imports: [ConfigModule, UserNotificationRepositoryModule],
controllers: [UserNotificationController],
providers: [UserNotificationRepository, UserNotificationService],
exports: [UserNotificationService],
})
export class UserNotificationModule {}