Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/db/models/all/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,23 +1090,25 @@
}, fields)
}

export const loadBookableDevicesWithFiltersLock = function(groups, abi, model, type, sdk, version, devicesFunc, limit = null) {
export const loadBookableDevicesWithFiltersLock = function(groups, filters, devicesFunc, limit = null) {
const filterOptions = []
let serials = []
if (abi) {
filterOptions.push({abi: {$eq: abi}})
}
if (model) {
filterOptions.push({model: {$eq: model}})
}
if (sdk) {
filterOptions.push({sdk: {$eq: sdk}})
}
if (version) {
filterOptions.push({version: {$eq: version}})
}
if (type) {
filterOptions.push({deviceType: {$eq: type}})

// Process dynamic filters
if (filters && typeof filters === 'object') {
Object.entries(filters).forEach(([field, condition]) => {
if (condition !== null && condition !== undefined) {

Check warning on line 1100 in lib/db/models/all/model.js

View workflow job for this annotation

GitHub Actions / lint-lib

Unexpected use of undefined
// Support both simple values and MongoDB operators
if (typeof condition === 'object' && !Array.isArray(condition)) {
// Handle MongoDB operators like {$ne: value}, {$not: {$eq: value}}, etc.
filterOptions.push({[field]: condition})
}
else {
// Handle simple equality for backwards compatibility
filterOptions.push({[field]: {$eq: condition}})
}
}
})
}

const pipeline = [
Expand Down Expand Up @@ -1541,7 +1543,7 @@
})
}

// TODO Check usage. Probably dead code

Check warning on line 1546 in lib/db/models/all/model.js

View workflow job for this annotation

GitHub Actions / lint-lib

Unexpected 'todo' comment: 'TODO Check usage. Probably dead code'
export const setAbsentDisconnectedDevices = function() {
return db.devices.updateOne(
{
Expand Down
6 changes: 5 additions & 1 deletion lib/units/api/controllers/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ function addGroupDevices(req, res) {
const version = apiutil.getBodyParameter(req.body, 'version')
const sdk = apiutil.getBodyParameter(req.body, 'sdk')
const type = apiutil.getBodyParameter(req.body, 'type')
let filters = apiutil.getBodyParameter(req.body, 'filters')
if (!filters) {
filters = {abi: abi, model: model, version: version, sdk: sdk, type: type}
}
const id = req.params.id
let email = null

Expand Down Expand Up @@ -264,7 +268,7 @@ function addGroupDevices(req, res) {
return _addGroupDevices(group, _.difference(_.without(serials.split(','), ''), group.devices))
}
if (amount) {
return dbapi.loadBookableDevicesWithFiltersLock(req.user.groups.subscribed, abi, model, type, sdk, version, function(devices) {
return dbapi.loadBookableDevicesWithFiltersLock(req.user.groups.subscribed, filters, function(devices) {
const serials = devices?.map(device => device.serial) || []
if ((serials.length > 0 && !needAmount) || (needAmount && serials.length === amount)) {
return _addGroupDevices(group, serials)
Expand Down
Loading