Initial Commit
This commit is contained in:
35
src/user/user.controller.ts
Normal file
35
src/user/user.controller.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { createUser, getUser } from "./user.service";
|
||||
import { CreateUserInput } from "./user.schema";
|
||||
|
||||
export async function createUserHandler(
|
||||
req: FastifyRequest<{
|
||||
Body: CreateUserInput;
|
||||
}>,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const body = req.body;
|
||||
|
||||
try {
|
||||
const user = await createUser(body);
|
||||
return res.code(201).send(user);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserHandler(
|
||||
req: FastifyRequest<{ Params: { userId: string } }>,
|
||||
res: FastifyReply
|
||||
) {
|
||||
const { userId } = req.params;
|
||||
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (user == null) return res.code(404).send({ error: "user not found" });
|
||||
|
||||
return res.code(200).send(user);
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
30
src/user/user.route.ts
Normal file
30
src/user/user.route.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { createUserHandler, getUserHandler } from "./user.controller";
|
||||
import { $user } from "./user.schema";
|
||||
|
||||
export default async function userRoutes(fastify: FastifyInstance) {
|
||||
fastify.post(
|
||||
"/",
|
||||
{
|
||||
schema: {
|
||||
body: $user("createUserInput"),
|
||||
response: {
|
||||
201: $user("createUserResponse"),
|
||||
},
|
||||
},
|
||||
},
|
||||
createUserHandler
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
"/:userId",
|
||||
{
|
||||
schema: {
|
||||
response: {
|
||||
200: $user("createUserResponse"),
|
||||
},
|
||||
},
|
||||
},
|
||||
getUserHandler
|
||||
);
|
||||
}
|
||||
56
src/user/user.schema.ts
Normal file
56
src/user/user.schema.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { buildJsonSchemas } from "fastify-zod";
|
||||
import mongoose from "mongoose";
|
||||
import { z } from "zod";
|
||||
|
||||
export const userModel = mongoose.model(
|
||||
"user",
|
||||
new mongoose.Schema({
|
||||
tenantId: String,
|
||||
pid: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
email: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
avatar: String,
|
||||
status: String,
|
||||
createdAt: Date,
|
||||
createdBy: mongoose.Types.ObjectId,
|
||||
lastLogin: Date,
|
||||
})
|
||||
);
|
||||
|
||||
const userCore = {
|
||||
firstName: z.string().max(30),
|
||||
lastName: z.string().max(30),
|
||||
email: z
|
||||
.string({
|
||||
required_error: "Email is required",
|
||||
invalid_type_error: "Email must be a valid string",
|
||||
})
|
||||
.email(),
|
||||
avatar: z.string().url().optional(),
|
||||
};
|
||||
|
||||
const createUserInput = z.object({
|
||||
...userCore,
|
||||
});
|
||||
|
||||
const createUserResponse = z.object({
|
||||
pid: z.string().cuid2(),
|
||||
...userCore,
|
||||
});
|
||||
|
||||
export type CreateUserInput = z.infer<typeof createUserInput>;
|
||||
|
||||
export const { schemas: userSchemas, $ref: $user } = buildJsonSchemas(
|
||||
{
|
||||
createUserInput,
|
||||
createUserResponse,
|
||||
},
|
||||
{ $id: "user" }
|
||||
);
|
||||
18
src/user/user.service.ts
Normal file
18
src/user/user.service.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { generateId } from "../utils/id";
|
||||
import { CreateUserInput, userModel } from "./user.schema";
|
||||
|
||||
export async function createUser(input: CreateUserInput) {
|
||||
const user = await userModel.create({
|
||||
tenantId: "abc",
|
||||
pid: generateId(),
|
||||
createdAt: new Date(),
|
||||
...input,
|
||||
});
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function getUser(userId: string) {
|
||||
const user = await userModel.findOne({ pid: userId });
|
||||
return user;
|
||||
}
|
||||
Reference in New Issue
Block a user