/api/administrator/subscriptions/update-coupon (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.administrator.subscriptions.UpdateCoupon.patch(req)Returns object
{
"couponid": "COUPON47",
"object": "coupon",
"stripeObject": {
"id": "COUPON47",
"object": "coupon",
"amount_off": 46,
"created": 1656123333,
"currency": "usd",
"duration": "repeating",
"duration_in_months": 3,
"livemode": false,
"max_redemptions": null,
"metadata": {},
"name": "new-name",
"percent_off": null,
"redeem_by": null,
"times_redeemed": 0,
"valid": true
},
"active": null,
"appid": "tests_1656123331",
"createdAt": "2022-06-25T02:15:33.324Z",
"updatedAt": "2022-06-25T02:15:33.753Z"
}
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
name | string | required | POST |
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-couponid | missing querystring couponid |
invalid querystring couponid | |
invalid-name | missing posted name |
invalid posted name |
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.couponid) {
throw new Error('invalid-couponid')
}
const coupon = await global.api.administrator.subscriptions.Coupon.get(req)
if (!coupon) {
throw new Error('invalid-couponid')
}
if (!req.body.name || !req.body.name.length) {
throw new Error('invalid-name')
}
const updateInfo = {
name: req.body.name
}
const couponNow = await stripeCache.execute('coupons', 'update', req.query.couponid, updateInfo, req.stripeKey)
await subscriptions.Storage.Coupon.update({
stripeObject: couponNow
}, {
where: {
couponid: req.query.couponid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.couponid)
return global.api.administrator.subscriptions.Coupon.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/administrator/subscriptions/update-coupon', function () {
before(TestHelper.disableMetrics)
after(TestHelper.enableMetrics)
describe('exceptions', () => {
describe('invalid-couponid', () => {
it('missing querystring couponid', async () => {
const administrator = await TestHelper.createOwner()
const req = TestHelper.createRequest('/api/administrator/subscriptions/update-coupon')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'name'
}
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/update-coupon?couponid=invalid')
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'name'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-couponid')
})
})
describe('invalid-name', () => {
it('missing posted name', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.couponid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
name: ''
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-name')
})
it('invalid posted name', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.couponid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
name: ''
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-name')
})
})
})
describe('receives', () => {
it('required posted name', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.couponid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'testing'
}
const couponNow = await req.patch(req)
assert.strictEqual(couponNow.stripeObject.name, 'testing')
})
})
describe('returns', () => {
it('object', async () => {
const administrator = await TestHelper.createOwner()
await TestHelper.createCoupon(administrator, {
duration: 'repeating',
duration_in_months: '3'
})
const req = TestHelper.createRequest(`/api/administrator/subscriptions/update-coupon?couponid=${administrator.coupon.couponid}`)
req.account = administrator.account
req.session = administrator.session
req.body = {
name: 'new-name'
}
req.filename = __filename
req.saveResponse = true
const coupon = await req.patch()
assert.strictEqual(coupon.object, 'coupon')
})
})
})