Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat/#112] 전반적인 조회 API 수정 #115

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public IncomeStatementDto getIncomeStatement(Member member, LocalDateTime startD
.multiply(new BigDecimal(100));
revenuePercentage = revenue.divide(revenue.add(expense), 4, RoundingMode.HALF_UP)
.multiply(new BigDecimal(100));
} else if (expense.compareTo(BigDecimal.ZERO) == 0 && revenue.compareTo(BigDecimal.ZERO) > 0) {
revenuePercentage = BigDecimal.valueOf(100);
} else if (revenue.compareTo(BigDecimal.ZERO) == 0 && expense.compareTo(BigDecimal.ZERO) > 0) {
expensePercentage = BigDecimal.valueOf(100);
}

return IncomeStatementDto.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OpenAPI openAPI() {
---

### 🔑 테스트 사용자 인증 토큰
**eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzYjk5ZjYwZC1mOWY1LTQ2MmUtODg5NS0yM2E1MmVkZDI0NTEiLCJhdXRoIjoiVVNFUiIsImV4cCI6MTcwNzc4OTE2MX0.hrEnXsmipLqpWSv9pw4GHHo3LfIo_yFVM7Ojg9DwN6I**
**eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzYjk5ZjYwZC1mOWY1LTQ2MmUtODg5NS0yM2E1MmVkZDI0NTEiLCJhdXRoIjoiVVNFUiIsImV4cCI6MTcwNzg0NjgxOX0.ovjnSjn0Dmm1d8AYTlL4Fklh5KhHEDNpmW-3zr3ZRks**

""");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static TransactionDto toTransactionDto(Transaction transaction) {
.transactionId(transaction.getId())
.date(transaction.getDate())
.summary(transaction.getSummary())
.type(transaction.getType())
.debitAccounts(debitAccounts)
.creditAccounts(creditAccounts)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.friends.easybud.member.domain.Member;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -31,6 +33,9 @@ public class Transaction extends BaseTimeEntity {
private LocalDateTime date;
private String summary;

@Enumerated(value = EnumType.STRING)
private TransactionType type;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
Expand All @@ -39,9 +44,10 @@ public class Transaction extends BaseTimeEntity {
private List<Account> accounts = new ArrayList<>();

@Builder
public Transaction(LocalDateTime date, String summary, Member member) {
public Transaction(LocalDateTime date, String summary, TransactionType type, Member member) {
this.date = date;
this.summary = summary;
this.type = type;
this.member = member;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.friends.easybud.transaction.domain;

public enum TransactionType {
EXPENSE_TRANSACTION, REVENUE_TRANSACTION, ACCOUNT_TRANSFER
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.friends.easybud.transaction.dto;

import com.friends.easybud.transaction.domain.AccountType;
import com.friends.easybud.transaction.domain.TransactionType;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
Expand Down Expand Up @@ -47,6 +48,9 @@ public static class TransactionDto {
@Schema(description = "적요", example = "스타벅스")
private String summary;

@Schema(description = "거래 유형", example = "EXPENSE_TRANSACTION")
private TransactionType type;

@ArraySchema(schema = @Schema(description = "차변 계정 목록", implementation = AccountDto.class))
private List<AccountDto> debitAccounts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public List<AccountInfo> getAccountInfosByAccountNameAndMember(AccountName accou
account.accountType.typeState)
.from(account)
.join(account.transaction, transaction)
.where(account.accountType.typeName.eq(accountName))
.where(account.accountType.typeName.eq(accountName),
transaction.member.id.eq(memberId))
.fetch();

return results.stream().map(tuple -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import com.friends.easybud.global.response.code.ErrorStatus;
import com.friends.easybud.member.domain.Member;
import com.friends.easybud.transaction.domain.Account;
import com.friends.easybud.transaction.domain.AccountName;
import com.friends.easybud.transaction.domain.AccountState;
import com.friends.easybud.transaction.domain.Transaction;
import com.friends.easybud.transaction.domain.TransactionType;
import com.friends.easybud.transaction.dto.TransactionRequest.AccountCreateDto;
import com.friends.easybud.transaction.dto.TransactionRequest.TransactionCreateDto;
import com.friends.easybud.transaction.repository.AccountRepository;
import com.friends.easybud.transaction.repository.TransactionRepository;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -47,6 +51,7 @@ private Transaction buildTransaction(TransactionCreateDto request, Member member
Transaction transaction = Transaction.builder()
.date(request.getDate())
.summary(request.getSummary())
.type(getTransactionType(request.getAccounts()))
.member(member)
.build();
return transaction;
Expand Down Expand Up @@ -116,4 +121,34 @@ private void checkTransactionOwnership(Member member, Transaction transaction) {
}
}

private TransactionType getTransactionType(List<AccountCreateDto> accounts) {
BigDecimal totalExpenses = BigDecimal.ZERO;
BigDecimal totalRevenues = BigDecimal.ZERO;

for (AccountCreateDto account : accounts) {
if (account.getAccountType().getTypeName() == AccountName.EXPENSE) {
if (account.getAccountType().getTypeState() == AccountState.INCREASE) {
totalExpenses = totalExpenses.add(account.getAmount());
} else if (account.getAccountType().getTypeState() == AccountState.DECREASE) {
totalExpenses = totalExpenses.subtract(account.getAmount());
}

} else if (account.getAccountType().getTypeName() == AccountName.REVENUE) {
if (account.getAccountType().getTypeState() == AccountState.INCREASE) {
totalRevenues = totalRevenues.add(account.getAmount());
} else if (account.getAccountType().getTypeState() == AccountState.DECREASE) {
totalRevenues = totalRevenues.subtract(account.getAmount());
}
}
}

if (totalExpenses.compareTo(totalRevenues) > 0) {
return TransactionType.EXPENSE_TRANSACTION;
} else if (totalRevenues.compareTo(totalExpenses) > 0) {
return TransactionType.REVENUE_TRANSACTION;
} else {
return TransactionType.ACCOUNT_TRANSFER;
}
}

}
Loading