/api/user/organizations/set-organization-owner (PATCH)
await global.api.user.organizations.SetOrganizationOwner.patch(req) Located in Organizations 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 organizationid | |
invalid-organizationid | missing querystring organizationid |
posted accountid is invalid | |
invalid-accountid | posted accountid is missing |
invalid-account | accessing account is not organization owner |
posted accountid is not member | |
posted accountid is organization owner |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const organizations = require('../../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.organizationid) {
throw new Error('invalid-organizationid')
}
const organization = await global.api.user.organizations.Organization.get(req)
if (!organization) {
throw new Error('invalid-organizationid')
}
if (organization.ownerid !== req.account.accountid) {
throw new Error('invalid-account')
}
if (!req.body || !req.body.accountid) {
throw new Error('invalid-accountid')
}
if (req.body.accountid === req.account.accountid) {
throw new Error('invalid-account')
}
req.query.accountid = req.body.accountid
const targetAccount = await global.api.administrator.Account.get(req)
if (!targetAccount) {
throw new Error('invalid-account')
}
let membership
const account = req.account
try {
req.account = { accountid: req.body.accountid }
membership = await global.api.user.organizations.OrganizationMembership.get(req)
} catch (error) {
}
req.account = account
if (!membership) {
throw new Error('invalid-account')
}
const query = req.query
req.query.accountid = req.body.accountid
const newOwner = await global.api.administrator.Account.get(req)
req.query = query
if (!newOwner || newOwner.deleted) {
throw new Error('invalid-account')
}
await organizations.StorageObject.setProperty(`${req.appid}/organization/${req.query.organizationid}`, 'ownerid', req.body.accountid)
organization.ownerid = req.body.accountid
return organization
}
}
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/organizations/set-organization-owner', () => {
describe('exceptions', () => {
describe('invalid-organizationid', () => {
it('missing querystring organizationid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/set-organization-owner')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organizationid')
})
it('invalid querystring organizationid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/set-organization-owner?organizationid=invalid')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organizationid')
})
})
describe('invalid-accountid', () => {
it('posted accountid is missing', async () => {
const owner = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
accountid: ''
}
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('posted accountid is invalid', async () => {
const owner = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('accessing account is not organization owner', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
it('posted accountid is organization owner', async () => {
const owner = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
accountid: owner.account.accountid
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
it('posted accountid is not member', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
accountid: user.account.accountid
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('object', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
const req = TestHelper.createRequest(`/api/user/organizations/set-organization-owner?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.filename = __filename
req.saveResponse = true
req.body = {
accountid: user.account.accountid
}
const organizationNow = await req.patch()
assert.strictEqual(user.account.accountid, organizationNow.ownerid)
})
})
})