Skip to content

Commit

Permalink
feat: add GET /defaultTriggers/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucky3028 committed Sep 25, 2023
1 parent 2cf47f3 commit 0e690e4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions apps/auto-run-ac-api/src/api/defaultTriggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SharedEnv } from 'cloudflare-env';
import { defaultTriggersTable as table } from '@/db/schema';
import { z } from 'zod';
import { getUrl, nanoid } from '@/lib';
import { eq } from 'drizzle-orm';

const app = new OpenAPIHono<{ Bindings: SharedEnv & Env; Variables: Variables }>();

Expand Down Expand Up @@ -86,4 +87,51 @@ export const defaultTriggersApi = app

return c.jsonT(response, 201, { Location: `${getUrl(c.req)}/${id}` });
},
)
.openapi(
createRoute({
request: {
params: z.object({ id: z.string() }),
},
responses: {
200: {
description: 'The trigger is found',
content: {
'application/json': {
schema: z.object({ success: z.boolean(), data: z.object({ trigger: defaultTriggerSchema }) }),
},
},
},
404: {
description: 'The trigger is not found',
content: {
'application/json': {
schema: z.object({
success: z.boolean(),
}),
},
},
},
},
method: 'get',
path: '/:id',
}),
async (c) => {
const { id } = c.req.valid('param');
// eslint-disable-next-line @typescript-eslint/naming-convention
const [value, ..._others] = await c.get('db').select().from(table).where(eq(table.id, id));

if (!value) {
return c.jsonT({ success: false }, 404);
}

const trigger = {
...value,
temp: value.triggerTemp,
ac: { mode: value.operationMode, temp: value.settingsTemp },
time: { hour: value.triggerTime.getUTCHours(), minute: value.triggerTime.getUTCMinutes() },
};

return c.jsonT({ success: true, data: { trigger } });
},
);

0 comments on commit 0e690e4

Please sign in to comment.