mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Compare commits
45 Commits
ef21b589c0
...
add-check-
Author | SHA1 | Date | |
---|---|---|---|
f80d097ff8 | |||
04bd156df1 | |||
731819aeaa | |||
68d2d3b53d | |||
3fcfe2d92f | |||
c0a069b460 | |||
30724d7d37 | |||
324661e1ee | |||
a83424f45b | |||
71f6ccb4db | |||
68692b7c8b | |||
4d60c1ed54 | |||
27dbe04299 | |||
9bebcb2f3e | |||
43ab0030f0 | |||
c48adb73b5 | |||
d255e6811e | |||
e58d2d4831 | |||
147cf0b582 | |||
4e6b6f6ac5 | |||
932a3efd1c | |||
0a1ccad120 | |||
f337e6c681 | |||
f5bf857071 | |||
d1d4d529a8 | |||
37b582f521 | |||
cf19f08dca | |||
ff370b2baa | |||
04f64407e1 | |||
d7eef5d03e | |||
c8d691b380 | |||
75d03366c2 | |||
52cb69cc84 | |||
a6053b3971 | |||
60d2c8330b | |||
fddd06e06d | |||
3160773c2a | |||
110ed4157a | |||
aa9e90bf08 | |||
c5dd5e28fd | |||
603e74af09 | |||
0e36f32ed6 | |||
705ceeba29 | |||
a37d5bb299 | |||
689a38ee0c |
@ -21,6 +21,7 @@ module.exports = {
|
|||||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||||
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
||||||
'@typescript-eslint/no-explicit-any': 'off',
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
|
"@typescript-eslint/no-unused-vars": 'warn',
|
||||||
},
|
},
|
||||||
settings: {
|
settings: {
|
||||||
'import/resolver': {
|
'import/resolver': {
|
||||||
|
@ -15,6 +15,7 @@ export enum ProductType {
|
|||||||
WL = 'WL',
|
WL = 'WL',
|
||||||
GD = 'GD',
|
GD = 'GD',
|
||||||
CUR = 'CUR',
|
CUR = 'CUR',
|
||||||
|
CUR_2 = 'CUR_2',
|
||||||
PC = 'PC',
|
PC = 'PC',
|
||||||
FOUR_S = '4S',
|
FOUR_S = '4S',
|
||||||
SIX_S = '6S',
|
SIX_S = '6S',
|
||||||
|
@ -25,6 +25,7 @@ import {
|
|||||||
InviteUserEntity,
|
InviteUserEntity,
|
||||||
InviteUserSpaceEntity,
|
InviteUserSpaceEntity,
|
||||||
} from '../modules/Invite-user/entities';
|
} from '../modules/Invite-user/entities';
|
||||||
|
import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities';
|
||||||
import {
|
import {
|
||||||
PowerClampDailyEntity,
|
PowerClampDailyEntity,
|
||||||
PowerClampHourlyEntity,
|
PowerClampHourlyEntity,
|
||||||
@ -46,7 +47,6 @@ import {
|
|||||||
SubspaceModelProductAllocationEntity,
|
SubspaceModelProductAllocationEntity,
|
||||||
} from '../modules/space-model/entities';
|
} from '../modules/space-model/entities';
|
||||||
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
|
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
|
||||||
import { SpaceLinkEntity } from '../modules/space/entities/space-link.entity';
|
|
||||||
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
|
||||||
import { SpaceEntity } from '../modules/space/entities/space.entity';
|
import { SpaceEntity } from '../modules/space/entities/space.entity';
|
||||||
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||||
@ -58,7 +58,6 @@ import {
|
|||||||
UserSpaceEntity,
|
UserSpaceEntity,
|
||||||
} from '../modules/user/entities';
|
} from '../modules/user/entities';
|
||||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||||
import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities';
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
@ -87,7 +86,6 @@ import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities
|
|||||||
PermissionTypeEntity,
|
PermissionTypeEntity,
|
||||||
CommunityEntity,
|
CommunityEntity,
|
||||||
SpaceEntity,
|
SpaceEntity,
|
||||||
SpaceLinkEntity,
|
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
UserSpaceEntity,
|
UserSpaceEntity,
|
||||||
DeviceUserPermissionEntity,
|
DeviceUserPermissionEntity,
|
||||||
@ -127,8 +125,8 @@ import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities
|
|||||||
logger: typeOrmLogger,
|
logger: typeOrmLogger,
|
||||||
extra: {
|
extra: {
|
||||||
charset: 'utf8mb4',
|
charset: 'utf8mb4',
|
||||||
max: 20, // set pool max size
|
max: 100, // set pool max size
|
||||||
idleTimeoutMillis: 5000, // close idle clients after 5 second
|
idleTimeoutMillis: 3000, // close idle clients after 5 second
|
||||||
connectionTimeoutMillis: 12_000, // return an error after 11 second if connection could not be established
|
connectionTimeoutMillis: 12_000, // return an error after 11 second if connection could not be established
|
||||||
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
||||||
},
|
},
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import { IsBoolean, IsDate, IsOptional } from 'class-validator';
|
|
||||||
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
|
||||||
import { Transform } from 'class-transformer';
|
import { Transform } from 'class-transformer';
|
||||||
import { parseToDate } from '../util/parseToDate';
|
import { IsBoolean, IsOptional } from 'class-validator';
|
||||||
import { BooleanValues } from '../constants/boolean-values.enum';
|
import { BooleanValues } from '../constants/boolean-values.enum';
|
||||||
|
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
||||||
|
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
||||||
|
|
||||||
export class PaginationRequestGetListDto {
|
export class PaginationRequestGetListDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -19,6 +18,7 @@ export class PaginationRequestGetListDto {
|
|||||||
return value.obj.includeSpaces === BooleanValues.TRUE;
|
return value.obj.includeSpaces === BooleanValues.TRUE;
|
||||||
})
|
})
|
||||||
public includeSpaces?: boolean = false;
|
public includeSpaces?: boolean = false;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsPageRequestParam({
|
@IsPageRequestParam({
|
||||||
message: 'Page must be bigger than 0',
|
message: 'Page must be bigger than 0',
|
||||||
@ -40,40 +40,4 @@ export class PaginationRequestGetListDto {
|
|||||||
description: 'Size request',
|
description: 'Size request',
|
||||||
})
|
})
|
||||||
size?: number;
|
size?: number;
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'name',
|
|
||||||
required: false,
|
|
||||||
description: 'Name to be filtered',
|
|
||||||
})
|
|
||||||
name?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'from',
|
|
||||||
required: false,
|
|
||||||
type: Number,
|
|
||||||
description: `Start time in UNIX timestamp format to filter`,
|
|
||||||
example: 1674172800000,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => parseToDate(value))
|
|
||||||
@IsDate({
|
|
||||||
message: `From must be in UNIX timestamp format in order to parse to Date instance`,
|
|
||||||
})
|
|
||||||
from?: Date;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'to',
|
|
||||||
required: false,
|
|
||||||
type: Number,
|
|
||||||
description: `End time in UNIX timestamp format to filter`,
|
|
||||||
example: 1674259200000,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => parseToDate(value))
|
|
||||||
@IsDate({
|
|
||||||
message: `To must be in UNIX timestamp format in order to parse to Date instance`,
|
|
||||||
})
|
|
||||||
to?: Date;
|
|
||||||
}
|
}
|
||||||
|
@ -3,28 +3,12 @@ import { DeviceStatusFirebaseController } from './controllers/devices-status.con
|
|||||||
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories/device-status.repository';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories/device-status.repository';
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
|
||||||
import {
|
|
||||||
PowerClampHourlyRepository,
|
|
||||||
PowerClampDailyRepository,
|
|
||||||
PowerClampMonthlyRepository,
|
|
||||||
} from '@app/common/modules/power-clamp/repositories';
|
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
DeviceStatusFirebaseService,
|
DeviceStatusFirebaseService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
DeviceStatusLogRepository,
|
DeviceStatusLogRepository,
|
||||||
PowerClampService,
|
|
||||||
PowerClampHourlyRepository,
|
|
||||||
PowerClampDailyRepository,
|
|
||||||
PowerClampMonthlyRepository,
|
|
||||||
SqlLoaderService,
|
|
||||||
OccupancyService,
|
|
||||||
AqiDataService,
|
|
||||||
],
|
],
|
||||||
controllers: [DeviceStatusFirebaseController],
|
controllers: [DeviceStatusFirebaseController],
|
||||||
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
||||||
|
@ -18,12 +18,6 @@ import {
|
|||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
|
||||||
import { PowerClampEnergyEnum } from '@app/common/constants/power.clamp.enargy.enum';
|
|
||||||
import { PresenceSensorEnum } from '@app/common/constants/presence.sensor.enum';
|
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -31,9 +25,6 @@ export class DeviceStatusFirebaseService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly powerClampService: PowerClampService,
|
|
||||||
private readonly occupancyService: OccupancyService,
|
|
||||||
private readonly aqiDataService: AqiDataService,
|
|
||||||
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -76,25 +67,94 @@ export class DeviceStatusFirebaseService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async addBatchDeviceStatusToOurDb(
|
||||||
|
batch: {
|
||||||
|
deviceTuyaUuid: string;
|
||||||
|
status: any;
|
||||||
|
log: any;
|
||||||
|
device: any;
|
||||||
|
}[],
|
||||||
|
): Promise<void> {
|
||||||
|
const allLogs = [];
|
||||||
|
|
||||||
|
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||||
|
|
||||||
|
for (const item of batch) {
|
||||||
|
const device = item.device;
|
||||||
|
if (!device?.uuid) {
|
||||||
|
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logs = item.log.properties.map((property) =>
|
||||||
|
this.deviceStatusLogRepository.create({
|
||||||
|
deviceId: device.uuid,
|
||||||
|
deviceTuyaId: item.deviceTuyaUuid,
|
||||||
|
productId: item.log.productId,
|
||||||
|
log: item.log,
|
||||||
|
code: property.code,
|
||||||
|
value: property.value,
|
||||||
|
eventId: item.log.dataId,
|
||||||
|
eventTime: new Date(property.time).toISOString(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
allLogs.push(...logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||||
|
|
||||||
|
const insertLogsPromise = (async () => {
|
||||||
|
const chunkSize = 300;
|
||||||
|
let insertedCount = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||||
|
const chunk = allLogs.slice(i, i + chunkSize);
|
||||||
|
try {
|
||||||
|
const result = await this.deviceStatusLogRepository
|
||||||
|
.createQueryBuilder()
|
||||||
|
.insert()
|
||||||
|
.into('device-status-log') // or use DeviceStatusLogEntity
|
||||||
|
.values(chunk)
|
||||||
|
.orIgnore() // skip duplicates
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
insertedCount += result.identifiers.length;
|
||||||
|
console.log(
|
||||||
|
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Insert error (skipped chunk):', error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
||||||
|
);
|
||||||
|
})();
|
||||||
|
|
||||||
|
await insertLogsPromise;
|
||||||
|
}
|
||||||
|
|
||||||
async addDeviceStatusToFirebase(
|
async addDeviceStatusToFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto & { device?: any },
|
||||||
): Promise<AddDeviceStatusDto | null> {
|
): Promise<AddDeviceStatusDto | null> {
|
||||||
try {
|
try {
|
||||||
const device = await this.getDeviceByDeviceTuyaUuid(
|
let device = addDeviceStatusDto.device;
|
||||||
addDeviceStatusDto.deviceTuyaUuid,
|
if (!device) {
|
||||||
);
|
device = await this.getDeviceByDeviceTuyaUuid(
|
||||||
|
addDeviceStatusDto.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
}
|
||||||
if (device?.uuid) {
|
if (device?.uuid) {
|
||||||
return await this.createDeviceStatusFirebase({
|
return await this.createDeviceStatusFirebase({
|
||||||
deviceUuid: device.uuid,
|
deviceUuid: device.uuid,
|
||||||
...addDeviceStatusDto,
|
...addDeviceStatusDto,
|
||||||
productType: device.productDevice.prodType,
|
productType: device.productDevice?.prodType,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Return null if device not found or no UUID
|
// Return null if device not found or no UUID
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Handle the error silently, perhaps log it internally or ignore it
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,6 +168,15 @@ export class DeviceStatusFirebaseService {
|
|||||||
relations: ['productDevice'],
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
async getAllDevices() {
|
||||||
|
return await this.deviceRepository.find({
|
||||||
|
where: {
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
relations: ['productDevice'],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async getDevicesInstructionStatus(deviceUuid: string) {
|
async getDevicesInstructionStatus(deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -211,64 +280,6 @@ export class DeviceStatusFirebaseService {
|
|||||||
return existingData;
|
return existingData;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productId,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.time).toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
|
||||||
const energyCodes = new Set([
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find((status) =>
|
|
||||||
energyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (energyStatus) {
|
|
||||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
|
||||||
addDeviceStatusDto.productType === ProductType.WPS
|
|
||||||
) {
|
|
||||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
|
||||||
|
|
||||||
const occupancyStatus = addDeviceStatusDto?.log?.properties?.find(
|
|
||||||
(status) => occupancyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (occupancyStatus) {
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
|
||||||
await this.aqiDataService.updateAQISensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Return the updated data
|
// Return the updated data
|
||||||
const snapshot: DataSnapshot = await get(dataRef);
|
const snapshot: DataSnapshot = await get(dataRef);
|
||||||
return snapshot.val();
|
return snapshot.val();
|
||||||
|
@ -8,7 +8,10 @@ import { TuyaWebSocketService } from './services/tuya.web.socket.service';
|
|||||||
import { OneSignalService } from './services/onesignal.service';
|
import { OneSignalService } from './services/onesignal.service';
|
||||||
import { DeviceMessagesService } from './services/device.messages.service';
|
import { DeviceMessagesService } from './services/device.messages.service';
|
||||||
import { DeviceRepositoryModule } from '../modules/device/device.repository.module';
|
import { DeviceRepositoryModule } from '../modules/device/device.repository.module';
|
||||||
import { DeviceNotificationRepository } from '../modules/device/repositories';
|
import {
|
||||||
|
DeviceNotificationRepository,
|
||||||
|
DeviceRepository,
|
||||||
|
} from '../modules/device/repositories';
|
||||||
import { DeviceStatusFirebaseModule } from '../firebase/devices-status/devices-status.module';
|
import { DeviceStatusFirebaseModule } from '../firebase/devices-status/devices-status.module';
|
||||||
import { CommunityPermissionService } from './services/community.permission.service';
|
import { CommunityPermissionService } from './services/community.permission.service';
|
||||||
import { CommunityRepository } from '../modules/community/repositories';
|
import { CommunityRepository } from '../modules/community/repositories';
|
||||||
@ -27,6 +30,7 @@ import { SosHandlerService } from './services/sos.handler.service';
|
|||||||
DeviceNotificationRepository,
|
DeviceNotificationRepository,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
SosHandlerService,
|
SosHandlerService,
|
||||||
|
DeviceRepository,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
HelperHashService,
|
HelperHashService,
|
||||||
|
@ -1,44 +1,63 @@
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AqiDataService {
|
export class AqiDataService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
private readonly sqlLoader: SqlLoaderService,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
|
||||||
) {}
|
) {}
|
||||||
async updateAQISensorHistoricalData(deviceUuid: string): Promise<void> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const device = await this.deviceRepository.findOne({
|
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
async updateAQISensorHistoricalData(): Promise<void> {
|
||||||
'fact_daily_space_aqi',
|
try {
|
||||||
'proceduce_update_daily_space_aqi',
|
const { dateStr } = this.getFormattedDates();
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
// Execute all procedures in parallel
|
||||||
|
await Promise.all([
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'proceduce_update_daily_space_aqi',
|
||||||
|
[dateStr],
|
||||||
|
'fact_daily_space_aqi',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update aqi data:', err);
|
console.error('Failed to update AQI sensor historical data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private getFormattedDates(): { dateStr: string } {
|
||||||
private async executeProcedure(
|
const now = new Date();
|
||||||
procedureFolderName: string,
|
return {
|
||||||
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
|
};
|
||||||
|
}
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
try {
|
||||||
await this.dataSource.query(query, params);
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
await this.dataSource.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -1,65 +1,68 @@
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class OccupancyService {
|
export class OccupancyService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
private readonly sqlLoader: SqlLoaderService,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
|
||||||
) {}
|
) {}
|
||||||
async updateOccupancySensorHistoricalDurationData(
|
|
||||||
deviceUuid: string,
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const device = await this.deviceRepository.findOne({
|
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
async updateOccupancyDataProcedures(): Promise<void> {
|
||||||
'fact_daily_space_occupancy_duration',
|
try {
|
||||||
'procedure_update_daily_space_occupancy_duration',
|
const { dateStr } = this.getFormattedDates();
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
// Execute all procedures in parallel
|
||||||
|
await Promise.all([
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'procedure_update_fact_space_occupancy',
|
||||||
|
[dateStr],
|
||||||
|
'fact_space_occupancy_count',
|
||||||
|
),
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'procedure_update_daily_space_occupancy_duration',
|
||||||
|
[dateStr],
|
||||||
|
'fact_daily_space_occupancy_duration',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update occupancy duration data:', err);
|
console.error('Failed to update occupancy data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async updateOccupancySensorHistoricalData(deviceUuid: string): Promise<void> {
|
private getFormattedDates(): { dateStr: string } {
|
||||||
try {
|
const now = new Date();
|
||||||
const now = new Date();
|
return {
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
const device = await this.deviceRepository.findOne({
|
};
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
|
||||||
'fact_space_occupancy_count',
|
|
||||||
'procedure_update_fact_space_occupancy',
|
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to insert or update occupancy data:', err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
private async executeProcedure(
|
|
||||||
procedureFolderName: string,
|
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
try {
|
||||||
await this.dataSource.query(query, params);
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
await this.dataSource.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PowerClampService {
|
export class PowerClampService {
|
||||||
@ -10,48 +10,72 @@ export class PowerClampService {
|
|||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async updateEnergyConsumedHistoricalData(deviceUuid: string): Promise<void> {
|
async updateEnergyConsumedHistoricalData(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const now = new Date();
|
const { dateStr, monthYear } = this.getFormattedDates();
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const hour = now.getHours();
|
|
||||||
const monthYear = now
|
|
||||||
.toLocaleDateString('en-US', {
|
|
||||||
month: '2-digit',
|
|
||||||
year: 'numeric',
|
|
||||||
})
|
|
||||||
.replace('/', '-'); // MM-YYYY
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
// Execute all procedures in parallel
|
||||||
'fact_hourly_device_energy_consumed_procedure',
|
await Promise.all([
|
||||||
[deviceUuid, dateStr, hour],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_hourly_device_energy_consumed_procedure',
|
||||||
|
[dateStr],
|
||||||
await this.executeProcedure(
|
'fact_device_energy_consumed',
|
||||||
'fact_daily_device_energy_consumed_procedure',
|
),
|
||||||
[deviceUuid, dateStr],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_daily_device_energy_consumed_procedure',
|
||||||
|
[dateStr],
|
||||||
await this.executeProcedure(
|
'fact_device_energy_consumed',
|
||||||
'fact_monthly_device_energy_consumed_procedure',
|
),
|
||||||
[deviceUuid, monthYear],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_monthly_device_energy_consumed_procedure',
|
||||||
|
[monthYear],
|
||||||
|
'fact_device_energy_consumed',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update energy data:', err);
|
console.error('Failed to update energy consumption data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async executeProcedure(
|
private getFormattedDates(): { dateStr: string; monthYear: string } {
|
||||||
|
const now = new Date();
|
||||||
|
return {
|
||||||
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
|
monthYear: now
|
||||||
|
.toLocaleDateString('en-US', {
|
||||||
|
month: '2-digit',
|
||||||
|
year: 'numeric',
|
||||||
|
})
|
||||||
|
.replace('/', '-'), // MM-YYYY
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(
|
try {
|
||||||
'fact_device_energy_consumed',
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
procedureFileName,
|
await this.dataSource.query(query, params);
|
||||||
);
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
await this.dataSource.query(query, params);
|
} catch (err) {
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -16,21 +16,46 @@ export class SosHandlerService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleSosEvent(devId: string, logData: any): Promise<void> {
|
async handleSosEventFirebase(device: any, logData: any): Promise<void> {
|
||||||
|
const sosTrueStatus = [{ code: 'sos', value: true }];
|
||||||
|
const sosFalseStatus = [{ code: 'sos', value: false }];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// ✅ Send true status
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
status: [{ code: 'sos', value: true }],
|
status: sosTrueStatus,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb([
|
||||||
|
{
|
||||||
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
|
status: sosTrueStatus,
|
||||||
|
log: logData,
|
||||||
|
device,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// ✅ Schedule false status
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
status: [{ code: 'sos', value: false }],
|
status: sosFalseStatus,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb([
|
||||||
|
{
|
||||||
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
|
status: sosFalseStatus,
|
||||||
|
log: logData,
|
||||||
|
device,
|
||||||
|
},
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error('Failed to send SOS false value', err);
|
this.logger.error('Failed to send SOS false value', err);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,24 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { SosHandlerService } from './sos.handler.service';
|
import { SosHandlerService } from './sos.handler.service';
|
||||||
|
import * as NodeCache from 'node-cache';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TuyaWebSocketService {
|
export class TuyaWebSocketService implements OnModuleInit {
|
||||||
private client: any;
|
private client: any;
|
||||||
private readonly isDevEnv: boolean;
|
private readonly isDevEnv: boolean;
|
||||||
|
private readonly deviceCache = new NodeCache({ stdTTL: 7200 }); // TTL = 2 hour
|
||||||
|
|
||||||
|
private messageQueue: {
|
||||||
|
devId: string;
|
||||||
|
status: any;
|
||||||
|
logData: any;
|
||||||
|
device: any;
|
||||||
|
}[] = [];
|
||||||
|
|
||||||
|
private isProcessing = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
@ -26,16 +37,36 @@ export class TuyaWebSocketService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
|
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
|
||||||
// Set up event handlers
|
|
||||||
this.setupEventHandlers();
|
this.setupEventHandlers();
|
||||||
|
|
||||||
// Start receiving messages
|
|
||||||
this.client.start();
|
this.client.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the queue processor every 15 seconds
|
||||||
|
setInterval(() => this.processQueue(), 15000);
|
||||||
|
|
||||||
|
// Refresh the cache every 1 hour
|
||||||
|
setInterval(() => this.initializeDeviceCache(), 30 * 60 * 1000); // 30 minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.initializeDeviceCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async initializeDeviceCache() {
|
||||||
|
try {
|
||||||
|
const allDevices = await this.deviceStatusFirebaseService.getAllDevices();
|
||||||
|
allDevices.forEach((device) => {
|
||||||
|
if (device.deviceTuyaUuid) {
|
||||||
|
this.deviceCache.set(device.deviceTuyaUuid, device);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(`✅ Refreshed cache with ${allDevices.length} devices.`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Failed to initialize device cache:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private setupEventHandlers() {
|
private setupEventHandlers() {
|
||||||
// Event handlers
|
|
||||||
this.client.open(() => {
|
this.client.open(() => {
|
||||||
console.log('open');
|
console.log('open');
|
||||||
});
|
});
|
||||||
@ -43,23 +74,38 @@ export class TuyaWebSocketService {
|
|||||||
this.client.message(async (ws: WebSocket, message: any) => {
|
this.client.message(async (ws: WebSocket, message: any) => {
|
||||||
try {
|
try {
|
||||||
const { devId, status, logData } = this.extractMessageData(message);
|
const { devId, status, logData } = this.extractMessageData(message);
|
||||||
|
if (!Array.isArray(logData?.properties)) {
|
||||||
|
this.client.ackMessage(message.messageId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const device = this.deviceCache.get(devId);
|
||||||
|
if (!device) {
|
||||||
|
// console.log(⛔ Unknown device: ${devId}, message ignored.);
|
||||||
|
this.client.ackMessage(message.messageId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.sosHandlerService.isSosTriggered(status)) {
|
if (this.sosHandlerService.isSosTriggered(status)) {
|
||||||
await this.sosHandlerService.handleSosEvent(devId, logData);
|
await this.sosHandlerService.handleSosEventFirebase(devId, logData);
|
||||||
} else {
|
} else {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: devId,
|
||||||
status: status,
|
status,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push to internal queue
|
||||||
|
this.messageQueue.push({ devId, status, logData, device });
|
||||||
|
|
||||||
|
// Acknowledge the message
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing message:', error);
|
console.error('❌ Error receiving message:', error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.reconnect(() => {
|
this.client.reconnect(() => {
|
||||||
console.log('reconnect');
|
console.log('reconnect');
|
||||||
});
|
});
|
||||||
@ -80,6 +126,37 @@ export class TuyaWebSocketService {
|
|||||||
console.error('WebSocket error:', error);
|
console.error('WebSocket error:', error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
private async processQueue() {
|
||||||
|
if (this.isProcessing) {
|
||||||
|
console.log('⏳ Skipping: still processing previous batch');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.messageQueue.length === 0) return;
|
||||||
|
|
||||||
|
this.isProcessing = true;
|
||||||
|
const batch = [...this.messageQueue];
|
||||||
|
this.messageQueue = [];
|
||||||
|
|
||||||
|
console.log(`🔁 Processing batch of size: ${batch.length}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb(
|
||||||
|
batch.map((item) => ({
|
||||||
|
deviceTuyaUuid: item.devId,
|
||||||
|
status: item.status,
|
||||||
|
log: item.logData,
|
||||||
|
device: item.device,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error processing batch:', error);
|
||||||
|
this.messageQueue.unshift(...batch); // retry
|
||||||
|
} finally {
|
||||||
|
this.isProcessing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private extractMessageData(message: any): {
|
private extractMessageData(message: any): {
|
||||||
devId: string;
|
devId: string;
|
||||||
status: any;
|
status: any;
|
||||||
|
@ -1,32 +1,3 @@
|
|||||||
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
|
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { SpaceEntity } from './space.entity';
|
|
||||||
import { Direction } from '@app/common/constants/direction.enum';
|
|
||||||
|
|
||||||
@Entity({ name: 'space-link' })
|
export class SpaceLinkEntity extends AbstractEntity {}
|
||||||
export class SpaceLinkEntity extends AbstractEntity {
|
|
||||||
@ManyToOne(() => SpaceEntity, { nullable: false, onDelete: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'start_space_id' })
|
|
||||||
public startSpace: SpaceEntity;
|
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, { nullable: false, onDelete: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'end_space_id' })
|
|
||||||
public endSpace: SpaceEntity;
|
|
||||||
|
|
||||||
@Column({
|
|
||||||
nullable: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
public disabled: boolean;
|
|
||||||
|
|
||||||
@Column({
|
|
||||||
nullable: false,
|
|
||||||
enum: Object.values(Direction),
|
|
||||||
})
|
|
||||||
direction: string;
|
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceLinkEntity>) {
|
|
||||||
super();
|
|
||||||
Object.assign(this, partial);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||||
import { SpaceDto } from '../dtos';
|
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { UserSpaceEntity } from '../../user/entities';
|
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
||||||
import { DeviceEntity } from '../../device/entities';
|
|
||||||
import { CommunityEntity } from '../../community/entities';
|
import { CommunityEntity } from '../../community/entities';
|
||||||
import { SpaceLinkEntity } from './space-link.entity';
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||||
|
import { SpaceDailyOccupancyDurationEntity } from '../../occupancy/entities';
|
||||||
|
import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
||||||
import { SceneEntity } from '../../scene/entities';
|
import { SceneEntity } from '../../scene/entities';
|
||||||
import { SpaceModelEntity } from '../../space-model';
|
import { SpaceModelEntity } from '../../space-model';
|
||||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
import { UserSpaceEntity } from '../../user/entities';
|
||||||
|
import { SpaceDto } from '../dtos';
|
||||||
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
||||||
import { SubspaceEntity } from './subspace/subspace.entity';
|
import { SubspaceEntity } from './subspace/subspace.entity';
|
||||||
import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
|
||||||
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
|
||||||
import { SpaceDailyOccupancyDurationEntity } from '../../occupancy/entities';
|
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||||
@ -75,16 +74,6 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
)
|
)
|
||||||
devices: DeviceEntity[];
|
devices: DeviceEntity[];
|
||||||
|
|
||||||
@OneToMany(() => SpaceLinkEntity, (connection) => connection.startSpace, {
|
|
||||||
nullable: true,
|
|
||||||
})
|
|
||||||
public outgoingConnections: SpaceLinkEntity[];
|
|
||||||
|
|
||||||
@OneToMany(() => SpaceLinkEntity, (connection) => connection.endSpace, {
|
|
||||||
nullable: true,
|
|
||||||
})
|
|
||||||
public incomingConnections: SpaceLinkEntity[];
|
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: true,
|
nullable: true,
|
||||||
type: 'text',
|
type: 'text',
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { DataSource, Repository } from 'typeorm';
|
import { DataSource, Repository } from 'typeorm';
|
||||||
import { InviteSpaceEntity } from '../entities/invite-space.entity';
|
import { InviteSpaceEntity } from '../entities/invite-space.entity';
|
||||||
import { SpaceLinkEntity } from '../entities/space-link.entity';
|
|
||||||
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
|
||||||
import { SpaceEntity } from '../entities/space.entity';
|
import { SpaceEntity } from '../entities/space.entity';
|
||||||
|
|
||||||
@ -13,11 +12,7 @@ export class SpaceRepository extends Repository<SpaceEntity> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
|
export class SpaceLinkRepository {}
|
||||||
constructor(private dataSource: DataSource) {
|
|
||||||
super(SpaceLinkEntity, dataSource.createEntityManager());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
-- Query Pipeline Starts Here
|
-- Query Pipeline Starts Here
|
||||||
@ -277,7 +276,10 @@ SELECT
|
|||||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||||
FROM daily_percentages p
|
FROM daily_percentages p
|
||||||
LEFT JOIN daily_averages a
|
LEFT JOIN daily_averages a
|
||||||
ON p.space_id = a.space_id AND p.event_date = a.event_date
|
ON p.space_id = a.space_id
|
||||||
|
AND p.event_date = a.event_date
|
||||||
|
JOIN params
|
||||||
|
ON params.event_date = a.event_date
|
||||||
ORDER BY p.space_id, p.event_date)
|
ORDER BY p.space_id, p.event_date)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
presence_logs AS (
|
presence_logs AS (
|
||||||
@ -86,8 +85,7 @@ final_data AS (
|
|||||||
ROUND(LEAST(raw_occupied_seconds, 86400) / 86400.0 * 100, 2) AS occupancy_percentage
|
ROUND(LEAST(raw_occupied_seconds, 86400) / 86400.0 * 100, 2) AS occupancy_percentage
|
||||||
FROM summed_intervals s
|
FROM summed_intervals s
|
||||||
JOIN params p
|
JOIN params p
|
||||||
ON p.space_id = s.space_id
|
ON p.event_date = s.event_date
|
||||||
AND p.event_date = s.event_date
|
|
||||||
)
|
)
|
||||||
|
|
||||||
INSERT INTO public."space-daily-occupancy-duration" (
|
INSERT INTO public."space-daily-occupancy-duration" (
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::date AS target_date
|
||||||
$2::date AS target_date
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -14,8 +13,7 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_A AS (
|
energy_phase_A AS (
|
||||||
@ -29,8 +27,7 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_B AS (
|
energy_phase_B AS (
|
||||||
@ -44,8 +41,7 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_C AS (
|
energy_phase_C AS (
|
||||||
@ -59,8 +55,7 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
final_data AS (
|
final_data AS (
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::date AS target_date
|
||||||
$2::date AS target_date,
|
|
||||||
$3::text AS target_hour
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -15,9 +13,7 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_A AS (
|
energy_phase_A AS (
|
||||||
@ -31,9 +27,7 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_B AS (
|
energy_phase_B AS (
|
||||||
@ -47,9 +41,7 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_C AS (
|
energy_phase_C AS (
|
||||||
@ -63,9 +55,7 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
final_data AS (
|
final_data AS (
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::text AS target_month -- Format should match 'MM-YYYY'
|
||||||
$2::text AS target_month -- Format should match 'MM-YYYY'
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -14,7 +13,6 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -29,7 +27,6 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -44,7 +41,6 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -59,7 +55,6 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
device_logs AS (
|
device_logs AS (
|
||||||
@ -87,8 +86,7 @@ SELECT summary.space_id,
|
|||||||
count_total_presence_detected
|
count_total_presence_detected
|
||||||
FROM summary
|
FROM summary
|
||||||
JOIN params P ON true
|
JOIN params P ON true
|
||||||
where summary.space_id = P.space_id
|
where (P.event_date IS NULL or summary.event_date::date = P.event_date)
|
||||||
and (P.event_date IS NULL or summary.event_date::date = P.event_date)
|
|
||||||
ORDER BY space_id, event_date)
|
ORDER BY space_id, event_date)
|
||||||
|
|
||||||
|
|
||||||
|
64
package-lock.json
generated
64
package-lock.json
generated
@ -18,6 +18,7 @@
|
|||||||
"@nestjs/jwt": "^10.2.0",
|
"@nestjs/jwt": "^10.2.0",
|
||||||
"@nestjs/passport": "^10.0.3",
|
"@nestjs/passport": "^10.0.3",
|
||||||
"@nestjs/platform-express": "^10.0.0",
|
"@nestjs/platform-express": "^10.0.0",
|
||||||
|
"@nestjs/schedule": "^6.0.0",
|
||||||
"@nestjs/swagger": "^7.3.0",
|
"@nestjs/swagger": "^7.3.0",
|
||||||
"@nestjs/terminus": "^11.0.0",
|
"@nestjs/terminus": "^11.0.0",
|
||||||
"@nestjs/throttler": "^6.4.0",
|
"@nestjs/throttler": "^6.4.0",
|
||||||
@ -38,6 +39,7 @@
|
|||||||
"ioredis": "^5.3.2",
|
"ioredis": "^5.3.2",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"nest-winston": "^1.10.2",
|
"nest-winston": "^1.10.2",
|
||||||
|
"node-cache": "^5.1.2",
|
||||||
"nodemailer": "^6.9.10",
|
"nodemailer": "^6.9.10",
|
||||||
"onesignal-node": "^3.4.0",
|
"onesignal-node": "^3.4.0",
|
||||||
"passport-jwt": "^4.0.1",
|
"passport-jwt": "^4.0.1",
|
||||||
@ -2538,6 +2540,19 @@
|
|||||||
"@nestjs/core": "^10.0.0"
|
"@nestjs/core": "^10.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@nestjs/schedule": {
|
||||||
|
"version": "6.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@nestjs/schedule/-/schedule-6.0.0.tgz",
|
||||||
|
"integrity": "sha512-aQySMw6tw2nhitELXd3EiRacQRgzUKD9mFcUZVOJ7jPLqIBvXOyvRWLsK9SdurGA+jjziAlMef7iB5ZEFFoQpw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"cron": "4.3.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
||||||
|
"@nestjs/core": "^10.0.0 || ^11.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@nestjs/schematics": {
|
"node_modules/@nestjs/schematics": {
|
||||||
"version": "10.2.3",
|
"version": "10.2.3",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-10.2.3.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-10.2.3.tgz",
|
||||||
@ -3215,6 +3230,12 @@
|
|||||||
"@types/node": "*"
|
"@types/node": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/luxon": {
|
||||||
|
"version": "3.6.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.6.2.tgz",
|
||||||
|
"integrity": "sha512-R/BdP7OxEMc44l2Ex5lSXHoIXTB2JLNa3y2QISIbr58U/YcsffyQrYW//hZSdrfxrjRZj3GcUoxMPGdO8gSYuw==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/@types/methods": {
|
"node_modules/@types/methods": {
|
||||||
"version": "1.1.4",
|
"version": "1.1.4",
|
||||||
"resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz",
|
"resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz",
|
||||||
@ -5426,6 +5447,19 @@
|
|||||||
"devOptional": true,
|
"devOptional": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/cron": {
|
||||||
|
"version": "4.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/cron/-/cron-4.3.0.tgz",
|
||||||
|
"integrity": "sha512-ciiYNLfSlF9MrDqnbMdRWFiA6oizSF7kA1osPP9lRzNu0Uu+AWog1UKy7SkckiDY2irrNjeO6qLyKnXC8oxmrw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/luxon": "~3.6.0",
|
||||||
|
"luxon": "~3.6.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18.x"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.6",
|
"version": "7.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||||
@ -9777,6 +9811,15 @@
|
|||||||
"yallist": "^3.0.2"
|
"yallist": "^3.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/luxon": {
|
||||||
|
"version": "3.6.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.6.1.tgz",
|
||||||
|
"integrity": "sha512-tJLxrKJhO2ukZ5z0gyjY1zPh3Rh88Ej9P7jNrZiHMUXHae1yvI2imgOZtL1TO8TW6biMMKfTtAOoEJANgtWBMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/magic-string": {
|
"node_modules/magic-string": {
|
||||||
"version": "0.30.8",
|
"version": "0.30.8",
|
||||||
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.8.tgz",
|
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.8.tgz",
|
||||||
@ -10142,6 +10185,27 @@
|
|||||||
"node": "^18 || ^20 || >= 21"
|
"node": "^18 || ^20 || >= 21"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/node-cache": {
|
||||||
|
"version": "5.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||||
|
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"clone": "2.x"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 8.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/node-cache/node_modules/clone": {
|
||||||
|
"version": "2.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||||
|
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/node-emoji": {
|
"node_modules/node-emoji": {
|
||||||
"version": "1.11.0",
|
"version": "1.11.0",
|
||||||
"resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-1.11.0.tgz",
|
"resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-1.11.0.tgz",
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
"@nestjs/jwt": "^10.2.0",
|
"@nestjs/jwt": "^10.2.0",
|
||||||
"@nestjs/passport": "^10.0.3",
|
"@nestjs/passport": "^10.0.3",
|
||||||
"@nestjs/platform-express": "^10.0.0",
|
"@nestjs/platform-express": "^10.0.0",
|
||||||
|
"@nestjs/schedule": "^6.0.0",
|
||||||
"@nestjs/swagger": "^7.3.0",
|
"@nestjs/swagger": "^7.3.0",
|
||||||
"@nestjs/terminus": "^11.0.0",
|
"@nestjs/terminus": "^11.0.0",
|
||||||
"@nestjs/throttler": "^6.4.0",
|
"@nestjs/throttler": "^6.4.0",
|
||||||
@ -50,6 +51,7 @@
|
|||||||
"ioredis": "^5.3.2",
|
"ioredis": "^5.3.2",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"nest-winston": "^1.10.2",
|
"nest-winston": "^1.10.2",
|
||||||
|
"node-cache": "^5.1.2",
|
||||||
"nodemailer": "^6.9.10",
|
"nodemailer": "^6.9.10",
|
||||||
"onesignal-node": "^3.4.0",
|
"onesignal-node": "^3.4.0",
|
||||||
"passport-jwt": "^4.0.1",
|
"passport-jwt": "^4.0.1",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { SeederModule } from '@app/common/seed/seeder.module';
|
import { SeederModule } from '@app/common/seed/seeder.module';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
|
||||||
import { WinstonModule } from 'nest-winston';
|
import { WinstonModule } from 'nest-winston';
|
||||||
import { AuthenticationModule } from './auth/auth.module';
|
import { AuthenticationModule } from './auth/auth.module';
|
||||||
import { AutomationModule } from './automation/automation.module';
|
import { AutomationModule } from './automation/automation.module';
|
||||||
@ -35,18 +35,32 @@ import { UserNotificationModule } from './user-notification/user-notification.mo
|
|||||||
import { UserModule } from './users/user.module';
|
import { UserModule } from './users/user.module';
|
||||||
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
||||||
|
|
||||||
|
import { ThrottlerGuard } from '@nestjs/throttler';
|
||||||
|
import { ThrottlerModule } from '@nestjs/throttler/dist/throttler.module';
|
||||||
|
import { isArray } from 'class-validator';
|
||||||
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
||||||
import { AqiModule } from './aqi/aqi.module';
|
import { AqiModule } from './aqi/aqi.module';
|
||||||
import { OccupancyModule } from './occupancy/occupancy.module';
|
import { OccupancyModule } from './occupancy/occupancy.module';
|
||||||
import { WeatherModule } from './weather/weather.module';
|
import { WeatherModule } from './weather/weather.module';
|
||||||
|
import { ScheduleModule as NestScheduleModule } from '@nestjs/schedule';
|
||||||
|
import { SchedulerModule } from './scheduler/scheduler.module';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
load: config,
|
load: config,
|
||||||
}),
|
}),
|
||||||
/* ThrottlerModule.forRoot({
|
ThrottlerModule.forRoot({
|
||||||
throttlers: [{ ttl: 100000, limit: 30 }],
|
throttlers: [{ ttl: 60000, limit: 100 }],
|
||||||
}), */
|
generateKey: (context) => {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
console.log('Real IP:', req.headers['x-forwarded-for']);
|
||||||
|
return req.headers['x-forwarded-for']
|
||||||
|
? isArray(req.headers['x-forwarded-for'])
|
||||||
|
? req.headers['x-forwarded-for'][0].split(':')[0]
|
||||||
|
: req.headers['x-forwarded-for'].split(':')[0]
|
||||||
|
: req.ip;
|
||||||
|
},
|
||||||
|
}),
|
||||||
WinstonModule.forRoot(winstonLoggerOptions),
|
WinstonModule.forRoot(winstonLoggerOptions),
|
||||||
ClientModule,
|
ClientModule,
|
||||||
AuthenticationModule,
|
AuthenticationModule,
|
||||||
@ -82,16 +96,18 @@ import { WeatherModule } from './weather/weather.module';
|
|||||||
OccupancyModule,
|
OccupancyModule,
|
||||||
WeatherModule,
|
WeatherModule,
|
||||||
AqiModule,
|
AqiModule,
|
||||||
|
SchedulerModule,
|
||||||
|
NestScheduleModule.forRoot(),
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: APP_INTERCEPTOR,
|
provide: APP_INTERCEPTOR,
|
||||||
useClass: LoggingInterceptor,
|
useClass: LoggingInterceptor,
|
||||||
},
|
},
|
||||||
/* {
|
{
|
||||||
provide: APP_GUARD,
|
provide: APP_GUARD,
|
||||||
useClass: ThrottlerGuard,
|
useClass: ThrottlerGuard,
|
||||||
}, */
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
@ -30,6 +30,8 @@ import { PowerClampService } from '@app/common/helper/services/power.clamp.servi
|
|||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule],
|
||||||
@ -59,6 +61,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [],
|
exports: [],
|
||||||
})
|
})
|
||||||
|
@ -64,6 +64,8 @@ import {
|
|||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
||||||
@ -78,6 +80,7 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
ProjectRepository,
|
ProjectRepository,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
|
// Todo: find out why this is needed
|
||||||
SpaceLinkService,
|
SpaceLinkService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
@ -117,6 +120,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [CommunityService, SpacePermissionService],
|
exports: [CommunityService, SpacePermissionService],
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { CommunityService } from '../services/community.service';
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@ -10,17 +9,18 @@ import {
|
|||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { AddCommunityDto } from '../dtos/add.community.dto';
|
import { AddCommunityDto } from '../dtos/add.community.dto';
|
||||||
import { GetCommunityParams } from '../dtos/get.community.dto';
|
import { GetCommunityParams } from '../dtos/get.community.dto';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
import { CommunityService } from '../services/community.service';
|
||||||
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { ProjectParam } from '../dtos';
|
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
|
||||||
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
||||||
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { ProjectParam } from '../dtos';
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -45,6 +45,21 @@ export class CommunityController {
|
|||||||
return await this.communityService.createCommunity(param, addCommunityDto);
|
return await this.communityService.createCommunity(param, addCommunityDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(PermissionsGuard)
|
||||||
|
@Permissions('COMMUNITY_VIEW')
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_SUMMARY,
|
||||||
|
description: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_DESCRIPTION,
|
||||||
|
})
|
||||||
|
@Get('v2')
|
||||||
|
async getCommunitiesV2(
|
||||||
|
@Param() param: ProjectParam,
|
||||||
|
@Query() query: PaginationRequestWithSearchGetListDto,
|
||||||
|
): Promise<any> {
|
||||||
|
return this.communityService.getCommunitiesV2(param, query);
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('COMMUNITY_VIEW')
|
@Permissions('COMMUNITY_VIEW')
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
import {
|
||||||
|
ORPHAN_COMMUNITY_NAME,
|
||||||
|
ORPHAN_SPACE_NAME,
|
||||||
|
} from '@app/common/constants/orphan-constant';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
@ -22,7 +25,7 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceService } from 'src/space/services';
|
import { SpaceService } from 'src/space/services';
|
||||||
import { SelectQueryBuilder } from 'typeorm';
|
import { QueryRunner, SelectQueryBuilder } from 'typeorm';
|
||||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
|
||||||
@ -69,12 +72,18 @@ export class CommunityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCommunityById(params: GetCommunityParams): Promise<BaseResponseDto> {
|
async getCommunityById(
|
||||||
|
params: GetCommunityParams,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
const { communityUuid, projectUuid } = params;
|
const { communityUuid, projectUuid } = params;
|
||||||
|
|
||||||
await this.validateProject(projectUuid);
|
await this.validateProject(projectUuid);
|
||||||
|
|
||||||
const community = await this.communityRepository.findOneBy({
|
const communityRepository =
|
||||||
|
queryRunner?.manager.getRepository(CommunityEntity) ||
|
||||||
|
this.communityRepository;
|
||||||
|
const community = await communityRepository.findOneBy({
|
||||||
uuid: communityUuid,
|
uuid: communityUuid,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -161,6 +170,75 @@ export class CommunityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCommunitiesV2(
|
||||||
|
{ projectUuid }: ProjectParam,
|
||||||
|
{
|
||||||
|
search,
|
||||||
|
includeSpaces,
|
||||||
|
...pageable
|
||||||
|
}: Partial<ExtendedTypeORMCustomModelFindAllQuery>,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const project = await this.validateProject(projectUuid);
|
||||||
|
|
||||||
|
let qb: undefined | SelectQueryBuilder<CommunityEntity> = undefined;
|
||||||
|
|
||||||
|
qb = this.communityRepository
|
||||||
|
.createQueryBuilder('c')
|
||||||
|
.where('c.project = :projectUuid', { projectUuid })
|
||||||
|
.andWhere(`c.name != '${ORPHAN_COMMUNITY_NAME}-${project.name}'`)
|
||||||
|
.distinct(true);
|
||||||
|
|
||||||
|
if (includeSpaces) {
|
||||||
|
qb.leftJoinAndSelect(
|
||||||
|
'c.spaces',
|
||||||
|
'space',
|
||||||
|
'space.disabled = :disabled AND space.spaceName != :orphanSpaceName',
|
||||||
|
{ disabled: false, orphanSpaceName: ORPHAN_SPACE_NAME },
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect('space.parent', 'parent')
|
||||||
|
.leftJoinAndSelect(
|
||||||
|
'space.children',
|
||||||
|
'children',
|
||||||
|
'children.disabled = :disabled',
|
||||||
|
{ disabled: false },
|
||||||
|
);
|
||||||
|
// .leftJoinAndSelect('space.spaceModel', 'spaceModel')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search) {
|
||||||
|
qb.andWhere(
|
||||||
|
`c.name ILIKE :search ${includeSpaces ? 'OR space.space_name ILIKE :search' : ''}`,
|
||||||
|
{ search },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const customModel = TypeORMCustomModel(this.communityRepository);
|
||||||
|
|
||||||
|
const { baseResponseDto, paginationResponseDto } =
|
||||||
|
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
|
||||||
|
if (includeSpaces) {
|
||||||
|
baseResponseDto.data = baseResponseDto.data.map((community) => ({
|
||||||
|
...community,
|
||||||
|
spaces: this.spaceService.buildSpaceHierarchy(community.spaces || []),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return new PageResponse<CommunityDto>(
|
||||||
|
baseResponseDto,
|
||||||
|
paginationResponseDto,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// Generic error handling
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'An error occurred while fetching communities.',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async updateCommunity(
|
async updateCommunity(
|
||||||
params: GetCommunityParams,
|
params: GetCommunityParams,
|
||||||
updateCommunityDto: UpdateCommunityNameDto,
|
updateCommunityDto: UpdateCommunityNameDto,
|
||||||
|
28
src/device/commands/cur2-commands.ts
Normal file
28
src/device/commands/cur2-commands.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
interface BaseCommand {
|
||||||
|
code: string;
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
export interface ControlCur2Command extends BaseCommand {
|
||||||
|
code: 'control';
|
||||||
|
value: 'open' | 'close' | 'stop';
|
||||||
|
}
|
||||||
|
export interface ControlCur2PercentCommand extends BaseCommand {
|
||||||
|
code: 'percent_control';
|
||||||
|
value: 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100;
|
||||||
|
}
|
||||||
|
export interface ControlCur2AccurateCalibrationCommand extends BaseCommand {
|
||||||
|
code: 'accurate_calibration';
|
||||||
|
value: 'start' | 'end'; // Assuming this is a numeric value for calibration
|
||||||
|
}
|
||||||
|
export interface ControlCur2TDirectionConCommand extends BaseCommand {
|
||||||
|
code: 'control_t_direction_con';
|
||||||
|
value: 'forward' | 'back';
|
||||||
|
}
|
||||||
|
export interface ControlCur2QuickCalibrationCommand extends BaseCommand {
|
||||||
|
code: 'tr_timecon';
|
||||||
|
value: number; // between 10 and 120
|
||||||
|
}
|
||||||
|
export interface ControlCur2MotorModeCommand extends BaseCommand {
|
||||||
|
code: 'elec_machinery_mode';
|
||||||
|
value: 'strong_power' | 'dry_contact';
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
import { DeviceService } from '../services/device.service';
|
|
||||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
import { GetDoorLockDevices, ProjectParam } from '../dtos';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { GetDevicesFilterDto, ProjectParam } from '../dtos';
|
||||||
|
import { DeviceService } from '../services/device.service';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -25,7 +25,7 @@ export class DeviceProjectController {
|
|||||||
})
|
})
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
@Param() param: ProjectParam,
|
@Param() param: ProjectParam,
|
||||||
@Query() query: GetDoorLockDevices,
|
@Query() query: GetDevicesFilterDto,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.getAllDevices(param, query);
|
return await this.deviceService.getAllDevices(param, query);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
|
IsArray,
|
||||||
IsEnum,
|
IsEnum,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -41,16 +42,7 @@ export class GetDeviceLogsDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
public endTime: string;
|
public endTime: string;
|
||||||
}
|
}
|
||||||
export class GetDoorLockDevices {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Device Type',
|
|
||||||
enum: DeviceTypeEnum,
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsEnum(DeviceTypeEnum)
|
|
||||||
@IsOptional()
|
|
||||||
public deviceType: DeviceTypeEnum;
|
|
||||||
}
|
|
||||||
export class GetDevicesBySpaceOrCommunityDto {
|
export class GetDevicesBySpaceOrCommunityDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Device Product Type',
|
description: 'Device Product Type',
|
||||||
@ -72,3 +64,23 @@ export class GetDevicesBySpaceOrCommunityDto {
|
|||||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||||
requireEither?: never; // This ensures at least one of them is provided
|
requireEither?: never; // This ensures at least one of them is provided
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class GetDevicesFilterDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Device Type',
|
||||||
|
enum: DeviceTypeEnum,
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsEnum(DeviceTypeEnum)
|
||||||
|
@IsOptional()
|
||||||
|
public deviceType: DeviceTypeEnum;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of Space IDs to filter devices',
|
||||||
|
required: false,
|
||||||
|
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
|
public spaces?: string[];
|
||||||
|
}
|
||||||
|
@ -53,7 +53,7 @@ import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
|||||||
import {
|
import {
|
||||||
GetDeviceLogsDto,
|
GetDeviceLogsDto,
|
||||||
GetDevicesBySpaceOrCommunityDto,
|
GetDevicesBySpaceOrCommunityDto,
|
||||||
GetDoorLockDevices,
|
GetDevicesFilterDto,
|
||||||
} from '../dtos/get.device.dto';
|
} from '../dtos/get.device.dto';
|
||||||
import {
|
import {
|
||||||
controlDeviceInterface,
|
controlDeviceInterface,
|
||||||
@ -955,19 +955,20 @@ export class DeviceService {
|
|||||||
|
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
param: ProjectParam,
|
param: ProjectParam,
|
||||||
query: GetDoorLockDevices,
|
{ deviceType, spaces }: GetDevicesFilterDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
try {
|
try {
|
||||||
await this.validateProject(param.projectUuid);
|
await this.validateProject(param.projectUuid);
|
||||||
if (query.deviceType === DeviceTypeEnum.DOOR_LOCK) {
|
if (deviceType === DeviceTypeEnum.DOOR_LOCK) {
|
||||||
return await this.getDoorLockDevices(param.projectUuid);
|
return await this.getDoorLockDevices(param.projectUuid, spaces);
|
||||||
} else if (!query.deviceType) {
|
} else if (!deviceType) {
|
||||||
const devices = await this.deviceRepository.find({
|
const devices = await this.deviceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
spaceDevice: {
|
spaceDevice: {
|
||||||
community: { project: { uuid: param.projectUuid } },
|
uuid: spaces && spaces.length ? In(spaces) : undefined,
|
||||||
spaceName: Not(ORPHAN_SPACE_NAME),
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
||||||
|
community: { project: { uuid: param.projectUuid } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
relations: [
|
relations: [
|
||||||
@ -1563,7 +1564,7 @@ export class DeviceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDoorLockDevices(projectUuid: string) {
|
async getDoorLockDevices(projectUuid: string, spaces?: string[]) {
|
||||||
await this.validateProject(projectUuid);
|
await this.validateProject(projectUuid);
|
||||||
|
|
||||||
const devices = await this.deviceRepository.find({
|
const devices = await this.deviceRepository.find({
|
||||||
@ -1573,6 +1574,7 @@ export class DeviceService {
|
|||||||
},
|
},
|
||||||
spaceDevice: {
|
spaceDevice: {
|
||||||
spaceName: Not(ORPHAN_SPACE_NAME),
|
spaceName: Not(ORPHAN_SPACE_NAME),
|
||||||
|
uuid: spaces && spaces.length ? In(spaces) : undefined,
|
||||||
community: {
|
community: {
|
||||||
project: {
|
project: {
|
||||||
uuid: projectUuid,
|
uuid: projectUuid,
|
||||||
|
@ -30,6 +30,8 @@ import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service
|
|||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [DoorLockController],
|
controllers: [DoorLockController],
|
||||||
@ -58,6 +60,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
OccupancyService,
|
OccupancyService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [DoorLockService],
|
exports: [DoorLockService],
|
||||||
})
|
})
|
||||||
|
@ -28,6 +28,8 @@ import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service
|
|||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [GroupController],
|
controllers: [GroupController],
|
||||||
@ -55,6 +57,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
OccupancyService,
|
OccupancyService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [GroupService],
|
exports: [GroupService],
|
||||||
})
|
})
|
||||||
|
@ -71,7 +71,6 @@ import {
|
|||||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SpaceUserService,
|
SpaceUserService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
@ -83,6 +82,8 @@ import { SubspaceProductAllocationService } from 'src/space/services/subspace/su
|
|||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||||
import { UserService, UserSpaceService } from 'src/users/services';
|
import { UserService, UserSpaceService } from 'src/users/services';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
|
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
|
||||||
@ -115,7 +116,6 @@ import { UserService, UserSpaceService } from 'src/users/services';
|
|||||||
TimeZoneRepository,
|
TimeZoneRepository,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
@ -152,6 +152,8 @@ import { UserService, UserSpaceService } from 'src/users/services';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [InviteUserService],
|
exports: [InviteUserService],
|
||||||
})
|
})
|
||||||
|
27
src/main.ts
27
src/main.ts
@ -1,15 +1,13 @@
|
|||||||
import { NestFactory } from '@nestjs/core';
|
|
||||||
import { AppModule } from './app.module';
|
|
||||||
import rateLimit from 'express-rate-limit';
|
|
||||||
import helmet from 'helmet';
|
|
||||||
import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.swagger.utils';
|
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
|
||||||
import { json, urlencoded } from 'body-parser';
|
|
||||||
import { SeederService } from '@app/common/seed/services/seeder.service';
|
|
||||||
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
|
|
||||||
import { Logger } from '@nestjs/common';
|
|
||||||
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
|
|
||||||
import { RequestContextMiddleware } from '@app/common/middleware/request-context.middleware';
|
import { RequestContextMiddleware } from '@app/common/middleware/request-context.middleware';
|
||||||
|
import { SeederService } from '@app/common/seed/services/seeder.service';
|
||||||
|
import { Logger, ValidationPipe } from '@nestjs/common';
|
||||||
|
import { NestFactory } from '@nestjs/core';
|
||||||
|
import { json, urlencoded } from 'body-parser';
|
||||||
|
import helmet from 'helmet';
|
||||||
|
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
|
||||||
|
import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.swagger.utils';
|
||||||
|
import { AppModule } from './app.module';
|
||||||
|
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
@ -23,13 +21,6 @@ async function bootstrap() {
|
|||||||
|
|
||||||
app.use(new RequestContextMiddleware().use);
|
app.use(new RequestContextMiddleware().use);
|
||||||
|
|
||||||
app.use(
|
|
||||||
rateLimit({
|
|
||||||
windowMs: 5 * 60 * 1000,
|
|
||||||
max: 500,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
helmet({
|
helmet({
|
||||||
contentSecurityPolicy: false,
|
contentSecurityPolicy: false,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
@ -49,7 +50,6 @@ import { SpaceModelProductAllocationService } from 'src/space-model/services/spa
|
|||||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||||
import {
|
import {
|
||||||
SpaceDeviceService,
|
SpaceDeviceService,
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
@ -60,7 +60,8 @@ import { SubspaceProductAllocationService } from 'src/space/services/subspace/su
|
|||||||
import { TagService } from 'src/tags/services';
|
import { TagService } from 'src/tags/services';
|
||||||
import { PowerClampController } from './controllers';
|
import { PowerClampController } from './controllers';
|
||||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
controllers: [PowerClampController],
|
controllers: [PowerClampController],
|
||||||
@ -90,7 +91,6 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
AutomationRepository,
|
AutomationRepository,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
TagService,
|
TagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
@ -111,6 +111,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SubspaceModelProductAllocationRepoitory,
|
SubspaceModelProductAllocationRepoitory,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [PowerClamp],
|
exports: [PowerClamp],
|
||||||
})
|
})
|
||||||
|
@ -54,7 +54,6 @@ import {
|
|||||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
@ -68,6 +67,8 @@ import { ProjectUserController } from './controllers/project-user.controller';
|
|||||||
import { CreateOrphanSpaceHandler } from './handler';
|
import { CreateOrphanSpaceHandler } from './handler';
|
||||||
import { ProjectService } from './services';
|
import { ProjectService } from './services';
|
||||||
import { ProjectUserService } from './services/project-user.service';
|
import { ProjectUserService } from './services/project-user.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
const CommandHandlers = [CreateOrphanSpaceHandler];
|
const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||||
|
|
||||||
@ -87,7 +88,6 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
|||||||
UserRepository,
|
UserRepository,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
TagService,
|
TagService,
|
||||||
@ -126,6 +126,8 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [ProjectService, CqrsModule],
|
exports: [ProjectService, CqrsModule],
|
||||||
})
|
})
|
||||||
|
@ -24,6 +24,7 @@ import { SpaceService } from 'src/space/services';
|
|||||||
import { PassThrough } from 'stream';
|
import { PassThrough } from 'stream';
|
||||||
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
||||||
import { CreateProjectDto, GetProjectParam } from '../dto';
|
import { CreateProjectDto, GetProjectParam } from '../dto';
|
||||||
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectService {
|
export class ProjectService {
|
||||||
@ -212,8 +213,14 @@ export class ProjectService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(uuid: string): Promise<ProjectEntity> {
|
async findOne(
|
||||||
const project = await this.projectRepository.findOne({ where: { uuid } });
|
uuid: string,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
): Promise<ProjectEntity> {
|
||||||
|
const projectRepository = queryRunner
|
||||||
|
? queryRunner.manager.getRepository(ProjectEntity)
|
||||||
|
: this.projectRepository;
|
||||||
|
const project = await projectRepository.findOne({ where: { uuid } });
|
||||||
if (!project) {
|
if (!project) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Invalid project with uuid ${uuid}`,
|
`Invalid project with uuid ${uuid}`,
|
||||||
|
32
src/schedule/constants/device-function-map.ts
Normal file
32
src/schedule/constants/device-function-map.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import {
|
||||||
|
ControlCur2AccurateCalibrationCommand,
|
||||||
|
ControlCur2Command,
|
||||||
|
ControlCur2PercentCommand,
|
||||||
|
ControlCur2QuickCalibrationCommand,
|
||||||
|
ControlCur2TDirectionConCommand,
|
||||||
|
} from 'src/device/commands/cur2-commands';
|
||||||
|
|
||||||
|
export enum ScheduleProductType {
|
||||||
|
CUR_2 = 'CUR_2',
|
||||||
|
}
|
||||||
|
export const DeviceFunctionMap: {
|
||||||
|
[T in ScheduleProductType]: (body: DeviceFunction[T]) => any;
|
||||||
|
} = {
|
||||||
|
[ScheduleProductType.CUR_2]: ({ code, value }) => {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
code,
|
||||||
|
value,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
type DeviceFunction = {
|
||||||
|
[ScheduleProductType.CUR_2]:
|
||||||
|
| ControlCur2Command
|
||||||
|
| ControlCur2PercentCommand
|
||||||
|
| ControlCur2AccurateCalibrationCommand
|
||||||
|
| ControlCur2TDirectionConCommand
|
||||||
|
| ControlCur2QuickCalibrationCommand;
|
||||||
|
};
|
@ -1,6 +1,6 @@
|
|||||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import {
|
import {
|
||||||
AddScheduleDto,
|
AddScheduleDto,
|
||||||
EnableScheduleDto,
|
EnableScheduleDto,
|
||||||
@ -11,14 +11,14 @@ import {
|
|||||||
getDeviceScheduleInterface,
|
getDeviceScheduleInterface,
|
||||||
} from '../interfaces/get.schedule.interface';
|
} from '../interfaces/get.schedule.interface';
|
||||||
|
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
|
||||||
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 { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { convertTimestampToDubaiTime } from '@app/common/helper/convertTimestampToDubaiTime';
|
import { convertTimestampToDubaiTime } from '@app/common/helper/convertTimestampToDubaiTime';
|
||||||
import {
|
import {
|
||||||
getEnabledDays,
|
getEnabledDays,
|
||||||
getScheduleStatus,
|
getScheduleStatus,
|
||||||
} from '@app/common/helper/getScheduleStatus';
|
} from '@app/common/helper/getScheduleStatus';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ScheduleService {
|
export class ScheduleService {
|
||||||
@ -49,22 +49,11 @@ export class ScheduleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Corrected condition for supported device types
|
// Corrected condition for supported device types
|
||||||
if (
|
this.ensureProductTypeSupportedForSchedule(
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
ProductType[deviceDetails.productDevice.prodType],
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_G &&
|
);
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.WH &&
|
return this.enableScheduleDeviceInTuya(
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
|
||||||
'This device is not supported for schedule',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return await this.enableScheduleDeviceInTuya(
|
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
enableScheduleDto,
|
enableScheduleDto,
|
||||||
);
|
);
|
||||||
@ -75,29 +64,6 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async enableScheduleDeviceInTuya(
|
|
||||||
deviceId: string,
|
|
||||||
enableScheduleDto: EnableScheduleDto,
|
|
||||||
): Promise<addScheduleDeviceInterface> {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/timer/device/${deviceId}/state`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'PUT',
|
|
||||||
path,
|
|
||||||
body: {
|
|
||||||
enable: enableScheduleDto.enable,
|
|
||||||
timer_id: enableScheduleDto.scheduleId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return response as addScheduleDeviceInterface;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error while updating schedule from Tuya',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async deleteDeviceSchedule(deviceUuid: string, scheduleId: string) {
|
async deleteDeviceSchedule(deviceUuid: string, scheduleId: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -107,21 +73,10 @@ export class ScheduleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Corrected condition for supported device types
|
// Corrected condition for supported device types
|
||||||
if (
|
this.ensureProductTypeSupportedForSchedule(
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
ProductType[deviceDetails.productDevice.prodType],
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_G &&
|
);
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.WH &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
|
||||||
'This device is not supported for schedule',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return await this.deleteScheduleDeviceInTuya(
|
return await this.deleteScheduleDeviceInTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
scheduleId,
|
scheduleId,
|
||||||
@ -133,25 +88,6 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async deleteScheduleDeviceInTuya(
|
|
||||||
deviceId: string,
|
|
||||||
scheduleId: string,
|
|
||||||
): Promise<addScheduleDeviceInterface> {
|
|
||||||
try {
|
|
||||||
const path = `/v2.0/cloud/timer/device/${deviceId}/batch?timer_ids=${scheduleId}`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'DELETE',
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
|
|
||||||
return response as addScheduleDeviceInterface;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error while deleting schedule from Tuya',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async addDeviceSchedule(deviceUuid: string, addScheduleDto: AddScheduleDto) {
|
async addDeviceSchedule(deviceUuid: string, addScheduleDto: AddScheduleDto) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -160,22 +96,10 @@ export class ScheduleService {
|
|||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Corrected condition for supported device types
|
this.ensureProductTypeSupportedForSchedule(
|
||||||
if (
|
ProductType[deviceDetails.productDevice.prodType],
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
);
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.WH &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
|
||||||
'This device is not supported for schedule',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
await this.addScheduleDeviceInTuya(
|
await this.addScheduleDeviceInTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
addScheduleDto,
|
addScheduleDto,
|
||||||
@ -187,40 +111,6 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addScheduleDeviceInTuya(
|
|
||||||
deviceId: string,
|
|
||||||
addScheduleDto: AddScheduleDto,
|
|
||||||
): Promise<addScheduleDeviceInterface> {
|
|
||||||
try {
|
|
||||||
const convertedTime = convertTimestampToDubaiTime(addScheduleDto.time);
|
|
||||||
const loops = getScheduleStatus(addScheduleDto.days);
|
|
||||||
|
|
||||||
const path = `/v2.0/cloud/timer/device/${deviceId}`;
|
|
||||||
const response = await this.tuya.request({
|
|
||||||
method: 'POST',
|
|
||||||
path,
|
|
||||||
body: {
|
|
||||||
time: convertedTime.time,
|
|
||||||
timezone_id: 'Asia/Dubai',
|
|
||||||
loops: `${loops}`,
|
|
||||||
functions: [
|
|
||||||
{
|
|
||||||
code: addScheduleDto.function.code,
|
|
||||||
value: addScheduleDto.function.value,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
category: `category_${addScheduleDto.category}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return response as addScheduleDeviceInterface;
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
'Error adding schedule from Tuya',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async getDeviceScheduleByCategory(deviceUuid: string, category: string) {
|
async getDeviceScheduleByCategory(deviceUuid: string, category: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -229,21 +119,10 @@ export class ScheduleService {
|
|||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
// Corrected condition for supported device types
|
// Corrected condition for supported device types
|
||||||
if (
|
this.ensureProductTypeSupportedForSchedule(
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
ProductType[deviceDetails.productDevice.prodType],
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_G &&
|
);
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.WH &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
|
||||||
'This device is not supported for schedule',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const schedules = await this.getScheduleDeviceInTuya(
|
const schedules = await this.getScheduleDeviceInTuya(
|
||||||
deviceDetails.deviceTuyaUuid,
|
deviceDetails.deviceTuyaUuid,
|
||||||
category,
|
category,
|
||||||
@ -270,7 +149,82 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getScheduleDeviceInTuya(
|
async updateDeviceSchedule(
|
||||||
|
deviceUuid: string,
|
||||||
|
updateScheduleDto: UpdateScheduleDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
|
|
||||||
|
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||||
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Corrected condition for supported device types
|
||||||
|
this.ensureProductTypeSupportedForSchedule(
|
||||||
|
ProductType[deviceDetails.productDevice.prodType],
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.updateScheduleDeviceInTuya(
|
||||||
|
deviceDetails.deviceTuyaUuid,
|
||||||
|
updateScheduleDto,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Error While Updating Schedule',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDeviceByDeviceUuid(
|
||||||
|
deviceUuid: string,
|
||||||
|
withProductDevice: boolean = true,
|
||||||
|
) {
|
||||||
|
return this.deviceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: deviceUuid,
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
...(withProductDevice && { relations: ['productDevice'] }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async addScheduleDeviceInTuya(
|
||||||
|
deviceId: string,
|
||||||
|
addScheduleDto: AddScheduleDto,
|
||||||
|
): Promise<addScheduleDeviceInterface> {
|
||||||
|
try {
|
||||||
|
const convertedTime = convertTimestampToDubaiTime(addScheduleDto.time);
|
||||||
|
const loops = getScheduleStatus(addScheduleDto.days);
|
||||||
|
|
||||||
|
const path = `/v2.0/cloud/timer/device/${deviceId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'POST',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
time: convertedTime.time,
|
||||||
|
timezone_id: 'Asia/Dubai',
|
||||||
|
loops: `${loops}`,
|
||||||
|
functions: [
|
||||||
|
{
|
||||||
|
...addScheduleDto.function,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
category: `category_${addScheduleDto.category}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as addScheduleDeviceInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error adding schedule from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getScheduleDeviceInTuya(
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
category: string,
|
category: string,
|
||||||
): Promise<getDeviceScheduleInterface> {
|
): Promise<getDeviceScheduleInterface> {
|
||||||
@ -291,57 +245,8 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getDeviceByDeviceUuid(
|
|
||||||
deviceUuid: string,
|
|
||||||
withProductDevice: boolean = true,
|
|
||||||
) {
|
|
||||||
return await this.deviceRepository.findOne({
|
|
||||||
where: {
|
|
||||||
uuid: deviceUuid,
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
...(withProductDevice && { relations: ['productDevice'] }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
async updateDeviceSchedule(
|
|
||||||
deviceUuid: string,
|
|
||||||
updateScheduleDto: UpdateScheduleDto,
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
|
||||||
|
|
||||||
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
private async updateScheduleDeviceInTuya(
|
||||||
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Corrected condition for supported device types
|
|
||||||
if (
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_G &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.WH &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
||||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
|
||||||
'This device is not supported for schedule',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
await this.updateScheduleDeviceInTuya(
|
|
||||||
deviceDetails.deviceTuyaUuid,
|
|
||||||
updateScheduleDto,
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message || 'Error While Updating Schedule',
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async updateScheduleDeviceInTuya(
|
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
updateScheduleDto: UpdateScheduleDto,
|
updateScheduleDto: UpdateScheduleDto,
|
||||||
): Promise<addScheduleDeviceInterface> {
|
): Promise<addScheduleDeviceInterface> {
|
||||||
@ -376,4 +281,69 @@ export class ScheduleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async enableScheduleDeviceInTuya(
|
||||||
|
deviceId: string,
|
||||||
|
enableScheduleDto: EnableScheduleDto,
|
||||||
|
): Promise<addScheduleDeviceInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/timer/device/${deviceId}/state`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'PUT',
|
||||||
|
path,
|
||||||
|
body: {
|
||||||
|
enable: enableScheduleDto.enable,
|
||||||
|
timer_id: enableScheduleDto.scheduleId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as addScheduleDeviceInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error while updating schedule from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async deleteScheduleDeviceInTuya(
|
||||||
|
deviceId: string,
|
||||||
|
scheduleId: string,
|
||||||
|
): Promise<addScheduleDeviceInterface> {
|
||||||
|
try {
|
||||||
|
const path = `/v2.0/cloud/timer/device/${deviceId}/batch?timer_ids=${scheduleId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'DELETE',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as addScheduleDeviceInterface;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Error while deleting schedule from Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ensureProductTypeSupportedForSchedule(deviceType: ProductType): void {
|
||||||
|
if (
|
||||||
|
![
|
||||||
|
ProductType.THREE_G,
|
||||||
|
ProductType.ONE_G,
|
||||||
|
ProductType.TWO_G,
|
||||||
|
ProductType.WH,
|
||||||
|
ProductType.ONE_1TG,
|
||||||
|
ProductType.TWO_2TG,
|
||||||
|
ProductType.THREE_3TG,
|
||||||
|
ProductType.GD,
|
||||||
|
ProductType.CUR_2,
|
||||||
|
].includes(deviceType)
|
||||||
|
) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This device is not supported for schedule',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
25
src/scheduler/scheduler.module.ts
Normal file
25
src/scheduler/scheduler.module.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { DatabaseModule } from '@app/common/database/database.module';
|
||||||
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { SchedulerService } from './scheduler.service';
|
||||||
|
import { ScheduleModule as NestScheduleModule } from '@nestjs/schedule';
|
||||||
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
NestScheduleModule.forRoot(),
|
||||||
|
TypeOrmModule.forFeature([]),
|
||||||
|
DatabaseModule,
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
SchedulerService,
|
||||||
|
SqlLoaderService,
|
||||||
|
PowerClampService,
|
||||||
|
OccupancyService,
|
||||||
|
AqiDataService,
|
||||||
|
],
|
||||||
|
})
|
||||||
|
export class SchedulerModule {}
|
92
src/scheduler/scheduler.service.ts
Normal file
92
src/scheduler/scheduler.service.ts
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||||
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SchedulerService {
|
||||||
|
constructor(
|
||||||
|
private readonly powerClampService: PowerClampService,
|
||||||
|
private readonly occupancyService: OccupancyService,
|
||||||
|
private readonly aqiDataService: AqiDataService,
|
||||||
|
) {
|
||||||
|
console.log('SchedulerService initialized!');
|
||||||
|
}
|
||||||
|
|
||||||
|
@Cron(CronExpression.EVERY_HOUR)
|
||||||
|
async runHourlyProcedures() {
|
||||||
|
console.log('\n======== Starting Procedures ========');
|
||||||
|
console.log(new Date().toISOString(), 'Scheduler running...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const results = await Promise.allSettled([
|
||||||
|
this.executeTask(
|
||||||
|
() => this.powerClampService.updateEnergyConsumedHistoricalData(),
|
||||||
|
'Energy Consumption',
|
||||||
|
),
|
||||||
|
this.executeTask(
|
||||||
|
() => this.occupancyService.updateOccupancyDataProcedures(),
|
||||||
|
'Occupancy Data',
|
||||||
|
),
|
||||||
|
this.executeTask(
|
||||||
|
() => this.aqiDataService.updateAQISensorHistoricalData(),
|
||||||
|
'AQI Data',
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
this.logResults(results);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('MAIN SCHEDULER ERROR:', error);
|
||||||
|
if (error.stack) {
|
||||||
|
console.error('Error stack:', error.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async executeTask(
|
||||||
|
task: () => Promise<void>,
|
||||||
|
name: string,
|
||||||
|
): Promise<{ name: string; status: string }> {
|
||||||
|
try {
|
||||||
|
console.log(`[${new Date().toISOString()}] Starting ${name} task...`);
|
||||||
|
await task();
|
||||||
|
console.log(
|
||||||
|
`[${new Date().toISOString()}] ${name} task completed successfully`,
|
||||||
|
);
|
||||||
|
return { name, status: 'success' };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`[${new Date().toISOString()}] ${name} task failed:`,
|
||||||
|
error.message,
|
||||||
|
);
|
||||||
|
if (error.stack) {
|
||||||
|
console.error('Task error stack:', error.stack);
|
||||||
|
}
|
||||||
|
return { name, status: 'failed' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private logResults(results: PromiseSettledResult<any>[]) {
|
||||||
|
const successCount = results.filter((r) => r.status === 'fulfilled').length;
|
||||||
|
const failedCount = results.length - successCount;
|
||||||
|
|
||||||
|
console.log('\n======== Task Results ========');
|
||||||
|
console.log(`Successful tasks: ${successCount}`);
|
||||||
|
console.log(`Failed tasks: ${failedCount}`);
|
||||||
|
|
||||||
|
if (failedCount > 0) {
|
||||||
|
console.log('\n======== Failed Tasks Details ========');
|
||||||
|
results.forEach((result, index) => {
|
||||||
|
if (result.status === 'rejected') {
|
||||||
|
console.error(`Task ${index + 1} failed:`, result.reason);
|
||||||
|
if (result.reason.stack) {
|
||||||
|
console.error('Error stack:', result.reason.stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n======== Scheduler Completed ========\n');
|
||||||
|
}
|
||||||
|
}
|
@ -51,8 +51,12 @@ export class SubSpaceModelService {
|
|||||||
for (const [index, dto] of dtos.entries()) {
|
for (const [index, dto] of dtos.entries()) {
|
||||||
const subspaceModel = savedSubspaces[index];
|
const subspaceModel = savedSubspaces[index];
|
||||||
|
|
||||||
const processedTags = await this.tagService.processTags(
|
const processedTags = await this.tagService.upsertTags(
|
||||||
dto.tags,
|
dto.tags.map((tag) => ({
|
||||||
|
tagName: tag.name,
|
||||||
|
productUuid: tag.productUuid,
|
||||||
|
tagUuid: tag.uuid,
|
||||||
|
})),
|
||||||
spaceModel.project.uuid,
|
spaceModel.project.uuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
|
@ -46,7 +46,6 @@ import { CommunityService } from 'src/community/services';
|
|||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
import { SceneService } from 'src/scene/services';
|
import { SceneService } from 'src/scene/services';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
@ -64,6 +63,8 @@ import {
|
|||||||
import { SpaceModelService, SubSpaceModelService } from './services';
|
import { SpaceModelService, SubSpaceModelService } from './services';
|
||||||
import { SpaceModelProductAllocationService } from './services/space-model-product-allocation.service';
|
import { SpaceModelProductAllocationService } from './services/space-model-product-allocation.service';
|
||||||
import { SubspaceModelProductAllocationService } from './services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from './services/subspace/subspace-model-product-allocation.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
const CommandHandlers = [
|
const CommandHandlers = [
|
||||||
PropogateUpdateSpaceModelHandler,
|
PropogateUpdateSpaceModelHandler,
|
||||||
@ -92,7 +93,6 @@ const CommandHandlers = [
|
|||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
TuyaService,
|
TuyaService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SpaceLinkRepository,
|
SpaceLinkRepository,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
@ -122,6 +122,8 @@ const CommandHandlers = [
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [CqrsModule, SpaceModelService],
|
exports: [CqrsModule, SpaceModelService],
|
||||||
})
|
})
|
||||||
|
@ -3,7 +3,8 @@ import { ApiProperty } from '@nestjs/swagger';
|
|||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
ArrayUnique,
|
ArrayUnique,
|
||||||
IsBoolean,
|
IsArray,
|
||||||
|
IsMongoId,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -12,7 +13,7 @@ import {
|
|||||||
NotEquals,
|
NotEquals,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { CreateProductAllocationDto } from './create-product-allocation.dto';
|
||||||
import { AddSubspaceDto } from './subspace';
|
import { AddSubspaceDto } from './subspace';
|
||||||
|
|
||||||
export class AddSpaceDto {
|
export class AddSpaceDto {
|
||||||
@ -47,14 +48,6 @@ export class AddSpaceDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
public icon?: string;
|
public icon?: string;
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Indicates whether the space is private or public',
|
|
||||||
example: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
@IsBoolean()
|
|
||||||
isPrivate: boolean;
|
|
||||||
|
|
||||||
@ApiProperty({ description: 'X position on canvas', example: 120 })
|
@ApiProperty({ description: 'X position on canvas', example: 120 })
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
x: number;
|
x: number;
|
||||||
@ -64,23 +57,19 @@ export class AddSpaceDto {
|
|||||||
y: number;
|
y: number;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'UUID of the Space',
|
description: 'UUID of the Space Model',
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsMongoId()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
spaceModelUuid?: string;
|
spaceModelUuid?: string;
|
||||||
|
|
||||||
@ApiProperty({ description: 'Y position on canvas', example: 200 })
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
direction?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'List of subspaces included in the model',
|
description: 'List of subspaces included in the model',
|
||||||
type: [AddSubspaceDto],
|
type: [AddSubspaceDto],
|
||||||
})
|
})
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@ArrayUnique((subspace) => subspace.subspaceName, {
|
@ArrayUnique((subspace) => subspace.subspaceName, {
|
||||||
message(validationArguments) {
|
message(validationArguments) {
|
||||||
@ -100,51 +89,21 @@ export class AddSpaceDto {
|
|||||||
subspaces?: AddSubspaceDto[];
|
subspaces?: AddSubspaceDto[];
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'List of tags associated with the space model',
|
description: 'List of allocations associated with the space',
|
||||||
type: [ProcessTagDto],
|
type: [CreateProductAllocationDto],
|
||||||
})
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ProcessTagDto)
|
@Type(() => CreateProductAllocationDto)
|
||||||
tags?: ProcessTagDto[];
|
productAllocations?: CreateProductAllocationDto[];
|
||||||
}
|
|
||||||
|
|
||||||
export class AddUserSpaceDto {
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'spaceUuid',
|
description: 'List of children spaces associated with the space',
|
||||||
required: true,
|
type: [AddSpaceDto],
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsOptional()
|
||||||
@IsNotEmpty()
|
@ValidateNested({ each: true })
|
||||||
public spaceUuid: string;
|
@Type(() => AddSpaceDto)
|
||||||
@ApiProperty({
|
children?: AddSpaceDto[];
|
||||||
description: 'userUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public userUuid: string;
|
|
||||||
constructor(dto: Partial<AddUserSpaceDto>) {
|
|
||||||
Object.assign(this, dto);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AddUserSpaceUsingCodeDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'userUuid',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public userUuid: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'inviteCode',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public inviteCode: string;
|
|
||||||
|
|
||||||
constructor(dto: Partial<AddUserSpaceDto>) {
|
|
||||||
Object.assign(this, dto);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
|
||||||
import { QueryRunner } from 'typeorm';
|
import { QueryRunner } from 'typeorm';
|
||||||
|
import { CreateProductAllocationDto } from './create-product-allocation.dto';
|
||||||
|
|
||||||
export enum AllocationsOwnerType {
|
export enum AllocationsOwnerType {
|
||||||
SPACE = 'space',
|
SPACE = 'space',
|
||||||
SUBSPACE = 'subspace',
|
SUBSPACE = 'subspace',
|
||||||
}
|
}
|
||||||
export class BaseCreateAllocationsDto {
|
export class BaseCreateAllocationsDto {
|
||||||
tags: ProcessTagDto[];
|
productAllocations: CreateProductAllocationDto[];
|
||||||
projectUuid: string;
|
projectUuid: string;
|
||||||
queryRunner: QueryRunner;
|
queryRunner: QueryRunner;
|
||||||
type: AllocationsOwnerType;
|
type: AllocationsOwnerType;
|
||||||
|
36
src/space/dtos/create-product-allocation.dto.ts
Normal file
36
src/space/dtos/create-product-allocation.dto.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import {
|
||||||
|
IsNotEmpty,
|
||||||
|
IsOptional,
|
||||||
|
IsString,
|
||||||
|
IsUUID,
|
||||||
|
ValidateIf,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateProductAllocationDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The name of the tag (if creating a new tag)',
|
||||||
|
example: 'New Tag',
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@ValidateIf((o) => !o.tagUuid)
|
||||||
|
tagName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'UUID of the tag (if selecting an existing tag)',
|
||||||
|
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||||
|
})
|
||||||
|
@IsUUID()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@ValidateIf((o) => !o.tagName)
|
||||||
|
tagUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'UUID of the product',
|
||||||
|
example: '550e8400-e29b-41d4-a716-446655440000',
|
||||||
|
})
|
||||||
|
@IsUUID()
|
||||||
|
@IsOptional()
|
||||||
|
productUuid: string;
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
export * from './add.space.dto';
|
export * from './add.space.dto';
|
||||||
export * from './community-space.param';
|
export * from './community-space.param';
|
||||||
|
export * from './create-allocations.dto';
|
||||||
|
export * from './create-product-allocation.dto';
|
||||||
export * from './get.space.param';
|
export * from './get.space.param';
|
||||||
export * from './user-space.param';
|
|
||||||
export * from './subspace';
|
|
||||||
export * from './project.param.dto';
|
export * from './project.param.dto';
|
||||||
export * from './update.space.dto';
|
export * from './subspace';
|
||||||
export * from './tag';
|
export * from './tag';
|
||||||
|
export * from './update.space.dto';
|
||||||
|
export * from './user-space.param';
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
IsArray,
|
IsArray,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
@ -6,8 +7,8 @@ import {
|
|||||||
IsString,
|
IsString,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { ProcessTagDto } from 'src/tags/dtos';
|
||||||
|
import { CreateProductAllocationDto } from '../create-product-allocation.dto';
|
||||||
|
|
||||||
export class AddSubspaceDto {
|
export class AddSubspaceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -24,7 +25,7 @@ export class AddSubspaceDto {
|
|||||||
})
|
})
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ProcessTagDto)
|
@Type(() => CreateProductAllocationDto)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
tags?: ProcessTagDto[];
|
productAllocations?: CreateProductAllocationDto[];
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
export * from './add.subspace.dto';
|
|
||||||
export * from './get.subspace.param';
|
|
||||||
export * from './add.subspace-device.param';
|
export * from './add.subspace-device.param';
|
||||||
export * from './update.subspace.dto';
|
export * from './add.subspace.dto';
|
||||||
export * from './delete.subspace.dto';
|
export * from './delete.subspace.dto';
|
||||||
export * from './modify.subspace.dto';
|
export * from './get.subspace.param';
|
||||||
|
export * from './update.subspace.dto';
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
import { ApiPropertyOptional, PartialType } from '@nestjs/swagger';
|
|
||||||
import { IsOptional, IsUUID } from 'class-validator';
|
|
||||||
import { AddSubspaceDto } from './add.subspace.dto';
|
|
||||||
|
|
||||||
export class ModifySubspaceDto extends PartialType(AddSubspaceDto) {
|
|
||||||
@ApiPropertyOptional({
|
|
||||||
description:
|
|
||||||
'UUID of the subspace (will present if updating an existing subspace)',
|
|
||||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@IsUUID()
|
|
||||||
uuid?: string;
|
|
||||||
}
|
|
@ -1,16 +1,14 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiPropertyOptional, PartialType } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsOptional, IsUUID } from 'class-validator';
|
||||||
|
import { AddSubspaceDto } from './add.subspace.dto';
|
||||||
|
|
||||||
export class UpdateSubspaceDto {
|
export class UpdateSubspaceDto extends PartialType(AddSubspaceDto) {
|
||||||
@ApiProperty({
|
@ApiPropertyOptional({
|
||||||
description: 'Name of the subspace',
|
description:
|
||||||
example: 'Living Room',
|
'UUID of the subspace (will present if updating an existing subspace)',
|
||||||
|
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsOptional()
|
||||||
@IsString()
|
@IsUUID()
|
||||||
subspaceName?: string;
|
uuid?: string;
|
||||||
|
|
||||||
@IsNotEmpty()
|
|
||||||
@IsString()
|
|
||||||
subspaceUuid: string;
|
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ import {
|
|||||||
NotEquals,
|
NotEquals,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { ModifySubspaceDto } from './subspace';
|
import { CreateProductAllocationDto } from './create-product-allocation.dto';
|
||||||
import { ModifyTagDto } from './tag/modify-tag.dto';
|
import { UpdateSubspaceDto } from './subspace';
|
||||||
|
|
||||||
export class UpdateSpaceDto {
|
export class UpdateSpaceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -46,25 +46,24 @@ export class UpdateSpaceDto {
|
|||||||
y?: number;
|
y?: number;
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
description: 'List of subspace modifications (add/update/delete)',
|
description: 'List of subspace modifications',
|
||||||
type: [ModifySubspaceDto],
|
type: [UpdateSubspaceDto],
|
||||||
})
|
})
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ModifySubspaceDto)
|
@Type(() => UpdateSubspaceDto)
|
||||||
subspaces?: ModifySubspaceDto[];
|
subspaces?: UpdateSubspaceDto[];
|
||||||
|
|
||||||
@ApiPropertyOptional({
|
@ApiPropertyOptional({
|
||||||
description:
|
description: 'List of allocations modifications',
|
||||||
'List of tag modifications (add/update/delete) for the space model',
|
type: [CreateProductAllocationDto],
|
||||||
type: [ModifyTagDto],
|
|
||||||
})
|
})
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ModifyTagDto)
|
@Type(() => CreateProductAllocationDto)
|
||||||
tags?: ModifyTagDto[];
|
productAllocations?: CreateProductAllocationDto[];
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'UUID of the Space',
|
description: 'UUID of the Space',
|
||||||
|
@ -5,11 +5,7 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import { UserSpaceService } from 'src/users/services';
|
import { UserSpaceService } from 'src/users/services';
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { DisableSpaceCommand } from '../commands';
|
import { DisableSpaceCommand } from '../commands';
|
||||||
import {
|
import { SpaceSceneService, SubSpaceService } from '../services';
|
||||||
SpaceLinkService,
|
|
||||||
SpaceSceneService,
|
|
||||||
SubSpaceService,
|
|
||||||
} from '../services';
|
|
||||||
|
|
||||||
@CommandHandler(DisableSpaceCommand)
|
@CommandHandler(DisableSpaceCommand)
|
||||||
export class DisableSpaceHandler
|
export class DisableSpaceHandler
|
||||||
@ -19,7 +15,6 @@ export class DisableSpaceHandler
|
|||||||
private readonly subSpaceService: SubSpaceService,
|
private readonly subSpaceService: SubSpaceService,
|
||||||
private readonly userService: UserSpaceService,
|
private readonly userService: UserSpaceService,
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
private readonly spaceLinkService: SpaceLinkService,
|
|
||||||
private readonly sceneService: SpaceSceneService,
|
private readonly sceneService: SpaceSceneService,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
@ -39,8 +34,6 @@ export class DisableSpaceHandler
|
|||||||
'subspaces',
|
'subspaces',
|
||||||
'parent',
|
'parent',
|
||||||
'devices',
|
'devices',
|
||||||
'outgoingConnections',
|
|
||||||
'incomingConnections',
|
|
||||||
'scenes',
|
'scenes',
|
||||||
'children',
|
'children',
|
||||||
'userSpaces',
|
'userSpaces',
|
||||||
@ -79,7 +72,6 @@ export class DisableSpaceHandler
|
|||||||
orphanSpace,
|
orphanSpace,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
),
|
),
|
||||||
this.spaceLinkService.deleteSpaceLink(space, queryRunner),
|
|
||||||
this.sceneService.deleteScenes(space, queryRunner),
|
this.sceneService.deleteScenes(space, queryRunner),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -16,10 +16,10 @@ export class ProductAllocationService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
|
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
|
||||||
const { projectUuid, queryRunner, tags, type } = dto;
|
const { projectUuid, queryRunner, productAllocations, type } = dto;
|
||||||
|
|
||||||
const allocationsData = await this.tagService.processTags(
|
const allocationsData = await this.tagService.upsertTags(
|
||||||
tags,
|
productAllocations,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
@ -29,15 +29,17 @@ export class ProductAllocationService {
|
|||||||
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
||||||
|
|
||||||
// Create the product-tag mapping based on the processed tags
|
// Create the product-tag mapping based on the processed tags
|
||||||
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
|
const productTagMapping = productAllocations.map(
|
||||||
const inputTag = uuid
|
({ tagUuid, tagName, productUuid }) => {
|
||||||
? createdTagsByUUID.get(uuid)
|
const inputTag = tagUuid
|
||||||
: createdTagsByName.get(name);
|
? createdTagsByUUID.get(tagUuid)
|
||||||
return {
|
: createdTagsByName.get(tagName);
|
||||||
tag: inputTag?.uuid,
|
return {
|
||||||
product: productUuid,
|
tag: inputTag?.uuid,
|
||||||
};
|
product: productUuid,
|
||||||
});
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case AllocationsOwnerType.SPACE: {
|
case AllocationsOwnerType.SPACE: {
|
||||||
|
@ -1,121 +1,6 @@
|
|||||||
import { SpaceLinkEntity } from '@app/common/modules/space/entities/space-link.entity';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
|
||||||
import { SpaceLinkRepository } from '@app/common/modules/space/repositories';
|
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
||||||
import { QueryRunner } from 'typeorm';
|
|
||||||
|
|
||||||
|
// todo: find out why we need to import this
|
||||||
|
// in community module in order for the whole system to work
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceLinkService {
|
export class SpaceLinkService {}
|
||||||
constructor(private readonly spaceLinkRepository: SpaceLinkRepository) {}
|
|
||||||
|
|
||||||
async saveSpaceLink(
|
|
||||||
startSpaceId: string,
|
|
||||||
endSpaceId: string,
|
|
||||||
direction: string,
|
|
||||||
queryRunner: QueryRunner,
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
// Check if a link between the startSpace and endSpace already exists
|
|
||||||
const existingLink = await queryRunner.manager.findOne(SpaceLinkEntity, {
|
|
||||||
where: {
|
|
||||||
startSpace: { uuid: startSpaceId },
|
|
||||||
endSpace: { uuid: endSpaceId },
|
|
||||||
disabled: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (existingLink) {
|
|
||||||
// Update the direction if the link exists
|
|
||||||
existingLink.direction = direction;
|
|
||||||
await queryRunner.manager.save(SpaceLinkEntity, existingLink);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const existingEndSpaceLink = await queryRunner.manager.findOne(
|
|
||||||
SpaceLinkEntity,
|
|
||||||
{
|
|
||||||
where: { endSpace: { uuid: endSpaceId } },
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
|
||||||
existingEndSpaceLink &&
|
|
||||||
existingEndSpaceLink.startSpace.uuid !== startSpaceId
|
|
||||||
) {
|
|
||||||
throw new Error(
|
|
||||||
`Space with ID ${endSpaceId} is already an endSpace in another link and cannot be reused.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find start space
|
|
||||||
const startSpace = await queryRunner.manager.findOne(SpaceEntity, {
|
|
||||||
where: { uuid: startSpaceId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!startSpace) {
|
|
||||||
throw new HttpException(
|
|
||||||
`Start space with ID ${startSpaceId} not found.`,
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find end space
|
|
||||||
const endSpace = await queryRunner.manager.findOne(SpaceEntity, {
|
|
||||||
where: { uuid: endSpaceId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!endSpace) {
|
|
||||||
throw new HttpException(
|
|
||||||
`End space with ID ${endSpaceId} not found.`,
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create and save the space link
|
|
||||||
const spaceLink = this.spaceLinkRepository.create({
|
|
||||||
startSpace,
|
|
||||||
endSpace,
|
|
||||||
direction,
|
|
||||||
});
|
|
||||||
|
|
||||||
await queryRunner.manager.save(SpaceLinkEntity, spaceLink);
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.message ||
|
|
||||||
`Failed to save space link. Internal Server Error: ${error.message}`,
|
|
||||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async deleteSpaceLink(
|
|
||||||
space: SpaceEntity,
|
|
||||||
queryRunner: QueryRunner,
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
const spaceLinks = await queryRunner.manager.find(SpaceLinkEntity, {
|
|
||||||
where: [
|
|
||||||
{ startSpace: space, disabled: false },
|
|
||||||
{ endSpace: space, disabled: false },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
if (spaceLinks.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const linkIds = spaceLinks.map((link) => link.uuid);
|
|
||||||
|
|
||||||
await queryRunner.manager
|
|
||||||
.createQueryBuilder()
|
|
||||||
.update(SpaceLinkEntity)
|
|
||||||
.set({ disabled: true })
|
|
||||||
.whereInIds(linkIds)
|
|
||||||
.execute();
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
`Failed to disable space links for the given space: ${error.message}`,
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -16,7 +16,7 @@ import {
|
|||||||
Inject,
|
Inject,
|
||||||
Injectable,
|
Injectable,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { In } from 'typeorm';
|
import { In, QueryRunner } from 'typeorm';
|
||||||
import { CommunityService } from '../../community/services';
|
import { CommunityService } from '../../community/services';
|
||||||
import { ProjectService } from '../../project/services';
|
import { ProjectService } from '../../project/services';
|
||||||
import { ProjectParam } from '../dtos';
|
import { ProjectParam } from '../dtos';
|
||||||
@ -69,12 +69,17 @@ export class ValidationService {
|
|||||||
async validateCommunityAndProject(
|
async validateCommunityAndProject(
|
||||||
communityUuid: string,
|
communityUuid: string,
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
) {
|
) {
|
||||||
const project = await this.projectService.findOne(projectUuid);
|
const project = await this.projectService.findOne(projectUuid, queryRunner);
|
||||||
const community = await this.communityService.getCommunityById({
|
|
||||||
communityUuid,
|
const community = await this.communityService.getCommunityById(
|
||||||
projectUuid,
|
{
|
||||||
});
|
communityUuid,
|
||||||
|
projectUuid,
|
||||||
|
},
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
|
||||||
return { community: community.data, project: project };
|
return { community: community.data, project: project };
|
||||||
}
|
}
|
||||||
@ -170,8 +175,14 @@ export class ValidationService {
|
|||||||
return space;
|
return space;
|
||||||
}
|
}
|
||||||
|
|
||||||
async validateSpaceModel(spaceModelUuid: string): Promise<SpaceModelEntity> {
|
async validateSpaceModel(
|
||||||
const queryBuilder = this.spaceModelRepository
|
spaceModelUuid: string,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
): Promise<SpaceModelEntity> {
|
||||||
|
const queryBuilder = (
|
||||||
|
queryRunner.manager.getRepository(SpaceModelEntity) ||
|
||||||
|
this.spaceModelRepository
|
||||||
|
)
|
||||||
.createQueryBuilder('spaceModel')
|
.createQueryBuilder('spaceModel')
|
||||||
.leftJoinAndSelect(
|
.leftJoinAndSelect(
|
||||||
'spaceModel.subspaceModels',
|
'spaceModel.subspaceModels',
|
||||||
|
@ -22,7 +22,6 @@ import {
|
|||||||
import { CommandBus } from '@nestjs/cqrs';
|
import { CommandBus } from '@nestjs/cqrs';
|
||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
import { SpaceModelService } from 'src/space-model/services';
|
import { SpaceModelService } from 'src/space-model/services';
|
||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
|
||||||
import { TagService } from 'src/tags/services/tags.service';
|
import { TagService } from 'src/tags/services/tags.service';
|
||||||
import { DataSource, In, Not, QueryRunner } from 'typeorm';
|
import { DataSource, In, Not, QueryRunner } from 'typeorm';
|
||||||
import { DisableSpaceCommand } from '../commands';
|
import { DisableSpaceCommand } from '../commands';
|
||||||
@ -32,9 +31,9 @@ import {
|
|||||||
GetSpaceParam,
|
GetSpaceParam,
|
||||||
UpdateSpaceDto,
|
UpdateSpaceDto,
|
||||||
} from '../dtos';
|
} from '../dtos';
|
||||||
|
import { CreateProductAllocationDto } from '../dtos/create-product-allocation.dto';
|
||||||
import { GetSpaceDto } from '../dtos/get.space.dto';
|
import { GetSpaceDto } from '../dtos/get.space.dto';
|
||||||
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
|
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
|
||||||
import { SpaceLinkService } from './space-link';
|
|
||||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||||
import { ValidationService } from './space-validation.service';
|
import { ValidationService } from './space-validation.service';
|
||||||
import { SubSpaceService } from './subspace';
|
import { SubSpaceService } from './subspace';
|
||||||
@ -44,7 +43,6 @@ export class SpaceService {
|
|||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly inviteSpaceRepository: InviteSpaceRepository,
|
private readonly inviteSpaceRepository: InviteSpaceRepository,
|
||||||
private readonly spaceLinkService: SpaceLinkService,
|
|
||||||
private readonly subSpaceService: SubSpaceService,
|
private readonly subSpaceService: SubSpaceService,
|
||||||
private readonly validationService: ValidationService,
|
private readonly validationService: ValidationService,
|
||||||
private readonly tagService: TagService,
|
private readonly tagService: TagService,
|
||||||
@ -57,50 +55,72 @@ export class SpaceService {
|
|||||||
async createSpace(
|
async createSpace(
|
||||||
addSpaceDto: AddSpaceDto,
|
addSpaceDto: AddSpaceDto,
|
||||||
params: CommunitySpaceParam,
|
params: CommunitySpaceParam,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
recursiveCallParentEntity?: SpaceEntity,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
const { parentUuid, direction, spaceModelUuid, subspaces, tags } =
|
const isRecursiveCall = !!queryRunner;
|
||||||
addSpaceDto;
|
|
||||||
|
const {
|
||||||
|
parentUuid,
|
||||||
|
spaceModelUuid,
|
||||||
|
subspaces,
|
||||||
|
productAllocations,
|
||||||
|
children,
|
||||||
|
} = addSpaceDto;
|
||||||
const { communityUuid, projectUuid } = params;
|
const { communityUuid, projectUuid } = params;
|
||||||
|
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
if (!queryRunner) {
|
||||||
|
queryRunner = this.dataSource.createQueryRunner();
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
const { community } =
|
const { community } =
|
||||||
await this.validationService.validateCommunityAndProject(
|
await this.validationService.validateCommunityAndProject(
|
||||||
communityUuid,
|
communityUuid,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
|
queryRunner,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.validateSpaceCreationCriteria({ spaceModelUuid, subspaces, tags });
|
this.validateSpaceCreationCriteria({
|
||||||
|
spaceModelUuid,
|
||||||
|
subspaces,
|
||||||
|
productAllocations,
|
||||||
|
});
|
||||||
|
|
||||||
const parent = parentUuid
|
const parent =
|
||||||
? await this.validationService.validateSpace(parentUuid)
|
parentUuid && !isRecursiveCall
|
||||||
: null;
|
? await this.validationService.validateSpace(parentUuid)
|
||||||
|
: null;
|
||||||
|
|
||||||
const spaceModel = spaceModelUuid
|
const spaceModel = spaceModelUuid
|
||||||
? await this.validationService.validateSpaceModel(spaceModelUuid)
|
? await this.validationService.validateSpaceModel(spaceModelUuid)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const space = queryRunner.manager.create(SpaceEntity, {
|
const space = queryRunner.manager.create(SpaceEntity, {
|
||||||
...addSpaceDto,
|
// todo: find a better way to handle this instead of naming every key
|
||||||
|
spaceName: addSpaceDto.spaceName,
|
||||||
|
icon: addSpaceDto.icon,
|
||||||
|
x: addSpaceDto.x,
|
||||||
|
y: addSpaceDto.y,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
parent: parentUuid ? parent : null,
|
parent: isRecursiveCall
|
||||||
|
? recursiveCallParentEntity
|
||||||
|
: parentUuid
|
||||||
|
? parent
|
||||||
|
: null,
|
||||||
community,
|
community,
|
||||||
});
|
});
|
||||||
|
|
||||||
const newSpace = await queryRunner.manager.save(space);
|
const newSpace = await queryRunner.manager.save(space);
|
||||||
|
this.checkDuplicateTags([
|
||||||
const subspaceTags =
|
...(productAllocations || []),
|
||||||
subspaces?.flatMap((subspace) => subspace.tags || []) || [];
|
...(subspaces?.flatMap(
|
||||||
|
(subspace) => subspace.productAllocations || [],
|
||||||
this.checkDuplicateTags([...tags, ...subspaceTags]);
|
) || []),
|
||||||
|
]);
|
||||||
|
|
||||||
if (spaceModelUuid) {
|
if (spaceModelUuid) {
|
||||||
// no need to check for existing dependencies here as validateSpaceCreationCriteria
|
|
||||||
// ensures no tags or subspaces are present along with spaceModelUuid
|
|
||||||
await this.spaceModelService.linkToSpace(
|
await this.spaceModelService.linkToSpace(
|
||||||
newSpace,
|
newSpace,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
@ -109,15 +129,6 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
// todo: remove this logic as we are not using space links anymore
|
|
||||||
direction && parent
|
|
||||||
? this.spaceLinkService.saveSpaceLink(
|
|
||||||
parent.uuid,
|
|
||||||
newSpace.uuid,
|
|
||||||
direction,
|
|
||||||
queryRunner,
|
|
||||||
)
|
|
||||||
: Promise.resolve(),
|
|
||||||
subspaces?.length
|
subspaces?.length
|
||||||
? this.subSpaceService.createSubspacesFromDto(
|
? this.subSpaceService.createSubspacesFromDto(
|
||||||
subspaces,
|
subspaces,
|
||||||
@ -126,12 +137,32 @@ export class SpaceService {
|
|||||||
projectUuid,
|
projectUuid,
|
||||||
)
|
)
|
||||||
: Promise.resolve(),
|
: Promise.resolve(),
|
||||||
tags?.length
|
productAllocations?.length
|
||||||
? this.createAllocations(tags, projectUuid, queryRunner, newSpace)
|
? this.createAllocations(
|
||||||
|
productAllocations,
|
||||||
|
projectUuid,
|
||||||
|
queryRunner,
|
||||||
|
newSpace,
|
||||||
|
)
|
||||||
: Promise.resolve(),
|
: Promise.resolve(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await queryRunner.commitTransaction();
|
if (children?.length) {
|
||||||
|
await Promise.all(
|
||||||
|
children.map((child) =>
|
||||||
|
this.createSpace(
|
||||||
|
{ ...child, parentUuid: newSpace.uuid },
|
||||||
|
{ communityUuid, projectUuid },
|
||||||
|
queryRunner,
|
||||||
|
newSpace,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isRecursiveCall) {
|
||||||
|
await queryRunner.commitTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
@ -139,34 +170,34 @@ export class SpaceService {
|
|||||||
message: 'Space created successfully',
|
message: 'Space created successfully',
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await queryRunner.rollbackTransaction();
|
!isRecursiveCall ? await queryRunner.rollbackTransaction() : null;
|
||||||
|
|
||||||
if (error instanceof HttpException) {
|
if (error instanceof HttpException) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} finally {
|
} finally {
|
||||||
await queryRunner.release();
|
!isRecursiveCall ? await queryRunner.release() : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private checkDuplicateTags(allTags: ProcessTagDto[]) {
|
private checkDuplicateTags(allocations: CreateProductAllocationDto[]) {
|
||||||
const tagUuidSet = new Set<string>();
|
const tagUuidSet = new Set<string>();
|
||||||
const tagNameProductSet = new Set<string>();
|
const tagNameProductSet = new Set<string>();
|
||||||
|
|
||||||
for (const tag of allTags) {
|
for (const allocation of allocations) {
|
||||||
if (tag.uuid) {
|
if (allocation.tagUuid) {
|
||||||
if (tagUuidSet.has(tag.uuid)) {
|
if (tagUuidSet.has(allocation.tagUuid)) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Duplicate tag UUID found: ${tag.uuid}`,
|
`Duplicate tag UUID found: ${allocation.tagUuid}`,
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
tagUuidSet.add(tag.uuid);
|
tagUuidSet.add(allocation.tagUuid);
|
||||||
} else {
|
} else {
|
||||||
const tagKey = `${tag.name}-${tag.productUuid}`;
|
const tagKey = `${allocation.tagName}-${allocation.productUuid}`;
|
||||||
if (tagNameProductSet.has(tagKey)) {
|
if (tagNameProductSet.has(tagKey)) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Duplicate tag found with name "${tag.name}" and product "${tag.productUuid}".`,
|
`Duplicate tag found with name "${allocation.tagName}" and product "${allocation.productUuid}".`,
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -195,12 +226,7 @@ export class SpaceService {
|
|||||||
'children.disabled = :disabled',
|
'children.disabled = :disabled',
|
||||||
{ disabled: false },
|
{ disabled: false },
|
||||||
)
|
)
|
||||||
.leftJoinAndSelect(
|
|
||||||
'space.incomingConnections',
|
|
||||||
'incomingConnections',
|
|
||||||
'incomingConnections.disabled = :incomingConnectionDisabled',
|
|
||||||
{ incomingConnectionDisabled: false },
|
|
||||||
)
|
|
||||||
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
|
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
|
||||||
.leftJoinAndSelect('productAllocations.tag', 'tag')
|
.leftJoinAndSelect('productAllocations.tag', 'tag')
|
||||||
.leftJoinAndSelect('productAllocations.product', 'product')
|
.leftJoinAndSelect('productAllocations.product', 'product')
|
||||||
@ -271,7 +297,6 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo refactor this method to eliminate wrong use of tags
|
|
||||||
async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
|
async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
|
||||||
const { communityUuid, spaceUuid, projectUuid } = params;
|
const { communityUuid, spaceUuid, projectUuid } = params;
|
||||||
try {
|
try {
|
||||||
@ -282,19 +307,6 @@ export class SpaceService {
|
|||||||
|
|
||||||
const queryBuilder = this.spaceRepository
|
const queryBuilder = this.spaceRepository
|
||||||
.createQueryBuilder('space')
|
.createQueryBuilder('space')
|
||||||
.leftJoinAndSelect('space.parent', 'parent')
|
|
||||||
.leftJoinAndSelect(
|
|
||||||
'space.children',
|
|
||||||
'children',
|
|
||||||
'children.disabled = :disabled',
|
|
||||||
{ disabled: false },
|
|
||||||
)
|
|
||||||
.leftJoinAndSelect(
|
|
||||||
'space.incomingConnections',
|
|
||||||
'incomingConnections',
|
|
||||||
'incomingConnections.disabled = :incomingConnectionDisabled',
|
|
||||||
{ incomingConnectionDisabled: false },
|
|
||||||
)
|
|
||||||
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
|
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
|
||||||
.leftJoinAndSelect('productAllocations.tag', 'spaceTag')
|
.leftJoinAndSelect('productAllocations.tag', 'spaceTag')
|
||||||
.leftJoinAndSelect('productAllocations.product', 'spaceProduct')
|
.leftJoinAndSelect('productAllocations.product', 'spaceProduct')
|
||||||
@ -321,7 +333,12 @@ export class SpaceService {
|
|||||||
.andWhere('space.disabled = :disabled', { disabled: false });
|
.andWhere('space.disabled = :disabled', { disabled: false });
|
||||||
|
|
||||||
const space = await queryBuilder.getOne();
|
const space = await queryBuilder.getOne();
|
||||||
|
if (!space) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Space with ID ${spaceUuid} not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
message: `Space with ID ${spaceUuid} successfully fetched`,
|
message: `Space with ID ${spaceUuid} successfully fetched`,
|
||||||
data: space,
|
data: space,
|
||||||
@ -331,7 +348,7 @@ export class SpaceService {
|
|||||||
throw error; // If it's an HttpException, rethrow it
|
throw error; // If it's an HttpException, rethrow it
|
||||||
} else {
|
} else {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'An error occurred while deleting the community',
|
'An error occurred while fetching the space',
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -423,7 +440,7 @@ export class SpaceService {
|
|||||||
|
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
const hasSubspace = updateSpaceDto.subspaces?.length > 0;
|
const hasSubspace = updateSpaceDto.subspaces?.length > 0;
|
||||||
const hasTags = updateSpaceDto.tags?.length > 0;
|
const hasAllocations = updateSpaceDto.productAllocations?.length > 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
@ -448,7 +465,7 @@ export class SpaceService {
|
|||||||
|
|
||||||
await this.updateSpaceProperties(space, updateSpaceDto, queryRunner);
|
await this.updateSpaceProperties(space, updateSpaceDto, queryRunner);
|
||||||
|
|
||||||
if (hasSubspace || hasTags) {
|
if (hasSubspace || hasAllocations) {
|
||||||
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
await queryRunner.manager.update(SpaceEntity, space.uuid, {
|
||||||
spaceModel: null,
|
spaceModel: null,
|
||||||
});
|
});
|
||||||
@ -492,7 +509,7 @@ export class SpaceService {
|
|||||||
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
await this.subSpaceService.unlinkModels(space.subspaces, queryRunner);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasTags && space.productAllocations && space.spaceModel) {
|
if (hasAllocations && space.productAllocations && space.spaceModel) {
|
||||||
await this.spaceProductAllocationService.unlinkModels(
|
await this.spaceProductAllocationService.unlinkModels(
|
||||||
space,
|
space,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
@ -508,13 +525,13 @@ export class SpaceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateSpaceDto.tags) {
|
if (updateSpaceDto.productAllocations) {
|
||||||
await queryRunner.manager.delete(SpaceProductAllocationEntity, {
|
await queryRunner.manager.delete(SpaceProductAllocationEntity, {
|
||||||
space: { uuid: space.uuid },
|
space: { uuid: space.uuid },
|
||||||
tag: {
|
tag: {
|
||||||
uuid: Not(
|
uuid: Not(
|
||||||
In(
|
In(
|
||||||
updateSpaceDto.tags
|
updateSpaceDto.productAllocations
|
||||||
.filter((tag) => tag.tagUuid)
|
.filter((tag) => tag.tagUuid)
|
||||||
.map((tag) => tag.tagUuid),
|
.map((tag) => tag.tagUuid),
|
||||||
),
|
),
|
||||||
@ -522,11 +539,7 @@ export class SpaceService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
await this.createAllocations(
|
await this.createAllocations(
|
||||||
updateSpaceDto.tags.map((tag) => ({
|
updateSpaceDto.productAllocations,
|
||||||
name: tag.name,
|
|
||||||
uuid: tag.tagUuid,
|
|
||||||
productUuid: tag.productUuid,
|
|
||||||
})),
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
space,
|
space,
|
||||||
@ -673,7 +686,7 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] {
|
buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] {
|
||||||
const map = new Map<string, SpaceEntity>();
|
const map = new Map<string, SpaceEntity>();
|
||||||
|
|
||||||
// Step 1: Create a map of spaces by UUID
|
// Step 1: Create a map of spaces by UUID
|
||||||
@ -702,13 +715,17 @@ export class SpaceService {
|
|||||||
|
|
||||||
private validateSpaceCreationCriteria({
|
private validateSpaceCreationCriteria({
|
||||||
spaceModelUuid,
|
spaceModelUuid,
|
||||||
tags,
|
productAllocations,
|
||||||
subspaces,
|
subspaces,
|
||||||
}: Pick<AddSpaceDto, 'spaceModelUuid' | 'tags' | 'subspaces'>): void {
|
}: Pick<
|
||||||
const hasTagsOrSubspaces =
|
AddSpaceDto,
|
||||||
(tags && tags.length > 0) || (subspaces && subspaces.length > 0);
|
'spaceModelUuid' | 'productAllocations' | 'subspaces'
|
||||||
|
>): void {
|
||||||
|
const hasProductsOrSubspaces =
|
||||||
|
(productAllocations && productAllocations.length > 0) ||
|
||||||
|
(subspaces && subspaces.length > 0);
|
||||||
|
|
||||||
if (spaceModelUuid && hasTagsOrSubspaces) {
|
if (spaceModelUuid && hasProductsOrSubspaces) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
'For space creation choose either space model or products and subspace',
|
'For space creation choose either space model or products and subspace',
|
||||||
HttpStatus.CONFLICT,
|
HttpStatus.CONFLICT,
|
||||||
@ -717,13 +734,13 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async createAllocations(
|
private async createAllocations(
|
||||||
tags: ProcessTagDto[],
|
productAllocations: CreateProductAllocationDto[],
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const allocationsData = await this.tagService.processTags(
|
const allocationsData = await this.tagService.upsertTags(
|
||||||
tags,
|
productAllocations,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
@ -733,15 +750,17 @@ export class SpaceService {
|
|||||||
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
||||||
|
|
||||||
// Create the product-tag mapping based on the processed tags
|
// Create the product-tag mapping based on the processed tags
|
||||||
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
|
const productTagMapping = productAllocations.map(
|
||||||
const inputTag = uuid
|
({ tagUuid, tagName, productUuid }) => {
|
||||||
? createdTagsByUUID.get(uuid)
|
const inputTag = tagUuid
|
||||||
: createdTagsByName.get(name);
|
? createdTagsByUUID.get(tagUuid)
|
||||||
return {
|
: createdTagsByName.get(tagName);
|
||||||
tag: inputTag?.uuid,
|
return {
|
||||||
product: productUuid,
|
tag: inputTag?.uuid,
|
||||||
};
|
product: productUuid,
|
||||||
});
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
await this.spaceProductAllocationService.createProductAllocations(
|
await this.spaceProductAllocationService.createProductAllocations(
|
||||||
space,
|
space,
|
||||||
|
@ -3,7 +3,7 @@ import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entit
|
|||||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||||
import { SubspaceProductAllocationRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
import { SubspaceProductAllocationRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { UpdateSpaceAllocationDto } from 'src/space/interfaces/update-subspace-allocation.dto';
|
import { UpdateSubspaceDto } from 'src/space/dtos';
|
||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
import { In, Not, QueryRunner } from 'typeorm';
|
import { In, Not, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
@ -60,31 +60,46 @@ export class SubspaceProductAllocationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateSubspaceProductAllocationsV2(
|
async updateSubspaceProductAllocationsV2(
|
||||||
subSpaces: UpdateSpaceAllocationDto[],
|
subSpaces: UpdateSubspaceDto[],
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
) {
|
) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
subSpaces.map(async (subspace) => {
|
subSpaces.map(async (subspace) => {
|
||||||
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
|
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
|
||||||
subspace: { uuid: subspace.uuid },
|
subspace: subspace.uuid ? { uuid: subspace.uuid } : undefined,
|
||||||
tag: {
|
tag: subspace.productAllocations
|
||||||
uuid: Not(
|
? {
|
||||||
In(
|
uuid: Not(
|
||||||
subspace.tags.filter((tag) => tag.uuid).map((tag) => tag.uuid),
|
In(
|
||||||
),
|
subspace.productAllocations
|
||||||
),
|
.filter((allocation) => allocation.tagUuid)
|
||||||
},
|
.map((allocation) => allocation.tagUuid),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
product: subspace.productAllocations
|
||||||
|
? {
|
||||||
|
uuid: Not(
|
||||||
|
In(
|
||||||
|
subspace.productAllocations
|
||||||
|
.filter((allocation) => allocation.productUuid)
|
||||||
|
.map((allocation) => allocation.productUuid),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
const subspaceEntity = await queryRunner.manager.findOne(
|
const subspaceEntity = await queryRunner.manager.findOne(
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
{
|
{
|
||||||
where: { uuid: subspace.uuid },
|
where: { uuid: subspace.uuid },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
const processedTags = await this.tagService.upsertTags(
|
||||||
const processedTags = await this.tagService.processTags(
|
subspace.productAllocations,
|
||||||
subspace.tags,
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
@ -97,11 +112,11 @@ export class SubspaceProductAllocationService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Create the product-tag mapping based on the processed tags
|
// Create the product-tag mapping based on the processed tags
|
||||||
const productTagMapping = subspace.tags.map(
|
const productTagMapping = subspace.productAllocations.map(
|
||||||
({ uuid, name, productUuid }) => {
|
({ tagUuid, tagName, productUuid }) => {
|
||||||
const inputTag = uuid
|
const inputTag = tagUuid
|
||||||
? createdTagsByUUID.get(uuid)
|
? createdTagsByUUID.get(tagUuid)
|
||||||
: createdTagsByName.get(name);
|
: createdTagsByName.get(tagName);
|
||||||
return {
|
return {
|
||||||
tag: inputTag?.uuid,
|
tag: inputTag?.uuid,
|
||||||
product: productUuid,
|
product: productUuid,
|
||||||
@ -118,71 +133,6 @@ export class SubspaceProductAllocationService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// async processDeleteActions(dtos: ModifyTagDto[], queryRunner: QueryRunner) {
|
|
||||||
// // : Promise<SubspaceProductAllocationEntity[]>
|
|
||||||
// try {
|
|
||||||
// // if (!dtos || dtos.length === 0) {
|
|
||||||
// // throw new Error('No DTOs provided for deletion.');
|
|
||||||
// // }
|
|
||||||
// // const tagUuidsToDelete = dtos
|
|
||||||
// // .filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
|
|
||||||
// // .map((dto) => dto.tagUuid);
|
|
||||||
// // if (tagUuidsToDelete.length === 0) return [];
|
|
||||||
// // const allocationsToUpdate = await queryRunner.manager.find(
|
|
||||||
// // SubspaceProductAllocationEntity,
|
|
||||||
// // {
|
|
||||||
// // where: { tag: In(tagUuidsToDelete) },
|
|
||||||
// // },
|
|
||||||
// // );
|
|
||||||
// // if (!allocationsToUpdate || allocationsToUpdate.length === 0) return [];
|
|
||||||
// // const deletedAllocations: SubspaceProductAllocationEntity[] = [];
|
|
||||||
// // const allocationUpdates: SubspaceProductAllocationEntity[] = [];
|
|
||||||
// // for (const allocation of allocationsToUpdate) {
|
|
||||||
// // const updatedTags = allocation.tags.filter(
|
|
||||||
// // (tag) => !tagUuidsToDelete.includes(tag.uuid),
|
|
||||||
// // );
|
|
||||||
// // if (updatedTags.length === allocation.tags.length) {
|
|
||||||
// // continue;
|
|
||||||
// // }
|
|
||||||
// // if (updatedTags.length === 0) {
|
|
||||||
// // deletedAllocations.push(allocation);
|
|
||||||
// // } else {
|
|
||||||
// // allocation.tags = updatedTags;
|
|
||||||
// // allocationUpdates.push(allocation);
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
// // if (allocationUpdates.length > 0) {
|
|
||||||
// // await queryRunner.manager.save(
|
|
||||||
// // SubspaceProductAllocationEntity,
|
|
||||||
// // allocationUpdates,
|
|
||||||
// // );
|
|
||||||
// // }
|
|
||||||
// // if (deletedAllocations.length > 0) {
|
|
||||||
// // await queryRunner.manager.remove(
|
|
||||||
// // SubspaceProductAllocationEntity,
|
|
||||||
// // deletedAllocations,
|
|
||||||
// // );
|
|
||||||
// // }
|
|
||||||
// // await queryRunner.manager
|
|
||||||
// // .createQueryBuilder()
|
|
||||||
// // .delete()
|
|
||||||
// // .from('subspace_product_tags')
|
|
||||||
// // .where(
|
|
||||||
// // 'subspace_product_allocation_uuid NOT IN ' +
|
|
||||||
// // queryRunner.manager
|
|
||||||
// // .createQueryBuilder()
|
|
||||||
// // .select('allocation.uuid')
|
|
||||||
// // .from(SubspaceProductAllocationEntity, 'allocation')
|
|
||||||
// // .getQuery() +
|
|
||||||
// // ')',
|
|
||||||
// // )
|
|
||||||
// // .execute();
|
|
||||||
// // return deletedAllocations;
|
|
||||||
// } catch (error) {
|
|
||||||
// throw this.handleError(error, `Failed to delete tags in subspace`);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
async unlinkModels(
|
async unlinkModels(
|
||||||
allocations: SubspaceProductAllocationEntity[],
|
allocations: SubspaceProductAllocationEntity[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
@ -205,67 +155,6 @@ export class SubspaceProductAllocationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// private async validateTagWithinSubspace(
|
|
||||||
// queryRunner: QueryRunner | undefined,
|
|
||||||
// tag: NewTagEntity & { product: string },
|
|
||||||
// subspace: SubspaceEntity,
|
|
||||||
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
|
|
||||||
// ): Promise<void> {
|
|
||||||
// // const existingTagInSpace = await (queryRunner
|
|
||||||
// // ? queryRunner.manager.findOne(SpaceProductAllocationEntity, {
|
|
||||||
// // where: {
|
|
||||||
// // product: { uuid: tag.product },
|
|
||||||
// // space: { uuid: subspace.space.uuid },
|
|
||||||
// // tag: { uuid: tag.uuid },
|
|
||||||
// // },
|
|
||||||
// // })
|
|
||||||
// // : this.spaceProductAllocationRepository.findOne({
|
|
||||||
// // where: {
|
|
||||||
// // product: { uuid: tag.product },
|
|
||||||
// // space: { uuid: subspace.space.uuid },
|
|
||||||
// // tag: { uuid: tag.uuid },
|
|
||||||
// // },
|
|
||||||
// // }));
|
|
||||||
// // const isExcluded = spaceAllocationsToExclude?.some(
|
|
||||||
// // (excludedAllocation) =>
|
|
||||||
// // excludedAllocation.product.uuid === tag.product &&
|
|
||||||
// // excludedAllocation.tags.some((t) => t.uuid === tag.uuid),
|
|
||||||
// // );
|
|
||||||
// // if (!isExcluded && existingTagInSpace) {
|
|
||||||
// // throw new HttpException(
|
|
||||||
// // `Tag ${tag.uuid} (Product: ${tag.product}) is already allocated at the space level (${subspace.space.uuid}). Cannot allocate the same tag in a subspace.`,
|
|
||||||
// // HttpStatus.BAD_REQUEST,
|
|
||||||
// // );
|
|
||||||
// // }
|
|
||||||
// // // ?: Check if the tag is already allocated in another "subspace" within the same space
|
|
||||||
// // const existingTagInSameSpace = await (queryRunner
|
|
||||||
// // ? queryRunner.manager.findOne(SubspaceProductAllocationEntity, {
|
|
||||||
// // where: {
|
|
||||||
// // product: { uuid: tag.product },
|
|
||||||
// // subspace: { space: subspace.space },
|
|
||||||
// // tag: { uuid: tag.uuid },
|
|
||||||
// // },
|
|
||||||
// // relations: ['subspace'],
|
|
||||||
// // })
|
|
||||||
// // : this.subspaceProductAllocationRepository.findOne({
|
|
||||||
// // where: {
|
|
||||||
// // product: { uuid: tag.product },
|
|
||||||
// // subspace: { space: subspace.space },
|
|
||||||
// // tag: { uuid: tag.uuid },
|
|
||||||
// // },
|
|
||||||
// // relations: ['subspace'],
|
|
||||||
// // }));
|
|
||||||
// // if (
|
|
||||||
// // existingTagInSameSpace &&
|
|
||||||
// // existingTagInSameSpace.subspace.uuid !== subspace.uuid
|
|
||||||
// // ) {
|
|
||||||
// // throw new HttpException(
|
|
||||||
// // `Tag ${tag.uuid} (Product: ${tag.product}) is already allocated in another subspace (${existingTagInSameSpace.subspace.uuid}) within the same space (${subspace.space.uuid}).`,
|
|
||||||
// // HttpStatus.BAD_REQUEST,
|
|
||||||
// // );
|
|
||||||
// // }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private createNewSubspaceAllocation(
|
private createNewSubspaceAllocation(
|
||||||
subspace: SubspaceEntity,
|
subspace: SubspaceEntity,
|
||||||
allocationData: { product: string; tag: string },
|
allocationData: { product: string; tag: string },
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
AddSubspaceDto,
|
AddSubspaceDto,
|
||||||
GetSpaceParam,
|
GetSpaceParam,
|
||||||
GetSubSpaceParam,
|
GetSubSpaceParam,
|
||||||
ModifySubspaceDto,
|
UpdateSubspaceDto,
|
||||||
} from '../../dtos';
|
} from '../../dtos';
|
||||||
|
|
||||||
import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
||||||
@ -103,13 +103,13 @@ export class SubSpaceService {
|
|||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
addSubspaceDtos.map(async ({ tags }, index) => {
|
addSubspaceDtos.map(async ({ productAllocations }, index) => {
|
||||||
// map the dto to the corresponding subspace
|
// map the dto to the corresponding subspace
|
||||||
const subspace = createdSubspaces[index];
|
const subspace = createdSubspaces[index];
|
||||||
await this.createAllocations({
|
await this.createAllocations({
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
tags,
|
productAllocations,
|
||||||
type: AllocationsOwnerType.SUBSPACE,
|
type: AllocationsOwnerType.SUBSPACE,
|
||||||
subspace,
|
subspace,
|
||||||
});
|
});
|
||||||
@ -145,7 +145,7 @@ export class SubSpaceService {
|
|||||||
space,
|
space,
|
||||||
);
|
);
|
||||||
const newSubspace = this.subspaceRepository.create({
|
const newSubspace = this.subspaceRepository.create({
|
||||||
...addSubspaceDto,
|
subspaceName: addSubspaceDto.subspaceName,
|
||||||
space,
|
space,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -305,7 +305,7 @@ export class SubSpaceService {
|
|||||||
} */
|
} */
|
||||||
|
|
||||||
async updateSubspaceInSpace(
|
async updateSubspaceInSpace(
|
||||||
subspaceDtos: ModifySubspaceDto[],
|
subspaceDtos: UpdateSubspaceDto[],
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
space: SpaceEntity,
|
space: SpaceEntity,
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
@ -324,42 +324,52 @@ export class SubSpaceService {
|
|||||||
disabled: true,
|
disabled: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
|
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
|
||||||
subspace: { uuid: Not(In(subspaceDtos.map((dto) => dto.uuid))) },
|
subspace: {
|
||||||
|
uuid: Not(
|
||||||
|
In(subspaceDtos.filter(({ uuid }) => uuid).map(({ uuid }) => uuid)),
|
||||||
|
),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// create or update subspaces provided in the list
|
// create or update subspaces provided in the list
|
||||||
const newSubspaces = this.subspaceRepository.create(
|
const newSubspaces = this.subspaceRepository.create(
|
||||||
subspaceDtos.filter((dto) => !dto.uuid),
|
subspaceDtos
|
||||||
|
.filter((dto) => !dto.uuid)
|
||||||
|
.map((dto) => ({
|
||||||
|
subspaceName: dto.subspaceName,
|
||||||
|
space,
|
||||||
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
[...newSubspaces, ...subspaceDtos.filter((dto) => dto.uuid)].map(
|
[
|
||||||
(subspace) => ({ ...subspace, space }),
|
...newSubspaces,
|
||||||
),
|
...subspaceDtos
|
||||||
|
.filter((dto) => dto.uuid)
|
||||||
|
.map((dto) => ({
|
||||||
|
subspaceName: dto.subspaceName,
|
||||||
|
space,
|
||||||
|
})),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
// create or update allocations for the subspaces
|
// create or update allocations for the subspaces
|
||||||
if (updatedSubspaces.length > 0) {
|
if (updatedSubspaces.length > 0) {
|
||||||
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
|
||||||
subspaceDtos.map((dto) => {
|
subspaceDtos.map((dto) => ({
|
||||||
if (!dto.uuid) {
|
...dto,
|
||||||
dto.uuid = updatedSubspaces.find(
|
uuid:
|
||||||
(subspace) => subspace.subspaceName === dto.subspaceName,
|
dto.uuid ||
|
||||||
)?.uuid;
|
updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName)
|
||||||
}
|
?.uuid,
|
||||||
return {
|
})),
|
||||||
tags: dto.tags || [],
|
|
||||||
uuid: dto.uuid,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`An error occurred while modifying subspaces: ${error.message}`,
|
`An error occurred while modifying subspaces: ${error.message}`,
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
@ -478,10 +488,10 @@ export class SubSpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
|
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
|
||||||
const { projectUuid, queryRunner, tags, type } = dto;
|
const { projectUuid, queryRunner, productAllocations, type } = dto;
|
||||||
|
if (!productAllocations) return;
|
||||||
const allocationsData = await this.newTagService.processTags(
|
const allocationsData = await this.newTagService.upsertTags(
|
||||||
tags,
|
productAllocations,
|
||||||
projectUuid,
|
projectUuid,
|
||||||
queryRunner,
|
queryRunner,
|
||||||
);
|
);
|
||||||
@ -491,15 +501,17 @@ export class SubSpaceService {
|
|||||||
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
|
||||||
|
|
||||||
// Create the product-tag mapping based on the processed tags
|
// Create the product-tag mapping based on the processed tags
|
||||||
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
|
const productTagMapping = productAllocations.map(
|
||||||
const inputTag = uuid
|
({ tagUuid, tagName, productUuid }) => {
|
||||||
? createdTagsByUUID.get(uuid)
|
const inputTag = tagUuid
|
||||||
: createdTagsByName.get(name);
|
? createdTagsByUUID.get(tagUuid)
|
||||||
return {
|
: createdTagsByName.get(tagName);
|
||||||
tag: inputTag?.uuid,
|
return {
|
||||||
product: productUuid,
|
tag: inputTag?.uuid,
|
||||||
};
|
product: productUuid,
|
||||||
});
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case AllocationsOwnerType.SUBSPACE: {
|
case AllocationsOwnerType.SUBSPACE: {
|
||||||
|
@ -79,7 +79,6 @@ import { SpaceValidationController } from './controllers/space-validation.contro
|
|||||||
import { DisableSpaceHandler } from './handlers';
|
import { DisableSpaceHandler } from './handlers';
|
||||||
import {
|
import {
|
||||||
SpaceDeviceService,
|
SpaceDeviceService,
|
||||||
SpaceLinkService,
|
|
||||||
SpaceSceneService,
|
SpaceSceneService,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SpaceUserService,
|
SpaceUserService,
|
||||||
@ -89,6 +88,8 @@ import {
|
|||||||
} from './services';
|
} from './services';
|
||||||
import { SpaceProductAllocationService } from './services/space-product-allocation.service';
|
import { SpaceProductAllocationService } from './services/space-product-allocation.service';
|
||||||
import { SubspaceProductAllocationService } from './services/subspace/subspace-product-allocation.service';
|
import { SubspaceProductAllocationService } from './services/subspace/subspace-product-allocation.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
export const CommandHandlers = [DisableSpaceHandler];
|
export const CommandHandlers = [DisableSpaceHandler];
|
||||||
|
|
||||||
@ -110,7 +111,6 @@ export const CommandHandlers = [DisableSpaceHandler];
|
|||||||
ProductRepository,
|
ProductRepository,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
SpaceDeviceService,
|
SpaceDeviceService,
|
||||||
SpaceLinkService,
|
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
@ -163,6 +163,8 @@ export const CommandHandlers = [DisableSpaceHandler];
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [SpaceService],
|
exports: [SpaceService],
|
||||||
})
|
})
|
||||||
|
@ -14,8 +14,8 @@ import {
|
|||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
import { CreateProductAllocationDto } from 'src/space/dtos';
|
||||||
import { In, QueryRunner } from 'typeorm';
|
import { In, QueryRunner } from 'typeorm';
|
||||||
import { ProcessTagDto } from '../dtos';
|
|
||||||
import { CreateTagDto } from '../dtos/tags.dto';
|
import { CreateTagDto } from '../dtos/tags.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -68,13 +68,13 @@ export class TagService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes an array of tag DTOs, creating or updating tags in the database.
|
* Processes an array of tag DTOs, creating or updating tags in the database.
|
||||||
* @param tagDtos - The array of tag DTOs to process.
|
* @param allocationDtos - The array of allocations DTOs to process.
|
||||||
* @param projectUuid - The UUID of the project to associate the tags with.
|
* @param projectUuid - The UUID of the project to associate the tags with.
|
||||||
* @param queryRunner - Optional TypeORM query runner for transaction management.
|
* @param queryRunner - Optional TypeORM query runner for transaction management.
|
||||||
* @returns An array of the processed tag entities.
|
* @returns An array of the processed tag entities.
|
||||||
*/
|
*/
|
||||||
async processTags(
|
async upsertTags(
|
||||||
tagDtos: ProcessTagDto[],
|
allocationDtos: CreateProductAllocationDto[],
|
||||||
projectUuid: string,
|
projectUuid: string,
|
||||||
queryRunner?: QueryRunner,
|
queryRunner?: QueryRunner,
|
||||||
): Promise<NewTagEntity[]> {
|
): Promise<NewTagEntity[]> {
|
||||||
@ -82,20 +82,22 @@ export class TagService {
|
|||||||
const dbManager = queryRunner
|
const dbManager = queryRunner
|
||||||
? queryRunner.manager
|
? queryRunner.manager
|
||||||
: this.tagRepository.manager;
|
: this.tagRepository.manager;
|
||||||
if (!tagDtos || tagDtos.length === 0) {
|
if (!allocationDtos || allocationDtos.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const [tagsWithUuid, tagsWithoutUuid]: [
|
const [allocationsWithTagUuid, allocationsWithoutTagUuid]: [
|
||||||
Pick<ProcessTagDto, 'uuid' | 'productUuid'>[],
|
Pick<CreateProductAllocationDto, 'tagUuid' | 'productUuid'>[],
|
||||||
Omit<ProcessTagDto, 'uuid'>[],
|
Omit<CreateProductAllocationDto, 'tagUuid'>[],
|
||||||
] = this.splitTagsByUuid(tagDtos);
|
] = this.splitTagsByUuid(allocationDtos);
|
||||||
|
|
||||||
// create a set of unique existing tag names for the project
|
// create a set of unique existing tag names for the project
|
||||||
const upsertedTagsByNameResult = await dbManager.upsert(
|
const upsertedTagsByNameResult = await dbManager.upsert(
|
||||||
NewTagEntity,
|
NewTagEntity,
|
||||||
Array.from(
|
Array.from(
|
||||||
new Set<string>(tagsWithoutUuid.map((tag) => tag.name)).values(),
|
new Set<string>(
|
||||||
|
allocationsWithoutTagUuid.map((allocation) => allocation.tagName),
|
||||||
|
).values(),
|
||||||
).map((name) => ({
|
).map((name) => ({
|
||||||
name,
|
name,
|
||||||
project: { uuid: projectUuid },
|
project: { uuid: projectUuid },
|
||||||
@ -111,20 +113,22 @@ export class TagService {
|
|||||||
|
|
||||||
let foundByUuidTags: NewTagEntity[] = [];
|
let foundByUuidTags: NewTagEntity[] = [];
|
||||||
// Fetch existing tags using UUIDs
|
// Fetch existing tags using UUIDs
|
||||||
if (tagsWithUuid.length) {
|
if (allocationsWithTagUuid.length) {
|
||||||
foundByUuidTags = await dbManager.find(NewTagEntity, {
|
foundByUuidTags = await dbManager.find(NewTagEntity, {
|
||||||
where: {
|
where: {
|
||||||
uuid: In([...tagsWithUuid.map((tag) => tag.uuid)]),
|
uuid: In([
|
||||||
|
...allocationsWithTagUuid.map((allocation) => allocation.tagUuid),
|
||||||
|
]),
|
||||||
project: { uuid: projectUuid },
|
project: { uuid: projectUuid },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure all provided UUIDs exist in the database
|
// Ensure all provided UUIDs exist in the database
|
||||||
if (foundByUuidTags.length !== tagsWithUuid.length) {
|
if (foundByUuidTags.length !== allocationsWithTagUuid.length) {
|
||||||
const foundUuids = new Set(foundByUuidTags.map((tag) => tag.uuid));
|
const foundUuids = new Set(foundByUuidTags.map((tag) => tag.uuid));
|
||||||
const missingUuids = tagsWithUuid.filter(
|
const missingUuids = allocationsWithTagUuid.filter(
|
||||||
({ uuid }) => !foundUuids.has(uuid),
|
({ tagUuid }) => !foundUuids.has(tagUuid),
|
||||||
);
|
);
|
||||||
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -179,20 +183,22 @@ export class TagService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private splitTagsByUuid(
|
private splitTagsByUuid(
|
||||||
tagDtos: ProcessTagDto[],
|
allocationsDtos: CreateProductAllocationDto[],
|
||||||
): [ProcessTagDto[], ProcessTagDto[]] {
|
): [CreateProductAllocationDto[], CreateProductAllocationDto[]] {
|
||||||
return tagDtos.reduce<[ProcessTagDto[], ProcessTagDto[]]>(
|
return allocationsDtos.reduce<
|
||||||
([withUuid, withoutUuid], tag) => {
|
[CreateProductAllocationDto[], CreateProductAllocationDto[]]
|
||||||
if (tag.uuid) {
|
>(
|
||||||
withUuid.push(tag);
|
([withUuid, withoutUuid], allocation) => {
|
||||||
|
if (allocation.tagUuid) {
|
||||||
|
withUuid.push(allocation);
|
||||||
} else {
|
} else {
|
||||||
if (!tag.name || !tag.productUuid) {
|
if (!allocation.tagName || !allocation.productUuid) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
`Tag name or product UUID is missing`,
|
`Tag name or product UUID is missing`,
|
||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
withoutUuid.push(tag);
|
withoutUuid.push(allocation);
|
||||||
}
|
}
|
||||||
return [withUuid, withoutUuid];
|
return [withUuid, withoutUuid];
|
||||||
},
|
},
|
||||||
|
@ -1,38 +1,5 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import {
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
IsBoolean,
|
|
||||||
IsNotEmpty,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
IsUUID,
|
|
||||||
} from 'class-validator';
|
|
||||||
|
|
||||||
export class AddSpaceDto {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Name of the space (e.g., Floor 1, Unit 101)',
|
|
||||||
example: 'Unit 101',
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
spaceName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'UUID of the parent space (if any, for hierarchical spaces)',
|
|
||||||
example: 'f5d7e9c3-44bc-4b12-88f1-1b3cda84752e',
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsUUID()
|
|
||||||
@IsOptional()
|
|
||||||
parentUuid?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Indicates whether the space is private or public',
|
|
||||||
example: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
@IsBoolean()
|
|
||||||
isPrivate: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AddUserSpaceDto {
|
export class AddUserSpaceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
|
@ -32,6 +32,8 @@ import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service
|
|||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||||
controllers: [VisitorPasswordController],
|
controllers: [VisitorPasswordController],
|
||||||
@ -61,6 +63,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
OccupancyService,
|
OccupancyService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [VisitorPasswordService],
|
exports: [VisitorPasswordService],
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user