only show user's views

This commit is contained in:
2025-05-01 09:45:34 +05:30
parent b741c929bf
commit 1f9970366c
2 changed files with 24 additions and 55 deletions

View File

@@ -1,12 +1,12 @@
import { AuthenticatedUser } from "../auth";
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
import { generateId } from "../utils/id";
import { AuthenticatedUser } from '../auth';
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
import { generateId } from '../utils/id';
import {
CreateViewInput,
UpdateViewInput,
viewFields,
viewModel,
} from "./view.schema";
} from './view.schema';
export async function createView(
input: CreateViewInput,
@@ -26,53 +26,22 @@ export async function getView(viewId: string, tenantId: string) {
});
}
export async function listViews(params: PageQueryParams, tenantId: string) {
export async function listViews(
params: PageQueryParams,
user: AuthenticatedUser
) {
const page = params.page || 1;
const pageSize = params.pageSize || 10;
const sortObj = getSortObject(params, viewFields);
const filterObj = getFilterObject(params, viewFields);
const viewList = await viewModel.aggregate([
{
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
},
{
$project: {
_id: 1,
pid: 1,
name: 1,
collection: 1,
createdBy: 1,
hide: 1,
sort: 1,
group: 1,
filters: 1,
match: 1,
},
},
{
$facet: {
metadata: [{ $count: "count" }],
data: [
{ $sort: sortObj },
{ $skip: (page - 1) * pageSize },
{ $limit: pageSize },
],
},
},
]);
if (viewList[0].data.length === 0)
return { views: [], metadata: { count: 0, page, pageSize } };
return {
permits: viewList[0]?.data,
metadata: {
count: viewList[0].metadata[0].count,
page,
pageSize,
},
};
return await viewModel.find({
$and: [
{ tenantId: user.tenantId },
{ createdBy: user.userId },
...filterObj,
],
});
}
export async function updateView(