Skip to content

Commit

Permalink
done,need to test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewjeska committed Aug 23, 2023
1 parent 4a46c9d commit 0bab56c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export interface DatabaseMethods {
original_access_code_id: string
pulled_backup_access_code_id: string
}) => void
findClimateSettingSchedule: (params: {
climate_setting_schedule_id: string
device_id?: string
}) => ClimateSettingSchedule | undefined
addClimateSettingSchedule: (
params: {
workspace_id: string
Expand All @@ -115,6 +119,7 @@ export interface DatabaseMethods {
name: string
} & Partial<ClimateSetting>
) => ClimateSettingSchedule
deleteClimateSettingSchedule: (params: ClimateSettingSchedule) => void

update: (t?: number) => void
}
Expand Down
39 changes: 39 additions & 0 deletions src/lib/database/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,44 @@ const initializer = immer<Database>((set, get) => ({
return new_climate_setting_schedule
},

findClimateSettingSchedule(params) {
return get().climate_setting_schedules.find((climate_setting_schedule) => {
const is_target_id =
climate_setting_schedule.climate_setting_schedule_id ===
params.climate_setting_schedule_id
const is_target_device =
climate_setting_schedule.device_id === params.device_id

return is_target_id || is_target_device
})
},

deleteClimateSettingSchedule(params) {
const target = get().climate_setting_schedules.find(
(climate_setting_schedule) =>
climate_setting_schedule.climate_setting_schedule_id ===
params.climate_setting_schedule_id
)
if (target == null) {
throw new Error(
"Could not find climate_setting_schedule with climate_setting_schedule_id"
)
}

set({
climate_setting_schedules: [
...get().climate_setting_schedules.filter(
(climate_setting_schedule) => {
const is_target =
climate_setting_schedule.climate_setting_schedule_id ===
target.climate_setting_schedule_id

return !is_target
}
),
],
})
},

update() {},
}))
36 changes: 36 additions & 0 deletions src/pages/api/thermostats/climate_setting_schedules/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NotFoundException } from "nextlove"
import { z } from "zod"

import { withRouteSpec } from "lib/middleware/with-route-spec.ts"

const json_body = z.object({
climate_setting_schedule_id: z.string(),
device_id: z.string().optional(),
sync: z.boolean().default(false),
})

export default withRouteSpec({
auth: "cst_ak_pk",
methods: ["POST", "DELETE"],
jsonBody: json_body,
jsonResponse: z.object({}),
} as const)(async (req, res) => {
const { climate_setting_schedule_id, device_id } = req.body

const climate_setting_schedule = req.db.findClimateSettingSchedule({
climate_setting_schedule_id,
device_id,
})

if (climate_setting_schedule == null) {
throw new NotFoundException({
type: "climate_setting_schedule_not_found",
message: `Could not find an climate_setting_schedule with device_id or climate_setting_schedule_id`,
data: { device_id, climate_setting_schedule_id },
})
}

req.db.deleteClimateSettingSchedule(climate_setting_schedule)

res.status(200).json({})
})
33 changes: 33 additions & 0 deletions src/pages/api/thermostats/climate_setting_schedules/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NotFoundException } from "nextlove"
import { z } from "zod"

import { climate_setting_schedule } from "lib/zod/index.ts"

import { withRouteSpec } from "lib/middleware/with-route-spec.ts"

export default withRouteSpec({
auth: "cst_ak_pk",
methods: ["GET", "POST"],
commonParams: z.object({
climate_setting_schedule_id: z.string(),
device_id: z.string().optional(),
}),
jsonResponse: z.object({
climate_setting_schedule,
}),
} as const)(async (req, res) => {
const { device_id, climate_setting_schedule_id } = req.commonParams

const climate_setting_schedule = req.db.findClimateSettingSchedule({
climate_setting_schedule_id,
device_id,
})

if (climate_setting_schedule == null) {
throw new NotFoundException({
type: "not_found",
message: "Climate Setting Schedule Not Found",
})
}
res.status(200).json({ climate_setting_schedule })
})

0 comments on commit 0bab56c

Please sign in to comment.