/api/user/organizations/set-organization-owner (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.SetOrganizationOwner.patch(req)Returns object
{
"organizationid": "orgn_329938ffcc4218b4",
"object": "organization",
"appid": "tests_1656039715",
"ownerid": "acct_e8f6fe6245cffefd",
"name": "My organization",
"pin": "12345",
"email": "Fred_Prohaska98@yahoo.com",
"createdAt": "2022-06-24T03:01:55.119Z",
"updatedAt": "2022-06-24T03:01:55.159Z"
}
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-account | accessing account is not organization owner |
posted accountid is organization owner | |
posted accountid is not member | |
invalid-accountid | posted accountid is missing |
posted accountid is invalid | |
invalid-organizationid | missing querystring organizationid |
invalid querystring organizationid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
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.Storage.Organization.update({
ownerid: req.body.accountid
}, {
where: {
organizationid: req.query.organizationid,
appid: req.appid || global.appid
}
})
organization.ownerid = req.body.accountid
await dashboard.StorageCache.remove(req.query.organizationid)
return global.api.user.organizations.Organization.get(req)
}
}
Test source (view on github)
/* 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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.fullName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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.fullName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.fullName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
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)
})
})
})