Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for updating action attempt status to pending via /_fake/update_action_attempt #209

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading