Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
chore(release): v0.1.1 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalbitresb12 authored Nov 24, 2023
2 parents 7a0ff07 + de1b005 commit 96c06ba
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Enable corepack before running setup-node
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/deploy-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ jobs:
concurrency: ${{ inputs.environment }}
steps:
- uses: actions/checkout@v4
- name: Enable corepack before running setup-node
run: corepack enable
- name: Setup Node ${{ vars.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Enable corepack before running setup-node
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand All @@ -32,11 +34,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Enable corepack before running setup-node
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand All @@ -50,11 +54,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Enable corepack before running setup-node
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/prchecks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Enable corepack before running setup-node
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: ${{ vars.NODE_VERSION }}
cache: 'yarn'
- name: Enable corepack
- name: Ensure corepack is used
run: corepack enable
- name: Install dependencies
run: yarn install --immutable
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plantguard-api",
"version": "0.1.0",
"version": "0.1.1",
"description": "The API backend for PlantGuard, an IoT plant water monitoring system, powered by Cloudflare Workers.",
"author": {
"name": "NexGenius",
Expand Down
2 changes: 1 addition & 1 deletion src/devices/register-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function registerDevice(db: KVNamespace, user: User, req: DeviceReg
const deviceKey = createDeviceKey(user.id, device.id);

await db.put(deviceKey, JSON.stringify(device), { metadata });
await db.put(serialNumberKey, device.userId);
await db.put(serialNumberKey, device.id);

return device;
}
52 changes: 51 additions & 1 deletion src/devices/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
DeviceListQuery,
DeviceListResponse,
DeviceListResponseData,
DeviceMeasurement,
DeviceMeasurementRequest,
DeviceMetadata,
DeviceUpdateRequest,
} from "../models/device";
import { assertModel, parseBody } from "../utils/model-parser";
import { PaginationMetadata } from "../utils/pagination";
import { createAuthenticatedRouter } from "../utils/router";
import { createDeviceKey, createSerialNumberKey, isValidDeviceId, parseDeviceKey } from "./utils";
import { createDeviceKey, createMeasurementKey, createSerialNumberKey, isValidDeviceId, parseDeviceKey } from "./utils";

const router = createAuthenticatedRouter({ base: "/api/v1/devices" });

Expand Down Expand Up @@ -43,6 +45,32 @@ router.get("/", async (request, env): Promise<DeviceListResponse> => {
return { data, pagination };
});

router.post("/measure", async (request, env) => {
const { serialNumber, ...rest } = await parseBody(request, DeviceMeasurementRequest);
const serialNumberKey = createSerialNumberKey(serialNumber);

const deviceId = await env.devices.get(serialNumberKey);
if (!deviceId) {
throw new StatusError(404);
}

// This indirectly checks if the current user is the owner of the device
const deviceKey = createDeviceKey(request.user.id, deviceId);
const device = await env.devices.get(deviceKey);
if (!device) {
throw new StatusError(404);
}

const measurementKey = createMeasurementKey(deviceId);
const measurement: DeviceMeasurement = {
...rest,
date: new Date(),
};
await env.devices.put(measurementKey, JSON.stringify(measurement));

return new Response(undefined, { status: 204 });
});

router.get("/:id", async (request, env): Promise<Device> => {
const { id } = request.params;
if (!isValidDeviceId(id)) {
Expand All @@ -58,6 +86,28 @@ router.get("/:id", async (request, env): Promise<Device> => {
return Device.parse(JSON.parse(device));
});

router.get("/:id/current", async (request, env): Promise<Response | DeviceMeasurement> => {
const { id } = request.params;
if (!isValidDeviceId(id)) {
throw new StatusError(400, "Invalid ID");
}

// This indirectly checks if the current user is the owner of the device
const deviceKey = createDeviceKey(request.user.id, id);
const device = await env.devices.get(deviceKey);
if (!device) {
throw new StatusError(404);
}

const measurementKey = createMeasurementKey(id);
const measurement = await env.devices.get(measurementKey);
if (!measurement) {
return new Response(undefined, { status: 204 });
}

return DeviceMeasurement.parse(JSON.parse(measurement));
});

router.patch("/:id", async (request, env): Promise<Device> => {
const { id } = request.params;
if (!isValidDeviceId(id)) {
Expand Down
6 changes: 6 additions & 0 deletions src/devices/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export function createSerialNumberKey(serialNumber: string): string {
return createKey([serialNumberKeyPrefix, serialNumber]);
}

const measurementKeyPrefix = "measure";

export function createMeasurementKey(deviceId: string): string {
return createKey([measurementKeyPrefix, deviceId]);
}

export function parseDeviceKey(key: string): DeviceKey {
const split = splitKey(key);
if (split[0] !== deviceKeyPrefix) {
Expand Down
14 changes: 13 additions & 1 deletion src/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const DeviceListResponseData = Device.pick({ id: true, name: true });
export type DeviceListResponseData = z.infer<typeof DeviceListResponseData>;

export const DeviceListResponse = z.object({
data: z.array(DeviceMetadata),
data: z.array(DeviceListResponseData),
pagination: PaginationMetadata,
});
export type DeviceListResponse = z.infer<typeof DeviceListResponse>;
Expand Down Expand Up @@ -80,3 +80,15 @@ export const DevicePairConfirmRequest = DevicePairRequest.pick({ code: true }).e
name: Device.shape.name,
});
export type DevicePairConfirmRequest = z.infer<typeof DevicePairConfirmRequest>;

export const DeviceMeasurement = z.object({
temperature: z.number(),
humidity: z.number(),
date: ZodDate,
});
export type DeviceMeasurement = z.infer<typeof DeviceMeasurement>;

export const DeviceMeasurementRequest = DeviceMeasurement.omit({ date: true }).merge(
Device.pick({ serialNumber: true }),
);
export type DeviceMeasurementRequest = z.infer<typeof DeviceMeasurementRequest>;
4 changes: 2 additions & 2 deletions src/pair/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ router.post("/init", async (request, env): Promise<DevicePairInitResponse> => {
const req = await parseBody(request, DevicePairInitRequest);
const serialNumberKey = createSerialNumberKey(req.serialNumber);

const possibleOwner = await env.devices.get(serialNumberKey);
if (possibleOwner) {
const possibleId = await env.devices.get(serialNumberKey);
if (possibleId) {
throw new StatusError(400, "The serial number has already been registered");
}

Expand Down

0 comments on commit 96c06ba

Please sign in to comment.