208 lines
4.9 KiB
TypeScript
208 lines
4.9 KiB
TypeScript
import { ApiClient } from './base'
|
|
import type { Currency } from './data'
|
|
import { type Page, type PageRequest } from './pagination'
|
|
import type { Profile } from './profile'
|
|
|
|
export interface TransactionVendor {
|
|
id: number
|
|
name: string
|
|
description: string
|
|
}
|
|
|
|
export interface TransactionVendorPayload {
|
|
name: string
|
|
description: string
|
|
}
|
|
|
|
export interface TransactionCategory {
|
|
id: number
|
|
parentId: number | null
|
|
name: string
|
|
description: string
|
|
color: string
|
|
}
|
|
|
|
export interface TransactionCategoryTree {
|
|
id: number
|
|
parentId: number | null
|
|
name: string
|
|
description: string
|
|
color: string
|
|
children: TransactionCategoryTree[]
|
|
depth: number
|
|
}
|
|
|
|
export interface Transaction {
|
|
id: number
|
|
timestamp: string
|
|
addedAt: string
|
|
amount: number
|
|
currency: string
|
|
description: string
|
|
vendorId: number | null
|
|
categoryId: number | null
|
|
}
|
|
|
|
export interface TransactionsListItem {
|
|
id: number
|
|
timestamp: string
|
|
addedAt: string
|
|
amount: number
|
|
currency: Currency
|
|
description: string
|
|
vendor: TransactionsListItemVendor | null
|
|
category: TransactionsListItemCategory | null
|
|
creditedAccount: TransactionsListItemAccount | null
|
|
debitedAccount: TransactionsListItemAccount | null
|
|
}
|
|
|
|
export interface TransactionsListItemVendor {
|
|
id: number
|
|
name: string
|
|
}
|
|
|
|
export interface TransactionsListItemCategory {
|
|
id: number
|
|
name: string
|
|
color: string
|
|
}
|
|
|
|
export interface TransactionsListItemAccount {
|
|
id: number
|
|
name: string
|
|
type: string
|
|
numberSuffix: string
|
|
}
|
|
|
|
export interface TransactionDetail {
|
|
id: number
|
|
timestamp: string
|
|
addedAt: string
|
|
amount: number
|
|
currency: Currency
|
|
description: string
|
|
vendor: TransactionVendor | null
|
|
category: TransactionCategory | null
|
|
creditedAccount: TransactionDetailAccount | null
|
|
debitedAccount: TransactionDetailAccount | null
|
|
tags: string[]
|
|
lineItems: TransactionDetailLineItem[]
|
|
}
|
|
|
|
export interface TransactionDetailAccount {
|
|
id: number
|
|
name: string
|
|
type: string
|
|
numberSuffix: string
|
|
}
|
|
|
|
export interface TransactionDetailLineItem {
|
|
idx: number
|
|
valuePerItem: number
|
|
quantity: number
|
|
description: string
|
|
category: TransactionCategory | null
|
|
}
|
|
|
|
export interface AddTransactionPayload {
|
|
timestamp: string
|
|
amount: number
|
|
currencyCode: string
|
|
description: string
|
|
vendorId: number | null
|
|
categoryId: number | null
|
|
creditedAccountId: number | null
|
|
debitedAccountId: number | null
|
|
tags: string[]
|
|
lineItems: AddTransactionPayloadLineItem[]
|
|
}
|
|
|
|
export interface AddTransactionPayloadLineItem {
|
|
valuePerItem: number
|
|
quantity: number
|
|
description: string
|
|
categoryId: number | null
|
|
}
|
|
|
|
export interface CreateCategoryPayload {
|
|
name: string
|
|
description: string
|
|
color: string
|
|
parentId: number | null
|
|
}
|
|
|
|
export class TransactionApiClient extends ApiClient {
|
|
readonly path: string
|
|
|
|
constructor(profile: Profile) {
|
|
super()
|
|
this.path = `/profiles/${profile.name}`
|
|
}
|
|
|
|
getVendors(): Promise<TransactionVendor[]> {
|
|
return super.getJson(this.path + '/vendors')
|
|
}
|
|
|
|
getVendor(id: number): Promise<TransactionVendor> {
|
|
return super.getJson(this.path + '/vendors/' + id)
|
|
}
|
|
|
|
createVendor(data: TransactionVendorPayload): Promise<TransactionVendor> {
|
|
return super.postJson(this.path + '/vendors', data)
|
|
}
|
|
|
|
updateVendor(id: number, data: TransactionVendorPayload): Promise<TransactionVendor> {
|
|
return super.putJson(this.path + '/vendors/' + id, data)
|
|
}
|
|
|
|
deleteVendor(id: number): Promise<void> {
|
|
return super.delete(this.path + '/vendors/' + id)
|
|
}
|
|
|
|
getCategories(): Promise<TransactionCategoryTree[]> {
|
|
return super.getJson(this.path + '/categories')
|
|
}
|
|
|
|
getCategory(id: number): Promise<TransactionCategory> {
|
|
return super.getJson(this.path + '/categories/' + id)
|
|
}
|
|
|
|
createCategory(data: CreateCategoryPayload): Promise<TransactionCategory> {
|
|
return super.postJson(this.path + '/categories', data)
|
|
}
|
|
|
|
updateCategory(id: number, data: CreateCategoryPayload): Promise<TransactionCategory> {
|
|
return super.postJson(this.path + '/categories/' + id, data)
|
|
}
|
|
|
|
deleteCategory(id: number): Promise<void> {
|
|
return super.delete(this.path + '/categories/' + id)
|
|
}
|
|
|
|
getTransactions(
|
|
paginationOptions: PageRequest | undefined = undefined,
|
|
): Promise<Page<TransactionsListItem>> {
|
|
return super.getJsonPage(this.path + '/transactions', paginationOptions)
|
|
}
|
|
|
|
getTransaction(id: number): Promise<TransactionDetail> {
|
|
return super.getJson(this.path + '/transactions/' + id)
|
|
}
|
|
|
|
addTransaction(data: AddTransactionPayload): Promise<TransactionDetail> {
|
|
return super.postJson(this.path + '/transactions', data)
|
|
}
|
|
|
|
updateTransaction(id: number, data: AddTransactionPayload): Promise<TransactionDetail> {
|
|
return super.putJson(this.path + '/transactions/' + id, data)
|
|
}
|
|
|
|
deleteTransaction(id: number): Promise<void> {
|
|
return super.delete(this.path + '/transactions/' + id)
|
|
}
|
|
|
|
getAllTags(): Promise<string[]> {
|
|
return super.getJson(this.path + '/transaction-tags')
|
|
}
|
|
}
|