Skip to content

Commit

Permalink
dodany NBPExchangeService i wszystko wokół
Browse files Browse the repository at this point in the history
dodać handling odpowiedzi
  • Loading branch information
JanisBe committed May 22, 2024
1 parent 7416b0f commit 1078709
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ <h2 mat-dialog-title>Rozliczenie dla grupy {{ data.group.groupName }}</h2>
@if (data.debts) {
<div>
@for (settlement of data.debts | keyvalue; track settlement) {
<p> {{ settlement.key }} </p>
@if (settlement.key !== 'PLN') {
<mat-checkbox>przelicz do PLN</mat-checkbox>
}
<p> {{ settlement.key }}
@if (settlement.key !== 'PLN') {
<mat-checkbox (change)="calculate($event.checked)">przelicz do PLN</mat-checkbox>
}
</p>
@for (debt of settlement.value; track debt) {
<div>
<b matTooltip="{{debt.from.mail}}">{{ debt.from.name }}</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {MatTooltip} from '@angular/material/tooltip';
import {JsonPipe, KeyValuePipe, NgFor, NgIf} from '@angular/common';
import {Settlement} from "../../../model/settlement";
import {MatCheckbox} from "@angular/material/checkbox";
import {ExpenseService} from "../../../service/expense.service";

@Component({
selector: 'settlement-dialog',
Expand All @@ -24,6 +25,7 @@ import {MatCheckbox} from "@angular/material/checkbox";
export class SettlementDialogComponent {
constructor(
public dialogRef: MatDialogRef<SettlementDialogComponent>,
public expenseService: ExpenseService,
@Inject(MAT_DIALOG_DATA) public data: SettlementDialogData,
) {
console.log(data)
Expand All @@ -41,6 +43,24 @@ export class SettlementDialogComponent {
// return debts.reduce((accumulator, curValue) => accumulator + curValue.amount, 0) > 0;
// } || calculateDebts(data.debts)

calculate(change: boolean) {
if (change) {
Object.keys(this.data.debts)
.filter(currency => currency !== this.data.group.defaultCurrency)
.forEach(currency => {
// Iterate over each transaction in the array for the current currency
this.data.debts[currency].forEach(transaction => {
console.log(`From: ${transaction.from.name}, To: ${transaction.to.name}, Amount: ${transaction.amount}`);
this.expenseService.recalculateForeignCurrency(transaction.amount, currency).subscribe(exchangeRate => {
transaction.amount = +Math.floor(exchangeRate).toFixed(2);
currency = this.data.group.defaultCurrency!;
console.log(`From: ${transaction.from.name}, To: ${transaction.to.name}, Amount: ${transaction.amount}`);
});
});
});

}
}
}

export interface SettlementDialogData {
Expand Down
9 changes: 9 additions & 0 deletions src/main/frontend/src/app/service/expense.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ export class ExpenseService {
findById(expenseId: number) {
return this.httpClient.get<Expense>(`${environment.API_URL}/expense/findById/${expenseId}`);
}

recalculateForeignCurrency(amount: number, currency: string) {
return this.httpClient.get<number>(`${environment.API_URL}/expense/recalculateForeignCurrency`, {
params: {
amount,
currency
}
});
}
}
9 changes: 9 additions & 0 deletions src/main/java/pl/janis/komornik/dto/ExchangeRateResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pl.janis.komornik.dto;

public record ExchangeRateResponse(
String table,
String currency,
String code,
Rate[] rates
) {
}
10 changes: 10 additions & 0 deletions src/main/java/pl/janis/komornik/dto/Rate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pl.janis.komornik.dto;

import java.math.BigDecimal;

public record Rate(
String no,
String effectiveDate,
BigDecimal mid
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pl.janis.komornik.entities.User;
import pl.janis.komornik.service.ExpenseService;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,5 +58,11 @@ public void deleteExpense(@PathVariable int id) {
expenseService.deleteExpense(id);
}


@GetMapping("/recalculateForeignCurrency")
public BigDecimal recalculateForeignCurrency(@RequestParam BigDecimal amount, @RequestParam String currency) {
if (amount != null && currency != null) {
return expenseService.recalculateForeignCurrency(amount, currency);
}
return null;
}
}
5 changes: 5 additions & 0 deletions src/main/java/pl/janis/komornik/service/ExpenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ExpenseService {
private final ExpenseMapper expenseMapper;
private final UserService userService;
private final GroupService groupService;
private final NBPExchangeService nbpExchangeService;

public ExpenseDto findById(int id) {
return expenseMapper.toDto(expenseRepository.findById(id).orElseThrow(() -> new ElementDoesNotExistException("No results")));
Expand Down Expand Up @@ -121,4 +122,8 @@ public ExpenseDto saveExpense(ExpenseDto expenseDto, User currentUser) {
final Expense expense = expenseMapper.toEntity(expenseDto);
return expenseMapper.toDto(expenseRepository.save(expense));
}

public BigDecimal recalculateForeignCurrency(BigDecimal amount, String currency) {
return nbpExchangeService.getExchangeRate(amount, currency);
}
}
25 changes: 25 additions & 0 deletions src/main/java/pl/janis/komornik/service/NBPExchangeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pl.janis.komornik.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import pl.janis.komornik.dto.ExchangeRateResponse;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
public class NBPExchangeService {

public BigDecimal getExchangeRate(BigDecimal amount, String currency) {
LocalDate today = LocalDate.now();
String formattedDate = today.format(DateTimeFormatter.ISO_LOCAL_DATE);
String url = String.format("https://api.nbp.pl/api/exchangerates/rates/a/%s/%s/?format=json", currency, formattedDate);
RestTemplate restTemplate = new RestTemplate();
ExchangeRateResponse result = restTemplate.getForObject(url, ExchangeRateResponse.class);
if (result != null) {
return result.rates()[0].mid().multiply(amount);
}
return BigDecimal.ONE;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true
spring.banner.location=classpath:banner.txt
jwt.duration=1
jwt.secret-key=ENC(cD/Zb7b/kxjvWeMmk1ka+AUkCZOeKfx+UJvxQnC6eGv16N4hNjZhj8gAJ5uluJZ4NlrFY+uQzSA0+1OKDa9AZaSpuKOujChmAoUhpnK93iJ4Bh2FGoQYvgXG0M/QkaAV5XQetJeHX1FZZV/IPaHIzWiBps3mc/rf72NdZaHmyrKQhVcSP6NKY5c4a/o94auV)
spring.config.import=classpath:application-secrets.properties
spring.config.import=classpath:application-secrets.properties
spring.threads.virtual.enabled=true

0 comments on commit 1078709

Please sign in to comment.