mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-11 07:38:49 +00:00
Refactor door lock controller and service
This commit is contained in:
@ -9,11 +9,13 @@ import {
|
|||||||
Get,
|
Get,
|
||||||
Delete,
|
Delete,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
|
Put,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddDoorLockOnlineDto } from '../dtos/add.online-temp.dto';
|
import { AddDoorLockOnlineDto } from '../dtos/add.online-temp.dto';
|
||||||
import { AddDoorLockOfflineTempDto } from '../dtos/add.offline-temp.dto';
|
import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto';
|
||||||
|
|
||||||
@ApiTags('Door Lock Module')
|
@ApiTags('Door Lock Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -55,13 +57,11 @@ export class DoorLockController {
|
|||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('temporary-password/offline/one-time/:doorLockUuid')
|
@Post('temporary-password/offline/one-time/:doorLockUuid')
|
||||||
async addOfflineOneTimeTemporaryPassword(
|
async addOfflineOneTimeTemporaryPassword(
|
||||||
@Body() addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto,
|
|
||||||
@Param('doorLockUuid') doorLockUuid: string,
|
@Param('doorLockUuid') doorLockUuid: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const temporaryPassword =
|
const temporaryPassword =
|
||||||
await this.doorLockService.addOfflineOneTimeTemporaryPassword(
|
await this.doorLockService.addOfflineOneTimeTemporaryPassword(
|
||||||
addDoorLockOfflineTempDto,
|
|
||||||
doorLockUuid,
|
doorLockUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -82,13 +82,14 @@ export class DoorLockController {
|
|||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('temporary-password/offline/multiple-time/:doorLockUuid')
|
@Post('temporary-password/offline/multiple-time/:doorLockUuid')
|
||||||
async addOfflineMultipleTimeTemporaryPassword(
|
async addOfflineMultipleTimeTemporaryPassword(
|
||||||
@Body() addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto,
|
@Body()
|
||||||
|
addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto,
|
||||||
@Param('doorLockUuid') doorLockUuid: string,
|
@Param('doorLockUuid') doorLockUuid: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const temporaryPassword =
|
const temporaryPassword =
|
||||||
await this.doorLockService.addOfflineMultipleTimeTemporaryPassword(
|
await this.doorLockService.addOfflineMultipleTimeTemporaryPassword(
|
||||||
addDoorLockOfflineTempDto,
|
addDoorLockOfflineTempMultipleTimeDto,
|
||||||
doorLockUuid,
|
doorLockUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -124,6 +125,29 @@ export class DoorLockController {
|
|||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Delete('temporary-password/online/:doorLockUuid/:passwordId')
|
||||||
|
async deleteDoorLockPassword(
|
||||||
|
@Param('doorLockUuid') doorLockUuid: string,
|
||||||
|
@Param('passwordId') passwordId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await this.doorLockService.deleteDoorLockPassword(
|
||||||
|
doorLockUuid,
|
||||||
|
passwordId,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
message: 'Temporary Password deleted Successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('temporary-password/offline/one-time/:doorLockUuid')
|
@Get('temporary-password/offline/one-time/:doorLockUuid')
|
||||||
async getOfflineOneTimeTemporaryPasswords(
|
async getOfflineOneTimeTemporaryPasswords(
|
||||||
@Param('doorLockUuid') doorLockUuid: string,
|
@Param('doorLockUuid') doorLockUuid: string,
|
||||||
@ -156,21 +180,29 @@ export class DoorLockController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Delete('temporary-password/:doorLockUuid/:passwordId')
|
@Put('temporary-password/:doorLockUuid/offline/:passwordId')
|
||||||
async deleteDoorLockPassword(
|
async updateOfflineTemporaryPassword(
|
||||||
|
@Body()
|
||||||
|
updateDoorLockOfflineTempDto: UpdateDoorLockOfflineTempDto,
|
||||||
@Param('doorLockUuid') doorLockUuid: string,
|
@Param('doorLockUuid') doorLockUuid: string,
|
||||||
@Param('passwordId') passwordId: string,
|
@Param('passwordId') passwordId: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
await this.doorLockService.deleteDoorLockPassword(
|
const temporaryPassword =
|
||||||
|
await this.doorLockService.updateOfflineTemporaryPassword(
|
||||||
|
updateDoorLockOfflineTempDto,
|
||||||
doorLockUuid,
|
doorLockUuid,
|
||||||
passwordId,
|
passwordId,
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.CREATED,
|
||||||
message: 'Temporary Password deleted Successfully',
|
success: true,
|
||||||
|
message: 'offline temporary password updated successfully',
|
||||||
|
data: temporaryPassword,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
@ -1,23 +1,7 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString, Length } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class AddDoorLockOfflineTempDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'name',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public name: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'password',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
@Length(7, 7)
|
|
||||||
public password: string;
|
|
||||||
|
|
||||||
|
export class AddDoorLockOfflineTempMultipleTimeDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'effectiveTime',
|
description: 'effectiveTime',
|
||||||
required: true,
|
required: true,
|
||||||
|
12
src/door-lock/dtos/update.offline-temp.dto.ts
Normal file
12
src/door-lock/dtos/update.offline-temp.dto.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class UpdateDoorLockOfflineTempDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'name',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public name: string;
|
||||||
|
}
|
@ -55,3 +55,10 @@ export interface deleteTemporaryPasswordInterface {
|
|||||||
result: boolean;
|
result: boolean;
|
||||||
msg?: string;
|
msg?: string;
|
||||||
}
|
}
|
||||||
|
export interface getPasswordOfflineInterface {
|
||||||
|
success: boolean;
|
||||||
|
result: {
|
||||||
|
records: [];
|
||||||
|
};
|
||||||
|
msg?: string;
|
||||||
|
}
|
||||||
|
@ -7,13 +7,15 @@ import {
|
|||||||
createTickInterface,
|
createTickInterface,
|
||||||
deleteTemporaryPasswordInterface,
|
deleteTemporaryPasswordInterface,
|
||||||
getPasswordInterface,
|
getPasswordInterface,
|
||||||
|
getPasswordOfflineInterface,
|
||||||
} from '../interfaces/door.lock.interface';
|
} from '../interfaces/door.lock.interface';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||||
|
|
||||||
import { PasswordEncryptionService } from './encryption.services';
|
import { PasswordEncryptionService } from './encryption.services';
|
||||||
import { AddDoorLockOfflineTempDto } from '../dtos/add.offline-temp.dto';
|
import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
|
import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DoorLockService {
|
export class DoorLockService {
|
||||||
@ -93,18 +95,13 @@ export class DoorLockService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const passwords = await this.getTemporaryPasswordsTuya(
|
const passwords = await this.getTemporaryOfflinePasswordsTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
|
'multiple',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (passwords.result.length > 0) {
|
if (passwords.result.records.length > 0) {
|
||||||
const passwordFiltered = passwords.result.filter(
|
return convertKeysToCamelCase(passwords.result.records);
|
||||||
(item) =>
|
|
||||||
(!item.schedule_list || item.schedule_list.length === 0) &&
|
|
||||||
item.type === 0,
|
|
||||||
);
|
|
||||||
|
|
||||||
return convertKeysToCamelCase(passwordFiltered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return passwords;
|
return passwords;
|
||||||
@ -128,18 +125,13 @@ export class DoorLockService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const passwords = await this.getTemporaryPasswordsTuya(
|
const passwords = await this.getTemporaryOfflinePasswordsTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
|
'once',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (passwords.result.length > 0) {
|
if (passwords.result.records.length > 0) {
|
||||||
const passwordFiltered = passwords.result.filter(
|
return convertKeysToCamelCase(passwords.result.records);
|
||||||
(item) =>
|
|
||||||
(!item.schedule_list || item.schedule_list.length === 0) &&
|
|
||||||
item.type === 0, //temp solution
|
|
||||||
);
|
|
||||||
|
|
||||||
return convertKeysToCamelCase(passwordFiltered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return passwords;
|
return passwords;
|
||||||
@ -162,13 +154,13 @@ export class DoorLockService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const passwords = await this.getTemporaryPasswordsTuya(
|
const passwords = await this.getOnlineTemporaryPasswordsTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (passwords.result.length > 0) {
|
if (passwords.result.length > 0) {
|
||||||
const passwordFiltered = passwords.result
|
const passwordFiltered = passwords.result
|
||||||
.filter((item) => item.type === 0) //temp solution
|
.filter((item) => item.type === 0)
|
||||||
.map((password: any) => {
|
.map((password: any) => {
|
||||||
if (password.schedule_list?.length > 0) {
|
if (password.schedule_list?.length > 0) {
|
||||||
password.schedule_list = password.schedule_list.map(
|
password.schedule_list = password.schedule_list.map(
|
||||||
@ -200,7 +192,7 @@ export class DoorLockService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getTemporaryPasswordsTuya(
|
async getOnlineTemporaryPasswordsTuya(
|
||||||
doorLockUuid: string,
|
doorLockUuid: string,
|
||||||
): Promise<getPasswordInterface> {
|
): Promise<getPasswordInterface> {
|
||||||
try {
|
try {
|
||||||
@ -219,25 +211,45 @@ export class DoorLockService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getTemporaryOfflinePasswordsTuya(
|
||||||
|
doorLockUuid: string,
|
||||||
|
type: string,
|
||||||
|
): Promise<getPasswordOfflineInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v1.0/devices/${doorLockUuid}/door-lock/offline-temp-password?pwd_type_codes=${type}&target_status=EFFECTIVE&page_no=1&page_size=100`;
|
||||||
|
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as getPasswordOfflineInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error getting offline temporary passwords from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async addOfflineMultipleTimeTemporaryPassword(
|
async addOfflineMultipleTimeTemporaryPassword(
|
||||||
addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto,
|
addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto,
|
||||||
doorLockUuid: string,
|
doorLockUuid: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const createOnlinePass = await this.addOnlineTemporaryPassword(
|
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
|
||||||
addDoorLockOfflineTempDto,
|
|
||||||
doorLockUuid,
|
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||||
'multiple',
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
if (!createOnlinePass) {
|
|
||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
|
} else if (deviceDetails.productDevice.prodType !== ProductType.DL) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This is not a door lock device',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const createOnceOfflinePass = await this.addOfflineTemporaryPasswordTuya(
|
const createOnceOfflinePass = await this.addOfflineTemporaryPasswordTuya(
|
||||||
addDoorLockOfflineTempDto,
|
deviceDetails.deviceTuyaUuid,
|
||||||
createOnlinePass.id,
|
|
||||||
createOnlinePass.deviceTuyaUuid,
|
|
||||||
'multiple',
|
'multiple',
|
||||||
|
addDoorLockOfflineTempMultipleTimeDto,
|
||||||
);
|
);
|
||||||
if (!createOnceOfflinePass.success) {
|
if (!createOnceOfflinePass.success) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -255,25 +267,22 @@ export class DoorLockService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addOfflineOneTimeTemporaryPassword(
|
async addOfflineOneTimeTemporaryPassword(doorLockUuid: string) {
|
||||||
addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto,
|
|
||||||
doorLockUuid: string,
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
const createOnlinePass = await this.addOnlineTemporaryPassword(
|
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
|
||||||
addDoorLockOfflineTempDto,
|
|
||||||
doorLockUuid,
|
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||||
'once',
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
if (!createOnlinePass) {
|
|
||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
|
} else if (deviceDetails.productDevice.prodType !== ProductType.DL) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This is not a door lock device',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const createOnceOfflinePass = await this.addOfflineTemporaryPasswordTuya(
|
const createOnceOfflinePass = await this.addOfflineTemporaryPasswordTuya(
|
||||||
addDoorLockOfflineTempDto,
|
deviceDetails.deviceTuyaUuid,
|
||||||
createOnlinePass.id,
|
|
||||||
createOnlinePass.deviceTuyaUuid,
|
|
||||||
'once',
|
'once',
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
if (!createOnceOfflinePass.success) {
|
if (!createOnceOfflinePass.success) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -292,10 +301,9 @@ export class DoorLockService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addOfflineTemporaryPasswordTuya(
|
async addOfflineTemporaryPasswordTuya(
|
||||||
addDoorLockDto: AddDoorLockOnlineInterface,
|
|
||||||
onlinePassId: number,
|
|
||||||
doorLockUuid: string,
|
doorLockUuid: string,
|
||||||
type: string,
|
type: string,
|
||||||
|
addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto,
|
||||||
): Promise<createTickInterface> {
|
): Promise<createTickInterface> {
|
||||||
try {
|
try {
|
||||||
const path = `/v1.1/devices/${doorLockUuid}/door-lock/offline-temp-password`;
|
const path = `/v1.1/devices/${doorLockUuid}/door-lock/offline-temp-password`;
|
||||||
@ -304,14 +312,12 @@ export class DoorLockService {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
path,
|
path,
|
||||||
body: {
|
body: {
|
||||||
name: addDoorLockDto.name,
|
|
||||||
...(type === 'multiple' && {
|
...(type === 'multiple' && {
|
||||||
effective_time: addDoorLockDto.effectiveTime,
|
effective_time: addDoorLockOfflineTempMultipleTimeDto.effectiveTime,
|
||||||
invalid_time: addDoorLockDto.invalidTime,
|
invalid_time: addDoorLockOfflineTempMultipleTimeDto.invalidTime,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
type,
|
type,
|
||||||
password_id: onlinePassId,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -326,8 +332,6 @@ export class DoorLockService {
|
|||||||
async addOnlineTemporaryPassword(
|
async addOnlineTemporaryPassword(
|
||||||
addDoorLockDto: AddDoorLockOnlineInterface,
|
addDoorLockDto: AddDoorLockOnlineInterface,
|
||||||
doorLockUuid: string,
|
doorLockUuid: string,
|
||||||
type: string = 'once',
|
|
||||||
isOnline: boolean = true,
|
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const passwordData = await this.getTicketAndEncryptedPassword(
|
const passwordData = await this.getTicketAndEncryptedPassword(
|
||||||
@ -348,8 +352,6 @@ export class DoorLockService {
|
|||||||
const createPass = await this.addOnlineTemporaryPasswordTuya(
|
const createPass = await this.addOnlineTemporaryPasswordTuya(
|
||||||
addDeviceObj,
|
addDeviceObj,
|
||||||
passwordData.deviceTuyaUuid,
|
passwordData.deviceTuyaUuid,
|
||||||
type,
|
|
||||||
addDeviceObj.scheduleList ? isOnline : false,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!createPass.success) {
|
if (!createPass.success) {
|
||||||
@ -429,13 +431,11 @@ export class DoorLockService {
|
|||||||
async addOnlineTemporaryPasswordTuya(
|
async addOnlineTemporaryPasswordTuya(
|
||||||
addDeviceObj: addDeviceObjectInterface,
|
addDeviceObj: addDeviceObjectInterface,
|
||||||
doorLockUuid: string,
|
doorLockUuid: string,
|
||||||
type: string,
|
|
||||||
isOnline: boolean = true,
|
|
||||||
): Promise<createTickInterface> {
|
): Promise<createTickInterface> {
|
||||||
try {
|
try {
|
||||||
const path = `/v1.0/devices/${doorLockUuid}/door-lock/temp-password`;
|
const path = `/v1.0/devices/${doorLockUuid}/door-lock/temp-password`;
|
||||||
let scheduleList;
|
let scheduleList;
|
||||||
if (isOnline) {
|
if (addDeviceObj.scheduleList.length > 0) {
|
||||||
scheduleList = addDeviceObj.scheduleList.map((schedule) => ({
|
scheduleList = addDeviceObj.scheduleList.map((schedule) => ({
|
||||||
effective_time: this.timeToMinutes(schedule.effectiveTime),
|
effective_time: this.timeToMinutes(schedule.effectiveTime),
|
||||||
invalid_time: this.timeToMinutes(schedule.invalidTime),
|
invalid_time: this.timeToMinutes(schedule.invalidTime),
|
||||||
@ -453,11 +453,11 @@ export class DoorLockService {
|
|||||||
invalid_time: addDeviceObj.invalidTime,
|
invalid_time: addDeviceObj.invalidTime,
|
||||||
password_type: 'ticket',
|
password_type: 'ticket',
|
||||||
ticket_id: addDeviceObj.ticketId,
|
ticket_id: addDeviceObj.ticketId,
|
||||||
...(isOnline && {
|
...(addDeviceObj.scheduleList.length > 0 && {
|
||||||
schedule_list: scheduleList,
|
schedule_list: scheduleList,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
type: '0', //temporary solution,
|
type: '0',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -579,4 +579,64 @@ export class DoorLockService {
|
|||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateOfflineTemporaryPassword(
|
||||||
|
updateDoorLockOfflineTempDto: UpdateDoorLockOfflineTempDto,
|
||||||
|
doorLockUuid: string,
|
||||||
|
passwordId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
|
||||||
|
|
||||||
|
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||||
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
|
} else if (deviceDetails.productDevice.prodType !== ProductType.DL) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This is not a door lock device',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const updateOfflinePass = await this.updateOfflineTemporaryPasswordTuya(
|
||||||
|
deviceDetails.deviceTuyaUuid,
|
||||||
|
updateDoorLockOfflineTempDto,
|
||||||
|
passwordId,
|
||||||
|
);
|
||||||
|
if (!updateOfflinePass.success) {
|
||||||
|
throw new HttpException(updateOfflinePass.msg, HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
result: updateOfflinePass.result,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Error updating offline temporary password from Tuya',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateOfflineTemporaryPasswordTuya(
|
||||||
|
doorLockUuid: string,
|
||||||
|
updateDoorLockOfflineTempDto: UpdateDoorLockOfflineTempDto,
|
||||||
|
passwordId: string,
|
||||||
|
): Promise<createTickInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v1.0/cloud/lock/${doorLockUuid}/door-lock/offline-temp-password/${passwordId}`;
|
||||||
|
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'PUT',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
password_name: updateDoorLockOfflineTempDto.name,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as createTickInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error updating offline temporary password from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user