-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
/_fake/update_action_attempt
(#207)
* Add /_fake/update_action_attempt, update action_attempt zod schema * Add test * ci: Format code * Don't allow updating action_type * ci: Generate code * Update the endpoint to accept a union of successful and failed action attempt payload, update the test * Format --------- Co-authored-by: Seam Bot <[email protected]>
- Loading branch information
1 parent
f8b6b00
commit e6b60f6
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NotFoundException } from "nextlove" | ||
import { z } from "zod" | ||
|
||
import { withRouteSpec } from "lib/middleware/with-route-spec.ts" | ||
|
||
export default withRouteSpec({ | ||
auth: "none", | ||
methods: ["PATCH", "POST"], | ||
jsonBody: z.union([ | ||
z.object({ | ||
status: z.literal("success"), | ||
action_attempt_id: z.string(), | ||
result: z.any(), | ||
}), | ||
z.object({ | ||
status: z.literal("error"), | ||
action_attempt_id: z.string(), | ||
error: z.object({ | ||
type: z.string(), | ||
message: z.string(), | ||
}), | ||
}), | ||
]), | ||
jsonResponse: z.object({}), | ||
} as const)(async (req, res) => { | ||
const { action_attempt_id, ...action_attempt_update_payload } = req.body | ||
|
||
const action_attempt = req.db.action_attempts.find( | ||
(a) => a.action_attempt_id === action_attempt_id, | ||
) | ||
|
||
if (action_attempt == null) { | ||
throw new NotFoundException({ | ||
type: "action_attempt_not_found", | ||
message: "Action attempt not found", | ||
}) | ||
} | ||
|
||
req.db.updateActionAttempt({ | ||
action_attempt_id, | ||
...action_attempt_update_payload, | ||
}) | ||
|
||
res.status(200).json({}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import test, { type ExecutionContext } from "ava" | ||
|
||
import { | ||
getTestServer, | ||
type SimpleAxiosError, | ||
} from "fixtures/get-test-server.ts" | ||
|
||
test("PATCH /_fake/update_action_attempt", async (t: ExecutionContext) => { | ||
const { axios, db, seed } = await getTestServer(t) | ||
|
||
const { | ||
data: { | ||
action_attempt: { action_attempt_id }, | ||
}, | ||
} = await axios.post( | ||
"/locks/lock_door", | ||
{ | ||
device_id: seed.ws2.device1_id, | ||
}, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${seed.ws2.cst}`, | ||
}, | ||
}, | ||
) | ||
|
||
const payload = { | ||
action_attempt_id, | ||
status: "success" as const, | ||
result: {}, | ||
} | ||
|
||
const { status } = await axios.patch("/_fake/update_action_attempt", payload) | ||
|
||
t.is(status, 200) | ||
|
||
const updated_action_attempt = db.action_attempts.find( | ||
(a) => a.action_attempt_id === action_attempt_id, | ||
) | ||
|
||
t.truthy(updated_action_attempt) | ||
t.is(updated_action_attempt?.status, "success") | ||
t.deepEqual(updated_action_attempt?.result, {}) | ||
}) | ||
|
||
test("PATCH /_fake/update_action_attempt - invalid action_attempt_id", async (t: ExecutionContext) => { | ||
const { axios } = await getTestServer(t) | ||
|
||
const payload = { | ||
action_attempt_id: "non-existent-action-attempt-id", | ||
status: "success" as const, | ||
result: {}, | ||
} | ||
|
||
const err = await t.throwsAsync<SimpleAxiosError>( | ||
async () => await axios.patch("/_fake/update_action_attempt", payload), | ||
) | ||
|
||
t.is(err?.status, 404) | ||
}) |