starling.js

import Customer from './entities/customer';
import Account from './entities/account';
import Address from './entities/address';
import Transaction from './entities/transaction';
import Card from './entities/card';
import OAuth from './entities/oauth';
import Contact from './entities/contact';
import Payment from './entities/payment';
import Mandate from './entities/mandate';

/**
 * Facade to dispatch operations to services
 */
class Starling {

  /**
   * Create an instance of the starling client
   * @param {Object=} options - configuration parameters
   */
  constructor (options) {
    const defaults = {
      apiUrl: 'https://api.starlingbank.com',
      oauthUrl: 'https://oauth.starlingbank.com',
      clientId: '',
      clientSecret: ''
    };

    this.config = Object.assign({}, defaults, options);

    this.customer = new Customer(this.config);
    this.account = new Account(this.config);
    this.address = new Address(this.config);
    this.transaction = new Transaction(this.config);
    this.payment = new Payment(this.config);
    this.mandate = new Mandate(this.config);
    this.contact = new Contact(this.config);
    this.card = new Card(this.config);
    this.oAuth = new OAuth(this.config);
  }

  /**
   * Gets the customer's details
   * @param {string=} accessToken - the oauth bearer token. If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  getCustomer (accessToken = this.config.accessToken) {
    return this.customer.getCustomer(accessToken);
  }

  /**
   * Gets the customer's account details
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  getAccount (accessToken = this.config.accessToken) {
    return this.account.getAccount(accessToken);
  }

  /**
   * Gets the customer's balance
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  getBalance (accessToken = this.config.accessToken) {
    return this.account.getBalance(accessToken);
  }

  /**
   * Gets the customer's addresses (current and previous)
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  getAddresses (accessToken = this.config.accessToken) {
    return this.address.getAddresses(accessToken);
  }

  /**
   * Gets the customer's transaction history
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @param {string} fromDate - filter transactions after this date. Format: YYYY-MM-DD (optional,
   *   defaults to most recent 100 transactions)
   * @param {string} toDate - filter transactions before this date. Format: YYYY-MM-DD (optional,
   *   defaults to current date if not provided)
   * @param {string=} source - the transaction type (e.g. faster payments, mastercard).
   * If not specified, results are not filtered by source.
   * @return {Promise} - the http request promise
   */
  getTransactions (accessToken = this.config.accessToken, fromDate, toDate, source) {
    return this.transaction.getTransactions(accessToken, fromDate, toDate, source);
  }

  /**
   * Gets the full details of a single transaction
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @param {string} transactionId - the unique transaction ID
   * @param {string=} source - the transaction type (e.g. faster payments, mastercard).
   * If not specified, only generic transaction information will be returned.
   * @return {Promise} - the http request promise
   */
  getTransaction (accessToken = this.config.accessToken, transactionId, source) {
    return this.transaction.getTransaction(accessToken, transactionId, source);
  }

  /**
   * Gets the customer's current direct-debit mandates
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  listMandates (accessToken = this.config.accessToken) {
    return this.mandate.listMandates(accessToken);
  }

  /**
   * Deletes specific direct debit mandate
   * @param {string} accessToken - the oauth bearer token.
   * @param {string} mandateId - the unique mandate ID
   * @return {Promise} - the http request promise
   */
  deleteMandate (accessToken = this.config.accessToken, mandateId) {
    return this.mandate.deleteMandate(accessToken, mandateId);
  }

  /**
   * Lists the customer's scheduled payments
   * @param {string} accessToken - the oauth bearer token.
   * @return {Promise} - the http request promise
   */
  listScheduledPayments (accessToken = this.config.accessToken) {
    return this.payment.listScheduledPayments(accessToken);
  }

  /**
   * Makes a payment on behalf of the customer to another UK bank account using the Faster Payments network
   * @param {string} accessToken - the oauth bearer token.
   *  @param {string} destinationAccountId - the account identifier of the recipient
   * @param {string} reference - The payment reference, max. 18 characters.
   * @param {string} amount - the amount to be send.
   * @param {string=} currency - the currency, optional, defaults to "GBP".
   * @return {Promise} - the http request promise
   */
  makeLocalPayment (accessToken = this.config.accessToken, destinationAccountId, reference, amount, currency = 'GBP') {
    return this.payment.makeLocalPayment(accessToken, destinationAccountId, reference, amount, currency);
  }

  /**
   * Gets the customer's contacts (payees)
   * @param {string} accessToken - the oauth bearer token.
   * @return {Promise} - the http request promise
   */
  getContacts (accessToken = this.config.accessToken) {
    return this.contact.getContacts(accessToken);
  }

  /**
   * Gets a specific contact (payee)
   * @param {string} accessToken - the oauth bearer token.
   * @param {string} contactId - the contact's ID.
   * @return {Promise} - the http request promise
   */
  getContactAccount (accessToken = this.config.accessToken, contactId) {
    return this.contact.getContactAccount(accessToken, contactId);
  }

  /**
   * Creates a contact (payee) for the customer
   * @param {string} accessToken - the oauth bearer token.
   * @param {string} name - the name of the new contact.
   * @param {string=} accountType - the account type (domestic or international), optional and defaults to UK_ACCOUNT_AND_SORT_CODE.
   * @param {string} accountNumber - the contact's bank account number.
   * @param {string} sortCode - the contact's sort code.
   * @param {string} customerId - the customer's ID.
   * @return {Promise} - the http request promise
   */
  createContact (accessToken = this.config.accessToken, name, accountType = 'UK_ACCOUNT_AND_SORT_CODE', accountNumber, sortCode, customerId) {
    return this.contact.createContact(accessToken, name, accountType, accountNumber, sortCode, customerId);
  }

  deleteContact (accessToken, contactId) {
    return this.contact.deleteContact(accessToken, contactId);
  }

  /**
   * Gets the customer's card
   * @param {string=} accessToken - the oauth bearer token.  If not
   * specified, the accessToken on the options object is used.
   * @return {Promise} - the http request promise
   */
  getCard (accessToken = this.config.accessToken) {
    return this.card.getCard(accessToken);
  }

  /**
   * Exchanges the authorization code for an access token
   * @param {string} authorizationCode - the authorization code, acquired from the user agent after the
   * user authenticates with starling
   * @return {Promise} - the http request promise
   */
  getAccessToken (authorizationCode) {
    return this.oAuth.getAccessToken(authorizationCode);
  }

  /**
   * Exchanges the authorization code for an access token
   * @param {string} refreshToken - the oauth refresh token, used to claim a new access token when the access token
   * expires. A new refresh token is also returned.
   * @return {Promise} - the http request promise
   */
  refreshAccessToken (refreshToken) {
    return this.oAuth.refreshAccessToken(refreshToken);
  }
}

module.exports = Starling;