mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
Refactor Visitor Password Controller and Service for Enhanced Security and Functionality
This commit is contained in:
@ -49,8 +49,8 @@ export class VisitorPasswordController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// @ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
// @UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('temporary-password/online/one-time')
|
@Post('temporary-password/online/one-time')
|
||||||
async addOnlineTemporaryPassword(
|
async addOnlineTemporaryPassword(
|
||||||
@Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto,
|
@Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto,
|
||||||
@ -86,8 +86,6 @@ export class VisitorPasswordController {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
|
||||||
message: 'offline temporary password added successfully',
|
|
||||||
data: temporaryPassword,
|
data: temporaryPassword,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -112,8 +110,6 @@ export class VisitorPasswordController {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
|
||||||
message: 'offline temporary password added successfully',
|
|
||||||
data: temporaryPassword,
|
data: temporaryPassword,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -123,7 +119,9 @@ export class VisitorPasswordController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Get('')
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get()
|
||||||
async GetVisitorPassword() {
|
async GetVisitorPassword() {
|
||||||
try {
|
try {
|
||||||
return await this.visitorPasswordService.getPasswords();
|
return await this.visitorPasswordService.getPasswords();
|
||||||
@ -134,6 +132,8 @@ export class VisitorPasswordController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('/devices')
|
@Get('/devices')
|
||||||
async GetVisitorDevices() {
|
async GetVisitorDevices() {
|
||||||
try {
|
try {
|
||||||
|
@ -17,6 +17,8 @@ import {
|
|||||||
import { EmailService } from '@app/common/util/email.service';
|
import { EmailService } from '@app/common/util/email.service';
|
||||||
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
||||||
import { DoorLockService } from 'src/door-lock/services';
|
import { DoorLockService } from 'src/door-lock/services';
|
||||||
|
import { GetDeviceDetailsInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
|
import { DeviceService } from 'src/device/services';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class VisitorPasswordService {
|
export class VisitorPasswordService {
|
||||||
@ -26,6 +28,7 @@ export class VisitorPasswordService {
|
|||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly emailService: EmailService,
|
private readonly emailService: EmailService,
|
||||||
private readonly doorLockService: DoorLockService,
|
private readonly doorLockService: DoorLockService,
|
||||||
|
private readonly deviceService: DeviceService,
|
||||||
private readonly passwordEncryptionService: PasswordEncryptionService,
|
private readonly passwordEncryptionService: PasswordEncryptionService,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -383,7 +386,7 @@ export class VisitorPasswordService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Return results if there are successful operations
|
// Return results if there are successful operations
|
||||||
if (successfulResults.length > 0) {
|
if (successfulResults?.length > 0) {
|
||||||
return {
|
return {
|
||||||
successOperations: successfulResults,
|
successOperations: successfulResults,
|
||||||
failedOperations: failedResults,
|
failedOperations: failedResults,
|
||||||
@ -416,9 +419,6 @@ export class VisitorPasswordService {
|
|||||||
const data = [];
|
const data = [];
|
||||||
deviceIds.forEach((deviceId) => {
|
deviceIds.forEach((deviceId) => {
|
||||||
data.push(
|
data.push(
|
||||||
this.doorLockService
|
|
||||||
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true)
|
|
||||||
.catch(() => {}),
|
|
||||||
this.doorLockService
|
this.doorLockService
|
||||||
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true)
|
.getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true)
|
||||||
.catch(() => {}),
|
.catch(() => {}),
|
||||||
@ -428,6 +428,9 @@ export class VisitorPasswordService {
|
|||||||
this.doorLockService
|
this.doorLockService
|
||||||
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true)
|
.getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true)
|
||||||
.catch(() => {}),
|
.catch(() => {}),
|
||||||
|
this.doorLockService
|
||||||
|
.getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true)
|
||||||
|
.catch(() => {}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
return (await Promise.all(data)).flat().filter((datum) => {
|
return (await Promise.all(data)).flat().filter((datum) => {
|
||||||
@ -436,13 +439,27 @@ export class VisitorPasswordService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getAllPassDevices() {
|
async getAllPassDevices() {
|
||||||
return await this.deviceRepository.find({
|
const devices = await this.deviceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
productDevice: {
|
productDevice: {
|
||||||
prodType: ProductType.DL,
|
prodType: ProductType.DL,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
|
const devicesData = await Promise.all(
|
||||||
|
devices?.map(async (device) => {
|
||||||
|
return {
|
||||||
|
productUuid: device.productDevice.uuid,
|
||||||
|
productType: device.productDevice.prodType,
|
||||||
|
...(await this.deviceService.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
device.deviceTuyaUuid,
|
||||||
|
)),
|
||||||
|
uuid: device.uuid,
|
||||||
|
} as GetDeviceDetailsInterface;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return devicesData;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addOnlineTemporaryPasswordOneTime(
|
async addOnlineTemporaryPasswordOneTime(
|
||||||
@ -622,7 +639,7 @@ export class VisitorPasswordService {
|
|||||||
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 (addDeviceObj.scheduleList.length > 0) {
|
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),
|
||||||
@ -640,19 +657,16 @@ export class VisitorPasswordService {
|
|||||||
invalid_time: addDeviceObj.invalidTime,
|
invalid_time: addDeviceObj.invalidTime,
|
||||||
password_type: 'ticket',
|
password_type: 'ticket',
|
||||||
ticket_id: addDeviceObj.ticketId,
|
ticket_id: addDeviceObj.ticketId,
|
||||||
...(addDeviceObj.scheduleList.length > 0 && {
|
...(addDeviceObj?.scheduleList?.length > 0 && {
|
||||||
schedule_list: scheduleList,
|
schedule_list: scheduleList,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
type: '0',
|
type: '0',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log('response', response);
|
|
||||||
|
|
||||||
return response as createTickInterface;
|
return response as createTickInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('error', error);
|
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.msg || 'Error adding online temporary password from Tuya',
|
error.msg || 'Error adding online temporary password from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
@ -789,12 +803,9 @@ export class VisitorPasswordService {
|
|||||||
type: '1',
|
type: '1',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
console.log('response', response);
|
|
||||||
|
|
||||||
return response as createTickInterface;
|
return response as createTickInterface;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('error', error);
|
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.msg || 'Error adding online temporary password from Tuya',
|
error.msg || 'Error adding online temporary password from Tuya',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
@ -7,6 +7,10 @@ import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|||||||
import { EmailService } from '@app/common/util/email.service';
|
import { EmailService } from '@app/common/util/email.service';
|
||||||
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
||||||
import { DoorLockModule } from 'src/door-lock/door.lock.module';
|
import { DoorLockModule } from 'src/door-lock/door.lock.module';
|
||||||
|
import { DeviceService } from 'src/device/services';
|
||||||
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||||
controllers: [VisitorPasswordController],
|
controllers: [VisitorPasswordController],
|
||||||
@ -14,6 +18,10 @@ import { DoorLockModule } from 'src/door-lock/door.lock.module';
|
|||||||
VisitorPasswordService,
|
VisitorPasswordService,
|
||||||
EmailService,
|
EmailService,
|
||||||
PasswordEncryptionService,
|
PasswordEncryptionService,
|
||||||
|
DeviceService,
|
||||||
|
ProductRepository,
|
||||||
|
DeviceStatusFirebaseService,
|
||||||
|
SpaceRepository,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
],
|
],
|
||||||
exports: [VisitorPasswordService],
|
exports: [VisitorPasswordService],
|
||||||
|
Reference in New Issue
Block a user