/api/user/connect/payouts (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.connect.Payouts.get(req)Returns array
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
all | boolean | optional | URL |
limit | integer | optional | URL |
offset | integer | optional | URL |
stripeid | boolean | optional | URL |
Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid-account | ineligible accessing account |
invalid-payoutid | missing querystring payoutid |
invalid querystring payoutid |
NodeJS source (view on github)
const connect = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-account')
}
const where = {
accountid: req.query.accountid,
appid: req.appid || global.appid
}
if (req.query.stripeid) {
where.stripeid = req.query.stripeid
}
let payoutids
if (req.query.all) {
payoutids = await connect.Storage.Payout.findAll({
where,
attributes: ['payoutid'],
order: [
['createdAt', 'DESC']
]
})
} else {
const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
payoutids = await connect.Storage.Payout.findAll({
where,
attributes: ['payoutid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
if (!payoutids || !payoutids.length) {
return null
}
const items = []
for (const payoutInfo of payoutids) {
req.query.payoutid = payoutInfo.dataValues.payoutid
const payout = await global.api.user.connect.Payout.get(req)
items.push(payout)
}
return items
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/connect/payouts', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
let cachedResponses, cachedPayouts
async function bundledData (retryNumber) {
if (retryNumber > 0) {
cachedResponses = {}
}
if (cachedResponses && cachedResponses.finished) {
return
}
cachedResponses = {}
cachedPayouts = []
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const user = await TestStripeAccounts.createSubmittedIndividual('NZ')
for (let i = 0, len = global.pageSize + 2; i < len; i++) {
await TestHelper.createPayout(user)
cachedPayouts.unshift(user.payout.payoutid)
if (i === 2) {
await TestStripeAccounts.createSubmittedIndividual('NZ', user)
}
}
const req1 = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}`)
req1.account = user.account
req1.session = user.session
cachedResponses.returns = await req1.get()
global.pageSize = 3
cachedResponses.pageSize = await req1.get()
global.pageSize = 2
const req2 = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}&stripeid=${user.stripeAccount.stripeid}`)
req2.account = user.account
req2.session = user.session
cachedResponses.stripeid = await req2.get()
const req3 = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}&offset=1`)
req3.account = user.account
req3.session = user.session
cachedResponses.offset = await req3.get()
const req4 = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}&limit=1`)
req4.account = user.account
req4.session = user.session
cachedResponses.limit = await req4.get()
const req5 = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}&all=true`)
req5.account = user.account
req5.session = user.session
cachedResponses.all = await req5.get()
cachedResponses.finished = true
}
describe('exceptions', () => {
describe('invalid-payoutid', () => {
it('missing querystring payoutid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/connect/payouts')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring payoutid', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/connect/payouts?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async function () {
await bundledData(this.test.currentRetry())
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/connect/payouts?accountid=${user.account.accountid}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('receives', function () {
it('optional querystring offset (integer)', async function () {
await bundledData(this.test.currentRetry())
const offset = 1
const payoutsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(payoutsNow[i].payoutid, cachedPayouts[offset + i])
}
})
it('optional querystring limit (integer)', async function () {
await bundledData(this.test.currentRetry())
const limit = 1
const payoutsNow = cachedResponses.limit
assert.strictEqual(payoutsNow.length, limit)
})
it('optional querystring all (boolean)', async function () {
await bundledData(this.test.currentRetry())
const payoutsNow = cachedResponses.all
assert.strictEqual(payoutsNow.length, cachedPayouts.length)
})
it('optional querystring stripeid (boolean)', async function () {
await bundledData(this.test.currentRetry())
const payoutsNow = cachedResponses.stripeid
assert.strictEqual(payoutsNow.length, cachedPayouts.length - 3)
})
})
describe('returns', function () {
it('array', async function () {
await bundledData(this.test.currentRetry())
const payouts = cachedResponses.returns
assert.strictEqual(payouts.length, global.pageSize)
})
})
describe('configuration', function () {
it('environment PAGE_SIZE', async function () {
await bundledData(this.test.currentRetry())
const payouts = cachedResponses.pageSize
assert.strictEqual(payouts.length, global.pageSize + 1)
})
})
})