Skip to content

Commit

Permalink
remove attributes with empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardovivanco committed May 23, 2024
1 parent b1d59cc commit 8087791
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions scripts/tableland/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ task('sync-tableland', 'npx hardhat sync-tableland --network <networkName>')
console.log(`API Device Definition By Manufacturer [${make}] total => ${deviceDefinitionByManufacturers.length}`);
console.log(`Tableland Device Definition By Manufacturer [${make}] total => ${tablelandDeviceDefinitionByManufacturers.length}`);

// Clear empty attributes
deviceDefinitionByManufacturers.forEach(dd => {
if (dd.device_attributes) {
dd.device_attributes = dd.device_attributes.filter(({ value }) => value);
}
});

// Insert new dd
let newDeviceDefinitionByManufacturers = [];
deviceDefinitionByManufacturers.forEach(element => {
Expand All @@ -582,10 +589,19 @@ task('sync-tableland', 'npx hardhat sync-tableland --network <networkName>')
const obj1 = element.device_attributes.find(item => item.name === obj2.name);
return obj1 && obj1.value !== obj2.value;
});

if (different.length > 0) {
updateDeviceDefinitionByManufacturers.push(element);
}

// If it has no different values, then validate if it has empty values.
if (different.length == 0) {
const hasEmptyValues = dds[0].metadata.device_attributes.some(({ value }) => !value);
if (hasEmptyValues) {
updateDeviceDefinitionByManufacturers.push(element);
}
}

}

}
Expand All @@ -596,7 +612,7 @@ task('sync-tableland', 'npx hardhat sync-tableland --network <networkName>')
tablelandDeviceDefinitionByManufacturers.forEach(element => {
const dds = deviceDefinitionByManufacturers.filter((c) => c.name_slug === element.id);
//console.log(element.ksuid, dds[0].device_definition_id);
if (dds == undefined || dds == 0){
if ((dds?.length ?? 0) == 0){
deleteDeviceDefinitionByManufacturers.push(element);
}
});
Expand All @@ -622,7 +638,7 @@ task('sync-tableland', 'npx hardhat sync-tableland --network <networkName>')

if (updateDeviceDefinitionByManufacturers.length > 0) {
console.log('\x1b[36m%s\x1b[0m', `Device Definition to update ${updateDeviceDefinitionByManufacturers.length} By Manufacturer [${make}]`);

const devices = updateDeviceDefinitionByManufacturers.map(function (dd) {
const deviceDefinitionInput: DeviceDefinitionInput = {
id: dd.name_slug,
Expand Down

0 comments on commit 8087791

Please sign in to comment.