Initial Commit

This commit is contained in:
2024-12-19 13:54:15 +05:30
commit 970a972b11
17 changed files with 1487 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { FastifyRequest, FastifyReply } from "fastify";
import { CreateOrgInput } from "./organization.schema";
import { createOrg } from "./organization.service";
export async function createOrgHandler(
req: FastifyRequest<{ Body: CreateOrgInput }>,
res: FastifyReply
) {
const input = req.body;
try {
const org = await createOrg(input);
return res.code(201).send(org);
} catch (err) {
return err;
}
}

View File

@@ -0,0 +1,18 @@
import { FastifyInstance } from "fastify";
import { $org } from "./organization.schema";
import { createOrgHandler } from "./organization.controller";
export default function organizationRoutes(fastify: FastifyInstance) {
fastify.post(
"/",
{
schema: {
body: $org("createOrgInput"),
response: {
201: $org("createOrgResponse"),
},
},
},
createOrgHandler
);
}

View File

@@ -0,0 +1,52 @@
import { buildJsonSchemas } from "fastify-zod";
import mongoose from "mongoose";
import { z } from "zod";
export const orgModel = mongoose.model(
"organization",
new mongoose.Schema({
tenantId: String,
pid: {
type: String,
unique: true,
},
name: String,
domain: {
type: String,
unique: true,
},
avatar: String,
type: String,
status: String,
createdAt: Date,
createdBy: mongoose.Types.ObjectId,
})
);
const orgCore = {
name: z.string().max(30),
domain: z.string().max(30),
avatar: z.string().url().optional(),
type: z.enum(["county", "builder"], {
message: "Must be county or builder",
}),
};
const createOrgInput = z.object({
...orgCore,
});
const createOrgResponse = z.object({
pid: z.string(),
...orgCore,
});
export type CreateOrgInput = z.infer<typeof createOrgInput>;
export const { schemas: orgSchemas, $ref: $org } = buildJsonSchemas(
{
createOrgInput,
createOrgResponse,
},
{ $id: "org" }
);

View File

@@ -0,0 +1,13 @@
import { generateId } from "../utils/id";
import { CreateOrgInput, orgModel } from "./organization.schema";
export async function createOrg(input: CreateOrgInput) {
const org = await orgModel.create({
tenantId: "abc",
pid: generateId(),
createdAt: new Date(),
...input,
});
return org;
}