|
| 1 | +import { Type, type Static } from "@sinclair/typebox"; |
| 2 | +import type { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { updateWalletCredential } from "../../../shared/db/wallet-credentials/update-wallet-credential"; |
| 5 | +import { WalletCredentialsError } from "../../../shared/db/wallet-credentials/get-wallet-credential"; |
| 6 | +import { createCustomError } from "../../middleware/error"; |
| 7 | +import { standardResponseSchema } from "../../schemas/shared-api-schemas"; |
| 8 | + |
| 9 | +const ParamsSchema = Type.Object({ |
| 10 | + id: Type.String({ |
| 11 | + description: "The ID of the wallet credential to update.", |
| 12 | + }), |
| 13 | +}); |
| 14 | + |
| 15 | +const requestBodySchema = Type.Object({ |
| 16 | + label: Type.Optional(Type.String()), |
| 17 | + isDefault: Type.Optional( |
| 18 | + Type.Boolean({ |
| 19 | + description: |
| 20 | + "Whether this credential should be set as the default for its type. Only one credential can be default per type.", |
| 21 | + }), |
| 22 | + ), |
| 23 | + entitySecret: Type.Optional( |
| 24 | + Type.String({ |
| 25 | + description: |
| 26 | + "32-byte hex string. Consult https://developers.circle.com/w3s/entity-secret-management to create and register an entity secret.", |
| 27 | + pattern: "^[0-9a-fA-F]{64}$", |
| 28 | + }), |
| 29 | + ), |
| 30 | +}); |
| 31 | + |
| 32 | +const responseSchema = Type.Object({ |
| 33 | + result: Type.Object({ |
| 34 | + id: Type.String(), |
| 35 | + type: Type.String(), |
| 36 | + label: Type.Union([Type.String(), Type.Null()]), |
| 37 | + isDefault: Type.Union([Type.Boolean(), Type.Null()]), |
| 38 | + createdAt: Type.String(), |
| 39 | + updatedAt: Type.String(), |
| 40 | + }), |
| 41 | +}); |
| 42 | + |
| 43 | +responseSchema.example = { |
| 44 | + result: { |
| 45 | + id: "123e4567-e89b-12d3-a456-426614174000", |
| 46 | + type: "circle", |
| 47 | + label: "My Updated Circle Credential", |
| 48 | + isDefault: true, |
| 49 | + createdAt: "2024-01-01T00:00:00.000Z", |
| 50 | + updatedAt: "2024-01-01T00:00:00.000Z", |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +export async function updateWalletCredentialRoute(fastify: FastifyInstance) { |
| 55 | + fastify.route<{ |
| 56 | + Params: Static<typeof ParamsSchema>; |
| 57 | + Body: Static<typeof requestBodySchema>; |
| 58 | + Reply: Static<typeof responseSchema>; |
| 59 | + }>({ |
| 60 | + method: "PUT", |
| 61 | + url: "/wallet-credentials/:id", |
| 62 | + schema: { |
| 63 | + summary: "Update wallet credential", |
| 64 | + description: |
| 65 | + "Update a wallet credential's label, default status, and entity secret.", |
| 66 | + tags: ["Wallet Credentials"], |
| 67 | + operationId: "updateWalletCredential", |
| 68 | + params: ParamsSchema, |
| 69 | + body: requestBodySchema, |
| 70 | + response: { |
| 71 | + ...standardResponseSchema, |
| 72 | + [StatusCodes.OK]: responseSchema, |
| 73 | + }, |
| 74 | + }, |
| 75 | + handler: async (req, reply) => { |
| 76 | + try { |
| 77 | + const credential = await updateWalletCredential({ |
| 78 | + id: req.params.id, |
| 79 | + label: req.body.label, |
| 80 | + isDefault: req.body.isDefault, |
| 81 | + entitySecret: req.body.entitySecret, |
| 82 | + }); |
| 83 | + |
| 84 | + reply.status(StatusCodes.OK).send({ |
| 85 | + result: { |
| 86 | + id: credential.id, |
| 87 | + type: credential.type, |
| 88 | + label: credential.label, |
| 89 | + isDefault: credential.isDefault, |
| 90 | + createdAt: credential.createdAt.toISOString(), |
| 91 | + updatedAt: credential.updatedAt.toISOString(), |
| 92 | + }, |
| 93 | + }); |
| 94 | + } catch (e) { |
| 95 | + if (e instanceof WalletCredentialsError) { |
| 96 | + throw createCustomError( |
| 97 | + e.message, |
| 98 | + StatusCodes.NOT_FOUND, |
| 99 | + "WALLET_CREDENTIAL_NOT_FOUND", |
| 100 | + ); |
| 101 | + } |
| 102 | + throw e; |
| 103 | + } |
| 104 | + }, |
| 105 | + }); |
| 106 | +} |
0 commit comments