refactoring and more work with clerk
This commit is contained in:
@@ -1,20 +1,40 @@
|
||||
import { unstable_cache } from 'next/cache';
|
||||
|
||||
import { db } from '../db';
|
||||
|
||||
export type IServiceRecentDashboards = Awaited<
|
||||
ReturnType<typeof getRecentDashboardsByUserId>
|
||||
>;
|
||||
export type IServiceDashboard = Awaited<ReturnType<typeof getDashboardById>>;
|
||||
export type IServiceDashboardWithProject = Awaited<
|
||||
export type IServiceDashboards = Awaited<
|
||||
ReturnType<typeof getDashboardsByProjectId>
|
||||
>[number];
|
||||
>;
|
||||
|
||||
export function getDashboardById(id: string) {
|
||||
return db.dashboard.findUniqueOrThrow({
|
||||
export async function getDashboardById(id: string) {
|
||||
const dashboard = await db.dashboard.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!dashboard) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return dashboard;
|
||||
}
|
||||
|
||||
export async function getDashboardsByOrganization(organizationSlug: string) {
|
||||
return db.dashboard.findMany({
|
||||
where: {
|
||||
organization_slug: organizationSlug,
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
},
|
||||
orderBy: {
|
||||
reports: {
|
||||
_count: 'desc',
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -28,59 +48,3 @@ export function getDashboardsByProjectId(projectId: string) {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getRecentDashboardsByUserId(userId: string) {
|
||||
const tag = `recentDashboards_${userId}`;
|
||||
|
||||
return unstable_cache(
|
||||
async (userId: string) => {
|
||||
return db.recentDashboards.findMany({
|
||||
where: {
|
||||
user_id: userId,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
},
|
||||
include: {
|
||||
project: true,
|
||||
dashboard: true,
|
||||
},
|
||||
take: 5,
|
||||
});
|
||||
},
|
||||
tag.split('_'),
|
||||
{
|
||||
revalidate: 3600,
|
||||
tags: [tag],
|
||||
}
|
||||
)(userId);
|
||||
}
|
||||
|
||||
export async function createRecentDashboard({
|
||||
organizationId,
|
||||
projectId,
|
||||
dashboardId,
|
||||
userId,
|
||||
}: {
|
||||
organizationId: string;
|
||||
projectId: string;
|
||||
dashboardId: string;
|
||||
userId: string;
|
||||
}) {
|
||||
await db.recentDashboards.deleteMany({
|
||||
where: {
|
||||
user_id: userId,
|
||||
project_id: projectId,
|
||||
dashboard_id: dashboardId,
|
||||
organization_slug: organizationId,
|
||||
},
|
||||
});
|
||||
return db.recentDashboards.create({
|
||||
data: {
|
||||
user_id: userId,
|
||||
organization_slug: organizationId,
|
||||
project_id: projectId,
|
||||
dashboard_id: dashboardId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user