/api/administrator/subscriptions/set-product-published (PATCH)
await global.api.administrator.subscriptions.SetProductPublished.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 productid | |
invalid-productid | missing querystring productid |
invalid-product | invalid querystring product is already 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.productid) {
throw new Error('invalid-productid')
}
const product = await global.api.administrator.subscriptions.Product.get(req)
if (!product) {
throw new Error('invalid-productid')
}
if (product.metadata.published || product.metadata.unpublished) {
throw new Error('invalid-product')
}
const updateInfo = {
metadata: {
published: dashboard.Timestamp.now
}
}
const productNow = await stripeCache.execute('products', 'update', req.query.productid, updateInfo, req.stripeKey)
await stripeCache.update(productNow)
await subscriptions.StorageList.add(`${req.appid}/published/products`, req.query.productid)
return productNow
}
}
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-product-published', () => {
describe('exceptions', () => {
describe('invalid-productid', () => {
it('missing querystring productid', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-product-published?productid=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-productid')
})
it('invalid querystring productid', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-product-published?productid=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-productid')
})
})
describe('invalid-product', () => {
it('invalid querystring product is already published', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
published: 'true'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-published?productid=${administrator.product.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-product')
})
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator)
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-published?productid=${administrator.product.id}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const product = await req.patch()
assert.notStrictEqual(product.metadata.published, undefined)
assert.notStrictEqual(product.metadata.published, null)
})
})
})