All files / src/routes me.ts

87.5% Statements 14/16
75% Branches 9/12
100% Functions 3/3
87.5% Lines 14/16

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                7x 1x   1x 1x       1x   1x       7x       2x 2x   2x 1x     1x           1x       1x        
import type { FastifyInstance } from "fastify";
import { eq } from "drizzle-orm";
import { db } from "../db/index.js";
import { users } from "../db/schema.js";
import { getUserAccounts } from "../lib/accounts.js";
 
export async function meRoutes(app: FastifyInstance) {
  // GET /me — return current user with accounts (authenticated)
  app.get("/me", { preHandler: [app.authenticate] }, async (request, reply) => {
    const userId = request.user.sub;
 
    const [user] = await db.select().from(users).where(eq(users.id, userId));
    Iif (!user) {
      return reply.status(404).send({ error: "User not found" });
    }
 
    const userAccounts = await getUserAccounts(userId);
 
    return { ...user, accounts: userAccounts };
  });
 
  // PATCH /me — update current user's profile (authenticated)
  app.patch<{ Body: { email?: string; name?: string } }>(
    "/me",
    { preHandler: [app.authenticate] },
    async (request, reply) => {
      const userId = request.user.sub;
      const { email, name } = request.body;
 
      if (!email && !name) {
        return reply.status(400).send({ error: "Nothing to update" });
      }
 
      const [user] = await db
        .update(users)
        .set({ ...(email && { email }), ...(name && { name }), updatedAt: new Date() })
        .where(eq(users.id, userId))
        .returning();
 
      Iif (!user) {
        return reply.status(404).send({ error: "User not found" });
      }
 
      return user;
    },
  );
}