view route updates
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { string, z } from "zod";
|
import { z } from "zod";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { buildJsonSchemas } from "fastify-zod";
|
import { buildJsonSchemas } from "fastify-zod";
|
||||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||||
|
|||||||
@@ -93,8 +93,9 @@ export async function getDefaultViewsHanlder(
|
|||||||
req: FastifyRequest,
|
req: FastifyRequest,
|
||||||
res: FastifyReply
|
res: FastifyReply
|
||||||
) {
|
) {
|
||||||
|
const { collection } = req.params as { collection: string };
|
||||||
try {
|
try {
|
||||||
const recInDb = await getDefaultViews(req.user);
|
const recInDb = await getDefaultViews(collection, req.user);
|
||||||
return res.code(200).send(recInDb);
|
return res.code(200).send(recInDb);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return err;
|
return err;
|
||||||
@@ -109,7 +110,7 @@ export async function updateDefaultViewsHanlder(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const recInDb = await updateDefaultView(input, req.user);
|
const recInDb = await updateDefaultView(input, req.user);
|
||||||
return res.code(200).send({ ...recInDb.defaultViews });
|
return res.code(200).send(recInDb?.defaultViews);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,9 +88,17 @@ export async function viewRoutes(fastify: FastifyInstance) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
fastify.get(
|
fastify.get(
|
||||||
"/defaultViews",
|
"/defaultViews/:collection",
|
||||||
{
|
{
|
||||||
config: { requiredClaims: ["view:delete"] },
|
schema: {
|
||||||
|
params: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
collection: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
config: { requiredClaims: ["view:read"] },
|
||||||
preHandler: [fastify.authorize],
|
preHandler: [fastify.authorize],
|
||||||
},
|
},
|
||||||
getDefaultViewsHanlder
|
getDefaultViewsHanlder
|
||||||
@@ -102,7 +110,7 @@ export async function viewRoutes(fastify: FastifyInstance) {
|
|||||||
schema: {
|
schema: {
|
||||||
body: $view("setDefaultView"),
|
body: $view("setDefaultView"),
|
||||||
},
|
},
|
||||||
config: { requiredClaims: ["view:delete"] },
|
config: { requiredClaims: ["view:write"] },
|
||||||
preHandler: [fastify.authorize],
|
preHandler: [fastify.authorize],
|
||||||
},
|
},
|
||||||
updateDefaultViewsHanlder
|
updateDefaultViewsHanlder
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { buildJsonSchemas } from "fastify-zod";
|
import { buildJsonSchemas } from "fastify-zod";
|
||||||
import mongoose from "mongoose";
|
import mongoose, { Collection } from "mongoose";
|
||||||
import { TypeOf, z } from "zod";
|
import { TypeOf, z } from "zod";
|
||||||
import { pageQueryParams } from "../pagination";
|
import { pageQueryParams } from "../pagination";
|
||||||
|
|
||||||
@@ -66,7 +66,10 @@ export const defaultViewModel = mongoose.model(
|
|||||||
"defaultView"
|
"defaultView"
|
||||||
);
|
);
|
||||||
|
|
||||||
const setDefaultView = z.record(z.string(), z.string());
|
const setDefaultView = z.object({
|
||||||
|
collection: z.string(),
|
||||||
|
viewId: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
export type SetDefaultView = z.infer<typeof setDefaultView>;
|
export type SetDefaultView = z.infer<typeof setDefaultView>;
|
||||||
|
|
||||||
|
|||||||
@@ -59,27 +59,19 @@ export async function deleteView(viewId: string) {
|
|||||||
return await viewModel.deleteOne({ pid: viewId });
|
return await viewModel.deleteOne({ pid: viewId });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDefaultViews(user: AuthenticatedUser) {
|
export async function getDefaultViews(
|
||||||
|
collection: string,
|
||||||
|
user: AuthenticatedUser
|
||||||
|
) {
|
||||||
const recInDb = await defaultViewModel.findOne({ userId: user.userId });
|
const recInDb = await defaultViewModel.findOne({ userId: user.userId });
|
||||||
if (!recInDb) return {};
|
if (!recInDb) return {};
|
||||||
|
|
||||||
const viewIds = Object.values(recInDb.defaultViews);
|
console.log(recInDb.defaultViews);
|
||||||
const views = await viewModel.find().where("pid").in(viewIds).exec();
|
|
||||||
|
|
||||||
const defaultViews = {};
|
const viewId = recInDb.defaultViews[collection];
|
||||||
console.log(viewIds);
|
if (!viewId) return {};
|
||||||
console.log(views);
|
|
||||||
|
|
||||||
for (const viewId of viewIds) {
|
return await viewModel.find({ pid: viewId });
|
||||||
const viewObj = views.find((item) => item.pid === viewId);
|
|
||||||
|
|
||||||
if (!viewObj) continue;
|
|
||||||
if (viewObj.createdBy.toString() !== user.userId.toString()) continue;
|
|
||||||
|
|
||||||
defaultViews[viewObj.collection] = viewObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
return defaultViews;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateDefaultView(
|
export async function updateDefaultView(
|
||||||
@@ -87,16 +79,21 @@ export async function updateDefaultView(
|
|||||||
user: AuthenticatedUser
|
user: AuthenticatedUser
|
||||||
) {
|
) {
|
||||||
const recInDb = await defaultViewModel.findOne({ userId: user.userId });
|
const recInDb = await defaultViewModel.findOne({ userId: user.userId });
|
||||||
|
|
||||||
if (!recInDb) {
|
if (!recInDb) {
|
||||||
return await defaultViewModel.create({
|
return await defaultViewModel.create({
|
||||||
defaultViews: input,
|
defaultViews: {
|
||||||
|
[input.collection]: input.viewId,
|
||||||
|
},
|
||||||
userId: user.userId,
|
userId: user.userId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const key = `defaultViews.${input.collection}`;
|
||||||
|
|
||||||
return await defaultViewModel.findOneAndUpdate(
|
return await defaultViewModel.findOneAndUpdate(
|
||||||
{ userId: user.userId },
|
{ userId: user.userId },
|
||||||
{ defaultViews: input },
|
{ $set: { [key]: input.viewId } },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user