318 lines
11 KiB
C++
318 lines
11 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module Nostro.App:Forms_impl;
|
||
|
|
import std;
|
||
|
|
import Nostro;
|
||
|
|
import Nostro.Ui;
|
||
|
|
import :Forms;
|
||
|
|
import :Format;
|
||
|
|
|
||
|
|
using namespace Nostro;
|
||
|
|
using namespace Nostro::App;
|
||
|
|
using Moment = std::chrono::system_clock::time_point;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::string_view Trimmed(std::string_view text) {
|
||
|
|
while(text.starts_with(' ')) {
|
||
|
|
text.remove_prefix(1);
|
||
|
|
}
|
||
|
|
while(text.ends_with(' ')) {
|
||
|
|
text.remove_suffix(1);
|
||
|
|
}
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Read<std::int64_t> App::ReadAmount(std::string_view text, Currency currency) {
|
||
|
|
std::string amount(Trimmed(text));
|
||
|
|
if(amount.empty()) {
|
||
|
|
return {std::nullopt, "Enter an amount"};
|
||
|
|
}
|
||
|
|
// A comma is a decimal separator to most of Europe, and nostro's own amounts have a point.
|
||
|
|
std::ranges::replace(amount, ',', '.');
|
||
|
|
const std::size_t point = amount.find('.');
|
||
|
|
const std::size_t whole = std::min(point, amount.size());
|
||
|
|
const std::size_t decimals = point == std::string::npos ? 0 : amount.size() - point - 1;
|
||
|
|
if(amount.starts_with('-') || whole == 0 || !std::ranges::all_of(amount, [](char c) { return (c >= '0' && c <= '9') || c == '.'; }) || std::ranges::count(amount, '.') > 1) {
|
||
|
|
return {std::nullopt, "Not an amount: digits, as in 12,50"};
|
||
|
|
}
|
||
|
|
if(decimals > GetCurrencyDecimals(currency)) {
|
||
|
|
return {std::nullopt, std::format("At most {} decimals", GetCurrencyDecimals(currency))};
|
||
|
|
}
|
||
|
|
// Twelve digits is hundreds of billions: more is a slip of the finger, and far more would not fit the number.
|
||
|
|
if(whole > 12) {
|
||
|
|
return {std::nullopt, "That is too much"};
|
||
|
|
}
|
||
|
|
const std::int64_t minor = ParseAmount(amount, currency);
|
||
|
|
if(minor == 0) {
|
||
|
|
return {std::nullopt, "An amount above zero"};
|
||
|
|
}
|
||
|
|
return {minor, {}};
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string App::PartyKind(std::string_view text) {
|
||
|
|
const std::string_view party = Trimmed(text);
|
||
|
|
if(party.empty()) {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
if(party.contains('@')) {
|
||
|
|
return "E-mail address";
|
||
|
|
}
|
||
|
|
return party.starts_with('+') ? "Phone number" : "IBAN";
|
||
|
|
}
|
||
|
|
|
||
|
|
Read<BunqCounterparty> App::ReadParty(std::string_view text) {
|
||
|
|
const std::string_view party = Trimmed(text);
|
||
|
|
if(party.empty()) {
|
||
|
|
return {std::nullopt, "An IBAN, an e-mail address or a phone number"};
|
||
|
|
}
|
||
|
|
const std::size_t at = party.find('@');
|
||
|
|
if(at != std::string_view::npos && (at == 0 || at + 1 == party.size() || party.contains(' '))) {
|
||
|
|
return {std::nullopt, "Not an e-mail address"};
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return {BunqCounterparty::Parse(party, {}), {}};
|
||
|
|
} catch(const std::invalid_argument&) {
|
||
|
|
return {std::nullopt, party.starts_with('+') ? "Not a phone number: +31 6 12345678" : "Not a valid IBAN: check it for a slip"};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Read<Moment> App::ReadDay(std::string_view day, std::string_view time, bool end) {
|
||
|
|
const std::string_view date = Trimmed(day);
|
||
|
|
const std::string_view clock = Trimmed(time);
|
||
|
|
if(date.empty()) {
|
||
|
|
return {std::nullopt, "A day: YYYY-MM-DD"};
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
// The day by itself first, so that the error names the field that is wrong.
|
||
|
|
const Moment start = ParseLocalDate(date);
|
||
|
|
if(clock.empty()) {
|
||
|
|
return {end ? ParseLocalDateEnd(date) : start, {}};
|
||
|
|
}
|
||
|
|
} catch(const std::runtime_error&) {
|
||
|
|
return {std::nullopt, "Not a day: YYYY-MM-DD"};
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return {ParseLocalDate(std::format("{} {}", date, clock)), {}};
|
||
|
|
} catch(const std::runtime_error&) {
|
||
|
|
return {std::nullopt, "Not a time: hh:mm"};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Read<std::uint32_t> App::ReadCount(std::string_view text) {
|
||
|
|
const std::string_view digits = Trimmed(text);
|
||
|
|
std::uint32_t count = 0;
|
||
|
|
const std::from_chars_result result = std::from_chars(digits.data(), digits.data() + digits.size(), count);
|
||
|
|
if(digits.empty() || result.ec != std::errc() || result.ptr != digits.data() + digits.size() || count == 0) {
|
||
|
|
return {std::nullopt, "A whole number, 1 or more"};
|
||
|
|
}
|
||
|
|
return {count, {}};
|
||
|
|
}
|
||
|
|
|
||
|
|
bool PaymentForm::Errors::Any() const noexcept {
|
||
|
|
return !to.empty() || !name.empty() || !amount.empty() || !day.empty() || !time.empty() || !every.empty() || !until.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
PaymentForm::Errors PaymentForm::Check(Currency currency, Moment now, PaymentOrder& order) const {
|
||
|
|
Errors errors;
|
||
|
|
Read<BunqCounterparty> party = ReadParty(to.value);
|
||
|
|
errors.to = party.error;
|
||
|
|
const std::string_view holder = Trimmed(name.value);
|
||
|
|
// A bank wants the holder's name with an IBAN; an e-mail address or a phone number finds its own.
|
||
|
|
if(party.value && party.value->type == "IBAN" && holder.empty()) {
|
||
|
|
errors.name = "The name of the account's holder";
|
||
|
|
}
|
||
|
|
const Read<std::int64_t> sum = ReadAmount(amount.value, currency);
|
||
|
|
errors.amount = sum.error;
|
||
|
|
|
||
|
|
BunqSchedule schedule;
|
||
|
|
if(when == When::Scheduled) {
|
||
|
|
const Read<Moment> first = ReadDay(day.value);
|
||
|
|
errors.day = first.error;
|
||
|
|
if(first.value) {
|
||
|
|
const Read<Moment> start = ReadDay(day.value, time.value);
|
||
|
|
errors.time = start.error;
|
||
|
|
if(LocalDay(*first.value) < LocalDay(now)) {
|
||
|
|
errors.day = "Today or later";
|
||
|
|
}
|
||
|
|
schedule.start = start.value.value_or(*first.value);
|
||
|
|
}
|
||
|
|
schedule.unit = std::string(scheduleUnits[unit].unit);
|
||
|
|
// Once has nothing to repeat and nothing to end.
|
||
|
|
if(unit != 0) {
|
||
|
|
if(!Trimmed(every.value).empty()) {
|
||
|
|
const Read<std::uint32_t> count = ReadCount(every.value);
|
||
|
|
errors.every = count.error;
|
||
|
|
schedule.every = count.value.value_or(1);
|
||
|
|
}
|
||
|
|
if(!Trimmed(until.value).empty()) {
|
||
|
|
const Read<Moment> last = ReadDay(until.value, {}, true);
|
||
|
|
errors.until = last.error;
|
||
|
|
if(last.value && first.value && *last.value <= schedule.start) {
|
||
|
|
errors.until = "After the first payment";
|
||
|
|
}
|
||
|
|
schedule.end = last.value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(!errors.Any()) {
|
||
|
|
party.value->name = std::string(holder);
|
||
|
|
order = PaymentOrder{std::move(*party.value), *sum.value, std::string(Trimmed(description.value)), when, std::move(schedule)};
|
||
|
|
}
|
||
|
|
return errors;
|
||
|
|
}
|
||
|
|
|
||
|
|
void PaymentForm::Clear() {
|
||
|
|
to.Clear();
|
||
|
|
name.Clear();
|
||
|
|
amount.Clear();
|
||
|
|
description.Clear();
|
||
|
|
when = When::Now;
|
||
|
|
day.Clear();
|
||
|
|
time.Clear();
|
||
|
|
unit = 0;
|
||
|
|
every.Clear();
|
||
|
|
until.Clear();
|
||
|
|
tried = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool RequestForm::Errors::Any() const noexcept {
|
||
|
|
return !from.empty() || !name.empty() || !amount.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
RequestForm::Errors RequestForm::Check(Currency currency, RequestOrder& order) const {
|
||
|
|
Errors errors;
|
||
|
|
Read<BunqCounterparty> party = ReadParty(from.value);
|
||
|
|
errors.from = party.error;
|
||
|
|
const std::string_view holder = Trimmed(name.value);
|
||
|
|
if(party.value && party.value->type == "IBAN" && holder.empty()) {
|
||
|
|
errors.name = "The name of the account's holder";
|
||
|
|
}
|
||
|
|
const Read<std::int64_t> sum = ReadAmount(amount.value, currency);
|
||
|
|
errors.amount = sum.error;
|
||
|
|
if(!errors.Any()) {
|
||
|
|
party.value->name = std::string(holder);
|
||
|
|
order = RequestOrder{std::move(*party.value), *sum.value, std::string(Trimmed(description.value))};
|
||
|
|
}
|
||
|
|
return errors;
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequestForm::Clear() {
|
||
|
|
from.Clear();
|
||
|
|
name.Clear();
|
||
|
|
amount.Clear();
|
||
|
|
description.Clear();
|
||
|
|
tried = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FilterForm::Errors::Any() const noexcept {
|
||
|
|
return !amount.empty() || !from.empty() || !to.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
FilterForm::Errors FilterForm::Check(Currency currency, TransactionQuery& query) const {
|
||
|
|
Errors errors;
|
||
|
|
TransactionQuery asked;
|
||
|
|
asked.limit = query.limit;
|
||
|
|
asked.search = std::string(Trimmed(search.value));
|
||
|
|
if(!Trimmed(amount.value).empty()) {
|
||
|
|
const Read<std::int64_t> sum = ReadAmount(amount.value, currency);
|
||
|
|
errors.amount = sum.error;
|
||
|
|
if(sum.value) {
|
||
|
|
// What went out is stored below zero.
|
||
|
|
asked.amount = received ? *sum.value : -*sum.value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(!Trimmed(from.value).empty()) {
|
||
|
|
const Read<Moment> first = ReadDay(from.value);
|
||
|
|
errors.from = first.error;
|
||
|
|
asked.from = first.value.value_or(asked.from);
|
||
|
|
}
|
||
|
|
if(!Trimmed(to.value).empty()) {
|
||
|
|
const Read<Moment> last = ReadDay(to.value, {}, true);
|
||
|
|
errors.to = last.error;
|
||
|
|
if(last.value && *last.value < asked.from) {
|
||
|
|
errors.to = "On or after the first day";
|
||
|
|
}
|
||
|
|
asked.to = last.value.value_or(asked.to);
|
||
|
|
}
|
||
|
|
if(!errors.Any()) {
|
||
|
|
query = std::move(asked);
|
||
|
|
}
|
||
|
|
return errors;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<std::string> FilterForm::Chips(Currency currency, Moment now) const {
|
||
|
|
std::vector<std::string> chips;
|
||
|
|
if(const Read<std::int64_t> sum = ReadAmount(amount.value, currency); sum.value) {
|
||
|
|
chips.push_back(std::format("{} {}", received ? "Received" : "Paid", Money(*sum.value, currency)));
|
||
|
|
}
|
||
|
|
if(const Read<Moment> first = ReadDay(from.value); first.value) {
|
||
|
|
chips.push_back(std::format("From {}", ShortDate(*first.value, now)));
|
||
|
|
}
|
||
|
|
if(const Read<Moment> last = ReadDay(to.value); last.value) {
|
||
|
|
chips.push_back(std::format("Until {}", ShortDate(*last.value, now)));
|
||
|
|
}
|
||
|
|
return chips;
|
||
|
|
}
|
||
|
|
|
||
|
|
void FilterForm::Reset() {
|
||
|
|
amount.Clear();
|
||
|
|
received = false;
|
||
|
|
from.Clear();
|
||
|
|
to.Clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
BunqKeyForm::BunqKeyForm() {
|
||
|
|
ips.Set("*");
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string BunqKeyForm::Check() const {
|
||
|
|
return key.value.empty() ? "The API key, from the bunq app" : std::string();
|
||
|
|
}
|
||
|
|
|
||
|
|
void BunqKeyForm::Clear() {
|
||
|
|
key.Clear();
|
||
|
|
ips.Set("*");
|
||
|
|
advanced = false;
|
||
|
|
tried = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool BunqOAuthForm::Errors::Any() const noexcept {
|
||
|
|
return !clientId.empty() || !redirectUri.empty() || !secret.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
BunqOAuthForm::Errors BunqOAuthForm::Check() const {
|
||
|
|
Errors errors;
|
||
|
|
if(Trimmed(clientId.value).empty()) {
|
||
|
|
errors.clientId = "The client id bunq gave the OAuth client";
|
||
|
|
}
|
||
|
|
const std::string_view uri = Trimmed(redirectUri.value);
|
||
|
|
if(!uri.starts_with("https://") && !uri.starts_with("http://")) {
|
||
|
|
errors.redirectUri = "The redirect address registered with the client: https://…";
|
||
|
|
}
|
||
|
|
if(secret.value.empty()) {
|
||
|
|
errors.secret = "The client's secret";
|
||
|
|
}
|
||
|
|
return errors;
|
||
|
|
}
|
||
|
|
|
||
|
|
void BunqOAuthForm::Begin(std::string newState) {
|
||
|
|
state = std::move(newState);
|
||
|
|
address = BunqConnector::OAuthAuthorizationUrl(sandbox ? BunqEnvironment::Sandbox : BunqEnvironment::Production, Trimmed(clientId.value), Trimmed(redirectUri.value), state);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string BunqOAuthForm::Code() const {
|
||
|
|
return BunqConnector::OAuthCodeFromRedirect(Trimmed(redirected.value), state);
|
||
|
|
}
|
||
|
|
|
||
|
|
void BunqOAuthForm::Clear() {
|
||
|
|
clientId.Clear();
|
||
|
|
redirectUri.Clear();
|
||
|
|
secret.Clear();
|
||
|
|
sandbox = false;
|
||
|
|
redirected.Clear();
|
||
|
|
state.clear();
|
||
|
|
address.clear();
|
||
|
|
tried = false;
|
||
|
|
}
|