diff --git a/src/main/java/com/friends/easybud/transaction/converter/TransactionConverter.java b/src/main/java/com/friends/easybud/transaction/converter/TransactionConverter.java index 4e91453..a549d4b 100644 --- a/src/main/java/com/friends/easybud/transaction/converter/TransactionConverter.java +++ b/src/main/java/com/friends/easybud/transaction/converter/TransactionConverter.java @@ -2,7 +2,11 @@ import static com.friends.easybud.transaction.dto.TransactionResponse.TransactionDto; +import com.friends.easybud.global.exception.GeneralException; +import com.friends.easybud.global.response.code.ErrorStatus; import com.friends.easybud.transaction.domain.Account; +import com.friends.easybud.transaction.domain.AccountState; +import com.friends.easybud.transaction.domain.AccountType; import com.friends.easybud.transaction.domain.Transaction; import com.friends.easybud.transaction.dto.TransactionResponse.AccountDto; import com.friends.easybud.transaction.dto.TransactionResponse.TransactionListDto; @@ -19,7 +23,13 @@ public static TransactionListDto toTransactionListDto(List transact } public static TransactionDto toTransactionDto(Transaction transaction) { - List accountDtos = transaction.getAccounts().stream() + List debitAccounts = transaction.getAccounts().stream() + .filter(account -> isDebitAccount(account.getAccountType())) // 차변 계정인지 확인 + .map(TransactionConverter::toAccountDto) + .collect(Collectors.toList()); + + List creditAccounts = transaction.getAccounts().stream() + .filter(account -> !isDebitAccount(account.getAccountType())) // 대변 계정인지 확인 .map(TransactionConverter::toAccountDto) .collect(Collectors.toList()); @@ -28,7 +38,8 @@ public static TransactionDto toTransactionDto(Transaction transaction) { .date(transaction.getDate()) .summary(transaction.getSummary()) .type(transaction.getType()) - .accounts(accountDtos) + .debitAccounts(debitAccounts) + .creditAccounts(creditAccounts) .build(); } @@ -46,4 +57,21 @@ public static AccountDto toAccountDto(Account account) { .amount(account.getAmount()).build(); } + private static boolean isDebitAccount(AccountType accountType) { + switch (accountType.getTypeName()) { + case ASSET: + return accountType.getTypeState() == AccountState.INCREASE; + case LIABILITY: + case EQUITY: + return accountType.getTypeState() == AccountState.DECREASE; + case EXPENSE: + return accountType.getTypeState() == AccountState.OCCURRENCE; + case REVENUE: + return false; // 수익은 항상 대변 + default: + throw new GeneralException(ErrorStatus.ACCOUNT_NOT_FOUND); + } + } + + } diff --git a/src/main/java/com/friends/easybud/transaction/dto/TransactionResponse.java b/src/main/java/com/friends/easybud/transaction/dto/TransactionResponse.java index e0fcc4b..59eba48 100644 --- a/src/main/java/com/friends/easybud/transaction/dto/TransactionResponse.java +++ b/src/main/java/com/friends/easybud/transaction/dto/TransactionResponse.java @@ -51,8 +51,11 @@ public static class TransactionDto { @Schema(description = "거래 유형", example = "EXPENSE_TRANSACTION") private TransactionType type; - @ArraySchema(schema = @Schema(description = "계정 목록", implementation = AccountDto.class)) - private List accounts; + @ArraySchema(schema = @Schema(description = "차변 계정 목록", implementation = AccountDto.class)) + private List debitAccounts; + + @ArraySchema(schema = @Schema(description = "대변 계정 목록", implementation = AccountDto.class)) + private List creditAccounts; }