Skip to content

Commit

Permalink
Merge pull request #4034 from airqo-platform/en-site-device-orgs
Browse files Browse the repository at this point in the history
just fixing list cohorts
  • Loading branch information
Baalmart authored Dec 10, 2024
2 parents dcc3452 + 93914f8 commit dfe6eb1
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/device-registry/models/Cohort.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ cohortSchema.statics.register = async function(args, next) {
response.errors = { message: error.message };
}

return response;

logger.error(`🐛🐛 Internal Server Error ${error.message}`);
next(new HttpError(response.message, response.status, response.errors));
}
Expand Down Expand Up @@ -196,12 +194,22 @@ cohortSchema.statics.list = async function(
foreignField: "cohorts",
as: "devices",
})
.unwind("$devices")
.lookup({
from: "sites",
localField: "devices.site_id",
foreignField: "_id",
as: "devices.site",
.project({
_id: 1,
visibility: 1,
cohort_tags: 1,
cohort_codes: 1,
name: 1,
createdAt: 1,
network: 1,
group: 1,
devices: {
$cond: {
if: { $eq: [{ $size: "$devices" }, 0] },
then: [],
else: "$devices",
},
},
})
.sort({ createdAt: -1 })
.project(inclusionProjection)
Expand All @@ -215,11 +223,10 @@ cohortSchema.statics.list = async function(
createdAt: { $first: "$createdAt" },
network: { $first: "$network" },
group: { $first: "$group" },
numberOfDevices: { $sum: 1 },
devices: { $push: "$devices" },
devices: { $first: "$devices" },
})
.skip(skip ? skip : 0)
.limit(limit ? limit : 1000)
.skip(skip ? parseInt(skip) : 0)
.limit(limit ? parseInt(limit) : 1000)
.allowDiskUse(true);

const cohorts = await pipeline.exec();
Expand All @@ -234,24 +241,28 @@ cohortSchema.statics.list = async function(
network: cohort.network,
createdAt: cohort.createdAt,
group: cohort.group,
numberOfDevices: cohort.numberOfDevices,
devices: cohort.devices.map((device) => ({
_id: device._id,
status: device.status,
name: device.name,
network: device.network,
group: device.group,
device_number: device.device_number,
description: device.description,
long_name: device.long_name,
createdAt: device.createdAt,
host_id: device.host_id,
site: device.site &&
device.site[0] && {
_id: device.site[0]._id,
name: device.site[0].name,
},
})),
numberOfDevices: cohort.devices ? cohort.devices.length : 0,
devices: cohort.devices
? cohort.devices
.filter((device) => Object.keys(device).length > 0)
.map((device) => ({
_id: device._id,
status: device.status,
name: device.name,
network: device.network,
group: device.group,
device_number: device.device_number,
description: device.description,
long_name: device.long_name,
createdAt: device.createdAt,
host_id: device.host_id,
site: device.site &&
device.site[0] && {
_id: device.site[0]._id,
name: device.site[0].name,
},
}))
: [],
}))
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));

Expand Down

0 comments on commit dfe6eb1

Please sign in to comment.