/api/user/organizations/create-organization (POST)
await global.api.user.organizations.CreateOrganization.post(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-organization-name | missing posted name |
posted name too long | |
invalid-organization-name-length | posted name too short |
invalid posted email | |
invalid-organization-email | missing posted email |
ineligible posted profileid missing fields | |
invalid posted profileid | |
invalid-profile | missing posted profileid |
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
organization-email | string | required | POST |
organization-name | string | required | POST |
profileid | string | required | POST |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const dashboard = require('@userdashboard/dashboard')
const organizations = require('../../../../../index.js')
module.exports = {
post: 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-account')
}
if (!req.body || !req.body.name || !req.body.name.length) {
throw new Error('invalid-organization-name')
}
if (!req.body.profileid || !req.body.profileid.length) {
throw new Error('invalid-profileid')
}
req.query.profileid = req.body.profileid
req.storage = organizations
const profile = await global.api.user.Profile.get(req)
if (!profile) {
throw new Error('invalid-profileid')
}
const requireProfileFields = global.membershipProfileFields
for (const field of requireProfileFields) {
if (field === 'full-name') {
if (!profile.firstName || !profile.lastName) {
throw new Error('invalid-profile')
}
continue
}
const displayName = global.profileFieldMap[field]
if (!profile[displayName]) {
throw new Error('invalid-profile')
}
}
if (global.minimumOrganizationNameLength > req.body.name.length ||
global.maximumOrganizationNameLength < req.body.name.length) {
throw new Error('invalid-organization-name-length')
}
if (!req.body.email || !req.body.email.length || req.body.email.indexOf('@') < 1) {
throw new Error('invalid-organization-email')
}
const organizationid = `organization_${await dashboard.UUID.generateID()}`
const organizationInfo = {
object: 'organization',
ownerid: req.query.accountid,
organizationid: organizationid,
name: req.body.name,
email: req.body.email,
created: dashboard.Timestamp.now
}
await organizations.Storage.write(`${req.appid}/organization/${organizationid}`, organizationInfo)
const membershipid = `membership_${await dashboard.UUID.generateID()}`
const membershipInfo = {
object: 'membership',
membershipid: membershipid,
organizationid: organizationid,
accountid: req.query.accountid,
created: dashboard.Timestamp.now,
profileid: req.body.profileid
}
await organizations.Storage.write(`${req.appid}/membership/${membershipid}`, membershipInfo)
await organizations.StorageList.addMany({
[`${req.appid}/organizations`]: organizationid,
[`${req.appid}/account/organizations/${req.query.accountid}`]: organizationid,
[`${req.appid}/memberships`]: membershipid,
[`${req.appid}/account/memberships/${req.query.accountid}`]: membershipid,
[`${req.appid}/organization/memberships/${organizationid}`]: membershipid
})
await organizations.Storage.write(`${req.appid}/map/organizationid/membershipid/${req.query.accountid}/${organizationid}`, membershipid)
return organizationInfo
}
}
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/create-organization', () => {
describe('exceptions', () => {
describe('invalid-organization-name', () => {
it('missing posted name', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: '',
email: owner.profile.contactEmail,
profileid: owner.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name')
})
})
})
describe('invalid-organization-name-length', () => {
it('posted name too short', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Sales',
email: owner.profile.contactEmail,
profileid: owner.profile.profileid
}
global.minimumOrganizationNameLength = 100
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name-length')
global.maximumOrganizationNameLength = 1
errorMessage = null
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name-length')
})
it('posted name too long', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Sales',
email: owner.profile.contactEmail,
profileid: owner.profile.profileid
}
global.maximumOrganizationNameLength = 1
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name-length')
global.maximumOrganizationNameLength = 1
errorMessage = null
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name-length')
})
})
describe('invalid-organization-email', () => {
it('missing posted email', 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,
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Family',
email: '',
profileid: owner.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-email')
})
it('invalid posted email', 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,
profileid: owner.profile.profileid
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Family',
email: 'invalid',
profileid: owner.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-email')
})
})
describe('invalid-profile', () => {
it('missing posted profileid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: ''
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('invalid posted profileid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: 'invalid'
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
it('ineligible posted profileid missing fields', async () => {
const owner = await TestHelper.createUser()
global.userProfileFields = global.membershipProfileFields = ['display-email']
await TestHelper.createProfile(owner, {
'display-email': owner.profile.contactEmail
})
global.userProfileFields = global.membershipProfileFields = ['display-name', 'display-email']
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: owner.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profile')
})
})
describe('receives', () => {
it('required posted organization-email', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: owner.profile.profileid
}
const organization = await req.post()
assert.strictEqual(organization.email, 'this@address.com')
})
it('required posted organization-name', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: owner.profile.profileid
}
const organization = await req.post()
assert.strictEqual(organization.name, 'this is the name')
})
it('required posted profileid', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: owner.profile.profileid
}
const organization = await req.post()
const req2 = TestHelper.createRequest(`/api/user/organizations/organization-membership?organizationid=${organization.organizationid}`)
req2.account = owner.account
req2.session = owner.session
const membership = await req2.get()
assert.strictEqual(membership.profileid, owner.profile.profileid)
})
})
describe('returns', () => {
it('object', 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
})
const req = TestHelper.createRequest(`/api/user/organizations/create-organization?accountid=${owner.account.accountid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'this is the name',
email: 'this@address.com',
profileid: owner.profile.profileid
}
req.filename = __filename
req.saveResponse = true
const organization = await req.post()
assert.strictEqual(organization.object, 'organization')
})
})
})