|
|
|
@ -1,39 +1,39 @@
|
|
|
|
|
import { VisitorPasswordRepository } from './../../../libs/common/src/modules/visitor-password/repositories/visitor-password.repository';
|
|
|
|
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
|
|
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
|
|
|
import {
|
|
|
|
|
Injectable,
|
|
|
|
|
BadRequestException,
|
|
|
|
|
HttpException,
|
|
|
|
|
HttpStatus,
|
|
|
|
|
BadRequestException,
|
|
|
|
|
Injectable,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
|
|
|
import {
|
|
|
|
|
addDeviceObjectInterface,
|
|
|
|
|
createTickInterface,
|
|
|
|
|
} from '../interfaces/visitor-password.interface';
|
|
|
|
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
|
|
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
|
|
|
import { VisitorPasswordRepository } from './../../../libs/common/src/modules/visitor-password/repositories/visitor-password.repository';
|
|
|
|
|
|
|
|
|
|
import { AddDoorLockTemporaryPasswordDto } 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';
|
|
|
|
|
import { DeviceService } from 'src/device/services';
|
|
|
|
|
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
|
|
|
|
import {
|
|
|
|
|
DaysEnum,
|
|
|
|
|
EnableDisableStatusEnum,
|
|
|
|
|
} from '@app/common/constants/days.enum';
|
|
|
|
|
import { PasswordType } from '@app/common/constants/password-type.enum';
|
|
|
|
|
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
|
|
|
|
import {
|
|
|
|
|
CommonHourMinutes,
|
|
|
|
|
CommonHours,
|
|
|
|
|
} from '@app/common/constants/hours-minutes.enum';
|
|
|
|
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
|
|
|
|
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
|
|
|
|
|
import { PasswordType } from '@app/common/constants/password-type.enum';
|
|
|
|
|
import { VisitorPasswordEnum } from '@app/common/constants/visitor-password.enum';
|
|
|
|
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
|
|
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
|
|
|
|
import { EmailService } from '@app/common/util/email.service';
|
|
|
|
|
import { DeviceService } from 'src/device/services';
|
|
|
|
|
import { DoorLockService } from 'src/door-lock/services';
|
|
|
|
|
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
|
|
|
|
import { Not } from 'typeorm';
|
|
|
|
|
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
|
|
|
|
|
import { AddDoorLockTemporaryPasswordDto } from '../dtos';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class VisitorPasswordService {
|
|
|
|
@ -57,6 +57,67 @@ export class VisitorPasswordService {
|
|
|
|
|
secretKey,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getPasswords(projectUuid: string) {
|
|
|
|
|
await this.validateProject(projectUuid);
|
|
|
|
|
|
|
|
|
|
const deviceIds = await this.deviceRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
productDevice: {
|
|
|
|
|
prodType: ProductType.DL,
|
|
|
|
|
},
|
|
|
|
|
spaceDevice: {
|
|
|
|
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
|
|
|
|
community: {
|
|
|
|
|
project: {
|
|
|
|
|
uuid: projectUuid,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
isActive: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = [];
|
|
|
|
|
deviceIds.forEach((deviceId) => {
|
|
|
|
|
data.push(
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsMultiple(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsMultiple(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const result = (await Promise.all(data)).flat().filter((datum) => {
|
|
|
|
|
return datum != null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new SuccessResponseDto({
|
|
|
|
|
message: 'Successfully retrieved temporary passwords',
|
|
|
|
|
data: result,
|
|
|
|
|
statusCode: HttpStatus.OK,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleTemporaryPassword(
|
|
|
|
|
addDoorLockTemporaryPasswordDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
|
userUuid: string,
|
|
|
|
@ -105,7 +166,7 @@ export class VisitorPasswordService {
|
|
|
|
|
statusCode: HttpStatus.CREATED,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
async addOfflineMultipleTimeTemporaryPassword(
|
|
|
|
|
private async addOfflineMultipleTimeTemporaryPassword(
|
|
|
|
|
addDoorLockOfflineMultipleDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
|
userUuid: string,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -169,6 +230,7 @@ export class VisitorPasswordService {
|
|
|
|
|
success: true,
|
|
|
|
|
result: createMultipleOfflinePass.result,
|
|
|
|
|
deviceUuid,
|
|
|
|
|
deviceName: deviceDetails.name,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
@ -231,7 +293,7 @@ export class VisitorPasswordService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addOfflineOneTimeTemporaryPassword(
|
|
|
|
|
private async addOfflineOneTimeTemporaryPassword(
|
|
|
|
|
addDoorLockOfflineOneTimeDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
|
userUuid: string,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -295,6 +357,7 @@ export class VisitorPasswordService {
|
|
|
|
|
success: true,
|
|
|
|
|
result: createOnceOfflinePass.result,
|
|
|
|
|
deviceUuid,
|
|
|
|
|
deviceName: deviceDetails.name,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
@ -357,7 +420,7 @@ export class VisitorPasswordService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addOfflineTemporaryPasswordTuya(
|
|
|
|
|
private async addOfflineTemporaryPasswordTuya(
|
|
|
|
|
doorLockUuid: string,
|
|
|
|
|
type: string,
|
|
|
|
|
addDoorLockOfflineMultipleDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
@ -387,7 +450,7 @@ export class VisitorPasswordService {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async addOnlineTemporaryPasswordMultipleTime(
|
|
|
|
|
private async addOnlineTemporaryPasswordMultipleTime(
|
|
|
|
|
addDoorLockOnlineMultipleDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
|
userUuid: string,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -448,6 +511,7 @@ export class VisitorPasswordService {
|
|
|
|
|
success: true,
|
|
|
|
|
id: createPass.result.id,
|
|
|
|
|
deviceUuid,
|
|
|
|
|
deviceName: passwordData.deviceName,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
@ -508,67 +572,8 @@ export class VisitorPasswordService {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async getPasswords(projectUuid: string) {
|
|
|
|
|
await this.validateProject(projectUuid);
|
|
|
|
|
|
|
|
|
|
const deviceIds = await this.deviceRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
productDevice: {
|
|
|
|
|
prodType: ProductType.DL,
|
|
|
|
|
},
|
|
|
|
|
spaceDevice: {
|
|
|
|
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
|
|
|
|
community: {
|
|
|
|
|
project: {
|
|
|
|
|
uuid: projectUuid,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
isActive: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = [];
|
|
|
|
|
deviceIds.forEach((deviceId) => {
|
|
|
|
|
data.push(
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsMultiple(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOnlineTemporaryPasswordsMultiple(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true, false)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
this.doorLockService
|
|
|
|
|
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true, true)
|
|
|
|
|
.catch(() => {}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
const result = (await Promise.all(data)).flat().filter((datum) => {
|
|
|
|
|
return datum != null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new SuccessResponseDto({
|
|
|
|
|
message: 'Successfully retrieved temporary passwords',
|
|
|
|
|
data: result,
|
|
|
|
|
statusCode: HttpStatus.OK,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addOnlineTemporaryPasswordOneTime(
|
|
|
|
|
private async addOnlineTemporaryPasswordOneTime(
|
|
|
|
|
addDoorLockOnlineOneTimeDto: AddDoorLockTemporaryPasswordDto,
|
|
|
|
|
userUuid: string,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -627,6 +632,7 @@ export class VisitorPasswordService {
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
id: createPass.result.id,
|
|
|
|
|
deviceName: passwordData.deviceName,
|
|
|
|
|
deviceUuid,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
@ -688,7 +694,7 @@ export class VisitorPasswordService {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async getTicketAndEncryptedPassword(
|
|
|
|
|
private async getTicketAndEncryptedPassword(
|
|
|
|
|
doorLockUuid: string,
|
|
|
|
|
passwordPlan: string,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -725,6 +731,7 @@ export class VisitorPasswordService {
|
|
|
|
|
ticketKey: ticketDetails.result.ticket_key,
|
|
|
|
|
encryptedPassword: decrypted,
|
|
|
|
|
deviceTuyaUuid: deviceDetails.deviceTuyaUuid,
|
|
|
|
|
deviceName: deviceDetails.name,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
@ -734,7 +741,7 @@ export class VisitorPasswordService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createDoorLockTicketTuya(
|
|
|
|
|
private async createDoorLockTicketTuya(
|
|
|
|
|
deviceUuid: string,
|
|
|
|
|
): Promise<createTickInterface> {
|
|
|
|
|
try {
|
|
|
|
@ -753,7 +760,7 @@ export class VisitorPasswordService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addOnlineTemporaryPasswordMultipleTuya(
|
|
|
|
|
private async addOnlineTemporaryPasswordMultipleTuya(
|
|
|
|
|
addDeviceObj: addDeviceObjectInterface,
|
|
|
|
|
doorLockUuid: string,
|
|
|
|
|
): Promise<createTickInterface> {
|
|
|
|
@ -795,7 +802,7 @@ export class VisitorPasswordService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWorkingDayValue(days) {
|
|
|
|
|
private getWorkingDayValue(days) {
|
|
|
|
|
// Array representing the days of the week
|
|
|
|
|
const weekDays = [
|
|
|
|
|
DaysEnum.SAT,
|
|
|
|
@ -827,36 +834,7 @@ export class VisitorPasswordService {
|
|
|
|
|
|
|
|
|
|
return workingDayValue;
|
|
|
|
|
}
|
|
|
|
|
getDaysFromWorkingDayValue(workingDayValue) {
|
|
|
|
|
// Array representing the days of the week
|
|
|
|
|
const weekDays = [
|
|
|
|
|
DaysEnum.SAT,
|
|
|
|
|
DaysEnum.FRI,
|
|
|
|
|
DaysEnum.THU,
|
|
|
|
|
DaysEnum.WED,
|
|
|
|
|
DaysEnum.TUE,
|
|
|
|
|
DaysEnum.MON,
|
|
|
|
|
DaysEnum.SUN,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Convert the integer to a binary string and pad with leading zeros to ensure 7 bits
|
|
|
|
|
const binaryString = workingDayValue
|
|
|
|
|
.toString(2)
|
|
|
|
|
.padStart(7, EnableDisableStatusEnum.DISABLED);
|
|
|
|
|
|
|
|
|
|
// Initialize an array to hold the days of the week
|
|
|
|
|
const days = [];
|
|
|
|
|
|
|
|
|
|
// Iterate through the binary string and weekDays array
|
|
|
|
|
for (let i = 0; i < binaryString.length; i++) {
|
|
|
|
|
if (binaryString[i] === EnableDisableStatusEnum.ENABLED) {
|
|
|
|
|
days.push(weekDays[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return days;
|
|
|
|
|
}
|
|
|
|
|
timeToMinutes(timeStr) {
|
|
|
|
|
private timeToMinutes(timeStr) {
|
|
|
|
|
try {
|
|
|
|
|
// Special case for "24:00"
|
|
|
|
|
if (timeStr === CommonHours.TWENTY_FOUR) {
|
|
|
|
@ -883,38 +861,7 @@ export class VisitorPasswordService {
|
|
|
|
|
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
minutesToTime(totalMinutes) {
|
|
|
|
|
try {
|
|
|
|
|
if (
|
|
|
|
|
typeof totalMinutes !== 'number' ||
|
|
|
|
|
totalMinutes < 0 ||
|
|
|
|
|
totalMinutes > CommonHourMinutes.TWENTY_FOUR
|
|
|
|
|
) {
|
|
|
|
|
throw new Error('Invalid minutes value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (totalMinutes === CommonHourMinutes.TWENTY_FOUR) {
|
|
|
|
|
return CommonHours.TWENTY_FOUR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hours = Math.floor(totalMinutes / 60);
|
|
|
|
|
const minutes = totalMinutes % 60;
|
|
|
|
|
|
|
|
|
|
const formattedHours = String(hours).padStart(
|
|
|
|
|
2,
|
|
|
|
|
EnableDisableStatusEnum.DISABLED,
|
|
|
|
|
);
|
|
|
|
|
const formattedMinutes = String(minutes).padStart(
|
|
|
|
|
2,
|
|
|
|
|
EnableDisableStatusEnum.DISABLED,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return `${formattedHours}:${formattedMinutes}`;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async getDeviceByDeviceUuid(
|
|
|
|
|
private async getDeviceByDeviceUuid(
|
|
|
|
|
deviceUuid: string,
|
|
|
|
|
withProductDevice: boolean = true,
|
|
|
|
|
projectUuid: string,
|
|
|
|
@ -939,7 +886,7 @@ export class VisitorPasswordService {
|
|
|
|
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async addOnlineTemporaryPasswordOneTimeTuya(
|
|
|
|
|
private async addOnlineTemporaryPasswordOneTimeTuya(
|
|
|
|
|
addDeviceObj: addDeviceObjectInterface,
|
|
|
|
|
doorLockUuid: string,
|
|
|
|
|
): Promise<createTickInterface> {
|
|
|
|
|