Merge pull request #218 from SyncrowIOT/SP-1101-investigate-mismatch-between-log-and-column-values

Sp 1101 investigate mismatch between log and column values
This commit is contained in:
faris Aljohari
2025-01-21 01:40:01 -06:00
committed by GitHub
2 changed files with 53 additions and 27 deletions

View File

@ -187,19 +187,19 @@ export class DeviceStatusFirebaseService {
code, code,
value, value,
})); }));
const newLog = this.deviceStatusLogRepository.create({ const newLogs = addDeviceStatusDto.log.properties.map((property) => {
deviceId: addDeviceStatusDto.deviceUuid, return this.deviceStatusLogRepository.create({
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid, deviceId: addDeviceStatusDto.deviceUuid,
productId: addDeviceStatusDto.log.productId, deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
log: addDeviceStatusDto.log, productId: addDeviceStatusDto.log.productId,
code: existingData.status[0].code, log: addDeviceStatusDto.log,
value: existingData.status[0].value, code: property.code,
eventId: addDeviceStatusDto.log.dataId, value: property.value,
eventTime: new Date( eventId: addDeviceStatusDto.log.dataId,
addDeviceStatusDto.log.properties[0].time, eventTime: new Date(property.time).toISOString(),
).toISOString(), });
}); });
await this.deviceStatusLogRepository.save(newLog); await this.deviceStatusLogRepository.save(newLogs);
// Save the updated data to Firebase // Save the updated data to Firebase
await set(dataRef, existingData); await set(dataRef, existingData);

View File

@ -9,7 +9,7 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { UserStatusEnum } from '@app/common/constants/user-status.enum'; import { UserStatusEnum } from '@app/common/constants/user-status.enum';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { generateRandomString } from '@app/common/helper/randomString'; import { generateRandomString } from '@app/common/helper/randomString';
import { In, IsNull, Not, QueryRunner } from 'typeorm'; import { EntityManager, In, IsNull, Not, QueryRunner } from 'typeorm';
import { DataSource } from 'typeorm'; import { DataSource } from 'typeorm';
import { UserEntity } from '@app/common/modules/user/entities'; import { UserEntity } from '@app/common/modules/user/entities';
import { RoleType } from '@app/common/constants/role.type.enum'; import { RoleType } from '@app/common/constants/role.type.enum';
@ -69,20 +69,27 @@ export class InviteUserService {
const userRepo = queryRunner.manager.getRepository(UserEntity); const userRepo = queryRunner.manager.getRepository(UserEntity);
await this.checkEmailAndProject({ email }); await this.checkEmailAndProject({ email });
const user = await userRepo.findOne({ const existingUser = await userRepo.findOne({
where: { where: {
email, email,
project: Not(IsNull()), project: Not(IsNull()),
}, },
}); });
if (user) { if (existingUser) {
throw new HttpException( throw new HttpException(
'User already has a project', 'User already has a project',
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
); );
} }
// Validate spaces
const validSpaces = await this.validateSpaces(
spaceUuids,
queryRunner.manager,
);
// Create invitation
const inviteUser = this.inviteUserRepository.create({ const inviteUser = this.inviteUserRepository.create({
firstName, firstName,
lastName, lastName,
@ -97,30 +104,25 @@ export class InviteUserService {
}); });
const invitedUser = await queryRunner.manager.save(inviteUser); const invitedUser = await queryRunner.manager.save(inviteUser);
const spaceRepo = queryRunner.manager.getRepository(SpaceEntity);
const spaces = await spaceRepo.find({
where: {
uuid: In(spaceUuids),
},
});
const spaceNames = spaces.map((space) => space.spaceName); // Link user to spaces
const spacePromises = validSpaces.map(async (space) => {
const spaceNamesString = spaceNames.join(', ');
const spacePromises = spaceUuids.map(async (spaceUuid) => {
const inviteUserSpace = this.inviteUserSpaceRepository.create({ const inviteUserSpace = this.inviteUserSpaceRepository.create({
inviteUser: { uuid: invitedUser.uuid }, inviteUser: { uuid: invitedUser.uuid },
space: { uuid: spaceUuid }, space: { uuid: space.uuid },
}); });
return queryRunner.manager.save(inviteUserSpace); return queryRunner.manager.save(inviteUserSpace);
}); });
await Promise.all(spacePromises); await Promise.all(spacePromises);
// Send invitation email
const spaceNames = validSpaces.map((space) => space.spaceName).join(', ');
await this.emailService.sendEmailWithInvitationTemplate(email, { await this.emailService.sendEmailWithInvitationTemplate(email, {
name: firstName, name: firstName,
invitationCode, invitationCode,
role: roleType, role: roleType,
spacesList: spaceNamesString, spacesList: spaceNames,
}); });
await queryRunner.commitTransaction(); await queryRunner.commitTransaction();
@ -147,6 +149,30 @@ export class InviteUserService {
await queryRunner.release(); await queryRunner.release();
} }
} }
private async validateSpaces(
spaceUuids: string[],
entityManager: EntityManager,
): Promise<SpaceEntity[]> {
const spaceRepo = entityManager.getRepository(SpaceEntity);
const validSpaces = await spaceRepo.find({
where: { uuid: In(spaceUuids) },
});
if (validSpaces.length !== spaceUuids.length) {
const validSpaceUuids = validSpaces.map((space) => space.uuid);
const invalidSpaceUuids = spaceUuids.filter(
(uuid) => !validSpaceUuids.includes(uuid),
);
throw new HttpException(
`Invalid space UUIDs: ${invalidSpaceUuids.join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
return validSpaces;
}
async checkEmailAndProject(dto: CheckEmailDto): Promise<BaseResponseDto> { async checkEmailAndProject(dto: CheckEmailDto): Promise<BaseResponseDto> {
const { email } = dto; const { email } = dto;