Skip to content

Commit

Permalink
feat: add support for updating action attempt status to pending via…
Browse files Browse the repository at this point in the history
… `/_fake/update_action_attempt` (#209)

* Adjust /_fake/update_action_attempt endpoint to support updating action attempt to pending

* Update the test

* Lint

* Minor test fix
  • Loading branch information
andrii-balitskyi authored Jul 18, 2024
1 parent 7a8d6cc commit 2fd5940
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
57 changes: 40 additions & 17 deletions src/pages/api/_fake/update_action_attempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@ import { z } from "zod"

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

const action_attempt_update_schema = 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(),
}),
}),
z.object({
status: z.literal("pending"),
action_attempt_id: z.string(),
}),
])

type ActionAttemptUpdateSchema = z.infer<typeof action_attempt_update_schema>

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(),
}),
}),
]),
jsonBody: action_attempt_update_schema,
jsonResponse: z.object({}),
} as const)(async (req, res) => {
const { action_attempt_id, ...action_attempt_update_payload } = req.body
const { action_attempt_id } = req.body

const action_attempt = req.db.action_attempts.find(
(a) => a.action_attempt_id === action_attempt_id,
Expand All @@ -36,10 +44,25 @@ export default withRouteSpec({
})
}

const update_payload = createUpdatePayload(req.body)

req.db.updateActionAttempt({
action_attempt_id,
...action_attempt_update_payload,
...update_payload,
})

res.status(200).json({})
})

function createUpdatePayload(body: ActionAttemptUpdateSchema) {
const { status } = body

switch (status) {
case "success":
return { status, result: body.result, error: null }
case "error":
return { status, error: body.error, result: null }
case "pending":
return { status, result: null, error: null }
}
}
4 changes: 4 additions & 0 deletions src/route-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export type Routes = {
message: string
}
}
| {
status: "pending"
action_attempt_id: string
}
commonParams: {}
formData: {}
jsonResponse: {
Expand Down
44 changes: 36 additions & 8 deletions test/api/_fake/update_action_attempt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,51 @@ test("PATCH /_fake/update_action_attempt", async (t: ExecutionContext) => {
},
)

const payload = {
// status: success
await axios.patch("/_fake/update_action_attempt", {
action_attempt_id,
status: "success" as const,
status: "success",
result: {},
}

const { status } = await axios.patch("/_fake/update_action_attempt", payload)

t.is(status, 200)
})

const updated_action_attempt = db.action_attempts.find(
let 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, {})

// status: error
const action_attempt_error = { message: "error message", type: "error type" }

await axios.patch("/_fake/update_action_attempt", {
action_attempt_id,
status: "error",
error: action_attempt_error,
})

updated_action_attempt = db.action_attempts.find(
(a) => a.action_attempt_id === action_attempt_id,
)

t.is(updated_action_attempt?.status, "error")
t.is(updated_action_attempt?.result, null)
t.deepEqual(updated_action_attempt?.error, action_attempt_error)

// status: pending
await axios.patch("/_fake/update_action_attempt", {
action_attempt_id,
status: "pending",
})

updated_action_attempt = db.action_attempts.find(
(a) => a.action_attempt_id === action_attempt_id,
)

t.is(updated_action_attempt?.status, "pending")
t.is(updated_action_attempt?.result, null)
t.is(updated_action_attempt?.error, null)
})

test("PATCH /_fake/update_action_attempt - invalid action_attempt_id", async (t: ExecutionContext) => {
Expand Down

0 comments on commit 2fd5940

Please sign in to comment.