Назад к задачам
Junior — Senior
6

Компонент для выполнения переводов с расчётом комиссии и отправкой уведомления

Компании, где спрашивали:

Т-БанкТ-Банк
Получайте помощь с лайвкодингом в реальном времени с Sobes Copilot
Условие задачи

Компонент реализует процесс перевода средств между пользователями. В нём реализована логика вычисления комиссии в зависимости от суммы перевода и интеграция с внешним сервисом отправки уведомлений.

Код‑ревью:

@Service
public class PaymentService {
    @Autowired
    private PaymentRepository paymentRepository;
    @Autowired
    private FeeRepository feeRepository;
    @Autowired
    private UserRepository userRepository;

    private NotificationRestClient notificationRestClient = new NotificationRestClient();
    private CbrRestClient cbrRestClient = new CbrRestClient();

    @Transactional
    public void processPayment(double amount, Currency currency, Long recipientId) {
        double amountInRub = amount * cbrRestClient.doRequest().getRates().get(currency.getCode());
        Long userId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userRepository.findUserById(userId).get();

        Payment payment = new Payment(amountInRub, user, recipientId);
        paymentRepository.save(payment);

        if (amountInRub < 1000) {
            Fee fee = new Fee(amountInRub * 0.015, user);
            feeRepository.save(fee);
        }
        if (amountInRub > 1000) {
            Fee fee = new Fee(amountInRub * 0.01, user);
            feeRepository.save(fee);
        }
        if (amountInRub > 5000) {
            Fee fee = new Fee(amountInRub * 0.005, user);
            feeRepository.save(fee);
        }

        try {
            notificationRestClient.notify(payment);
        } catch (Throwable t) {
            // do nothing
        }
    }
}

@Entity
class Fee {
    private Double value;

    @ManyToOne
    private User user;

    public Fee() {}
    public Fee(Double value, User user) {
        this.value = value;
        this.user = user;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    public void setUser(User user) {
        this.user = user;
    }
}