/api/administrator/subscriptions/subscription-item (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.SubscriptionItem.get(req)Returns object
{
"subscriptionitemid": "si_LwH3Q0GPLd4xbN",
"object": "subscriptionitem",
"stripeObject": {
"id": "si_LwH3Q0GPLd4xbN",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656123211,
"metadata": {},
"plan": {
"id": "price_1LEOV0HHqepMFuCXL6cBwXOA",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656123206,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwH3yjj8gEPMIi",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOV0HHqepMFuCXL6cBwXOA",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656123206,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwH3yjj8gEPMIi",
"recurring": {
"aggregate_usage": null,
"interval": "month",
"interval_count": 1,
"trial_period_days": null,
"usage_type": "licensed"
},
"tax_behavior": "inclusive",
"tiers_mode": null,
"transform_quantity": null,
"type": "recurring",
"unit_amount": 1000,
"unit_amount_decimal": "1000"
},
"quantity": 1,
"subscription": "sub_1LEOV5HHqepMFuCXtFbuHN9S",
"tax_rates": []
},
"accountid": "acct_7f8a3980066c5a16",
"subscriptionid": "sub_1LEOV5HHqepMFuCXtFbuHN9S",
"customerid": "cus_LwH3h1TB7i9xv3",
"appid": "tests_1656123206",
"createdAt": "2022-06-25T02:13:33.479Z",
"updatedAt": "2022-06-25T02:13:33.480Z"
}
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-subscriptionitemid | missing querystring subscriptionitemid |
invalid querystring subscriptionitemid |
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.subscriptionitemid) {
throw new Error('invalid-subscriptionitemid')
}
let subscriptionItem = await dashboard.StorageCache.get(req.query.subscriptionitemid)
if (!subscriptionItem) {
const subscriptionItemInfo = await subscriptions.Storage.SubscriptionItem.findOne({
where: {
subscriptionitemid: req.query.subscriptionitemid,
appid: req.appid || global.appid
}
})
if (!subscriptionItemInfo) {
throw new Error('invalid-subscriptionitemid')
}
subscriptionItem = {}
for (const field of subscriptionItemInfo._options.attributes) {
subscriptionItem[field] = subscriptionItemInfo.get(field)
}
await dashboard.StorageCache.set(req.query.subscriptionitemid, subscriptionItem)
}
return subscriptionItem
}
}
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')
describe('/api/administrator/subscriptions/subscription-item', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-subscriptionitemid', () => {
it('missing querystring subscriptionitemid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/subscription-item')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
it('invalid querystring subscriptionitemid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/subscription-item?subscriptionitemid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/subscription-item?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const subscriptionItemNow = await req.get()
assert.strictEqual(subscriptionItemNow.subscriptionitemid, user.subscription.stripeObject.items.data[0].id)
})
})
})