/api/administrator/subscriptions/set-coupon-unpublished (PATCH)
await global.api.administrator.subscriptions.SetCouponUnpublished.patch(req) Located in Stripe Subscriptions module API
Returns object
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 |
ineligible querystring coupon is already unpublished | |
invalid-coupon | ineligible querystring coupon is not published |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('@userdashboard/dashboard')
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: 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')
}
if (!coupon.metadata.published || coupon.metadata.unpublished) {
throw new Error('invalid-coupon')
}
const updateInfo = {
metadata: {
unpublished: dashboard.Timestamp.now
}
}
const couponNow = await stripeCache.execute('coupons', 'update', req.query.couponid, updateInfo, req.stripeKey)
await stripeCache.update(couponNow)
await subscriptions.StorageList.remove(`${req.appid}/published/coupons`, req.query.couponid)
await subscriptions.StorageList.add(`${req.appid}/unpublished/coupons`, req.query.couponid)
return couponNow
}
}
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/set-coupon-unpublished', () => {
describe('exceptions', () => {
describe('invalid-couponid', () => {
it('missing querystring couponid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-coupon-unpublished')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} 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/set-coupon-unpublished?couponid=invalid')
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
})
describe('invalid-coupon', () => {
it('ineligible querystring coupon is not published', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
percent_off: '25',
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-coupon-unpublished?couponid=${administrator.coupon.id}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-coupon')
})
it('ineligible querystring coupon is already unpublished', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
published: 'true',
unpublished: 'true',
percent_off: '25',
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-coupon-unpublished?couponid=${administrator.coupon.id}`)
req.account = administrator.account
req.session = administrator.session
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-coupon')
})
})
})
describe('returns', () => {
it('object', 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/set-coupon-unpublished?couponid=${administrator.coupon.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const coupon = await req.patch()
assert.notStrictEqual(coupon.metadata.unpublished, undefined)
assert.notStrictEqual(coupon.metadata.unpublished, null)
})
})
})