Skip to content
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 @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import moneybuddy.fr.moneybuddy.model.Transaction;
import moneybuddy.fr.moneybuddy.service.TransactionService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -23,14 +24,20 @@
public class TransactionController {
private final TransactionService transactionService;

@GetMapping("/subAccount/{subAccountId}")
public ResponseEntity<List<Transaction>> getTransactions(
@GetMapping("/subAccount")
public ResponseEntity<Page<Transaction>> getTransactions(
@RequestHeader("Authorization") String authHeader,
@PathVariable String subAccountId,
@RequestParam(required = false) boolean isGoal) {
@RequestParam(required = false) String subAccountId,
@RequestParam(required = false) boolean isGoal,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "50") int size,
@RequestParam(defaultValue = "createdAt") String sortBy,
@RequestParam(defaultValue = "desc") String sortDir) {
String token = authHeader.substring(7);
return ResponseEntity.status(HttpStatus.OK)
.body(transactionService.getTransactions(token, subAccountId, isGoal));
.body(
transactionService.getTransactions(
token, subAccountId, isGoal, page, size, sortBy, sortDir));
}

@GetMapping("/goal/{goalId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class AddMoney {
@Positive(message = "Le montant doit être supérieur à zéro")
private BigDecimal amount;

@NotBlank(message = "Emoji est obligatoire")
private String emoji;

private String description;
private String goalId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class SubAccount {
@Builder.Default private BigDecimal money = BigDecimal.ZERO.setScale(2);
@Builder.Default private BigDecimal income = BigDecimal.ZERO.setScale(2);
@Builder.Default private int coin = 0;
@Builder.Default private int energy = 5;

@Builder.Default private String iconStyle = "bottts-neutral";
@Builder.Default private String iconName = "Mason";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public class Transaction {
private String oldAmount;
private String newAmount;
private String description;
private String emoji;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
@Repository
public interface TransactionRepository extends MongoRepository<Transaction, String> {

List<Transaction> findAllByChildId(String childId);
@Query(value = "{ 'goalId': { $ne: null }}")
Page<Transaction> findByChildIdAndGoal(String childId, Pageable pageable);

@Query(value = "{ 'goalId': { $ne: null }}")
List<Transaction> findByChildAndGoal(String childId);
Page<Transaction> findByAccountIdAndGoal(String accountId, Pageable pageable);

List<Transaction> findAllByGoalId(String goalId);

Page<Transaction> findAllByAccountId(String accountId, Pageable pageable);

Page<Transaction> findAllByAccountIdAndChildId(
String accountId, String childId, Pageable pageable);

Page<Transaction> findAllByParentId(String parentId, Pageable pageable);

Page<Transaction> findAllByChildId(String childId, Pageable pageable);
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/moneybuddy/fr/moneybuddy/service/CoinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import moneybuddy.fr.moneybuddy.model.enums.TransactionCategory;
import moneybuddy.fr.moneybuddy.model.enums.TransactionType;
import moneybuddy.fr.moneybuddy.repository.SubAccountRepository;
import moneybuddy.fr.moneybuddy.repository.TransactionRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CoinService {
private final SubAccountRepository subAccountRepository;
private final TransactionRepository transactionRepository;
private final TransactionService transactionService;

public int updateCoin(SubAccount subAccount, int coin, boolean isAdd) {
int currentCoin = subAccount.getCoin();
Expand All @@ -38,10 +37,31 @@ public int updateCoin(SubAccount subAccount, int coin, boolean isAdd) {
return currentCoin;
}

public void updateCoinForTask(SubAccount subAccount, Task task, boolean isAdd) {
public void updateCoinForCourseOrChapter(
SubAccount subAccount, int coinReward, String description) {
if (coinReward == 0) return;

int currentBalance = updateCoin(subAccount, coinReward, true);

Transaction transaction =
Transaction.builder()
.childId(subAccount.getId())
.accountId(subAccount.getAccountId())
.amount(String.valueOf(coinReward))
.oldAmount(String.valueOf(currentBalance))
.newAmount(String.valueOf(subAccount.getCoin()))
.description(description)
.type(TransactionType.CREDIT)
.category(TransactionCategory.COIN)
.createdAt(LocalDateTime.now())
.build();
transactionService.createTransaction(transaction);
}

public void updateCoinForTask(SubAccount subAccount, Task task) {
if (task.getCoinReward() == 0) return;

int currentBalance = updateCoin(subAccount, task.getCoinReward(), isAdd);
int currentBalance = updateCoin(subAccount, task.getCoinReward(), true);

Transaction transaction =
Transaction.builder()
Expand All @@ -52,10 +72,10 @@ public void updateCoinForTask(SubAccount subAccount, Task task, boolean isAdd) {
.oldAmount(String.valueOf(currentBalance))
.newAmount(String.valueOf(subAccount.getCoin()))
.description(task.getDescription())
.type(isAdd ? TransactionType.CREDIT : TransactionType.DEBIT)
.type(TransactionType.CREDIT)
.category(TransactionCategory.COIN)
.createdAt(LocalDateTime.now())
.build();
transactionRepository.save(transaction);
transactionService.createTransaction(transaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void createTransactionForIncome(Income income) {
.description(income.getTask().getDescription())
.incomeId(income.getId())
.build();

transactionService.createTransaction(transaction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import moneybuddy.fr.moneybuddy.model.enums.TransactionCategory;
import moneybuddy.fr.moneybuddy.model.enums.TransactionType;
import moneybuddy.fr.moneybuddy.repository.SubAccountRepository;
import moneybuddy.fr.moneybuddy.repository.TransactionRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MoneyService {
private final SubAccountRepository subAccountRepository;
private final TransactionRepository transactionRepository;
private final TransactionService transactionService;
private final JwtService jwtService;

public void updateMoney(AddMoney request, String token, boolean isAdd) {
Expand Down Expand Up @@ -67,10 +66,12 @@ public void updateMoney(AddMoney request, String token, boolean isAdd) {
.oldAmount(String.valueOf(currentBalance))
.newAmount(String.valueOf(newBalance))
.description(request.getDescription())
.emoji(request.getEmoji())
.type(isAdd ? TransactionType.CREDIT : TransactionType.DEBIT)
.category(TransactionCategory.MONEY)
.createdAt(LocalDateTime.now())
.build();
transactionRepository.save(transaction);

transactionService.createTransaction(transaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public CompletedCourse completeCourse(String token, String courseId) {

if (status == CompletedCourse.COMPLETED) {
if (subAccount.getRole() == SubAccountRole.CHILD)
coinService.updateCoin(subAccount, course.getCoinReward(), true);
coinService.updateCoinForCourseOrChapter(
subAccount,
course.getCoinReward(),
String.format("Cours %s complété !", course.getTitle()));

completeChapter(subAccount, course.getChapterId());
}
Expand All @@ -54,6 +57,9 @@ public void completeChapter(SubAccount subAccount, String chapterId) {
userProgressService.markChapterAsCompleted(subAccount, chapter);

if (subAccount.getRole() == SubAccountRole.CHILD)
coinService.updateCoin(subAccount, chapter.getCoinReward(), true);
coinService.updateCoinForCourseOrChapter(
subAccount,
chapter.getCoinReward(),
String.format("Chapitre %s complété !", chapter.getTitle()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public ResponseEntity<AuthResponse> completeTask(TaskComplete req, String token,

if (req.isDone()) {
incomeService.increaseSubAccountIncome(subAccount, task.getMoneyReward(), task);
coinService.updateCoinForTask(subAccount, task, true);
coinService.updateCoinForTask(subAccount, task);
task.setStatus(TaskStatus.COMPLETED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class TransactionService {
private final SubAccountService subAccountService;
private final TransactionRepository transactionRepository;
private final JwtService jwtService;
private final Utils utils;

public Page<Transaction> getAllTransactions(
Expand All @@ -36,11 +37,37 @@ public Page<Transaction> getAllTransactions(
return transactions;
}

public List<Transaction> getTransactions(String token, String subAccountId, boolean isGoal) {
List<Transaction> transactions =
isGoal
? transactionRepository.findByChildAndGoal(subAccountId)
: transactionRepository.findAllByChildId(subAccountId);
public Page<Transaction> getTransactions(
String token,
String subAccountIdParams,
boolean isGoal,
int page,
int size,
String sortBy,
String sortDir) {
SubAccountRole subAccountRole = jwtService.extractSubAccountRole(token);
subAccountRole =
subAccountRole.equals(SubAccountRole.OWNER) ? SubAccountRole.PARENT : subAccountRole;
String accountId = jwtService.extractSubAccountAccountId(token);
String subAccountId = jwtService.extractSubAccountId(token);

Pageable pageable = utils.pagination(page, size, sortBy, sortDir);
Page<Transaction> transactions;

if (subAccountRole.equals(SubAccountRole.PARENT)) {
transactions =
isGoal
? transactionRepository.findByAccountIdAndGoal(accountId, pageable)
: subAccountIdParams != null
? transactionRepository.findAllByAccountIdAndChildId(
accountId, subAccountIdParams, pageable)
: transactionRepository.findAllByAccountId(accountId, pageable);
} else {
transactions =
isGoal
? transactionRepository.findByChildIdAndGoal(subAccountId, pageable)
: transactionRepository.findAllByChildId(subAccountId, pageable);
}

return transactions;
}
Expand All @@ -64,9 +91,7 @@ public Page<Transaction> getAllTransactionsBySubAccountId(
}

public List<Transaction> getTransactionByGoalId(String goalId) {

List<Transaction> transactions = transactionRepository.findAllByGoalId(goalId);
return transactions;
return transactionRepository.findAllByGoalId(goalId);
}

public void createTransaction(Transaction transaction) {
Expand Down
Loading