/api/administrator/subscriptions/delete-coupon (DELETE)
await global.api.administrator.subscriptions.DeleteCoupon.delete(req) Located in Stripe Subscriptions module API
Returns boolean
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 querystring couponid | |
invalid-couponid | missing querystring couponid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.couponid) {
throw new Error('invalid-couponid')
}
const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
if (!coupon) {
throw new Error('invalid-couponid')
}
await stripeCache.execute('coupons', 'del', req.query.couponid, req.stripeKey)
await subscriptions.StorageList.remove(`${req.appid}/coupons`, req.query.couponid)
if (coupon.metadata.unpublished) {
await subscriptions.StorageList.remove(`${req.appid}/unpublished/coupons`, req.query.couponid)
} else if (coupon.metadata.published) {
await subscriptions.StorageList.remove(`${req.appid}/published/coupons`, req.query.couponid)
}
await stripeCache.delete(req.query.couponid, req.stripeKey)
return true
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/administrator/subscriptions/delete-coupon', () => {
describe('exceptions', () => {
describe('invalid-couponid', () => {
it('missing querystring couponid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/delete-coupon')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} 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/delete-coupon?couponid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
})
})
describe('returns', () => {
it('boolean', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
published: 'true',
percent_off: '25',
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/delete-coupon?couponid=${administrator.coupon.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})