diff --git a/docs/openapi-spec.yaml b/docs/openapi-spec.yaml index a1c1caf..0fa5fef 100644 --- a/docs/openapi-spec.yaml +++ b/docs/openapi-spec.yaml @@ -208,7 +208,7 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/Channel" + $ref: "#/components/schemas/Channel" /channels-from-sender: get: summary: Get channels by sender @@ -235,6 +235,11 @@ paths: /channels-from-receiver: get: summary: Get channels by receiver + description: | + Example `curl` command: + + ```bash + curl http://localhost:3000/channels-from-receiver\?receiverAddress\=addr_test1qz6fcx7lay9czf838z4m0ppkyzzgspp9kz5uwede3ry4fmxmkhwgrz8c7pyr8dmqvcmf7sa2dagvvtfmw5585w847jgqwrhsv8 parameters: - name: receiverAddress in: query diff --git a/src/api/entry-points/routes.ts b/src/api/entry-points/routes.ts index 5fb14ad..7fe62cf 100644 --- a/src/api/entry-points/routes.ts +++ b/src/api/entry-points/routes.ts @@ -7,11 +7,13 @@ import { openChannel } from "../../offchain/builders/open-channel.ts"; import { updateChannel } from "../../offchain/builders/update-channel.ts"; import { getAllChannels } from "../../offchain/queries/all-channels.ts"; import { getChannelById } from "../../offchain/queries/channel-by-id.ts"; +import { getChannelsFromReceiver } from "../../offchain/queries/channels-from-receiver.ts"; import { getChannelsFromSender } from "../../offchain/queries/channels-from-sender.ts"; import { - CloseChannelSchema, ClaimChannelSchema, + CloseChannelSchema, GetChannelsByIDSchema, + GetChannelsFromReceiver, GetChannelsFromSender, OpenChannelSchema, UpdateChannelSchema, @@ -241,4 +243,38 @@ export const setRoutes = async (lucid: Lucid, app: e.Application) => { } } }); + + /** + * Get channel from receiver + */ + app.get( + Routes.CHANNELS_FROM_RECEIVER, + async (req: Request, res: Response) => { + logger.info("handling request", Routes.CHANNELS_FROM_RECEIVER); + try { + const { receiverAddress } = GetChannelsFromReceiver.parse(req.query); + const channelsFromSender = await getChannelsFromReceiver( + lucid, + receiverAddress, + ); + res.status(200).json(serializedResult(channelsFromSender)); + logger.info( + `channel from receiver with address ${receiverAddress} found`, + Routes.CHANNELS_FROM_RECEIVER, + ); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (error: any) { + if (error instanceof z.ZodError) { + res.status(400).json({ error: error.errors }); + logger.error(`bad request: ${error}`, Routes.CHANNELS_FROM_RECEIVER); + } else { + res.status(500).json({ error: `${getErrorString(error.stack)}` }); + logger.error( + `internal server error: ${error.stack}`, + Routes.CHANNELS_FROM_RECEIVER, + ); + } + } + }, + ); }; diff --git a/src/shared/api-types.ts b/src/shared/api-types.ts index 2634191..1958c9d 100644 --- a/src/shared/api-types.ts +++ b/src/shared/api-types.ts @@ -150,6 +150,10 @@ export const GetChannelsFromSender = z.object({ senderAddress: addressSchema, }); +export const GetChannelsFromReceiver = z.object({ + receiverAddress: addressSchema, +}); + export type channelIdType = z.infer; export type OpenChannelParams = z.infer; export type UpdateChannelParams = z.infer; @@ -159,3 +163,6 @@ export type BuildMessageParams = z.infer; export type GetChannelsByIDParams = z.infer; export type GetChannelsFromSenderParams = z.infer; +export type GetChannelsFromReceiverParams = z.infer< + typeof GetChannelsFromReceiver +>;