All files / src/routes/protected users.ts

89.47% Statements 17/19
70% Branches 7/10
100% Functions 3/3
89.47% Lines 17/19

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 49 50 51 52 5341x   1x 1x   1x 2x 2x   2x       2x   2x             1x         1x 2x   2x   2x                 2x             1x     1x  
import { AppRouteImplementation } from "@ts-rest/express";
import { contract } from "@cooper/ts-rest/src/contract";
import { authenticate } from "@cooper/backend/src/middleware/authenticate";
import guard from "@cooper/backend/src/middleware/guard";
 
const getSelfHandler: AppRouteImplementation<typeof contract.protected.users.getSelf> = async function ({ req, res }) {
  const db = req.app.locals.database;
  const user = db.auth.users.getUser(guard(res.session).username);
 
  Iif (!user) {
    throw user;
  }
 
  const { username, firstName, lastName } = user;
 
  return {
    status: 200,
    body: {
      user: { username, firstName, lastName },
    },
  };
};
export const getSelf = {
  middleware: [authenticate],
  handler: getSelfHandler,
};
 
const getUserHandler: AppRouteImplementation<typeof contract.protected.users.getUser> = async function ({ req }) {
  const db = req.app.locals.database;
 
  const user = db.auth.users.getUser(req.params.username);
 
  Iif (!user) {
    return {
      status: 400,
      body: {
        error: "Invalid username",
      },
    };
  }
 
  return {
    status: 200,
    body: {
      user,
    },
  };
};
export const getUser = {
  middleware: [authenticate],
  handler: getUserHandler,
};