All files / apps/web/src/handlers/currency rates.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29              6x                         8x 1x         7x    
import { TRPCError } from "@trpc/server";
import { z } from "zod";
 
import { authProcedure } from "~web/handlers/trpc";
import { currencyCodeSchema } from "~web/handlers/validation";
import { getExchangeRates } from "~web/providers/exchange-rate";
 
export const procedure = authProcedure
	.meta({
		title: "Get exchange rates",
		description:
			"Returns exchange rates from a given currency to a list of other currencies.",
	})
	.input(
		z.strictObject({
			from: currencyCodeSchema,
			to: currencyCodeSchema.array().min(1),
		}),
	)
	.query(async ({ ctx, input }) => {
		if (input.to.includes(input.from)) {
			throw new TRPCError({
				code: "BAD_REQUEST",
				message: 'Currency code "from" and "to" must be different',
			});
		}
		return getExchangeRates(ctx, input.from, input.to);
	});