All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.proshin.finapi.account.FpAccount Maven / Gradle / Ivy

There is a newer version: 0.1.116
Show newest version
/*
 * Copyright 2018 Roman Proshin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.proshin.finapi.account;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Optional;
import org.json.JSONObject;
import org.proshin.finapi.accesstoken.AccessToken;
import org.proshin.finapi.account.in.FpEditParameters;
import org.proshin.finapi.account.out.ClearingAccount;
import org.proshin.finapi.account.out.FpClearingAccount;
import org.proshin.finapi.account.out.FpHolder;
import org.proshin.finapi.account.out.Holder;
import org.proshin.finapi.account.out.Order;
import org.proshin.finapi.account.out.Status;
import org.proshin.finapi.bankconnection.BankConnection;
import org.proshin.finapi.bankconnection.FpBankConnections;
import org.proshin.finapi.endpoint.Endpoint;
import org.proshin.finapi.primitives.IterableJsonArray;
import org.proshin.finapi.primitives.optional.OptionalBigDecimalOf;
import org.proshin.finapi.primitives.optional.OptionalOf;
import org.proshin.finapi.primitives.optional.OptionalOffsetDateTimeOf;

@SuppressWarnings("ClassWithTooManyMethods")
public final class FpAccount implements Account {

    private final Endpoint endpoint;
    private final AccessToken token;
    private final JSONObject origin;
    private final String url;

    public FpAccount(final Endpoint endpoint, final AccessToken token, final JSONObject origin, final String url) {
        this.endpoint = endpoint;
        this.token = token;
        this.origin = origin;
        this.url = url;
    }

    @Override
    public Long id() {
        return this.origin.getLong("id");
    }

    /**
     * @todo #69 Think if account should create a lazy-loaded bank connection that contains only its identifier and
     *  only if necessary - loads the rest info
     */
    @Override
    public BankConnection bankConnection() {
        return new FpBankConnections(this.endpoint, this.token)
            .one(this.origin.getLong("bankConnectionId"));
    }

    @Override
    public Optional name() {
        return new OptionalOf<>(this.origin, "accountName", JSONObject::getString).get();
    }

    @Override
    public String number() {
        return this.origin.getString("accountNumber");
    }

    @Override
    public Optional subNumber() {
        return new OptionalOf<>(this.origin, "subAccountNumber", JSONObject::getString).get();
    }

    @Override
    public Optional iban() {
        return new OptionalOf<>(this.origin, "iban", JSONObject::getString).get();
    }

    @Override
    public Holder holder() {
        return new FpHolder(this.origin);
    }

    @Override
    public Optional currency() {
        return new OptionalOf<>(this.origin, "accountCurrency", JSONObject::getString).get();
    }

    @Override
    public Type type() {
        return new Type.TypeOf(this.origin.getInt("accountTypeId")).get();
    }

    @Override
    public Optional balance() {
        return new OptionalBigDecimalOf(this.origin, "balance").get();
    }

    @Override
    public Optional overdraft() {
        return new OptionalBigDecimalOf(this.origin, "overdraft").get();
    }

    @Override
    public Optional overdraftLimit() {
        return new OptionalBigDecimalOf(this.origin, "overdraftLimit").get();
    }

    @Override
    public Optional availableFunds() {
        return new OptionalBigDecimalOf(this.origin, "availableFunds").get();
    }

    @Override
    public Optional lastSuccessfulUpdate() {
        return new OptionalOffsetDateTimeOf(this.origin, "lastSuccessfulUpdate").get();
    }

    @Override
    public Optional lastUpdateAttempt() {
        return new OptionalOffsetDateTimeOf(this.origin, "lastUpdateAttempt").get();
    }

    @Override
    public boolean isNew() {
        return this.origin.getBoolean("isNew");
    }

    @Override
    public Status status() {
        return Status.valueOf(this.origin.getString("status"));
    }

    @Override
    public Iterable supportedOrders() {
        return new IterableJsonArray<>(
            this.origin.getJSONArray("supportedOrders"),
            (array, index) -> Order.valueOf(array.getString(index))
        );
    }

    @Override
    public Iterable clearingAccounts() {
        return new IterableJsonArray<>(
            this.origin.getJSONArray("clearingAccounts"),
            (array, index) -> new FpClearingAccount(array.getJSONObject(index))
        );
    }

    /**
     * @todo #69 Create infrastructure for tests which will validate submitted data so that tests can assure that
     *  the library submits correct JSON structures to the API (valid URL + HTTP method + auth header + JSON body).
     */
    @Override
    public void edit(final FpEditParameters parameters) {
        this.endpoint.patch(this.url + this.id(), this.token, parameters);
    }

    @Override
    public void delete(final Long id) {
        this.endpoint.delete(
            this.url + this.id(),
            this.token
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy