Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
feat(http): adds ability to post with own recorded_at prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ff6347 committed Mar 29, 2022
1 parent 5beb2a8 commit 83966c2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
107 changes: 100 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"eslint": "7.32.0",
"glob": "7.2.0",
"is-ci": "3.0.1",
"jest-each": "27.5.1",
"jsonwebtoken": "8.5.1",
"nock": "13.1.3",
"node-fetch": "2.6.1",
Expand All @@ -73,6 +74,7 @@
"@supabase/supabase-js": "1.31.1",
"bcrypt": "5.0.1",
"config": "3.3.7",
"date-fns": "2.28.0",
"fastify": "3.27.4",
"fastify-auth": "1.1.0",
"fastify-blipp": "3.1.0",
Expand Down
14 changes: 13 additions & 1 deletion src/integrations/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TODO: Should this file be moved to sensors-records.ts?
import { FastifyPluginAsync } from "fastify";
import fp from "fastify-plugin";
import isDate from "date-fns/isDate";
import { definitions } from "@technologiestiftung/stadtpuls-supabase-definitions";
import { AuthToken } from "../common/jwt";
import S from "fluent-json-schema";
Expand All @@ -17,6 +18,7 @@ interface HTTPPostBody {
latitude?: number;
longitude?: number;
altitude?: number;
recorded_at?: string;
measurements: number[];
}

Expand All @@ -33,6 +35,7 @@ const postHTTPBodySchema = S.object()
.prop("latitude", S.number().minimum(-90).maximum(90))
.prop("longitude", S.number().minimum(-180).maximum(180))
.prop("altitude", S.number().minimum(0).maximum(10000))
.prop("recorded_at", S.string().format("date-time"))
.prop("measurements", S.array().items(S.number()).required());

const postHTTPParamsSchema = S.object()
Expand Down Expand Up @@ -108,6 +111,7 @@ const http: FastifyPluginAsync = async (fastify) => {
const latitude = request.body.latitude;
const longitude = request.body.longitude;
const altitude = request.body.altitude;
const recorded_at_string = request.body.recorded_at;

const {
data: updatedSensors,
Expand All @@ -125,7 +129,15 @@ const http: FastifyPluginAsync = async (fastify) => {
}
fastify.log.info(updatedSensors, "updated lat, lon, alt");

const recordedAt = new Date().toISOString();
let recordedAt: string | undefined;
if (recorded_at_string) {
if (isDate(new Date(recorded_at_string))) {
recordedAt = new Date(recorded_at_string).toISOString();
}
} else {
recordedAt = new Date().toISOString();
}

const { data: record, error: recordError } = await fastify.supabase
.from<definitions["records"]>("records")
.insert([
Expand Down

0 comments on commit 83966c2

Please sign in to comment.