-
Notifications
You must be signed in to change notification settings - Fork 3
✨ server: add wallet provisioning endpoint #949
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@exactly/server": patch | ||
| --- | ||
|
|
||
| ✨ add wallet provisioning endpoint |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,17 @@ import { Address } from "@exactly/common/validation"; | |
| import database, { cards, credentials } from "../database"; | ||
| import auth from "../middleware/auth"; | ||
| import { sendPushNotification } from "../utils/onesignal"; | ||
| import { autoCredit, createCard, getCard, getPIN, getSecrets, getUser, setPIN, updateCard } from "../utils/panda"; | ||
| import { | ||
| autoCredit, | ||
| createCard, | ||
| getCard, | ||
| getPIN, | ||
| getProcessorDetails, | ||
| getSecrets, | ||
| getUser, | ||
| setPIN, | ||
| updateCard, | ||
| } from "../utils/panda"; | ||
| import { addCapita, deriveAssociateId } from "../utils/pax"; | ||
| import { getAccount } from "../utils/persona"; | ||
| import { customer } from "../utils/sardine"; | ||
|
|
@@ -77,6 +87,8 @@ const CreatedCardResponse = object({ | |
| productId: pipe(string(), metadata({ examples: ["402"] })), | ||
| }); | ||
|
|
||
| const WalletResponse = object({ cardId: string(), cardSecret: string() }); | ||
|
|
||
| const UpdateCard = union([ | ||
| pipe( | ||
| strictObject({ mode: pipe(number(), integer(), minValue(0), maxValue(MAX_INSTALLMENTS)) }), | ||
|
|
@@ -566,6 +578,57 @@ async function encryptPIN(pin: string) { | |
| if (!mutex.isLocked()) mutexes.delete(credentialId); | ||
| }); | ||
| }, | ||
| ) | ||
| .get( | ||
| "/wallet", | ||
| auth(), | ||
| describeRoute({ | ||
| summary: "Get wallet provisioning credentials", | ||
| tags: ["Card"], | ||
| security: [{ credentialAuth: [] }], | ||
| validateResponse: true, | ||
| responses: { | ||
| 200: { | ||
| description: "Wallet provisioning credentials", | ||
| content: { | ||
| "application/json": { | ||
| schema: resolver(WalletResponse, { errorMode: "ignore" }), | ||
| }, | ||
| }, | ||
| }, | ||
| 403: { | ||
| description: "Forbidden", | ||
| content: { | ||
| "application/json": { schema: resolver(object({ code: literal("no panda") }), { errorMode: "ignore" }) }, | ||
| }, | ||
| }, | ||
| 404: { | ||
| description: "Not found", | ||
| content: { | ||
| "application/json": { schema: resolver(object({ code: literal("no card") }), { errorMode: "ignore" }) }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| async (c) => { | ||
| const { credentialId } = c.req.valid("cookie"); | ||
| const credential = await database.query.credentials.findFirst({ | ||
| where: eq(credentials.id, credentialId), | ||
| columns: { pandaId: true, account: true }, | ||
| with: { cards: { columns: { id: true }, where: inArray(cards.status, ["ACTIVE", "FROZEN"]) } }, | ||
| }); | ||
| if (!credential) return c.json({ code: "no credential" }, 500); | ||
| setUser({ id: parse(Address, credential.account) }); | ||
| if (!credential.pandaId) return c.json({ code: "no panda" }, 403); | ||
aguxez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!credential.cards[0]) return c.json({ code: "no card" }, 404); | ||
| try { | ||
| const provisioning = await getProcessorDetails(credential.cards[0].id); | ||
| return c.json({ cardId: provisioning.processorCardId, cardSecret: provisioning.timeBasedSecret } satisfies InferOutput<typeof WalletResponse>, 200); | ||
|
Comment on lines
+582
to
+626
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protect
Comment on lines
+615
to
+626
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose the wallet card deterministically. The relation query can return multiple |
||
| } catch (error) { | ||
| if (error instanceof ServiceError && error.status === 404) return c.json({ code: "no card" }, 404); | ||
| throw error; | ||
| } | ||
aguxez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ); | ||
|
|
||
| const CardUUID = pipe(string(), uuid()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document the 500 responses this route already emits.
validateResponseis enabled, but the handler returns500 { code: "no credential" }on Line 620 and can propagate other 5xx failures on Lines 627-629. The OpenAPI block omits 500 entirely, so the contract does not match runtime behavior.🩹 Proposed contract update
responses: { description: "Wallet provisioning credentials", content: { "application/json": { schema: resolver(WalletResponse, { errorMode: "ignore" }), }, }, }, description: "Forbidden", content: { "application/json": { schema: resolver(object({ code: literal("no panda") }), { errorMode: "ignore" }) }, }, }, description: "Not found", content: { "application/json": { schema: resolver(object({ code: literal("no card") }), { errorMode: "ignore" }) }, }, }, + 500: { + description: "Internal server error", + content: { + "application/json": { schema: resolver(object({ code: literal("no credential") }), { errorMode: "ignore" }) }, + }, + }, },Also applies to: 620-629