feat(inbox): add Caixa de Entrada page for managing companion notifications
- Create inbox page with pending items management: - InboxCard: displays notification summary with parsed data - InboxDetailsDialog: view full notification details - ProcessDialog: convert notification to transaction (lancamento) - Add server actions for inbox operations: - getInboxItems: fetch pending inbox items - processInboxItem: create lancamento from inbox item - discardInboxItem: discard unwanted notifications - Add navigation link to sidebar under 'Gestão Financeira' - Add revalidation config for inbox-related paths
This commit is contained in:
183
app/(dashboard)/caixa-de-entrada/actions.ts
Normal file
183
app/(dashboard)/caixa-de-entrada/actions.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
"use server";
|
||||
|
||||
import { inboxItems, lancamentos } from "@/db/schema";
|
||||
import { handleActionError } from "@/lib/actions/helpers";
|
||||
import type { ActionResult } from "@/lib/actions/types";
|
||||
import { db } from "@/lib/db";
|
||||
import { getUser } from "@/lib/auth/server";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
import { getCurrentPeriod } from "@/lib/utils/period";
|
||||
|
||||
const processInboxSchema = z.object({
|
||||
inboxItemId: z.string().uuid("ID do item inválido"),
|
||||
name: z.string().min(1, "Nome é obrigatório"),
|
||||
amount: z.coerce.number().positive("Valor deve ser positivo"),
|
||||
purchaseDate: z.string().min(1, "Data é obrigatória"),
|
||||
transactionType: z.enum(["Despesa", "Receita"]),
|
||||
condition: z.string().min(1, "Condição é obrigatória"),
|
||||
paymentMethod: z.string().min(1, "Forma de pagamento é obrigatória"),
|
||||
categoriaId: z.string().uuid("Categoria inválida"),
|
||||
contaId: z.string().uuid("Conta inválida").optional(),
|
||||
cartaoId: z.string().uuid("Cartão inválido").optional(),
|
||||
note: z.string().optional(),
|
||||
});
|
||||
|
||||
const discardInboxSchema = z.object({
|
||||
inboxItemId: z.string().uuid("ID do item inválido"),
|
||||
reason: z.string().optional(),
|
||||
});
|
||||
|
||||
const bulkDiscardSchema = z.object({
|
||||
inboxItemIds: z.array(z.string().uuid()).min(1, "Selecione ao menos um item"),
|
||||
});
|
||||
|
||||
function revalidateInbox() {
|
||||
revalidatePath("/caixa-de-entrada");
|
||||
revalidatePath("/lancamentos");
|
||||
revalidatePath("/dashboard");
|
||||
}
|
||||
|
||||
export async function processInboxItemAction(
|
||||
input: z.infer<typeof processInboxSchema>
|
||||
): Promise<ActionResult> {
|
||||
try {
|
||||
const user = await getUser();
|
||||
const data = processInboxSchema.parse(input);
|
||||
|
||||
// Verificar se item existe e pertence ao usuário
|
||||
const [item] = await db
|
||||
.select()
|
||||
.from(inboxItems)
|
||||
.where(
|
||||
and(
|
||||
eq(inboxItems.id, data.inboxItemId),
|
||||
eq(inboxItems.userId, user.id),
|
||||
eq(inboxItems.status, "pending")
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (!item) {
|
||||
return { success: false, error: "Item não encontrado ou já processado." };
|
||||
}
|
||||
|
||||
// Determinar período baseado na data de compra
|
||||
const purchaseDate = new Date(data.purchaseDate);
|
||||
const period = getCurrentPeriod(purchaseDate);
|
||||
|
||||
// Criar lançamento
|
||||
const [newLancamento] = await db
|
||||
.insert(lancamentos)
|
||||
.values({
|
||||
userId: user.id,
|
||||
name: data.name,
|
||||
amount: data.amount.toString(),
|
||||
purchaseDate: purchaseDate,
|
||||
transactionType: data.transactionType,
|
||||
condition: data.condition,
|
||||
paymentMethod: data.paymentMethod,
|
||||
categoriaId: data.categoriaId,
|
||||
contaId: data.contaId,
|
||||
cartaoId: data.cartaoId,
|
||||
note: data.note,
|
||||
period,
|
||||
})
|
||||
.returning({ id: lancamentos.id });
|
||||
|
||||
// Marcar item como processado
|
||||
await db
|
||||
.update(inboxItems)
|
||||
.set({
|
||||
status: "processed",
|
||||
processedAt: new Date(),
|
||||
lancamentoId: newLancamento.id,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(inboxItems.id, data.inboxItemId));
|
||||
|
||||
revalidateInbox();
|
||||
|
||||
return { success: true, message: "Lançamento criado com sucesso!" };
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function discardInboxItemAction(
|
||||
input: z.infer<typeof discardInboxSchema>
|
||||
): Promise<ActionResult> {
|
||||
try {
|
||||
const user = await getUser();
|
||||
const data = discardInboxSchema.parse(input);
|
||||
|
||||
// Verificar se item existe e pertence ao usuário
|
||||
const [item] = await db
|
||||
.select()
|
||||
.from(inboxItems)
|
||||
.where(
|
||||
and(
|
||||
eq(inboxItems.id, data.inboxItemId),
|
||||
eq(inboxItems.userId, user.id),
|
||||
eq(inboxItems.status, "pending")
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (!item) {
|
||||
return { success: false, error: "Item não encontrado ou já processado." };
|
||||
}
|
||||
|
||||
// Marcar item como descartado
|
||||
await db
|
||||
.update(inboxItems)
|
||||
.set({
|
||||
status: "discarded",
|
||||
discardedAt: new Date(),
|
||||
discardReason: data.reason,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(inboxItems.id, data.inboxItemId));
|
||||
|
||||
revalidateInbox();
|
||||
|
||||
return { success: true, message: "Item descartado." };
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function bulkDiscardInboxItemsAction(
|
||||
input: z.infer<typeof bulkDiscardSchema>
|
||||
): Promise<ActionResult> {
|
||||
try {
|
||||
const user = await getUser();
|
||||
const data = bulkDiscardSchema.parse(input);
|
||||
|
||||
// Marcar todos os itens como descartados
|
||||
await db
|
||||
.update(inboxItems)
|
||||
.set({
|
||||
status: "discarded",
|
||||
discardedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
inArray(inboxItems.id, data.inboxItemIds),
|
||||
eq(inboxItems.userId, user.id),
|
||||
eq(inboxItems.status, "pending")
|
||||
)
|
||||
);
|
||||
|
||||
revalidateInbox();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${data.inboxItemIds.length} item(s) descartado(s).`,
|
||||
};
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
}
|
||||
82
app/(dashboard)/caixa-de-entrada/data.ts
Normal file
82
app/(dashboard)/caixa-de-entrada/data.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Data fetching functions for Caixa de Entrada
|
||||
*/
|
||||
|
||||
import { db } from "@/lib/db";
|
||||
import { inboxItems, categorias, contas, cartoes } from "@/db/schema";
|
||||
import { eq, desc, and } from "drizzle-orm";
|
||||
import type { InboxItem, SelectOption } from "@/components/caixa-de-entrada/types";
|
||||
|
||||
export async function fetchInboxItems(
|
||||
userId: string,
|
||||
status: "pending" | "processed" | "discarded" = "pending"
|
||||
): Promise<InboxItem[]> {
|
||||
const items = await db
|
||||
.select()
|
||||
.from(inboxItems)
|
||||
.where(and(eq(inboxItems.userId, userId), eq(inboxItems.status, status)))
|
||||
.orderBy(desc(inboxItems.createdAt));
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
export async function fetchInboxItemById(
|
||||
userId: string,
|
||||
itemId: string
|
||||
): Promise<InboxItem | null> {
|
||||
const [item] = await db
|
||||
.select()
|
||||
.from(inboxItems)
|
||||
.where(and(eq(inboxItems.id, itemId), eq(inboxItems.userId, userId)))
|
||||
.limit(1);
|
||||
|
||||
return item ?? null;
|
||||
}
|
||||
|
||||
export async function fetchCategoriasForSelect(
|
||||
userId: string,
|
||||
type?: string
|
||||
): Promise<SelectOption[]> {
|
||||
const query = db
|
||||
.select({ id: categorias.id, name: categorias.name })
|
||||
.from(categorias)
|
||||
.where(
|
||||
type
|
||||
? and(eq(categorias.userId, userId), eq(categorias.type, type))
|
||||
: eq(categorias.userId, userId)
|
||||
)
|
||||
.orderBy(categorias.name);
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
export async function fetchContasForSelect(userId: string): Promise<SelectOption[]> {
|
||||
const items = await db
|
||||
.select({ id: contas.id, name: contas.name })
|
||||
.from(contas)
|
||||
.where(and(eq(contas.userId, userId), eq(contas.status, "ativo")))
|
||||
.orderBy(contas.name);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
export async function fetchCartoesForSelect(
|
||||
userId: string
|
||||
): Promise<(SelectOption & { lastDigits?: string })[]> {
|
||||
const items = await db
|
||||
.select({ id: cartoes.id, name: cartoes.name })
|
||||
.from(cartoes)
|
||||
.where(and(eq(cartoes.userId, userId), eq(cartoes.status, "ativo")))
|
||||
.orderBy(cartoes.name);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
export async function fetchPendingInboxCount(userId: string): Promise<number> {
|
||||
const items = await db
|
||||
.select({ id: inboxItems.id })
|
||||
.from(inboxItems)
|
||||
.where(and(eq(inboxItems.userId, userId), eq(inboxItems.status, "pending")));
|
||||
|
||||
return items.length;
|
||||
}
|
||||
33
app/(dashboard)/caixa-de-entrada/loading.tsx
Normal file
33
app/(dashboard)/caixa-de-entrada/loading.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
|
||||
export default function Loading() {
|
||||
return (
|
||||
<main className="flex flex-col items-start gap-6">
|
||||
<div className="flex w-full flex-col gap-6">
|
||||
<div className="flex justify-between">
|
||||
<Skeleton className="h-10 w-48" />
|
||||
<Skeleton className="h-10 w-32" />
|
||||
</div>
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
{Array.from({ length: 6 }).map((_, i) => (
|
||||
<Card key={i} className="p-4">
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-between">
|
||||
<Skeleton className="h-5 w-24" />
|
||||
<Skeleton className="h-5 w-16" />
|
||||
</div>
|
||||
<Skeleton className="h-4 w-full" />
|
||||
<Skeleton className="h-4 w-3/4" />
|
||||
<div className="flex gap-2 pt-2">
|
||||
<Skeleton className="h-8 w-20" />
|
||||
<Skeleton className="h-8 w-20" />
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
30
app/(dashboard)/caixa-de-entrada/page.tsx
Normal file
30
app/(dashboard)/caixa-de-entrada/page.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { InboxPage } from "@/components/caixa-de-entrada/inbox-page";
|
||||
import { getUserId } from "@/lib/auth/server";
|
||||
import {
|
||||
fetchInboxItems,
|
||||
fetchCategoriasForSelect,
|
||||
fetchContasForSelect,
|
||||
fetchCartoesForSelect,
|
||||
} from "./data";
|
||||
|
||||
export default async function Page() {
|
||||
const userId = await getUserId();
|
||||
|
||||
const [items, categorias, contas, cartoes] = await Promise.all([
|
||||
fetchInboxItems(userId, "pending"),
|
||||
fetchCategoriasForSelect(userId),
|
||||
fetchContasForSelect(userId),
|
||||
fetchCartoesForSelect(userId),
|
||||
]);
|
||||
|
||||
return (
|
||||
<main className="flex flex-col items-start gap-6">
|
||||
<InboxPage
|
||||
items={items}
|
||||
categorias={categorias}
|
||||
contas={contas}
|
||||
cartoes={cartoes}
|
||||
/>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user