Merge pull request #79 from SyncrowIOT/create-vistor-password

Create vistor password
This commit is contained in:
faris Aljohari
2024-08-13 01:44:25 +03:00
committed by GitHub
13 changed files with 1471 additions and 55 deletions

View File

@ -1,4 +1,9 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import {
Injectable,
HttpException,
HttpStatus,
BadRequestException,
} from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config';
import {
@ -84,7 +89,10 @@ export class DoorLockService {
);
}
}
async getOfflineMultipleTimeTemporaryPasswords(doorLockUuid: string) {
async getOfflineMultipleTimeTemporaryPasswords(
doorLockUuid: string,
fromVisitor?: boolean,
) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
@ -100,11 +108,25 @@ export class DoorLockService {
deviceDetails.deviceTuyaUuid,
'multiple',
);
if (passwords.result.records.length > 0) {
return convertKeysToCamelCase(passwords.result.records);
if (!passwords.result.records.length && fromVisitor) {
throw new BadRequestException();
}
if (passwords.result.records.length > 0) {
return fromVisitor
? convertKeysToCamelCase(passwords.result.records).map((password) => {
return {
passwordId: `${password.pwdId}`,
invalidTime: `${password.gmtExpired}`,
effectiveTime: `${password.gmtStart}`,
passwordCreated: `${password.gmtCreate}`,
createdTime: password.pwdName,
passwordStatus: `${password.status}`,
passwordType: 'OFFLINE_MULTIPLE',
deviceUuid: doorLockUuid,
};
})
: convertKeysToCamelCase(passwords.result.records);
}
return passwords;
} catch (error) {
throw new HttpException(
@ -114,10 +136,12 @@ export class DoorLockService {
);
}
}
async getOfflineOneTimeTemporaryPasswords(doorLockUuid: string) {
async getOfflineOneTimeTemporaryPasswords(
doorLockUuid: string,
fromVisitor?: boolean,
) {
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) {
@ -130,9 +154,24 @@ export class DoorLockService {
deviceDetails.deviceTuyaUuid,
'once',
);
if (!passwords.result.records.length && fromVisitor) {
throw new BadRequestException();
}
if (passwords.result.records.length > 0) {
return convertKeysToCamelCase(passwords.result.records);
return fromVisitor
? convertKeysToCamelCase(passwords.result.records).map((password) => {
return {
passwordId: `${password.pwdId}`,
invalidTime: `${password.gmtExpired}`,
effectiveTime: `${password.gmtStart}`,
createdTime: `${password.gmtCreate}`,
passwordName: password.pwdName,
passwordStatus: `${password.status}`,
passwordType: 'OFFLINE_ONETIME',
deviceUuid: doorLockUuid,
};
})
: convertKeysToCamelCase(passwords.result.records);
}
return passwords;
@ -143,7 +182,10 @@ export class DoorLockService {
);
}
}
async getOnlineTemporaryPasswords(doorLockUuid: string) {
async getOnlineTemporaryPasswords(
doorLockUuid: string,
fromVisitor?: boolean,
) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
@ -158,8 +200,7 @@ export class DoorLockService {
const passwords = await this.getOnlineTemporaryPasswordsTuya(
deviceDetails.deviceTuyaUuid,
);
if (passwords.result.length > 0) {
if (passwords.result?.length > 0) {
const passwordFiltered = passwords.result
.filter((item) => item.type === 0)
.map((password: any) => {
@ -182,9 +223,25 @@ export class DoorLockService {
return password;
});
return convertKeysToCamelCase(passwordFiltered);
return fromVisitor
? convertKeysToCamelCase(passwordFiltered).map((password) => {
return {
passwodId: `${password.id}`,
invalidTime: `${password.invalidTime}`,
effectiveTime: `${password.effectiveTime}`,
createdTime: '',
scheduleList: password?.scheduleList,
passwodName: password.name,
passwordStatus: '',
passwordType: 'ONLINE_MULTIPLE',
deviceUuid: doorLockUuid,
};
})
: convertKeysToCamelCase(passwordFiltered);
}
if (fromVisitor) {
throw new BadRequestException();
}
return passwords;
} catch (error) {
throw new HttpException(
@ -193,6 +250,74 @@ export class DoorLockService {
);
}
}
async getOnlineTemporaryPasswordsOneTime(
doorLockUuid: string,
fromVisitor?: boolean,
) {
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 passwords = await this.getOnlineTemporaryPasswordsTuya(
deviceDetails.deviceTuyaUuid,
);
if (passwords.result?.length > 0) {
const passwordFiltered = passwords.result
.filter((item) => item.type === 1)
.map((password: any) => {
if (password.schedule_list?.length > 0) {
password.schedule_list = password.schedule_list.map(
(schedule) => {
schedule.working_day = this.getDaysFromWorkingDayValue(
schedule.working_day,
);
schedule.effective_time = this.minutesToTime(
schedule.effective_time,
);
schedule.invalid_time = this.minutesToTime(
schedule.invalid_time,
);
return schedule;
},
);
}
return password;
});
return fromVisitor
? convertKeysToCamelCase(passwordFiltered).map((password) => {
return {
passwodId: `${password.id}`,
invalidTime: `${password.invalidTime}`,
effectiveTime: `${password.effectiveTime}`,
createdTime: '',
passwodName: password.name,
passwordStatus: '',
passwordType: 'ONLINE_ONETIME',
deviceUuid: doorLockUuid,
};
})
: convertKeysToCamelCase(passwordFiltered);
}
if (fromVisitor) {
throw new BadRequestException();
}
return passwords;
} catch (error) {
throw new HttpException(
error.message || 'Error getting online temporary passwords',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getOnlineTemporaryPasswordsTuya(
doorLockUuid: string,
): Promise<getPasswordInterface> {