/api/user/subscriptions/subscription-item (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.SubscriptionItem.get(req)Returns object
{
"subscriptionitemid": "si_LwHMzovcPnQRwh",
"object": "subscriptionitem",
"stripeObject": {
"id": "si_LwHMzovcPnQRwh",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1656124346,
"metadata": {},
"plan": {
"id": "price_1LEOnJHHqepMFuCXstzkC3Je",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 1000,
"amount_decimal": "1000",
"billing_scheme": "per_unit",
"created": 1656124341,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_LwHMmvJirqxYPE",
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"price": {
"id": "price_1LEOnJHHqepMFuCXstzkC3Je",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1656124341,
"currency": "usd",
"custom_unit_amount": null,
"livemode": false,
"lookup_key": null,
"metadata": {},
"nickname": null,
"product": "prod_LwHMmvJirqxYPE",
"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_1LEOnNHHqepMFuCXpt1N72pk",
"tax_rates": []
},
"accountid": "acct_4c6631b809bbc484",
"subscriptionid": "sub_1LEOnNHHqepMFuCXpt1N72pk",
"customerid": "cus_LwHMaNANorkjy9",
"appid": "tests_1656124341",
"createdAt": "2022-06-25T02:32:28.242Z",
"updatedAt": "2022-06-25T02:32:28.242Z"
}
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-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)
}
req.query.subscriptionid = subscriptionItem.subscriptionid
const subscription = await global.api.user.subscriptions.Subscription.get(req)
if (!subscription) {
throw new Error('invalid-subscriptionitemid')
}
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/user/subscriptions/subscription-item', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-subscriptionitemid', () => {
it('missing querystring subscriptionitemid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/subscription-item')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
it('invalid querystring subscriptionitemid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/subscription-item?subscriptionitemid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-subscriptionitemid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/subscription-item?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
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('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPrice()
const user = await TestStripeAccounts.createUserWithPaidSubscription(administrator.price)
const req = TestHelper.createRequest(`/api/user/subscriptions/subscription-item?subscriptionitemid=${user.subscription.stripeObject.items.data[0].id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const subscriptionItemNow = await req.get()
assert.strictEqual(subscriptionItemNow.subscriptionitemid, user.subscription.stripeObject.items.data[0].id)
})
})
})