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; export type UpdateViewInput = z.infer; 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; export const { schemas: viewSchemas, $ref: $view } = buildJsonSchemas( { createViewInput, updateViewInput, pageQueryParams, setDefaultView, }, { $id: "view" } );