/api/user/subscriptions/tax-rate (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.TaxRate.get(req)Returns object
{
"taxrateid": "txr_1LEOpBHHqepMFuCXZcONO2Jz",
"object": "taxrate",
"stripeObject": {
"id": "txr_1LEOpBHHqepMFuCXZcONO2Jz",
"object": "tax_rate",
"active": true,
"country": "US",
"created": 1656124457,
"description": "Sales tax in NY",
"display_name": "NY Sales Tax",
"inclusive": true,
"jurisdiction": "US",
"livemode": false,
"metadata": {},
"percentage": 17.5,
"state": "NY",
"tax_type": "sales_tax"
},
"appid": "tests_1656124457",
"createdAt": "2022-06-25T02:34:17.443Z",
"updatedAt": "2022-06-25T02:34:17.443Z"
}
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-taxrate | invalid querystring taxrate is not active |
invalid-taxrateid | missing querystring taxrateid |
invalid querystring taxrateid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const subscriptions = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.taxrateid) {
throw new Error('invalid-taxrateid')
}
let taxRate = await dashboard.StorageCache.get(req.query.taxrateid)
if (!taxRate) {
const taxRateInfo = await subscriptions.Storage.TaxRate.findOne({
where: {
taxrateid: req.query.taxrateid,
appid: req.appid || global.appid
}
})
if (!taxRateInfo) {
throw new Error('invalid-taxrateid')
}
taxRate = {}
for (const field of taxRateInfo._options.attributes) {
taxRate[field] = taxRateInfo.get(field)
}
await dashboard.StorageCache.set(req.query.taxrateid, taxRate)
}
if (!taxRate.stripeObject.active) {
throw new Error('invalid-tax-rate')
}
return taxRate
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/subscriptions/tax-rate', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-taxrateid', () => {
it('missing querystring taxrateid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/tax-rate')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxrateid')
})
it('invalid querystring taxrateid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/tax-rate?taxrateid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxrateid')
})
})
describe('invalid-taxrate', () => {
it('invalid querystring taxrate is not active', async () => {
const owner = await TestHelper.createOwner()
await TestHelper.createTaxRate(owner, {
active: 'false'
})
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/tax-rate?taxrateid=${owner.taxRate.taxrateid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-tax-rate')
})
})
})
describe('returns', () => {
it('object', async () => {
const owner = await TestHelper.createOwner()
await TestHelper.createTaxRate(owner, {
active: true
})
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/tax-rate?taxrateid=${owner.taxRate.taxrateid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const taxrateid = await req.get()
assert.strictEqual(taxrateid.object, 'taxrate')
})
})
})