mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 05:44:55 +00:00
Remove unused Mongoose-related modules and configurations
This commit is contained in:
@ -1,23 +0,0 @@
|
||||
import { MongooseModuleOptions } from '@nestjs/mongoose';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export const mongooseConfig = async (
|
||||
configService: ConfigService,
|
||||
): Promise<MongooseModuleOptions> => {
|
||||
return {
|
||||
uri: configService.get<string>('MONGODB_URI'),
|
||||
dbName: 'syncrow-dev', // Specify your database name here
|
||||
connectionFactory: (connection) => {
|
||||
connection.on('connected', () => {
|
||||
console.log('Mongoose connected to MongoDB');
|
||||
});
|
||||
connection.on('error', (err) => {
|
||||
console.error('Mongoose connection error:', err);
|
||||
});
|
||||
connection.on('disconnected', () => {
|
||||
console.log('Mongoose disconnected from MongoDB');
|
||||
});
|
||||
return connection;
|
||||
},
|
||||
};
|
||||
};
|
||||
@ -1,27 +0,0 @@
|
||||
import { Controller, Post, Param } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||
import { DeviceStatusMongoService } from '../services/devices-status.service';
|
||||
|
||||
@ApiTags('Device Status Mongo Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'device-status-mongo',
|
||||
})
|
||||
export class DeviceStatusMongoController {
|
||||
constructor(
|
||||
private readonly deviceStatusMongoService: DeviceStatusMongoService,
|
||||
) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@Post(':deviceTuyaUuid')
|
||||
async addDeviceStatus(
|
||||
@Param('deviceTuyaUuid') deviceTuyaUuid: string,
|
||||
): Promise<AddDeviceStatusDto> {
|
||||
return this.deviceStatusMongoService.addDeviceStatusByDeviceUuid(
|
||||
deviceTuyaUuid,
|
||||
);
|
||||
}
|
||||
|
||||
// Add other endpoints as needed
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { DeviceStatusRepository } from './repository/devices-status.repository';
|
||||
import { DeviceStatusSchema } from './schema/devices-status.schema';
|
||||
import { DeviceStatusMongoController } from './controllers/devices-status.controller';
|
||||
import { DeviceStatusMongoService } from './services/devices-status.service';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: 'DeviceStatus', schema: DeviceStatusSchema },
|
||||
]),
|
||||
],
|
||||
providers: [
|
||||
DeviceStatusMongoService,
|
||||
DeviceStatusRepository,
|
||||
DeviceRepository,
|
||||
],
|
||||
controllers: [DeviceStatusMongoController],
|
||||
exports: [DeviceStatusMongoService, DeviceStatusRepository],
|
||||
})
|
||||
export class DeviceStatusMongoModule {}
|
||||
@ -1,38 +0,0 @@
|
||||
import {
|
||||
IsString,
|
||||
IsArray,
|
||||
ValidateNested,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
class StatusDto {
|
||||
@IsString()
|
||||
code: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
value: any;
|
||||
}
|
||||
|
||||
export class AddDeviceStatusDto {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
deviceUuid?: string;
|
||||
|
||||
@IsString()
|
||||
deviceTuyaUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
productUuid?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
productType?: string;
|
||||
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => StatusDto)
|
||||
status: StatusDto[];
|
||||
}
|
||||
@ -1,6 +0,0 @@
|
||||
import { Document } from 'mongoose';
|
||||
|
||||
export interface IRepository<T extends Document> {
|
||||
create(dto: Partial<T>): Promise<T>;
|
||||
// Add more methods as needed (e.g., find, update, delete)
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { AddDeviceStatusInterface } from '../schema/devices-status.schema';
|
||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceStatusRepository {
|
||||
constructor(
|
||||
@InjectModel('DeviceStatus')
|
||||
private readonly deviceStatusModel: Model<AddDeviceStatusInterface>,
|
||||
) {}
|
||||
|
||||
async addDeviceStatus(
|
||||
addDeviceStatusDto: AddDeviceStatusDto,
|
||||
): Promise<AddDeviceStatusDto> {
|
||||
const { deviceTuyaUuid, status } = addDeviceStatusDto;
|
||||
|
||||
// Update or insert the device document
|
||||
await this.deviceStatusModel.findOneAndUpdate(
|
||||
{ deviceTuyaUuid },
|
||||
{
|
||||
$set: {
|
||||
deviceUuid: addDeviceStatusDto.deviceUuid,
|
||||
productUuid: addDeviceStatusDto.productUuid,
|
||||
productType: addDeviceStatusDto.productType,
|
||||
},
|
||||
},
|
||||
{ upsert: true, new: true, runValidators: true },
|
||||
);
|
||||
|
||||
// Update the status array
|
||||
for (const statusItem of status) {
|
||||
await this.deviceStatusModel.updateOne(
|
||||
{ deviceTuyaUuid, 'status.code': statusItem.code }, // Filter to find if the code already exists
|
||||
{ $set: { 'status.$.value': statusItem.value } }, // Update the value if code exists
|
||||
);
|
||||
|
||||
// If the code doesn't exist, add it to the array
|
||||
await this.deviceStatusModel.updateOne(
|
||||
{ deviceTuyaUuid, 'status.code': { $ne: statusItem.code } }, // Check if the code does not exist
|
||||
{ $push: { status: statusItem } }, // Add the new status item
|
||||
);
|
||||
}
|
||||
|
||||
// Return the updated document
|
||||
return this.deviceStatusModel.findOne({ deviceTuyaUuid });
|
||||
}
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
import { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface AddDeviceStatusInterface extends Document {
|
||||
deviceUuid: string;
|
||||
deviceTuyaUuid: string;
|
||||
productUuid: string;
|
||||
productType: string;
|
||||
status: [];
|
||||
}
|
||||
|
||||
export const DeviceStatusSchema = new Schema<AddDeviceStatusInterface>(
|
||||
{
|
||||
deviceUuid: { type: String, required: true },
|
||||
deviceTuyaUuid: { type: String, required: true },
|
||||
productUuid: { type: String, required: true },
|
||||
productType: { type: String, required: true },
|
||||
status: [],
|
||||
},
|
||||
{ collection: 'devices-status' },
|
||||
);
|
||||
@ -1,140 +0,0 @@
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { DeviceStatusRepository } from '../repository/devices-status.repository';
|
||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
@Injectable()
|
||||
export class DeviceStatusMongoService {
|
||||
private tuya: TuyaContext;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceStatusRepository: DeviceStatusRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
const tuyaEuUrl = this.configService.get<string>('tuya-config.TUYA_EU_URL');
|
||||
this.tuya = new TuyaContext({
|
||||
baseUrl: tuyaEuUrl,
|
||||
accessKey,
|
||||
secretKey,
|
||||
});
|
||||
}
|
||||
async addDeviceStatusByDeviceUuid(
|
||||
deviceTuyaUuid: string,
|
||||
): Promise<AddDeviceStatusDto> {
|
||||
try {
|
||||
const device = await this.getDeviceByDeviceTuyaUuid(deviceTuyaUuid);
|
||||
if (device.uuid) {
|
||||
const deviceStatus = await this.getDevicesInstructionStatus(
|
||||
device.uuid,
|
||||
);
|
||||
if (deviceStatus.productUuid) {
|
||||
const deviceStatusSaved = await this.addDeviceStatusToMongo({
|
||||
deviceUuid: device.uuid,
|
||||
deviceTuyaUuid: deviceTuyaUuid,
|
||||
status: deviceStatus.status,
|
||||
productUuid: deviceStatus.productUuid,
|
||||
productType: deviceStatus.productType,
|
||||
});
|
||||
|
||||
return deviceStatusSaved;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Device Tuya UUID not found',
|
||||
error.status || HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
async addDeviceStatusToMongo(
|
||||
addDeviceStatusDto: AddDeviceStatusDto,
|
||||
): Promise<AddDeviceStatusDto | null> {
|
||||
try {
|
||||
const device = await this.getDeviceByDeviceTuyaUuid(
|
||||
addDeviceStatusDto.deviceTuyaUuid,
|
||||
);
|
||||
if (device?.uuid) {
|
||||
return await this.deviceStatusRepository.addDeviceStatus(
|
||||
addDeviceStatusDto,
|
||||
);
|
||||
}
|
||||
// Return null if device not found or no UUID
|
||||
return null;
|
||||
} catch (error) {
|
||||
// Handle the error silently, perhaps log it internally or ignore it
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) {
|
||||
return await this.deviceRepository.findOne({
|
||||
where: {
|
||||
deviceTuyaUuid,
|
||||
},
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
}
|
||||
async getDevicesInstructionStatus(deviceUuid: string) {
|
||||
try {
|
||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Not Found');
|
||||
}
|
||||
const deviceStatus = await this.getDevicesInstructionStatusTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
status: deviceStatus.result[0].status,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching device functions status',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDevicesInstructionStatusTuya(
|
||||
deviceUuid: string,
|
||||
): Promise<GetDeviceDetailsFunctionsStatusInterface> {
|
||||
try {
|
||||
const path = `/v1.0/iot-03/devices/status`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
query: {
|
||||
device_ids: deviceUuid,
|
||||
},
|
||||
});
|
||||
return response as GetDeviceDetailsFunctionsStatusInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching device functions status from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDeviceByDeviceUuid(
|
||||
deviceUuid: string,
|
||||
withProductDevice: boolean = true,
|
||||
) {
|
||||
return await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
...(withProductDevice && { relations: ['productDevice'] }),
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { DeviceStatusMongoModule } from './devices-status/devices-status.module'; // Adjust path as needed
|
||||
|
||||
@Module({
|
||||
imports: [DeviceStatusMongoModule],
|
||||
providers: [], // Providers specific to MongooseSharedModule, if any
|
||||
exports: [], // Export anything specific to MongooseSharedModule, if needed
|
||||
})
|
||||
export class MongooseSharedModule {}
|
||||
Reference in New Issue
Block a user