/api/user/set-account-profile (PATCH)
await global.api.user.SetAccountProfile.patch(req) Located in Dashboard 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 accountid | |
invalid-accountid | missing querystring accountid |
invalid-account | ineligible accessing account |
invalid posted profileid | |
invalid-profileid | missing posted profileid |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.accountid) {
throw new Error('invalid-accountid')
}
const account = await global.api.user.Account.get(req)
if (!account) {
throw new Error('invalid-accountid')
}
if (!req.body || !req.body.profileid) {
throw new Error('invalid-profileid')
}
req.query.profileid = req.body.profileid
const profile = await global.api.user.Profile.get(req)
if (!profile) {
throw new Error('invalid-profileid')
}
const profileFields = req.userProfileFields || global.userProfileFields
const accountProperties = {
profileid: req.query.profileid
}
for (const field of profileFields) {
const displayName = global.profileFieldMap[field]
switch (field) {
case 'full-name':
accountProperties.firstName = req.body['first-name']
accountProperties.lastName = req.body['last-name']
continue
case 'contact-email':
accountProperties.contactEmail = req.body[field]
continue
case 'display-email':
accountProperties.displayName = req.body[field]
continue
case 'display-name':
accountProperties.displayName = req.body[field]
continue
case 'company-name':
accountProperties.companyName = req.body[field]
continue
case 'dob':
accountProperties.dob = dashboard.Format.date(req.body.dob)
continue
default:
accountProperties[displayName] = req.body[field]
continue
}
}
await dashboard.StorageObject.setProperties(`${req.appid}/account/${req.query.accountid}`, accountProperties)
return global.api.user.Account.get(req)
}
}
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/user/set-account-profile', () => {
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-account-profile')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/set-account-profile?accountid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const user = await TestHelper.createUser()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user2.account.accountid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-profileid', () => {
it('missing posted profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('invalid posted profileid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: 'invalid'
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestHelper.createUser()
const profile1 = user.profile
await TestHelper.createProfile(user, {
'first-name': user.profile.firstName,
'last-name': user.profile.lastName,
'contact-email': user.profile.contactEmail,
default: 'true'
})
const req = TestHelper.createRequest(`/api/user/set-account-profile?accountid=${user.account.accountid}`)
req.account = user.account
req.session = user.session
req.body = {
profileid: profile1.profileid
}
req.filename = __filename
req.saveResponse = true
const account = await req.patch()
assert.strictEqual(account.object, 'account')
})
})
})