Skip to content

Commit

Permalink
just adding the query parameters for site and device online status
Browse files Browse the repository at this point in the history
  • Loading branch information
Baalmart committed Sep 23, 2024
1 parent 86839aa commit d17bfd0
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/device-registry/routes/v2/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,44 @@ router.get(
.optional()
.notEmpty()
.trim(),
query("online_status")
.optional()
.notEmpty()
.withMessage("the online_status should not be empty if provided")
.bail()
.trim()
.toLowerCase()
.isIn(["online", "offline"])
.withMessage(
"the online_status value is not among the expected ones which include: online, offline"
),
query("last_active_before")
.optional()
.notEmpty()
.withMessage("last_active_before date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_before date must be a valid datetime."),
query("last_active_after")
.optional()
.notEmpty()
.withMessage("last_active_after date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_after date must be a valid datetime."),
query("last_active")
.optional()
.notEmpty()
.withMessage("last_active date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active date must be a valid datetime."),
],
]),
deviceController.list
Expand Down
132 changes: 132 additions & 0 deletions src/device-registry/routes/v2/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,72 @@ router.get(
.isIn(constants.NETWORKS)
.withMessage("the tenant value is not among the expected ones"),
]),
oneOf([
[
query("id")
.optional()
.notEmpty()
.trim()
.isMongoId()
.withMessage("id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);

Check warning on line 126 in src/device-registry/routes/v2/sites.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/routes/v2/sites.js#L125-L126

Added lines #L125 - L126 were not covered by tests
}),
query("site_id")
.optional()
.notEmpty()
.trim()
.isMongoId()
.withMessage("site_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);

Check warning on line 136 in src/device-registry/routes/v2/sites.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/routes/v2/sites.js#L135-L136

Added lines #L135 - L136 were not covered by tests
}),
query("name")
.optional()
.notEmpty()
.trim(),
query("online_status")
.optional()
.notEmpty()
.withMessage("the online_status should not be empty if provided")
.bail()
.trim()
.toLowerCase()
.isIn(["online", "offline"])
.withMessage(
"the online_status value is not among the expected ones which include: online, offline"
),
query("last_active_before")
.optional()
.notEmpty()
.withMessage("last_active_before date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_before date must be a valid datetime."),
query("last_active_after")
.optional()
.notEmpty()
.withMessage("last_active_after date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_after date must be a valid datetime."),
query("last_active")
.optional()
.notEmpty()
.withMessage("last_active date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active date must be a valid datetime."),
],
]),
siteController.list
);
router.get(
Expand All @@ -128,6 +194,72 @@ router.get(
.isIn(constants.NETWORKS)
.withMessage("the tenant value is not among the expected ones"),
]),
oneOf([
[
query("id")
.optional()
.notEmpty()
.trim()
.isMongoId()
.withMessage("id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);

Check warning on line 207 in src/device-registry/routes/v2/sites.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/routes/v2/sites.js#L206-L207

Added lines #L206 - L207 were not covered by tests
}),
query("site_id")
.optional()
.notEmpty()
.trim()
.isMongoId()
.withMessage("site_id must be an object ID")
.bail()
.customSanitizer((value) => {
return ObjectId(value);

Check warning on line 217 in src/device-registry/routes/v2/sites.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/routes/v2/sites.js#L216-L217

Added lines #L216 - L217 were not covered by tests
}),
query("name")
.optional()
.notEmpty()
.trim(),
query("online_status")
.optional()
.notEmpty()
.withMessage("the online_status should not be empty if provided")
.bail()
.trim()
.toLowerCase()
.isIn(["online", "offline"])
.withMessage(
"the online_status value is not among the expected ones which include: online, offline"
),
query("last_active_before")
.optional()
.notEmpty()
.withMessage("last_active_before date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_before date must be a valid datetime."),
query("last_active_after")
.optional()
.notEmpty()
.withMessage("last_active_after date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active_after date must be a valid datetime."),
query("last_active")
.optional()
.notEmpty()
.withMessage("last_active date cannot be empty IF provided")
.bail()
.trim()
.toDate()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage("last_active date must be a valid datetime."),
],
]),
siteController.listSummary
);
router.get("/weather", siteController.listWeatherStations);
Expand Down
60 changes: 60 additions & 0 deletions src/device-registry/utils/generate-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ const generateFilter = {
visibility,
deviceName,
status,
online_status,
last_active,
last_active_before,
last_active_after,
} = { ...req.query, ...req.params };

const filter = {};
Expand All @@ -925,6 +929,32 @@ const generateFilter = {
filter.device_number = parseInt(channel);
}

if (last_active) {
filter.lastActive = {};
const start = new Date(last_active);
filter["lastActive"]["$gte"] = start;

Check warning on line 935 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L933-L935

Added lines #L933 - L935 were not covered by tests
}

if (last_active_after) {
filter.last_active_after = {};
const start = new Date(last_active_after);
filter["lastActive"]["$gte"] = start;

Check warning on line 941 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L939-L941

Added lines #L939 - L941 were not covered by tests
}

if (last_active_before) {
filter.last_active_before = {};
const start = new Date(last_active_before);
filter["lastActive"]["$lte"] = start;

Check warning on line 947 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L945-L947

Added lines #L945 - L947 were not covered by tests
}

if (online_status) {
if (online_status.toLowerCase() === "online") {
filter["isOnline"] = true;

Check warning on line 952 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L952

Added line #L952 was not covered by tests
} else if (online_status.toLowerCase() === "offline") {
filter["isOnline"] = false;

Check warning on line 954 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L954

Added line #L954 was not covered by tests
}
}

if (category) {
filter.category = category;
}
Expand Down Expand Up @@ -1035,6 +1065,10 @@ const generateFilter = {
network,
group,
google_place_id,
online_status,
last_active,
last_active_before,
last_active_after,
} = { ...req.query, ...req.params, ...req.body };
const filter = {};
logText("we are generating the filter man!");
Expand Down Expand Up @@ -1078,6 +1112,32 @@ const generateFilter = {
filter["visibility"] = true;
}

if (last_active) {
filter.lastActive = {};
const start = new Date(last_active);
filter["lastActive"]["$gte"] = start;

Check warning on line 1118 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L1116-L1118

Added lines #L1116 - L1118 were not covered by tests
}

if (last_active_after) {
filter.last_active_after = {};
const start = new Date(last_active_after);
filter["lastActive"]["$gte"] = start;

Check warning on line 1124 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L1122-L1124

Added lines #L1122 - L1124 were not covered by tests
}

if (last_active_before) {
filter.last_active_before = {};
const start = new Date(last_active_before);
filter["lastActive"]["$lte"] = start;

Check warning on line 1130 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L1128-L1130

Added lines #L1128 - L1130 were not covered by tests
}

if (online_status) {
if (online_status.toLowerCase() === "online") {
filter["isOnline"] = true;

Check warning on line 1135 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L1135

Added line #L1135 was not covered by tests
} else if (online_status.toLowerCase() === "offline") {
filter["isOnline"] = false;

Check warning on line 1137 in src/device-registry/utils/generate-filter.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/utils/generate-filter.js#L1137

Added line #L1137 was not covered by tests
}
}

if (site_codes) {
const siteCodesArray = site_codes.toString().split(",");
filter["site_codes"] = { $in: siteCodesArray };
Expand Down

0 comments on commit d17bfd0

Please sign in to comment.