UNPKG

starkbank

Version:

SDK to facilitate Node integrations with Stark Bank

139 lines (132 loc) 6.54 kB
const rest = require('../utils/rest.js'); const check = require('starkcore').check; const Resource = require('starkcore').Resource; class Split extends Resource { /** * * Split object * * @description When you initialize a Split, the entity will not be automatically * created in the Stark Bank API. The 'create' function sends the objects * to the Stark Bank API and returns the list of created objects. * * Parameters (required): * @param amount [int]: value to send to receivers. ex: 1000 (= R$ 10.00) * @param receiverId [string]: split receiver unique id. ex: '5656565656565656' * * Attributes (return-only): * @param id [string]: unique id returned when split is created. ex: '5656565656565656' * @param source [string]: source receivable which generated this split object. ex: '5656565656565656' * @param externalId [string]: unique id, generated by the system, to avoid duplicated splits. ex: 'invoice/1234/receiver/5678' * @param tags [list of strings, default None]: list of strings for tagging * @param scheduled [string, default now]: payment scheduled date or datetime. ex: '2020-03-10 10:30:00.000000+00:00' * @param status [string]: current payment status. ex: 'success', 'failed', 'canceled', 'failed' or 'processing' * @param created [string]: creation datetime for the payment. ex: '2020-03-10 10:30:00.000000+00:00' * @param updated [string]: update datetime for the payment. ex: '2020-03-10 10:30:00.000000+00:00' * */ constructor({ amount, receiverId, id = null, source = null, externalId = null, tags = null, scheduled = null, status = null, created = null, updated = null }) { super(id); this.amount = amount; this.receiverId = receiverId; this.source = source; this.externalId = externalId; this.tags = tags; this.scheduled = scheduled; this.status = status; this.created = check.datetime(created); this.updated = check.datetime(updated); } } exports.Split = Split; let resource = {'class': exports.Split, 'name': 'Split'}; exports.resource = resource; exports.get = async function get(id, {user} = {}) { /** * * Retrieve a specific Split * * @description Receive a single Split object previously created in the Stark Bank API by passing its id * * Parameters (required): * @param id [string]: object unique id. ex: '5656565656565656' * * Parameters (optional): * @param user [Project object]: Project object. Not necessary if starkbank.user was set before function call * * Return: * @returns Split object with updated attributes * */ return rest.getId(resource, id, user); }; exports.query = async function query({ limit, after, before, tags, ids, receiverIds, status, user } = {}) { /** * * Retrieve Splits * * @description Receive a generator of Split objects previously created in the Stark Bank API * * Parameters (optional): * @param limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 * @param after [string, default null] date filter for objects created only after specified date. ex: '2020-03-10' * @param before [string, default null] date filter for objects created only before specified date. ex: '2020-03-10' * @param tags [list of strings, default null]: tags to filter retrieved objects. ex: ['tony', 'stark'] * @param ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545'] * @param receiverIds [list of strings, default None]: list of receiver ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545'] * @param status [string, default null]: filter for status of retrieved objects. ex: 'success', 'failed', 'canceled', 'failed' or 'processing' * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call * * Return: * @returns generator of Split objects with updated attributes * */ let query = { limit: limit, after: check.date(after), before: check.date(before), tags: tags, ids: ids, receiverIds: receiverIds, status: status, }; return rest.getList(resource, query, user); }; exports.page = async function ({ cursor, limit, after, before, tags, ids, receiverIds, status, user } = {}) { /** * * Retrieve Splits * * @description Receive a list of up to 100 Split objects previously created in the Stark Bank API and the cursor to the next page. * Use this function instead of query if you want to manually page your requests. * * Parameters (optional): * @param cursor [string, default null]: cursor returned on the previous page function call * @param limit [integer, default null]: maximum number of objects to be retrieved. Unlimited if null. ex: 35 * @param after [string, default null] date filter for objects created only after specified date. ex: '2020-03-10' * @param before [string, default null] date filter for objects created only before specified date. ex: '2020-03-10' * @param tags [list of strings, default null]: tags to filter retrieved objects. ex: ['tony', 'stark'] * @param ids [list of strings, default null]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545'] * @param receiverIds [list of strings, default None]: list of receiver ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545'] * @param status [string, default null]: filter for status of retrieved objects. ex: 'success', 'failed', 'canceled', 'failed' or 'processing' * @param user [Organization/Project object, default null]: Organization or Project object. Not necessary if starkbank.user was set before function call * * Return: * @returns list of Split objects with updated attributes and cursor to retrieve the next page of objects * */ let query = { cursor: cursor, limit: limit, after: check.date(after), before: check.date(before), tags: tags, ids: ids, receiverIds: receiverIds, status: status, }; return rest.getPage(resource, query, user); };