Refactor device service methods for better error handling and readability

This commit is contained in:
faris Aljohari
2024-03-25 14:27:50 +03:00
parent ca86e07981
commit fba0063268

View File

@ -6,11 +6,13 @@ import {
AddDeviceInRoomDto,
} from '../dtos/add.device.dto';
import {
DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface,
GetDevicesByGroupIdInterface,
GetDevicesByRoomIdInterface,
GetProductInterface,
addDeviceInRoomInterface,
controlDeviceInterface,
} from '../interfaces/get.device.interface';
@ -19,11 +21,16 @@ import {
GetDeviceByRoomIdDto,
} from '../dtos/get.device.dto';
import { ControlDeviceDto } from '../dtos/control.device.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { ProductRepository } from '@app/common/modules/product/repositories';
@Injectable()
export class DeviceService {
private tuya: TuyaContext;
constructor(private readonly configService: ConfigService) {
constructor(
private readonly configService: ConfigService,
private readonly productRepository: ProductRepository,
) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
// const clientId = this.configService.get<string>('auth-config.CLIENT_ID');
@ -45,7 +52,7 @@ export class DeviceService {
};
} catch (error) {
throw new HttpException(
'Error fetching devices',
'Error fetching devices by room',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -67,7 +74,7 @@ export class DeviceService {
return response as GetDevicesByRoomIdInterface;
} catch (error) {
throw new HttpException(
'Error fetching devices ',
'Error fetching devices by room from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -86,7 +93,7 @@ export class DeviceService {
};
} catch (error) {
throw new HttpException(
'Error fetching devices',
'Error fetching devices by group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -109,7 +116,7 @@ export class DeviceService {
return response as GetDevicesByGroupIdInterface;
} catch (error) {
throw new HttpException(
'Error fetching devices ',
'Error fetching devices by group from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -147,7 +154,7 @@ export class DeviceService {
return response as addDeviceInRoomInterface;
} catch (error) {
throw new HttpException(
'Error adding device',
'Error adding device in room from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -185,7 +192,7 @@ export class DeviceService {
return response as addDeviceInRoomInterface;
} catch (error) {
throw new HttpException(
'Error adding device',
'Error adding device in group from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -221,15 +228,15 @@ export class DeviceService {
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error control device',
'Error control device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDevicesByDeviceId(deviceId: string) {
async getDeviceDetailsByDeviceId(deviceId: string) {
try {
const response = await this.getDevicesByDeviceIdTuya(deviceId);
const response = await this.getDeviceDetailsByDeviceIdTuya(deviceId);
return {
success: response.success,
@ -238,13 +245,12 @@ export class DeviceService {
};
} catch (error) {
throw new HttpException(
'Error fetching device',
'Error fetching device details',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDevicesByDeviceIdTuya(
async getDeviceDetailsByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {
try {
@ -253,35 +259,110 @@ export class DeviceService {
method: 'GET',
path,
});
return response as GetDeviceDetailsInterface;
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(response);
const productType: string = await this.getProductTypeByProductId(
camelCaseResponse.result.productId,
);
return {
result: {
...camelCaseResponse.result,
productType: productType,
},
success: camelCaseResponse.success,
msg: camelCaseResponse.msg,
} as GetDeviceDetailsInterface;
} catch (error) {
throw new HttpException(
'Error fetching device ',
'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDevicesInstructionByDeviceId(deviceId: string) {
async getProductIdByDeviceId(deviceId: string) {
try {
const response = await this.getDevicesInstructionByDeviceIdTuya(deviceId);
const deviceDetails: GetDeviceDetailsInterface =
await this.getDeviceDetailsByDeviceId(deviceId);
return deviceDetails.result.productId;
} catch (error) {
throw new HttpException(
'Error fetching product id by device id',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getProductByProductId(productId: string): Promise<GetProductInterface> {
try {
const product = await this.productRepository
.createQueryBuilder('product')
.where('product.prodId = :productId', { productId })
.select(['product.prodId', 'product.prodType'])
.getOne();
if (product) {
return {
productType: product.prodType,
productId: product.prodId,
};
} else {
throw new HttpException('Product not found', HttpStatus.NOT_FOUND);
}
} catch (error) {
throw new HttpException(
'Error fetching product by product id from db',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getProductTypeByProductId(productId: string) {
try {
const product = await this.getProductByProductId(productId);
return product.productType;
} catch (error) {
throw new HttpException(
'Error getting product type by product id',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceInstructionByDeviceId(
deviceId: string,
): Promise<DeviceInstructionResponse> {
try {
const response = await this.getDeviceInstructionByDeviceIdTuya(deviceId);
const productId: string = await this.getProductIdByDeviceId(deviceId);
const productType: string =
await this.getProductTypeByProductId(productId);
return {
success: response.success,
result: {
category: response.result.category,
function: response.result.functions,
productId: productId,
productType: productType,
functions: response.result.functions.map((fun: any) => {
return {
code: fun.code,
values: fun.values,
dataType: fun.type,
};
}),
},
msg: response.msg,
};
} catch (error) {
throw new HttpException(
'Error fetching device functions',
'Error fetching device functions by device id',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDevicesInstructionByDeviceIdTuya(
async getDeviceInstructionByDeviceIdTuya(
deviceId: string,
): Promise<GetDeviceDetailsFunctionsInterface> {
try {
@ -293,7 +374,7 @@ export class DeviceService {
return response as GetDeviceDetailsFunctionsInterface;
} catch (error) {
throw new HttpException(
'Error fetching device functions',
'Error fetching device functions from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -309,7 +390,7 @@ export class DeviceService {
};
} catch (error) {
throw new HttpException(
'Error fetching device functions',
'Error fetching device functions status',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@ -330,7 +411,7 @@ export class DeviceService {
return response as unknown as GetDeviceDetailsFunctionsStatusInterface;
} catch (error) {
throw new HttpException(
'Error fetching device functions',
'Error fetching device functions status from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}