/api/user/subscriptions/coupon (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.subscriptions.Coupon.get(req)Returns object
{
"couponid": "COUPON48",
"object": "coupon",
"stripeObject": {
"id": "COUPON48",
"object": "coupon",
"amount_off": 47,
"created": 1656123573,
"currency": "usd",
"duration": "once",
"duration_in_months": null,
"livemode": false,
"max_redemptions": null,
"metadata": {},
"name": "coupon name",
"percent_off": null,
"redeem_by": null,
"times_redeemed": 0,
"valid": true
},
"active": null,
"appid": "tests_1656123572",
"createdAt": "2022-06-25T02:19:33.297Z",
"updatedAt": "2022-06-25T02:19:33.297Z"
}
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 = {
auth: false,
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)
if (coupon.max_redemptions && coupon.max_redemptions === coupon.times_redeemed) {
throw new Error('invalid-coupon')
}
}
return coupon
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/subscriptions/coupon', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-couponid', () => {
it('missing querystring couponid', async () => {
const req = TestHelper.createRequest('/api/user/subscriptions/coupon')
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
it('invalid querystring couponid', async () => {
const req = TestHelper.createRequest('/api/user/subscriptions/coupon?couponid=invalid')
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.createAdministrator()
await TestHelper.createCoupon(administrator, {
active: 'true'
})
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/coupon?couponid=${administrator.coupon.couponid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const coupon = await req.get()
assert.strictEqual(coupon.couponid, administrator.coupon.couponid)
})
})
})