Files
permit-api/src/view/view.schema.ts
2025-05-26 16:00:05 +05:30

85 lines
2.2 KiB
TypeScript

import { buildJsonSchemas } from "fastify-zod";
import mongoose, { Collection } from "mongoose";
import { TypeOf, z } from "zod";
import { pageQueryParams } from "../pagination";
export const viewModel = mongoose.model(
"view",
new mongoose.Schema({
tenantId: String,
pid: {
type: String,
unique: true,
required: true,
},
name: {
type: String,
required: true,
},
collection: String,
createdBy: {
type: mongoose.Types.ObjectId,
ref: "user",
},
hide: new mongoose.Schema({}, { strict: false, _id: false }),
sort: new mongoose.Schema({}, { strict: false, _id: false }),
group: new mongoose.Schema({}, { strict: false, _id: false }),
filters: [new mongoose.Schema({}, { strict: false, _id: false })],
match: [new mongoose.Schema({}, { strict: false, _id: false })],
})
);
export const viewFields = ["name", "collection"];
const createViewInput = z.object({
name: z.string(),
collection: z.string(),
hide: z.record(z.string(), z.any()).optional(),
sort: z.record(z.string(), z.any()).optional(),
group: z.record(z.any()).optional(),
filters: z.array(z.any()).optional(),
match: z.array(z.any()).optional(),
});
const updateViewInput = z.object({
name: z.string().optional(),
collection: z.string().optional(),
hide: z.record(z.string(), z.any()).optional(),
sort: z.record(z.string(), z.any()).optional(),
group: z.record(z.any()).optional(),
filters: z.array(z.any()).optional(),
match: z.array(z.any()).optional(),
});
export type CreateViewInput = z.infer<typeof createViewInput>;
export type UpdateViewInput = z.infer<typeof updateViewInput>;
export const defaultViewModel = mongoose.model(
"defaultView",
new mongoose.Schema({
userId: {
type: mongoose.Types.ObjectId,
ref: "user",
},
defaultViews: Object,
}),
"defaultView"
);
const setDefaultView = z.object({
collection: z.string(),
viewId: z.string(),
});
export type SetDefaultView = z.infer<typeof setDefaultView>;
export const { schemas: viewSchemas, $ref: $view } = buildJsonSchemas(
{
createViewInput,
updateViewInput,
pageQueryParams,
setDefaultView,
},
{ $id: "view" }
);