83 lines
1.8 KiB
TypeScript
83 lines
1.8 KiB
TypeScript
import { buildJsonSchemas } from "fastify-zod";
|
|
import mongoose from "mongoose";
|
|
import { z } from "zod";
|
|
import { pageMetadata, pageQueryParams } from "../pagination";
|
|
|
|
const orgSchema = new mongoose.Schema({
|
|
tenantId: {
|
|
type: String,
|
|
required: true,
|
|
},
|
|
pid: {
|
|
type: String,
|
|
unique: true,
|
|
},
|
|
name: String,
|
|
domain: {
|
|
type: String,
|
|
},
|
|
avatar: String,
|
|
type: String,
|
|
isClient: Boolean,
|
|
status: String,
|
|
createdAt: Date,
|
|
createdBy: String,
|
|
updatedAt: Date,
|
|
}).index({ tenantId: 1, domain: 1 }, { unique: true });
|
|
|
|
export const orgFields = Object.keys(orgSchema.paths).filter(
|
|
(path) => path !== "__v"
|
|
);
|
|
export const orgModel = mongoose.model("organization", orgSchema);
|
|
|
|
const orgCore = {
|
|
name: z.string().max(30),
|
|
domain: z.string().max(30).optional(),
|
|
avatar: z.string().url().optional(),
|
|
type: z.enum(["county", "builder"], {
|
|
message: "Must be county or builder",
|
|
}),
|
|
isClient: z.boolean().optional(),
|
|
};
|
|
|
|
const createOrgInput = z.object({
|
|
...orgCore,
|
|
});
|
|
|
|
const createOrgResponse = z.object({
|
|
_id: z.string().optional(),
|
|
pid: z.string(),
|
|
...orgCore,
|
|
});
|
|
|
|
const listOrgResponse = z.object({
|
|
orgs: z.array(createOrgResponse),
|
|
metadata: pageMetadata,
|
|
});
|
|
|
|
const updateOrgInput = z.object({
|
|
name: z.string().max(30).optional(),
|
|
domain: z.string().max(30).optional(),
|
|
avatar: z.string().url().optional(),
|
|
type: z
|
|
.enum(["county", "builder"], {
|
|
message: "Must be county or builder",
|
|
})
|
|
.optional(),
|
|
isClient: z.boolean().optional(),
|
|
});
|
|
|
|
export type CreateOrgInput = z.infer<typeof createOrgInput>;
|
|
export type UpdateOrgInput = z.infer<typeof updateOrgInput>;
|
|
|
|
export const { schemas: orgSchemas, $ref: $org } = buildJsonSchemas(
|
|
{
|
|
createOrgInput,
|
|
createOrgResponse,
|
|
listOrgResponse,
|
|
updateOrgInput,
|
|
pageQueryParams,
|
|
},
|
|
{ $id: "org" }
|
|
);
|