/api/administrator/subscriptions/set-plan-unpublished (PATCH)
await global.api.administrator.subscriptions.SetPlanUnpublished.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 planid | |
invalid-planid | missing querystring planid |
ineligible querystring plan is already unpublished | |
invalid-plan | ineligible querystring plan 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.planid) {
throw new Error('invalid-planid')
}
const plan = await global.api.administrator.subscriptions.Plan.get(req)
if (!plan) {
throw new Error('invalid-planid')
}
if (!plan.metadata.published || plan.metadata.unpublished) {
throw new Error('invalid-plan')
}
const updateInfo = {
metadata: {
unpublished: dashboard.Timestamp.now
}
}
const planNow = await stripeCache.execute('plans', 'update', req.query.planid, updateInfo, req.stripeKey)
await stripeCache.update(planNow)
await subscriptions.StorageList.remove(`${req.appid}/published/plans`, req.query.planid)
await subscriptions.StorageList.add(`${req.appid}/unpublished/plans`, req.query.planid)
return planNow
}
}
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')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
describe('/api/administrator/subscriptions/set-plan-unpublished', () => {
describe('exceptions', () => {
describe('invalid-planid', () => {
it('missing querystring planid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-plan-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-planid')
})
it('invalid querystring planid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-plan-unpublished?planid=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-planid')
})
})
describe('invalid-plan', () => {
it('ineligible querystring plan is not published', async () => {
const administrator = await TestStripeAccounts.createOwnerWithNotPublishedPlan()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-plan-unpublished?planid=${administrator.plan.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-plan')
})
it('ineligible querystring plan is already unpublished', async () => {
const administrator = await TestStripeAccounts.createOwnerWithUnpublishedPlan()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-plan-unpublished?planid=${administrator.plan.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-plan')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestStripeAccounts.createOwnerWithPlan()
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-plan-unpublished?planid=${administrator.plan.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const plan = await req.patch()
assert.notStrictEqual(plan.metadata.unpublished, undefined)
assert.notStrictEqual(plan.metadata.unpublished, null)
})
})
})