Files
ezbookkeeping/src/models/imported_transaction.ts
T

148 lines
5.4 KiB
TypeScript

import { TransactionType } from '@/core/transaction.ts';
import type { TransactionCreateRequest, TransactionGeoLocationResponse } from './transaction.ts';
export class ImportTransaction implements ImportTransactionResponse {
public type: number;
public categoryId: string;
public originalCategoryName: string;
public time: number;
public utcOffset: number;
public sourceAccountId: string;
public originalSourceAccountName: string;
public originalSourceAccountCurrency: string;
public destinationAccountId: string;
public originalDestinationAccountName?: string;
public originalDestinationAccountCurrency?: string;
public sourceAmount: number;
public destinationAmount: number;
public tagIds: string[];
public originalTagNames: string[];
public comment: string;
public geoLocation?: TransactionGeoLocationResponse;
public actualCategoryName: string;
public actualSourceAccountName: string;
public actualDestinationAccountName?: string;
public index: number;
public selected: boolean;
public valid: boolean;
private constructor(response: ImportTransactionResponse, index: number) {
this.type = response.type;
this.categoryId = response.categoryId;
this.originalCategoryName = response.originalCategoryName;
this.time = response.time;
this.utcOffset = response.utcOffset;
this.sourceAccountId = response.sourceAccountId;
this.originalSourceAccountName = response.originalSourceAccountName;
this.originalSourceAccountCurrency = response.originalSourceAccountCurrency;
this.destinationAccountId = response.destinationAccountId || '';
this.originalDestinationAccountName = response.originalDestinationAccountName;
this.originalDestinationAccountCurrency = response.originalDestinationAccountCurrency;
this.sourceAmount = response.sourceAmount;
this.destinationAmount = response.destinationAmount || 0;
this.tagIds = response.tagIds || [];
this.originalTagNames = response.originalTagNames || [];
this.comment = response.comment;
this.geoLocation = response.geoLocation;
this.actualCategoryName = response.originalCategoryName;
this.actualSourceAccountName = response.originalSourceAccountName;
this.actualDestinationAccountName = response.originalDestinationAccountName;
this.index = index;
this.selected = false;
this.valid = this.isTransactionValid();
}
public toCreateRequest(): TransactionCreateRequest {
return {
type: this.type,
categoryId: this.categoryId,
time: this.time,
utcOffset: this.utcOffset,
sourceAccountId: this.sourceAccountId,
destinationAccountId: this.type === TransactionType.Transfer ? this.destinationAccountId : '0',
sourceAmount: this.sourceAmount,
destinationAmount: this.type === TransactionType.Transfer ? this.destinationAmount : 0,
hideAmount: false,
tagIds: this.tagIds,
pictureIds: [],
comment: this.comment,
geoLocation: this.geoLocation,
clientSessionId: ''
};
}
public isTransactionValid(): boolean {
if (this.type !== TransactionType.ModifyBalance && (!this.categoryId || this.categoryId === '0')) {
return false;
}
if (!this.sourceAccountId || this.sourceAccountId === '0') {
return false;
}
if (this.type === TransactionType.Transfer && (!this.destinationAccountId || this.destinationAccountId === '0')) {
return false;
}
if (this.tagIds && this.tagIds.length) {
for (const tagId of this.tagIds) {
if (!tagId || tagId === '0') {
return false;
}
}
}
return true;
}
public static of(response: ImportTransactionResponse, index: number): ImportTransaction {
return new ImportTransaction(response, index);
}
}
export interface ImportTransactionRequest {
readonly transactions: ImportTransactionRequestItem[];
}
export interface ImportTransactionRequestItem {
readonly time: string;
readonly utcOffset: string;
readonly type: string;
readonly categoryName?: string;
readonly sourceAccountName?: string;
readonly destinationAccountName?: string;
readonly sourceAmount: string;
readonly destinationAmount?: string;
readonly geoLocation?: string;
readonly tagNames?: string;
readonly comment?: string;
}
export interface ImportTransactionResponse {
readonly type: number;
readonly categoryId: string;
readonly originalCategoryName: string;
readonly time: number;
readonly utcOffset: number;
readonly sourceAccountId: string;
readonly originalSourceAccountName: string;
readonly originalSourceAccountCurrency: string;
readonly destinationAccountId?: string;
readonly originalDestinationAccountName?: string;
readonly originalDestinationAccountCurrency?: string;
readonly sourceAmount: number;
readonly destinationAmount?: number;
readonly tagIds: string[];
readonly originalTagNames: string[];
readonly comment: string;
readonly geoLocation?: TransactionGeoLocationResponse;
}
export interface ImportTransactionResponsePageWrapper {
readonly items: ImportTransactionResponse[];
readonly totalCount: number;
}