add user route

This commit is contained in:
2025-02-05 16:22:40 +05:30
parent 2d62e9712b
commit 42d68615d2
3 changed files with 42 additions and 3 deletions

View File

@@ -17,6 +17,25 @@ export async function createUserHandler(
}
}
export async function getCurrentUserHandler(
req: FastifyRequest,
res: FastifyReply
) {
if (req.user.type !== "user") {
return res.code(400).send();
}
try {
const user = await getUser(req.user.userId);
if (user == null)
return res.code(404).send({ error: "resource not found" });
return res.code(200).send(user);
} catch (err) {
return err;
}
}
export async function getUserHandler(req: FastifyRequest, res: FastifyReply) {
const { userId } = req.params as { userId: string };

View File

@@ -1,5 +1,9 @@
import { FastifyInstance } from "fastify";
import { createUserHandler, getUserHandler } from "./user.controller";
import {
createUserHandler,
getCurrentUserHandler,
getUserHandler,
} from "./user.controller";
import { $user } from "./user.schema";
export default async function userRoutes(fastify: FastifyInstance) {
@@ -18,6 +22,18 @@ export default async function userRoutes(fastify: FastifyInstance) {
createUserHandler
);
fastify.get(
"/me",
{
schema: {
response: {
200: $user("createUserResponse"),
},
},
},
getCurrentUserHandler
);
fastify.get(
"/:userId",
{

View File

@@ -1,3 +1,4 @@
import mongoose from "mongoose";
import { generateId } from "../utils/id";
import { CreateUserInput, UpdateUserInput, userModel } from "./user.schema";
@@ -14,10 +15,13 @@ export async function createUser(input: CreateUserInput, tenantId: string) {
}
export async function getUser(userId: string) {
const user = await userModel.findOne({
if (mongoose.Types.ObjectId.isValid(userId)) {
return await userModel.findById(userId);
}
return await userModel.findOne({
$and: [{ pid: userId }],
});
return user;
}
export async function getUserByEmail(email: string) {