|
1 | | -import type { SocketStream } from "@fastify/websocket"; |
2 | 1 | import { type Static, Type } from "@sinclair/typebox"; |
3 | 2 | import type { FastifyInstance } from "fastify"; |
4 | 3 | import { StatusCodes } from "http-status-codes"; |
5 | 4 | import { TransactionDB } from "../../../shared/db/transactions/db"; |
6 | | -import { logger } from "../../../shared/utils/logger"; |
7 | 5 | import { createCustomError } from "../../middleware/error"; |
8 | 6 | import { standardResponseSchema } from "../../schemas/shared-api-schemas"; |
9 | 7 | import { |
10 | 8 | TransactionSchema, |
11 | 9 | toTransactionSchema, |
12 | 10 | } from "../../schemas/transaction"; |
13 | | -import { |
14 | | - findOrAddWSConnectionInSharedState, |
15 | | - formatSocketMessage, |
16 | | - getStatusMessageAndConnectionStatus, |
17 | | - onClose, |
18 | | - onError, |
19 | | - onMessage, |
20 | | -} from "../../utils/websocket"; |
21 | 11 |
|
22 | 12 | // INPUT |
23 | 13 | const requestSchema = Type.Object({ |
@@ -62,7 +52,7 @@ responseBodySchema.example = { |
62 | 52 | }, |
63 | 53 | }; |
64 | 54 |
|
65 | | -export async function checkTxStatus(fastify: FastifyInstance) { |
| 55 | +export async function getTransactionStatusRoute(fastify: FastifyInstance) { |
66 | 56 | fastify.route<{ |
67 | 57 | Params: Static<typeof requestSchema>; |
68 | 58 | Reply: Static<typeof responseBodySchema>; |
@@ -96,41 +86,51 @@ export async function checkTxStatus(fastify: FastifyInstance) { |
96 | 86 | result: toTransactionSchema(transaction), |
97 | 87 | }); |
98 | 88 | }, |
99 | | - wsHandler: async (connection: SocketStream, request) => { |
100 | | - const { queueId } = request.params; |
| 89 | + }); |
| 90 | +} |
101 | 91 |
|
102 | | - findOrAddWSConnectionInSharedState(connection, queueId, request); |
| 92 | +// An alterate route that accepts the queueId as a query param. |
| 93 | +export async function getTransactionStatusQueryParamRoute( |
| 94 | + fastify: FastifyInstance, |
| 95 | +) { |
| 96 | + fastify.route<{ |
| 97 | + Querystring: Static<typeof requestSchema>; |
| 98 | + Reply: Static<typeof responseBodySchema>; |
| 99 | + }>({ |
| 100 | + method: "GET", |
| 101 | + url: "/transaction/status", |
| 102 | + schema: { |
| 103 | + summary: "Get transaction status", |
| 104 | + description: "Get the status for a transaction request.", |
| 105 | + tags: ["Transaction"], |
| 106 | + operationId: "status", |
| 107 | + querystring: requestSchema, |
| 108 | + response: { |
| 109 | + ...standardResponseSchema, |
| 110 | + [StatusCodes.OK]: responseBodySchema, |
| 111 | + }, |
| 112 | + }, |
| 113 | + handler: async (request, reply) => { |
| 114 | + const { queueId } = request.query; |
| 115 | + if (!queueId) { |
| 116 | + throw createCustomError( |
| 117 | + "Queue ID is required.", |
| 118 | + StatusCodes.BAD_REQUEST, |
| 119 | + "QUEUE_ID_REQUIRED", |
| 120 | + ); |
| 121 | + } |
103 | 122 |
|
104 | 123 | const transaction = await TransactionDB.get(queueId); |
105 | | - const returnData = transaction ? toTransactionSchema(transaction) : null; |
106 | | - |
107 | | - const { message, closeConnection } = |
108 | | - await getStatusMessageAndConnectionStatus(returnData); |
109 | | - |
110 | | - connection.socket.send(await formatSocketMessage(returnData, message)); |
111 | | - |
112 | | - if (closeConnection) { |
113 | | - connection.socket.close(); |
114 | | - return; |
| 124 | + if (!transaction) { |
| 125 | + throw createCustomError( |
| 126 | + "Transaction not found.", |
| 127 | + StatusCodes.BAD_REQUEST, |
| 128 | + "TRANSACTION_NOT_FOUND", |
| 129 | + ); |
115 | 130 | } |
116 | 131 |
|
117 | | - connection.socket.on("error", (error) => { |
118 | | - logger({ |
119 | | - service: "websocket", |
120 | | - level: "error", |
121 | | - message: "Websocket error", |
122 | | - error, |
123 | | - }); |
124 | | - |
125 | | - onError(error, connection, request); |
126 | | - }); |
127 | | - |
128 | | - connection.socket.on("message", async (_message, _isBinary) => { |
129 | | - onMessage(connection, request); |
130 | | - }); |
131 | | - |
132 | | - connection.socket.on("close", () => { |
133 | | - onClose(connection, request); |
| 132 | + reply.status(StatusCodes.OK).send({ |
| 133 | + result: toTransactionSchema(transaction), |
134 | 134 | }); |
135 | 135 | }, |
136 | 136 | }); |
|
0 commit comments