All files / apps/web/src/handlers/user-settings get.ts

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

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          7x       7x             3x 3x           3x    
import type { DB } from "~db/types.gen";
import { authProcedure } from "~web/handlers/trpc";
 
type Settings = Omit<DB["userSettings"], "userId" | "updatedAt">;
 
export const DEFAULT_ACCOUNT_SETTINGS: Settings = {
	manualAcceptDebts: false,
};
 
export const procedure = authProcedure
	.meta({
		title: "Get user settings",
		description:
			"Returns the current  user's settings, falling back to defaults if none are set.",
	})
	.query(async ({ ctx }) => {
		const { database } = ctx;
		const user = await database
			.selectFrom("userSettings")
			.select(["userSettings.manualAcceptDebts"])
			.where("userSettings.userId", "=", ctx.auth.userId)
			.limit(1)
			.executeTakeFirst();
		return user || DEFAULT_ACCOUNT_SETTINGS;
	});