Skip to content

Commit

Permalink
more refectoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mpfeil committed Sep 27, 2024
1 parent c17d04b commit 8b5de20
Show file tree
Hide file tree
Showing 9 changed files with 865 additions and 20 deletions.
20 changes: 12 additions & 8 deletions packages/api/lib/controllers/boxesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const
handleError = require('../helpers/errorHandler'),
jsonstringify = require('stringify-stream');
const { createDevice } = require('@sensebox/opensensemap-api-models/src/device');
const { findByUserId } = require('@sensebox/opensensemap-api-models/src/password');
const { removeDevice, checkPassword } = require('@sensebox/opensensemap-api-models/src/user/user');

/**
* @apiDefine Addons
Expand Down Expand Up @@ -500,10 +502,8 @@ const getBox = async function getBox (req, res) {
*/
const postNewBox = async function postNewBox (req, res) {
try {
// let newBox = await req.user.addBox(req._userParams);
// newBox = await Box.populate(newBox, Box.BOX_SUB_PROPS_FOR_POPULATION);

const newDevice = await createDevice(req.user.id, req._userParams);
// TODO: only return specific fields newBox = await Box.populate(newBox, Box.BOX_SUB_PROPS_FOR_POPULATION);

Check failure on line 506 in packages/api/lib/controllers/boxesController.js

View workflow job for this annotation

GitHub Actions / 🔬 Lint code

Unexpected 'todo' comment: 'TODO: only return specific fields newBox...'

res.send(201, { message: 'Box successfully created', data: newDevice });
clearCache(['getBoxes', 'getStats']);
Expand Down Expand Up @@ -581,11 +581,14 @@ const deleteBox = async function deleteBox (req, res) {
const { password, boxId } = req._userParams;

try {
await req.user.checkPassword(password);
const box = await req.user.removeBox(boxId);
res.send({ code: 'Ok', message: 'box and all associated measurements marked for deletion' });
const hashedPassword = await findByUserId(req.user.id);

await checkPassword(password, hashedPassword);
const device = await removeDevice(boxId);

res.send({ code: 'Ok', message: 'device and all associated measurements marked for deletion' });
clearCache(['getBoxes', 'getStats']);
postToMattermost(`Box deleted: ${req.user.name} (${redactEmail(req.user.email)}) just deleted "${box.name}" (${boxId})`);
postToMattermost(`Device deleted: ${req.user.name} (${redactEmail(req.user.email)}) just deleted "${device.name}" (${boxId})`);

} catch (err) {
return handleError(err);
Expand Down Expand Up @@ -717,7 +720,8 @@ module.exports = {
deleteBox: [
checkContentType,
retrieveParameters([
{ predef: 'boxId', required: true },
{ name: 'boxId', required: true, dataType: 'String' },
// { predef: 'boxId', required: true },
{ predef: 'password' }
]),
checkPrivilege,
Expand Down
5 changes: 4 additions & 1 deletion packages/api/lib/helpers/userParamHelpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { checkDeviceOwner } = require('@sensebox/opensensemap-api-models/src/user/user');

const { BadRequestError, UnprocessableEntityError, InvalidArgumentError, ForbiddenError } = require('restify-errors'),
{ utils: { parseAndValidateTimestamp }, db: { mongoose }, decoding: { validators: { transformAndValidateCoords } } } = require('@sensebox/opensensemap-api-models'),
moment = require('moment'),
Expand Down Expand Up @@ -548,7 +550,8 @@ const checkPrivilege = async function checkPrivilege (req) {

if (req._userParams.boxId) {
try {
req.user.checkBoxOwner(req._userParams.boxId);
await checkDeviceOwner(req.user.id, req._userParams.boxId);
// req.user.checkBoxOwner(req._userParams.boxId);

return;
} catch (err) {
Expand Down
26 changes: 26 additions & 0 deletions packages/models/migrations/0007_add_device_models.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ALTER TYPE "model" ADD VALUE 'home_v2_lora';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_v2_ethernet';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_v2_ethernet_feinstaub';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_v2_wifi';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_v2_wifi_feinstaub';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_ethernet';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_wifi';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_ethernet_feinstaub';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'home_wifi_feinstaub';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sds011';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sds011_dht11';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sds011_dht22';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sds011_bmp180';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sds011_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms1003';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms1003_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms3003';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms3003_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms5003';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms5003_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms7003';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_pms7003_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sps30_bme280';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'luftdaten_sps30_sht3x';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'hackair_home_v2';--> statement-breakpoint
ALTER TYPE "model" ADD VALUE 'custom';
Loading

0 comments on commit 8b5de20

Please sign in to comment.