Initial Commit
This commit is contained in:
17
src/organization/organization.controller.ts
Normal file
17
src/organization/organization.controller.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
18
src/organization/organization.route.ts
Normal file
18
src/organization/organization.route.ts
Normal 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
|
||||
);
|
||||
}
|
||||
52
src/organization/organization.schema.ts
Normal file
52
src/organization/organization.schema.ts
Normal 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" }
|
||||
);
|
||||
13
src/organization/organization.service.ts
Normal file
13
src/organization/organization.service.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user