Skip to content

Commit abf6a28

Browse files
committed
feat: add rudimentary delete implementation
1 parent 350364e commit abf6a28

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

app/routes/api.boxes.$deviceId.$sensorId.measurements.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
11
import { type ActionFunctionArgs, redirect } from 'react-router'
2+
import { getUserFromJwt } from '~/lib/jwt'
3+
import { getUserDevices } from '~/models/device.server'
4+
import { deleteMeasurementsForSensor } from '~/models/measurement.server'
5+
import { StandardResponse } from '~/utils/response-utils'
6+
7+
export async function action({ request, params }: ActionFunctionArgs) {
8+
try {
9+
const { deviceId, sensorId } = params
10+
if (!deviceId || !sensorId)
11+
return StandardResponse.badRequest(
12+
'Invalid device id or sensor id specified',
13+
)
14+
15+
const jwtResponse = await getUserFromJwt(request)
16+
17+
if (typeof jwtResponse === 'string')
18+
return StandardResponse.forbidden(
19+
'Invalid JWT authorization. Please sign in to obtain new JWT.',
20+
)
21+
22+
if (request.method !== 'DELETE')
23+
return StandardResponse.methodNotAllowed('Endpoint only supports DELETE')
24+
25+
const userDevices = await getUserDevices(jwtResponse.id)
26+
if (!userDevices.some((d) => d.id === deviceId))
27+
return StandardResponse.forbidden(
28+
'You are not allowed to delete data of the given device',
29+
)
30+
31+
const device = userDevices.find((d) => d.id === deviceId)
32+
if (!device?.sensors.some((s) => s.id === sensorId))
33+
return StandardResponse.forbidden(
34+
'You are not allowed to delete data of the given sensor',
35+
)
36+
37+
// TODO add more parameters (from-date, to-date etc)
38+
await deleteMeasurementsForSensor(sensorId)
39+
return StandardResponse.ok({})
40+
} catch (err: any) {
41+
return StandardResponse.internalServerError(
42+
err.message || 'An unexpected error occured',
43+
)
44+
}
245

3-
/**
4-
* Following REST and the other routes, we should forward
5-
* to a consistent route schema and use
6-
* /boxes/:deviceId/sensors/:sensorId/measurements
7-
* instead of
8-
* /boxes/:deviceId/:sensorId/measurements
9-
*/
10-
export async function action({ params }: ActionFunctionArgs) {
1146
const MEASUREMENTS_ROUTE = `/api/boxes/${params.deviceId}/sensors/${params.sensorId}/measurements`
1247
return redirect(MEASUREMENTS_ROUTE, {
1348
status: 308, // Permanent Redirect

app/routes/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ const routes: { noauth: RouteInfo[]; auth: RouteInfo[] } = {
152152
{
153153
path: `boxes/:boxId/:sensorId/measurements`,
154154
method: 'DELETE',
155-
deprecationNotice:
156-
'Use boxes/:boxId/sensors/:sensorId/measurements instead',
157155
},
158156
{
159157
path: `boxes/:boxId/sensors/:sensorId/measurements`,

0 commit comments

Comments
 (0)