mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
Remove infraestrutura de series recorrentes
This commit is contained in:
@@ -1,153 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
RiPauseCircleLine,
|
||||
RiPlayCircleLine,
|
||||
RiRefreshLine,
|
||||
RiStopCircleLine,
|
||||
} from "@remixicon/react";
|
||||
import { useTransition } from "react";
|
||||
import { toast } from "sonner";
|
||||
import type { RecurringSeriesData } from "@/features/dashboard/recurring/recurring-series-queries";
|
||||
import {
|
||||
cancelRecurringSeriesAction,
|
||||
pauseRecurringSeriesAction,
|
||||
resumeRecurringSeriesAction,
|
||||
} from "@/features/recurring/actions";
|
||||
import MoneyValues from "@/shared/components/money-values";
|
||||
import { Badge } from "@/shared/components/ui/badge";
|
||||
import { Button } from "@/shared/components/ui/button";
|
||||
import { WidgetEmptyState } from "@/shared/components/widget-empty-state";
|
||||
import { formatMonthYearLabel } from "@/shared/utils/period";
|
||||
|
||||
type RecurringSeriesWidgetProps = {
|
||||
data: RecurringSeriesData;
|
||||
};
|
||||
|
||||
export function RecurringSeriesWidget({ data }: RecurringSeriesWidgetProps) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
if (data.series.length === 0) {
|
||||
return (
|
||||
<WidgetEmptyState
|
||||
icon={<RiRefreshLine className="size-6 text-muted-foreground" />}
|
||||
title="Nenhuma série recorrente"
|
||||
description="Séries recorrentes aparecerão aqui quando forem criadas."
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const handlePause = (seriesId: string) => {
|
||||
startTransition(async () => {
|
||||
const result = await pauseRecurringSeriesAction({ seriesId });
|
||||
if (result.success) {
|
||||
toast.success(result.message);
|
||||
} else {
|
||||
toast.error(result.error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleResume = (seriesId: string) => {
|
||||
startTransition(async () => {
|
||||
const result = await resumeRecurringSeriesAction({ seriesId });
|
||||
if (result.success) {
|
||||
toast.success(result.message);
|
||||
} else {
|
||||
toast.error(result.error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleCancel = (seriesId: string) => {
|
||||
if (
|
||||
!confirm(
|
||||
"Tem certeza que deseja cancelar esta série recorrente? Lançamentos passados serão mantidos.",
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
startTransition(async () => {
|
||||
const result = await cancelRecurringSeriesAction({ seriesId });
|
||||
if (result.success) {
|
||||
toast.success(result.message);
|
||||
} else {
|
||||
toast.error(result.error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4 px-0">
|
||||
<ul className="flex flex-col gap-2">
|
||||
{data.series.map((item) => (
|
||||
<li
|
||||
key={item.id}
|
||||
className="flex items-center gap-3 border-b border-dashed pb-2 last:border-b-0 last:pb-0"
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-2 min-w-0 flex-1">
|
||||
<p className="truncate text-foreground text-sm font-medium">
|
||||
{item.name}
|
||||
</p>
|
||||
<Badge
|
||||
variant={item.status === "active" ? "default" : "secondary"}
|
||||
className="shrink-0 text-[10px] px-1.5 py-0"
|
||||
>
|
||||
{item.status === "active" ? "Ativo" : "Pausado"}
|
||||
</Badge>
|
||||
</div>
|
||||
<MoneyValues amount={item.amount} />
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between text-xs text-muted-foreground mt-0.5">
|
||||
<span>
|
||||
Dia {item.dayOfMonth} · {item.paymentMethod}
|
||||
{item.categoryName ? ` · ${item.categoryName}` : ""}
|
||||
</span>
|
||||
<span>Próx: {formatMonthYearLabel(item.nextPeriod)}</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1 mt-1.5">
|
||||
{item.status === "active" ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
disabled={isPending}
|
||||
onClick={() => handlePause(item.id)}
|
||||
>
|
||||
<RiPauseCircleLine className="size-3.5 mr-1" />
|
||||
Pausar
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
disabled={isPending}
|
||||
onClick={() => handleResume(item.id)}
|
||||
>
|
||||
<RiPlayCircleLine className="size-3.5 mr-1" />
|
||||
Continuar
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs text-destructive hover:text-destructive"
|
||||
disabled={isPending}
|
||||
onClick={() => handleCancel(item.id)}
|
||||
>
|
||||
<RiStopCircleLine className="size-3.5 mr-1" />
|
||||
Cancelar
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import { fetchPaymentConditions } from "./payments/payment-conditions-queries";
|
||||
import { fetchPaymentMethods } from "./payments/payment-methods-queries";
|
||||
import { fetchPaymentStatus } from "./payments/payment-status-queries";
|
||||
import { fetchPurchasesByCategory } from "./purchases-by-category-queries";
|
||||
import { fetchRecurringSeries } from "./recurring/recurring-series-queries";
|
||||
import { fetchTopEstablishments } from "./top-establishments-queries";
|
||||
|
||||
async function fetchDashboardDataInternal(userId: string, period: string) {
|
||||
@@ -40,7 +39,6 @@ async function fetchDashboardDataInternal(userId: string, period: string) {
|
||||
purchasesByCategoryData,
|
||||
incomeByCategoryData,
|
||||
expensesByCategoryData,
|
||||
recurringSeriesData,
|
||||
] = await Promise.all([
|
||||
fetchDashboardCardMetrics(userId, period),
|
||||
fetchDashboardAccounts(userId),
|
||||
@@ -61,7 +59,6 @@ async function fetchDashboardDataInternal(userId: string, period: string) {
|
||||
fetchPurchasesByCategory(userId, period),
|
||||
fetchIncomeByCategory(userId, period),
|
||||
fetchExpensesByCategory(userId, period),
|
||||
fetchRecurringSeries(userId),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -84,7 +81,6 @@ async function fetchDashboardDataInternal(userId: string, period: string) {
|
||||
purchasesByCategoryData,
|
||||
incomeByCategoryData,
|
||||
expensesByCategoryData,
|
||||
recurringSeriesData,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import type { RecurringSeriesTemplate } from "@/db/schema";
|
||||
import { categories, recurringSeries } from "@/db/schema";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
|
||||
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
||||
import { addMonthsToPeriod } from "@/shared/utils/period";
|
||||
|
||||
export type RecurringSeriesItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
amount: number;
|
||||
categoryName: string | null;
|
||||
categoryIcon: string | null;
|
||||
paymentMethod: string;
|
||||
dayOfMonth: number;
|
||||
status: "active" | "paused" | "cancelled";
|
||||
nextPeriod: string;
|
||||
lastGeneratedPeriod: string;
|
||||
};
|
||||
|
||||
export type RecurringSeriesData = {
|
||||
series: RecurringSeriesItem[];
|
||||
};
|
||||
|
||||
export async function fetchRecurringSeries(
|
||||
userId: string,
|
||||
): Promise<RecurringSeriesData> {
|
||||
const adminPayerId = await getAdminPayerId(userId);
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: recurringSeries.id,
|
||||
status: recurringSeries.status,
|
||||
dayOfMonth: recurringSeries.dayOfMonth,
|
||||
lastGeneratedPeriod: recurringSeries.lastGeneratedPeriod,
|
||||
templateData: recurringSeries.templateData,
|
||||
})
|
||||
.from(recurringSeries)
|
||||
.where(
|
||||
and(
|
||||
eq(recurringSeries.userId, userId),
|
||||
inArray(recurringSeries.status, ["active", "paused"]),
|
||||
),
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return { series: [] };
|
||||
}
|
||||
|
||||
// Fetch category names for all series in one query
|
||||
const categoryIds = rows
|
||||
.map((r) => (r.templateData as RecurringSeriesTemplate).categoryId)
|
||||
.filter((id): id is string => id !== null);
|
||||
|
||||
const categoryMap = new Map<string, { name: string; icon: string | null }>();
|
||||
if (categoryIds.length > 0) {
|
||||
const cats = await db
|
||||
.select({
|
||||
id: categories.id,
|
||||
name: categories.name,
|
||||
icon: categories.icon,
|
||||
})
|
||||
.from(categories)
|
||||
.where(inArray(categories.id, categoryIds));
|
||||
for (const cat of cats) {
|
||||
categoryMap.set(cat.id, { name: cat.name, icon: cat.icon });
|
||||
}
|
||||
}
|
||||
|
||||
const series = rows
|
||||
.filter((row) => {
|
||||
// If admin pagador exists, only show series belonging to admin
|
||||
if (!adminPayerId) return true;
|
||||
const template = row.templateData as RecurringSeriesTemplate;
|
||||
return template.payerId === adminPayerId || template.payerId === null;
|
||||
})
|
||||
.map((row): RecurringSeriesItem => {
|
||||
const template = row.templateData as RecurringSeriesTemplate;
|
||||
const category = template.categoryId
|
||||
? categoryMap.get(template.categoryId)
|
||||
: null;
|
||||
return {
|
||||
id: row.id,
|
||||
name: template.name,
|
||||
amount: Math.abs(toNumber(template.amount)),
|
||||
categoryName: category?.name ?? null,
|
||||
categoryIcon: category?.icon ?? null,
|
||||
paymentMethod: template.paymentMethod,
|
||||
dayOfMonth: row.dayOfMonth,
|
||||
status: row.status as "active" | "paused",
|
||||
nextPeriod: addMonthsToPeriod(row.lastGeneratedPeriod, 1),
|
||||
lastGeneratedPeriod: row.lastGeneratedPeriod,
|
||||
};
|
||||
});
|
||||
|
||||
return { series };
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import { PaymentOverviewWidget } from "@/features/dashboard/components/payment-o
|
||||
import { PaymentStatusWidget } from "@/features/dashboard/components/payment-status-widget";
|
||||
import { PurchasesByCategoryWidget } from "@/features/dashboard/components/purchases-by-category-widget";
|
||||
import { RecurringExpensesWidget } from "@/features/dashboard/components/recurring-expenses-widget";
|
||||
import { RecurringSeriesWidget } from "@/features/dashboard/components/recurring-series-widget";
|
||||
import { SpendingOverviewWidget } from "@/features/dashboard/components/spending-overview-widget";
|
||||
import type { DashboardData } from "../fetch-dashboard-data";
|
||||
|
||||
@@ -164,15 +163,6 @@ export const widgetsConfig: WidgetConfig[] = [
|
||||
<RecurringExpensesWidget data={data.recurringExpensesData} />
|
||||
),
|
||||
},
|
||||
{
|
||||
id: "recurring-series",
|
||||
title: "Séries Recorrentes",
|
||||
subtitle: "Gerencie seus lançamentos recorrentes",
|
||||
icon: <RiRefreshLine className="size-4" />,
|
||||
component: ({ data }) => (
|
||||
<RecurringSeriesWidget data={data.recurringSeriesData} />
|
||||
),
|
||||
},
|
||||
{
|
||||
id: "installment-expenses",
|
||||
title: "Lançamentos Parcelados",
|
||||
|
||||
Reference in New Issue
Block a user