/api/administrator/subscriptions/tax-code (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.TaxCode.get(req)Returns object
{
"taxcodeid": "txcd_41060003",
"object": "taxcode",
"description": "Milk type beverages from non-animal sources, such as soy, rice, almond, coconut, peanut, and hemp.",
"name": "Milk substitutes",
"createdAt": "2022-06-25T00:53:27.461Z",
"updatedAt": "2022-06-25T00:53:27.461Z"
}
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-taxcodeid | missing querystring taxcodeid |
invalid querystring taxcodeid |
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.taxcodeid) {
throw new Error('invalid-taxcodeid')
}
let taxRate = await dashboard.StorageCache.get(req.query.taxcodeid)
if (!taxRate) {
const taxRateInfo = await subscriptions.Storage.TaxCode.findOne({
where: {
taxcodeid: req.query.taxcodeid
}
})
if (!taxRateInfo) {
throw new Error('invalid-taxcodeid')
}
taxRate = {}
for (const field of taxRateInfo._options.attributes) {
taxRate[field] = taxRateInfo.get(field)
}
await dashboard.StorageCache.set(req.query.taxcodeid, taxRate)
}
return taxRate
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/administrator/subscriptions/tax-code', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-taxcodeid', () => {
it('missing querystring taxcodeid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/tax-code')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxcodeid')
})
it('invalid querystring taxcodeid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/tax-code?taxcodeid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-taxcodeid')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/tax-code?taxcodeid=txcd_41060003')
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const setupIntent = await req.get()
assert.strictEqual(setupIntent.object, 'taxcode')
})
})
})