87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import { Body, Controller, Get, Param, Patch, Post, Query } from "@nestjs/common";
|
|
import { ok } from "../common/response";
|
|
import { UpdateDerivedDto } from "./dto/update-derived.dto";
|
|
import { CreateManualTransactionDto } from "./dto/create-manual-transaction.dto";
|
|
import { TransactionsService } from "./transactions.service";
|
|
|
|
@Controller("transactions")
|
|
export class TransactionsController {
|
|
constructor(private readonly transactionsService: TransactionsService) {}
|
|
|
|
@Get()
|
|
async list(@Query() query: Record<string, string>) {
|
|
const data = await this.transactionsService.list({
|
|
userId: query.user_id,
|
|
startDate: query.start_date,
|
|
endDate: query.end_date,
|
|
accountId: query.account_id,
|
|
minAmount: query.min_amount,
|
|
maxAmount: query.max_amount,
|
|
category: query.category,
|
|
source: query.source,
|
|
search: query.search,
|
|
includeHidden: query.include_hidden
|
|
});
|
|
return ok(data);
|
|
}
|
|
|
|
@Post("import")
|
|
async importCsv() {
|
|
const data = await this.transactionsService.importCsv();
|
|
return ok(data);
|
|
}
|
|
|
|
@Post("sync")
|
|
async sync(@Body() payload: { userId: string; startDate?: string; endDate?: string }) {
|
|
const endDate = payload.endDate ?? new Date().toISOString().slice(0, 10);
|
|
const startDate =
|
|
payload.startDate ??
|
|
new Date(new Date().setDate(new Date(endDate).getDate() - 30))
|
|
.toISOString()
|
|
.slice(0, 10);
|
|
const data = await this.transactionsService.sync(payload.userId, startDate, endDate);
|
|
return ok(data);
|
|
}
|
|
|
|
@Post("manual")
|
|
async manual(@Body() payload: CreateManualTransactionDto) {
|
|
const data = await this.transactionsService.createManualTransaction(payload);
|
|
return ok(data);
|
|
}
|
|
|
|
@Get("summary")
|
|
async summary(@Query() query: Record<string, string>) {
|
|
const endDate = query.end_date ?? new Date().toISOString().slice(0, 10);
|
|
const startDate =
|
|
query.start_date ??
|
|
new Date(new Date().setDate(new Date(endDate).getDate() - 30))
|
|
.toISOString()
|
|
.slice(0, 10);
|
|
const data = await this.transactionsService.summary(query.user_id ?? "", startDate, endDate);
|
|
return ok(data);
|
|
}
|
|
|
|
@Get("cashflow")
|
|
async cashflow(@Query() query: Record<string, string>) {
|
|
const months = query.months ? Number(query.months) : 6;
|
|
const data = await this.transactionsService.cashflow(query.user_id ?? "", months);
|
|
return ok(data);
|
|
}
|
|
|
|
@Get("merchants")
|
|
async merchants(@Query() query: Record<string, string>) {
|
|
const limit = query.limit ? Number(query.limit) : 6;
|
|
const data = await this.transactionsService.merchantInsights(
|
|
query.user_id ?? "",
|
|
limit
|
|
);
|
|
return ok(data);
|
|
}
|
|
|
|
@Patch(":id/derived")
|
|
async updateDerived(@Param("id") id: string, @Body() payload: UpdateDerivedDto) {
|
|
const data = await this.transactionsService.updateDerived(id, payload);
|
|
return ok(data);
|
|
}
|
|
}
|