diff --git a/src/door-lock/services/door.lock.service.ts b/src/door-lock/services/door.lock.service.ts index a5892a6..0d90822 100644 --- a/src/door-lock/services/door.lock.service.ts +++ b/src/door-lock/services/door.lock.service.ts @@ -192,6 +192,58 @@ export class DoorLockService { ); } } + + async getOnlineTemporaryPasswordsOneTime(doorLockUuid: 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 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 convertKeysToCamelCase(passwordFiltered); + } + + 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 { diff --git a/src/vistor-password/controllers/visitor-password.controller.ts b/src/vistor-password/controllers/visitor-password.controller.ts index 5e1e39d..c0f09b6 100644 --- a/src/vistor-password/controllers/visitor-password.controller.ts +++ b/src/vistor-password/controllers/visitor-password.controller.ts @@ -6,6 +6,7 @@ import { HttpException, HttpStatus, UseGuards, + Get, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { @@ -122,4 +123,26 @@ export class VisitorPasswordController { ); } } + @Get('') + async GetVisitorPassword() { + try { + return await this.visitorPasswordService.getPasswords(); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @Get('/devices') + async GetVisitorDevices() { + try { + return await this.visitorPasswordService.getAllPassDevices(); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/vistor-password/services/visitor-password.service.ts b/src/vistor-password/services/visitor-password.service.ts index 66da260..2e64faa 100644 --- a/src/vistor-password/services/visitor-password.service.ts +++ b/src/vistor-password/services/visitor-password.service.ts @@ -16,6 +16,7 @@ import { } from '../dtos'; import { EmailService } from '@app/common/util/email.service'; import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services'; +import { DoorLockService } from 'src/door-lock/services'; @Injectable() export class VisitorPasswordService { @@ -24,6 +25,7 @@ export class VisitorPasswordService { private readonly configService: ConfigService, private readonly deviceRepository: DeviceRepository, private readonly emailService: EmailService, + private readonly doorLockService: DoorLockService, private readonly passwordEncryptionService: PasswordEncryptionService, ) { const accessKey = this.configService.get('auth-config.ACCESS_KEY'); @@ -403,6 +405,63 @@ export class VisitorPasswordService { ); } } + async getPasswords() { + const deviceIds = await this.deviceRepository.find({ + where: { + productDevice: { + prodType: ProductType.DL, + }, + }, + }); + const data = []; + deviceIds.forEach((deviceId) => { + data.push( + this.doorLockService + .getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid) + .catch(() => {}), + this.doorLockService + .getOnlineTemporaryPasswordsOneTime(deviceId.uuid) + .catch(() => {}), + this.doorLockService + .getOnlineTemporaryPasswords(deviceId.uuid) + .catch(() => {}), + this.doorLockService + .getOfflineOneTimeTemporaryPasswords(deviceId.uuid) + .catch(() => {}), + ); + }); + return (await Promise.all(data)).flat().map((item) => { + return { + id: item.pwdId || item.id || null, + name: item.pwdName || item.name || '', + type: item.pwdTypeCode || item.type || '', + startTime: item.gmtStart || item.effectiveTime || null, + endTime: item.gmtExpired || item.invalidTime || null, + status: item.status || item.success || null, + additionalInfo: { + optUid: item.optUid || null, + hasClearPwd: item.hasClearPwd || false, + phase: item.phase || null, + phone: item.phone || '', + timeZone: item.timeZone || '', + result: item.result || null, + tid: item.tid || null, + t: item.t || null, + }, + }; + }); + } + + async getAllPassDevices() { + return await this.deviceRepository.find({ + where: { + productDevice: { + prodType: ProductType.DL, + }, + }, + }); + } + async addOnlineTemporaryPasswordOneTime( addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto, ) { diff --git a/src/vistor-password/visitor-password.module.ts b/src/vistor-password/visitor-password.module.ts index 50ea9fb..51239f4 100644 --- a/src/vistor-password/visitor-password.module.ts +++ b/src/vistor-password/visitor-password.module.ts @@ -6,8 +6,9 @@ import { DeviceRepositoryModule } from '@app/common/modules/device'; import { DeviceRepository } from '@app/common/modules/device/repositories'; import { EmailService } from '@app/common/util/email.service'; import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services'; +import { DoorLockModule } from 'src/door-lock/door.lock.module'; @Module({ - imports: [ConfigModule, DeviceRepositoryModule], + imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule], controllers: [VisitorPasswordController], providers: [ VisitorPasswordService,