mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 19:01:47 +00:00
refactor: agrega queries e cache do dashboard
This commit is contained in:
67
src/features/dashboard/navbar-queries.ts
Normal file
67
src/features/dashboard/navbar-queries.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { unstable_cache } from "next/cache";
|
||||
import { payers } from "@/db/schema";
|
||||
import { fetchPendingInboxCount } from "@/features/inbox/queries";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
|
||||
import {
|
||||
type DashboardNotificationsSnapshot,
|
||||
fetchDashboardNotifications,
|
||||
} from "./notifications-queries";
|
||||
|
||||
export type DashboardNavbarData = {
|
||||
pagadorAvatarUrl: string | null;
|
||||
preLancamentosCount: number;
|
||||
notificationsSnapshot: DashboardNotificationsSnapshot;
|
||||
};
|
||||
|
||||
async function fetchAdminPayerAvatarUrl(
|
||||
userId: string,
|
||||
): Promise<string | null> {
|
||||
const adminPayerId = await getAdminPayerId(userId);
|
||||
|
||||
if (!adminPayerId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const payer = await db.query.payers.findFirst({
|
||||
columns: {
|
||||
avatarUrl: true,
|
||||
},
|
||||
where: eq(payers.id, adminPayerId),
|
||||
});
|
||||
|
||||
return payer?.avatarUrl ?? null;
|
||||
}
|
||||
|
||||
async function fetchDashboardNavbarDataInternal(
|
||||
userId: string,
|
||||
currentPeriod: string,
|
||||
): Promise<DashboardNavbarData> {
|
||||
const [pagadorAvatarUrl, notificationsSnapshot, preLancamentosCount] =
|
||||
await Promise.all([
|
||||
fetchAdminPayerAvatarUrl(userId),
|
||||
fetchDashboardNotifications(userId, currentPeriod),
|
||||
fetchPendingInboxCount(userId),
|
||||
]);
|
||||
|
||||
return {
|
||||
pagadorAvatarUrl,
|
||||
preLancamentosCount,
|
||||
notificationsSnapshot,
|
||||
};
|
||||
}
|
||||
|
||||
export function fetchDashboardNavbarData(
|
||||
userId: string,
|
||||
currentPeriod: string,
|
||||
) {
|
||||
return unstable_cache(
|
||||
() => fetchDashboardNavbarDataInternal(userId, currentPeriod),
|
||||
[`dashboard-navbar-${userId}-${currentPeriod}`],
|
||||
{
|
||||
tags: [`dashboard-${userId}`],
|
||||
revalidate: 60,
|
||||
},
|
||||
)();
|
||||
}
|
||||
Reference in New Issue
Block a user