404 when updating tags of non-existing devices

This commit is contained in:
Zaid Humaimidi committed 2023-08-28 18:17:22 -07:00
1 parent a0ad6ec3f1
commit ea956fbc9c
1 file changed
+16 -2
+16 -2
View File
@@ -226,17 +226,31 @@ async function handler(
const deviceId = decodeURIComponent(r[1]); const deviceId = decodeURIComponent(r[1]);
const tag = decodeURIComponent(r[2]); const tag = decodeURIComponent(r[2]);
if (request.method === "POST") { if (request.method === "POST") {
await collections.devices.updateOne( const updateRes = await collections.devices.updateOne(
{ _id: deviceId }, { _id: deviceId },
{ $addToSet: { _tags: tag } } { $addToSet: { _tags: tag } }
); );
if (!updateRes.matchedCount) {
response.writeHead(404);
response.end("No such device");
return;
}
response.writeHead(200); response.writeHead(200);
response.end(); response.end();
} else if (request.method === "DELETE") { } else if (request.method === "DELETE") {
await collections.devices.updateOne( const updateRes = await collections.devices.updateOne(
{ _id: deviceId }, { _id: deviceId },
{ $pull: { _tags: tag } } { $pull: { _tags: tag } }
); );
if (!updateRes.matchedCount) {
response.writeHead(404);
response.end("No such device");
return;
}
response.writeHead(200); response.writeHead(200);
response.end(); response.end();
} else { } else {