Files
permit-api/src/permit/permit.schema.ts
2025-03-01 11:02:45 +05:30

164 lines
3.5 KiB
TypeScript

import { z } from "zod";
import mongoose from "mongoose";
import { buildJsonSchemas } from "fastify-zod";
import { pageMetadata, pageQueryParams } from "../pagination";
const permitSchema = new mongoose.Schema({
tenantId: {
type: String,
required: true,
},
pid: {
type: String,
unique: true,
},
permitNumber: String,
county: {
type: mongoose.Types.ObjectId,
ref: "organization",
},
client: {
type: mongoose.Types.ObjectId,
ref: "organization",
},
permitDate: Date,
stage: new mongoose.Schema(
{
pipeline: Array,
currentStage: Number,
},
{ _id: false }
),
status: String,
assignedTo: {
type: mongoose.Types.ObjectId,
ref: "user",
},
link: String,
address: Object,
recordType: String,
description: String,
applicationDetails: Object,
applicationInfo: Object,
applicationInfoTable: Object,
conditions: Array,
ownerDetails: String,
parcelInfo: Object,
paymentData: Object,
professionalsList: Array,
inspections: Object,
createdAt: Date,
updatedAt: Date,
createdBy: {
type: mongoose.Types.ObjectId,
ref: "user",
},
newProcessingStatus: Array,
newPayment: Array,
newConditions: Array,
professionals: Object,
recordid: String,
relatedRecords: Object,
accelaStatus: String,
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true });
export const permitFields = Object.keys(permitSchema.paths).filter(
(path) => path !== "__v"
);
export const permitModel = mongoose.model("permit", permitSchema);
const permitCore = {
permitNumber: z.string(),
county: z.string().optional(),
client: z.string().optional(),
permitDate: z.date(),
stage: z
.object({
pipeline: z.array(
z.object({
name: z.string(),
date: z.date().nullable().optional(),
description: z.string().optional(),
comment: z.string().optional(),
})
),
currentStage: z.number(),
})
.optional(),
status: z.string().optional(),
assignedTo: z.string().optional(),
};
const createPermitInput = z.object({
...permitCore,
});
const createPermitResponse = z.object({
pid: z.string(),
...permitCore,
});
const getPermitResponse = z.object({
pid: z.string(),
...permitCore,
county: z
.object({
name: z.string(),
avatar: z.string().optional(),
})
.optional(),
client: z
.object({
name: z.string(),
avatar: z.string().optional(),
})
.optional(),
assignedTo: z
.object({
name: z.string(),
avatar: z.string().optional(),
})
.optional(),
});
const listPermitResponse = z.object({
permits: z.array(getPermitResponse),
metadata: pageMetadata,
});
const updatePermitInput = z.object({
county: z.string().optional(),
client: z.string().optional(),
permitDate: z.date().optional(),
stage: z
.object({
pipeline: z.array(
z.object({
name: z.string(),
date: z.date().nullable().optional(),
description: z.string().optional(),
comment: z.string().optional(),
})
),
currentStage: z.number(),
})
.optional(),
status: z.string().optional(),
assignedTo: z.string().optional(),
});
export type CreatePermitInput = z.infer<typeof createPermitInput>;
export type UpdatePermitInput = z.infer<typeof updatePermitInput>;
export const { schemas: permitSchemas, $ref: $permit } = buildJsonSchemas(
{
createPermitInput,
createPermitResponse,
getPermitResponse,
listPermitResponse,
updatePermitInput,
pageQueryParams,
},
{ $id: "permit" }
);