Skip to content

Commit

Permalink
just a small hotfix for device updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Baalmart committed Nov 28, 2024
1 parent ca75dd7 commit c8e68e3
Showing 1 changed file with 149 additions and 142 deletions.
291 changes: 149 additions & 142 deletions src/device-registry/models/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,161 +237,167 @@ deviceSchema.plugin(uniqueValidator, {
message: `{VALUE} must be unique!`,
});

deviceSchema.post("save", async function(doc) {});

deviceSchema.pre("save", function(next) {
// Set default network if not provided
if (!this.network) {
this.network = constants.DEFAULT_NETWORK;
}

// Manage serial_number based on device_number and network
if (this.network === "airqo" && this.device_number) {
this.serial_number = String(this.device_number); // Assign device_number as a string
} else if (!this.serial_number && this.network !== "airqo") {
next(
new HttpError(
"Devices not part of the AirQo network must include a serial_number as a string.",
httpStatus.BAD_REQUEST
)
);
}

// Generate name based on generation version and count
if (this.generation_version && this.generation_count) {
this.name = `aq_g${this.generation_version}_${this.generation_count}`;
}

// Handle alias generation
if (!this.alias && (this.long_name || this.name)) {
this.alias = (this.long_name || this.name).trim().replace(/ /g, "_");
if (!this.alias) {
return next(
new HttpError(
"Unable to generate ALIAS for the device.",
httpStatus.INTERNAL_SERVER_ERROR
)
);
}
}

// Sanitize name
const sanitizeName = (name) => {
return name
.replace(/[^a-zA-Z0-9]/g, "_")
.slice(0, 41)
.trim()
.toLowerCase();
};

if (this.name) {
this.name = sanitizeName(this.name);
} else if (this.long_name) {
this.name = sanitizeName(this.long_name);
}

// Set long_name if not provided
if (!this.long_name && this.name) {
this.long_name = this.name;
}

// Encrypt keys if modified
if (this.isModified("name") && this.writeKey && this.readKey) {
this.writeKey = this._encryptKey(this.writeKey);
this.readKey = this._encryptKey(this.readKey);
}
deviceSchema.pre(
[
"update",
"findByIdAndUpdate",
"updateMany",
"updateOne",
"save",
"findOneAndUpdate",
],
async function(next) {
try {

Check warning on line 250 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L250

Added line #L250 was not covered by tests
// Determine if this is a new document or an update
const isNew = this.isNew;
const updateData = this.getUpdate ? this.getUpdate() : this;

Check warning on line 253 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L252-L253

Added lines #L252 - L253 were not covered by tests

if (isNew) {

Check warning on line 255 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L255

Added line #L255 was not covered by tests
// Set default network if not provided
if (!this.network) {
this.network = constants.DEFAULT_NETWORK;

Check warning on line 258 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L257-L258

Added lines #L257 - L258 were not covered by tests
}

// Manage serial_number based on device_number and network
if (this.network === "airqo" && this.device_number) {
this.serial_number = String(this.device_number); // Assign device_number as a string
} else if (!this.serial_number && this.network !== "airqo") {
next(

Check warning on line 265 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L262-L265

Added lines #L262 - L265 were not covered by tests
new HttpError(
"Devices not part of the AirQo network must include a serial_number as a string.",
httpStatus.BAD_REQUEST
)
);
}

// Generate name based on generation version and count
if (this.generation_version && this.generation_count) {
this.name = `aq_g${this.generation_version}_${this.generation_count}`;

Check warning on line 275 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L274-L275

Added lines #L274 - L275 were not covered by tests
}

// Handle alias generation
if (!this.alias && (this.long_name || this.name)) {
this.alias = (this.long_name || this.name).trim().replace(/ /g, "_");
if (!this.alias) {
return next(

Check warning on line 282 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L279-L282

Added lines #L279 - L282 were not covered by tests
new HttpError(
"Unable to generate ALIAS for the device.",
httpStatus.INTERNAL_SERVER_ERROR
)
);
}
}

// Sanitize name
const sanitizeName = (name) => {
return name

Check warning on line 293 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L292-L293

Added lines #L292 - L293 were not covered by tests
.replace(/[^a-zA-Z0-9]/g, "_")
.slice(0, 41)
.trim()
.toLowerCase();
};

// Generate device codes
this.device_codes = [this._id, this.name];
if (this.device_number) {
this.device_codes.push(this.device_number);
}
if (this.alias) {
this.device_codes.push(this.alias);
}
if (this.serial_number) {
this.device_codes.push(this.serial_number);
}
if (this.name) {
this.name = sanitizeName(this.name);
} else if (this.long_name) {
this.name = sanitizeName(this.long_name);

Check warning on line 303 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L300-L303

Added lines #L300 - L303 were not covered by tests
}

// Set long_name if not provided
if (!this.long_name && this.name) {
this.long_name = this.name;

Check warning on line 308 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L307-L308

Added lines #L307 - L308 were not covered by tests
}

// Encrypt keys if modified
if (this.isModified("name") && this.writeKey && this.readKey) {
this.writeKey = this._encryptKey(this.writeKey);
this.readKey = this._encryptKey(this.readKey);

Check warning on line 314 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L312-L314

Added lines #L312 - L314 were not covered by tests
}

// Generate device codes
this.device_codes = [this._id, this.name];
if (this.device_number) {
this.device_codes.push(this.device_number);

Check warning on line 320 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L318-L320

Added lines #L318 - L320 were not covered by tests
}
if (this.alias) {
this.device_codes.push(this.alias);

Check warning on line 323 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L322-L323

Added lines #L322 - L323 were not covered by tests
}
if (this.serial_number) {
this.device_codes.push(this.serial_number);

Check warning on line 326 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L325-L326

Added lines #L325 - L326 were not covered by tests
}

// Check for duplicate values in cohorts array
const duplicateValues = this.cohorts.filter(
(value, index, self) => self.indexOf(value) !== index

Check warning on line 331 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L330-L331

Added lines #L330 - L331 were not covered by tests
);

// Check for duplicate values in cohorts array
const duplicateValues = this.cohorts.filter(
(value, index, self) => self.indexOf(value) !== index
);

if (duplicateValues.length > 0) {
return next(
new HttpError(
"Duplicate values found in cohorts array.",
httpStatus.BAD_REQUEST
)
);
}
if (duplicateValues.length > 0) {
return next(

Check warning on line 335 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L334-L335

Added lines #L334 - L335 were not covered by tests
new HttpError(
"Duplicate values found in cohorts array.",
httpStatus.BAD_REQUEST
)
);
}
}

return next();
});
// Handling the network condition
if (updateData.network === "airqo") {
if (updateData.device_number) {
updateData.serial_number = String(updateData.device_number);
} else if (updateData.serial_number) {
updateData.device_number = Number(updateData.serial_number);

Check warning on line 349 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L345-L349

Added lines #L345 - L349 were not covered by tests
}
}

deviceSchema.pre(
["update", "findByIdAndUpdate", "updateMany", "updateOne"],
function(next) {
// Enable validation on update
this.setOptions({ runValidators: true });

const updateData = this.getUpdate(); // Get the data being updated

// Handling the network condition
if (updateData.network === "airqo") {
if (updateData.device_number) {
updateData.serial_number = String(updateData.device_number);
} else if (updateData.serial_number) {
updateData.device_number = Number(updateData.serial_number);
// Sanitize name if modified
if (updateData.name) {
const sanitizeName = (name) => {
return name

Check warning on line 356 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L354-L356

Added lines #L354 - L356 were not covered by tests
.replace(/[^a-zA-Z0-9]/g, "_")
.slice(0, 41)
.trim()
.toLowerCase();
};
updateData.name = sanitizeName(updateData.name);

Check warning on line 362 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L362

Added line #L362 was not covered by tests
}
}

// Sanitize name if modified
if (updateData.name) {
const sanitizeName = (name) => {
return name
.replace(/[^a-zA-Z0-9]/g, "_")
.slice(0, 41)
.trim()
.toLowerCase();
};
updateData.name = sanitizeName(updateData.name);
}
// Generate access code if present in update
if (updateData.access_code) {
const access_code = accessCodeGenerator.generate({

Check warning on line 367 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L366-L367

Added lines #L366 - L367 were not covered by tests
length: 16,
excludeSimilarCharacters: true,
});
updateData.access_code = access_code.toUpperCase();

Check warning on line 371 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L371

Added line #L371 was not covered by tests
}

// Generate access code if present in update
if (updateData.access_code) {
const access_code = accessCodeGenerator.generate({
length: 16,
excludeSimilarCharacters: true,
});
updateData.access_code = access_code.toUpperCase();
}
// Handle $addToSet for device_codes, previous_sites, and pictures
const addToSetUpdates = {};

Check warning on line 375 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L375

Added line #L375 was not covered by tests

// Handle $addToSet for device_codes, previous_sites, and pictures
const addToSetUpdates = {};
if (updateData.device_codes) {
addToSetUpdates.device_codes = { $each: updateData.device_codes };
delete updateData.device_codes; // Remove from main update object

Check warning on line 379 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L377-L379

Added lines #L377 - L379 were not covered by tests
}

if (updateData.device_codes) {
addToSetUpdates.device_codes = { $each: updateData.device_codes };
delete updateData.device_codes; // Remove from main update object
}
if (updateData.previous_sites) {
addToSetUpdates.previous_sites = { $each: updateData.previous_sites };
delete updateData.previous_sites; // Remove from main update object

Check warning on line 384 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L382-L384

Added lines #L382 - L384 were not covered by tests
}

if (updateData.previous_sites) {
addToSetUpdates.previous_sites = { $each: updateData.previous_sites };
delete updateData.previous_sites; // Remove from main update object
}
if (updateData.pictures) {
addToSetUpdates.pictures = { $each: updateData.pictures };
delete updateData.pictures; // Remove from main update object

Check warning on line 389 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L387-L389

Added lines #L387 - L389 were not covered by tests
}

if (updateData.pictures) {
addToSetUpdates.pictures = { $each: updateData.pictures };
delete updateData.pictures; // Remove from main update object
}
// If there are any $addToSet updates, merge them into the main update object
if (Object.keys(addToSetUpdates).length > 0) {
updateData.$addToSet = addToSetUpdates;

Check warning on line 394 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L393-L394

Added lines #L393 - L394 were not covered by tests
}

// If there are any $addToSet updates, merge them into the main update object
if (Object.keys(addToSetUpdates).length > 0) {
updateData.$addToSet = addToSetUpdates;
next();

Check warning on line 397 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L397

Added line #L397 was not covered by tests
} catch (error) {
return next(error);

Check warning on line 399 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L399

Added line #L399 was not covered by tests
}

next();
}
);
deviceSchema.methods = {
Expand Down Expand Up @@ -587,6 +593,7 @@ deviceSchema.statics = {
},
async modify({ filter = {}, update = {}, opts = {} } = {}, next) {
try {
logText("we are now inside the modify function for devices....");

Check warning on line 596 in src/device-registry/models/Device.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/models/Device.js#L596

Added line #L596 was not covered by tests
const invalidKeys = ["name", "_id", "writeKey", "readKey"];
const sanitizedUpdate = sanitizeObject(update, invalidKeys);

Expand Down

0 comments on commit c8e68e3

Please sign in to comment.