/api/administrator/subscriptions/coupon (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.Coupon.get(req)Returns object
{
"couponid": "COUPON30",
"object": "coupon",
"stripeObject": {
"id": "COUPON30",
"object": "coupon",
"amount_off": 30,
"created": 1656122217,
"currency": "usd",
"duration": "repeating",
"duration_in_months": 3,
"livemode": false,
"max_redemptions": null,
"metadata": {},
"name": "coupon name",
"percent_off": null,
"redeem_by": null,
"times_redeemed": 0,
"valid": true
},
"active": null,
"appid": "tests_1656122217",
"createdAt": "2022-06-25T01:56:57.724Z",
"updatedAt": "2022-06-25T01:56:57.724Z"
}
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-couponid | missing querystring couponid |
invalid querystring couponid |
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.couponid) {
throw new Error('invalid-couponid')
}
let coupon = await dashboard.StorageCache.get(req.query.couponid)
if (!coupon) {
const couponInfo = await subscriptions.Storage.Coupon.findOne({
where: {
couponid: req.query.couponid,
appid: req.appid || global.appid
}
})
if (!couponInfo) {
throw new Error('invalid-couponid')
}
coupon = {}
for (const field of couponInfo._options.attributes) {
coupon[field] = couponInfo.get(field)
}
await dashboard.StorageCache.set(req.query.couponid, coupon)
}
return coupon
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/administrator/subscriptions/coupon', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-couponid', () => {
it('missing querystring couponid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/coupon')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
it('invalid querystring couponid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/coupon?couponid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/coupon?couponid=${administrator.coupon.couponid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const coupon = await req.get()
assert.strictEqual(coupon.couponid, administrator.coupon.couponid)
})
})
})