add view routes

This commit is contained in:
2025-02-27 16:45:10 +05:30
parent 63bcc4bafd
commit 550f600a35
4 changed files with 102 additions and 0 deletions

View File

0
src/view/view.route.ts Normal file
View File

64
src/view/view.schema.ts Normal file
View File

@@ -0,0 +1,64 @@
import { buildJsonSchemas } from "fastify-zod";
import mongoose from "mongoose";
import { 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 { schemas: viewSchemas, $ref: $view } = buildJsonSchemas(
{
createViewInput,
updateViewInput,
pageQueryParams,
},
{ $id: "view" }
);

38
src/view/view.service.ts Normal file
View File

@@ -0,0 +1,38 @@
import { AuthenticatedUser } from "../auth";
import { generateId } from "../utils/id";
import { CreateViewInput, UpdateViewInput, viewModel } from "./view.schema";
export async function createView(
input: CreateViewInput,
user: AuthenticatedUser
) {
return await viewModel.create({
tenantId: user.tenantId,
pid: generateId(),
createdBy: user.userId,
...input,
});
}
export async function getView(viewId: string, tenantId: string) {
return await viewModel.findOne({
$and: [{ pid: viewId }, { tenantId: tenantId }],
});
}
export async function listViews() {}
export async function updateView(
viewId: string,
input: UpdateViewInput,
tenantId: string
) {
return await viewModel.findOneAndUpdate(
{ $and: [{ pid: viewId }, { tenantId: tenantId }] },
{ ...input }
);
}
export async function deletView(viewId: string) {
return await viewModel.findOneAndDelete({ pid: viewId });
}