/api/administrator/subscriptions/set-product-inactive (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.SetProductInactive.patch(req)Returns object
{
"productid": "prod_LwH0lyTydWXGY1",
"object": "product",
"stripeObject": {
"id": "prod_LwH0lyTydWXGY1",
"object": "product",
"active": false,
"attributes": [],
"created": 1656123029,
"default_price": null,
"description": null,
"images": [],
"livemode": false,
"metadata": {},
"name": "product211",
"package_dimensions": null,
"shippable": null,
"statement_descriptor": "product211 description",
"tax_code": "txcd_41060003",
"type": "service",
"unit_label": "thing",
"updated": 1656123029,
"url": null
},
"active": false,
"appid": "tests_1656123028",
"createdAt": "2022-06-25T02:10:29.424Z",
"updatedAt": "2022-06-25T02:10:29.734Z"
}
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-product | ineligible querystring product is not active |
invalid-productid | missing querystring productid |
invalid querystring productid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const stripeCache = require('../../../../stripe-cache.js')
const subscriptions = require('../../../../../index.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.stripeObject.active) {
throw new Error('invalid-product')
}
const updateInfo = {
active: 'false'
}
const productNow = await stripeCache.execute('products', 'update', req.query.productid, updateInfo, req.stripeKey)
await subscriptions.Storage.Product.update({
stripeObject: productNow,
active: productNow.active
}, {
where: {
productid: req.query.productid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.productid)
return global.api.administrator.subscriptions.Product.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/administrator/subscriptions/set-product-inactive', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-productid', () => {
it('missing querystring productid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-product-inactive')
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()
const req = TestHelper.createRequest('/api/administrator/subscriptions/set-product-inactive?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('ineligible querystring product is not active', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createProduct(administrator, {
active: 'false'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-inactive?productid=${administrator.product.productid}`)
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, {
active: 'true'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-product-inactive?productid=${administrator.product.productid}`)
req.account = administrator.account
req.session = administrator.session
req.filename = __filename
req.saveResponse = true
const price = await req.patch()
assert.strictEqual(price.stripeObject.active, false)
})
})
})