diff --git a/src/view/view.controller.ts b/src/view/view.controller.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/view/view.route.ts b/src/view/view.route.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/view/view.schema.ts b/src/view/view.schema.ts new file mode 100644 index 0000000..1ce0306 --- /dev/null +++ b/src/view/view.schema.ts @@ -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; +export type UpdateViewInput = z.infer; + +export const { schemas: viewSchemas, $ref: $view } = buildJsonSchemas( + { + createViewInput, + updateViewInput, + pageQueryParams, + }, + { $id: "view" } +); diff --git a/src/view/view.service.ts b/src/view/view.service.ts new file mode 100644 index 0000000..ecf096a --- /dev/null +++ b/src/view/view.service.ts @@ -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 }); +}