change set to transaction

This commit is contained in:
faris Aljohari
2025-02-25 15:33:04 +03:00
parent 1140709e80
commit 630b3ee2b9

View File

@ -10,7 +10,13 @@ import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { firebaseDataBase } from '../../firebase.config'; import { firebaseDataBase } from '../../firebase.config';
import { Database, DataSnapshot, get, ref, set } from 'firebase/database'; import {
Database,
DataSnapshot,
get,
ref,
runTransaction,
} from 'firebase/database';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories'; import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
@Injectable() @Injectable()
export class DeviceStatusFirebaseService { export class DeviceStatusFirebaseService {
@ -154,39 +160,48 @@ export class DeviceStatusFirebaseService {
this.firebaseDb, this.firebaseDb,
`device-status/${addDeviceStatusDto.deviceUuid}`, `device-status/${addDeviceStatusDto.deviceUuid}`,
); );
const snapshot: DataSnapshot = await get(dataRef);
const existingData = snapshot.val() || {};
// Assign default values if fields are not present // Use a transaction to handle concurrent updates
if (!existingData.deviceTuyaUuid) { await runTransaction(dataRef, (existingData) => {
existingData.deviceTuyaUuid = addDeviceStatusDto.deviceTuyaUuid; if (!existingData) {
} existingData = {};
if (!existingData.productUuid) { }
existingData.productUuid = addDeviceStatusDto.productUuid;
}
if (!existingData.productType) {
existingData.productType = addDeviceStatusDto.productType;
}
if (!existingData.status) {
existingData.status = [];
}
// Create a map to track existing status codes // Assign default values if fields are not present
const statusMap = new Map( if (!existingData.deviceTuyaUuid) {
existingData.status.map((item) => [item.code, item.value]), existingData.deviceTuyaUuid = addDeviceStatusDto.deviceTuyaUuid;
); }
if (!existingData.productUuid) {
existingData.productUuid = addDeviceStatusDto.productUuid;
}
if (!existingData.productType) {
existingData.productType = addDeviceStatusDto.productType;
}
if (!existingData.status) {
existingData.status = [];
}
// Update or add status codes // Create a map to track existing status codes
const statusMap = new Map(
existingData.status.map((item) => [item.code, item.value]),
);
for (const statusItem of addDeviceStatusDto.status) { // Update or add status codes
statusMap.set(statusItem.code, statusItem.value);
}
// Convert the map back to an array format for (const statusItem of addDeviceStatusDto.status) {
existingData.status = Array.from(statusMap, ([code, value]) => ({ statusMap.set(statusItem.code, statusItem.value);
code, }
value,
})); // Convert the map back to an array format
existingData.status = Array.from(statusMap, ([code, value]) => ({
code,
value,
}));
return existingData;
});
// Save logs to your repository
const newLogs = addDeviceStatusDto.log.properties.map((property) => { const newLogs = addDeviceStatusDto.log.properties.map((property) => {
return this.deviceStatusLogRepository.create({ return this.deviceStatusLogRepository.create({
deviceId: addDeviceStatusDto.deviceUuid, deviceId: addDeviceStatusDto.deviceUuid,
@ -200,10 +215,9 @@ export class DeviceStatusFirebaseService {
}); });
}); });
await this.deviceStatusLogRepository.save(newLogs); await this.deviceStatusLogRepository.save(newLogs);
// Save the updated data to Firebase
await set(dataRef, existingData);
// Return the updated data // Return the updated data
return existingData; const snapshot: DataSnapshot = await get(dataRef);
return snapshot.val();
} }
} }