Adiciona CRUD manual de transações (income/expense) com filtro mensal, CRUD de recorrências fixas com verificação mensal de cobertura (±10% por categoria), endpoint de ignore/unignore e telas Vue para Transações e Configurações. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
import { api } from '@/services/api'
|
|
import type { Category } from './categories'
|
|
|
|
export interface RecurringExpense {
|
|
id: number
|
|
name: string
|
|
expected_amount: number
|
|
day_of_month: number
|
|
category_id: number | null
|
|
active: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
export interface RecurringStatus extends RecurringExpense {
|
|
covered: boolean
|
|
ignored: boolean
|
|
reason?: string
|
|
}
|
|
|
|
export const useRecurringStore = defineStore('recurring', () => {
|
|
const items = ref<RecurringExpense[]>([])
|
|
const monthlyStatus = ref<RecurringStatus[]>([])
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
|
|
async function fetchAll() {
|
|
loading.value = true
|
|
error.value = null
|
|
try {
|
|
items.value = await api.get<RecurringExpense[]>('/recurring')
|
|
} catch (e: any) {
|
|
error.value = e.message
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function fetchMonthlyStatus(month: string) {
|
|
try {
|
|
monthlyStatus.value = await api.get<RecurringStatus[]>(`/recurring/status?month=${month}`)
|
|
} catch {
|
|
monthlyStatus.value = []
|
|
}
|
|
}
|
|
|
|
async function create(input: Omit<RecurringExpense, 'id' | 'active' | 'created_at' | 'updated_at'>) {
|
|
const item = await api.post<RecurringExpense>('/recurring', input)
|
|
items.value.push(item)
|
|
return item
|
|
}
|
|
|
|
async function update(id: number, input: Omit<RecurringExpense, 'id' | 'active' | 'created_at' | 'updated_at'>) {
|
|
const item = await api.put<RecurringExpense>(`/recurring/${id}`, input)
|
|
const idx = items.value.findIndex((x) => x.id === id)
|
|
if (idx !== -1) items.value[idx] = item
|
|
return item
|
|
}
|
|
|
|
async function remove(id: number) {
|
|
await api.delete(`/recurring/${id}`)
|
|
items.value = items.value.filter((x) => x.id !== id)
|
|
}
|
|
|
|
async function ignore(id: number, month: string, reason: string) {
|
|
await api.post(`/recurring/${id}/ignore`, { month, reason })
|
|
await fetchMonthlyStatus(month)
|
|
}
|
|
|
|
async function unignore(id: number, month: string) {
|
|
await api.delete(`/recurring/${id}/ignore?month=${month}`)
|
|
await fetchMonthlyStatus(month)
|
|
}
|
|
|
|
const pendingCount = (month: string) =>
|
|
monthlyStatus.value.filter((s) => !s.covered).length
|
|
|
|
return { items, monthlyStatus, loading, error, fetchAll, fetchMonthlyStatus, create, update, remove, ignore, unignore, pendingCount }
|
|
})
|