Files
openmonetis/lib/pagadores/access.ts
Felipe Coutinho a7f63fb77a refactor: migrate from ESLint to Biome and extract SQL queries to data.ts
- Replace ESLint with Biome for linting and formatting
- Configure Biome with tabs, double quotes, and organized imports
- Move all SQL/Drizzle queries from page.tsx files to data.ts files
- Create new data.ts files for: ajustes, dashboard, relatorios/categorias
- Update existing data.ts files: extrato, fatura (add lancamentos queries)
- Remove all drizzle-orm imports from page.tsx files
- Update README.md with new tooling info

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 13:15:37 +00:00

96 lines
2.3 KiB
TypeScript

import { and, eq } from "drizzle-orm";
import { pagadores, pagadorShares, user as usersTable } from "@/db/schema";
import { db } from "@/lib/db";
export type PagadorWithAccess = typeof pagadores.$inferSelect & {
canEdit: boolean;
sharedByName: string | null;
sharedByEmail: string | null;
shareId: string | null;
};
export async function fetchPagadoresWithAccess(
userId: string,
): Promise<PagadorWithAccess[]> {
const [owned, shared] = await Promise.all([
db.query.pagadores.findMany({
where: eq(pagadores.userId, userId),
}),
db
.select({
shareId: pagadorShares.id,
pagador: pagadores,
ownerName: usersTable.name,
ownerEmail: usersTable.email,
})
.from(pagadorShares)
.innerJoin(pagadores, eq(pagadorShares.pagadorId, pagadores.id))
.leftJoin(usersTable, eq(pagadores.userId, usersTable.id))
.where(eq(pagadorShares.sharedWithUserId, userId)),
]);
const ownedMapped: PagadorWithAccess[] = owned.map((item) => ({
...item,
canEdit: true,
sharedByName: null,
sharedByEmail: null,
shareId: null,
}));
const sharedMapped: PagadorWithAccess[] = shared.map((item) => ({
...item.pagador,
shareCode: null,
canEdit: false,
sharedByName: item.ownerName ?? null,
sharedByEmail: item.ownerEmail ?? null,
shareId: item.shareId,
}));
return [...ownedMapped, ...sharedMapped];
}
export async function getPagadorAccess(userId: string, pagadorId: string) {
const pagador = await db.query.pagadores.findFirst({
where: and(eq(pagadores.id, pagadorId)),
});
if (!pagador) {
return null;
}
if (pagador.userId === userId) {
return {
pagador,
canEdit: true,
share: null as typeof pagadorShares.$inferSelect | null,
};
}
const share = await db.query.pagadorShares.findFirst({
where: and(
eq(pagadorShares.pagadorId, pagadorId),
eq(pagadorShares.sharedWithUserId, userId),
),
});
if (!share) {
return null;
}
return { pagador, canEdit: false, share };
}
export async function userCanEditPagador(userId: string, pagadorId: string) {
const pagadorRow = await db.query.pagadores.findFirst({
columns: { id: true },
where: and(eq(pagadores.id, pagadorId), eq(pagadores.userId, userId)),
});
return Boolean(pagadorRow);
}
export async function userHasPagadorAccess(userId: string, pagadorId: string) {
const access = await getPagadorAccess(userId, pagadorId);
return Boolean(access);
}