/api/user/organizations/create-organization (POST)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.CreateOrganization.post(req)Returns object
{
"organizationid": "orgn_ba984a87bbce49af",
"object": "organization",
"appid": "tests_1656039703",
"ownerid": "acct_5e50a72c20f78ae1",
"name": "this is the name",
"pin": "12345",
"email": "this@address.com",
"createdAt": "2022-06-24T03:01:43.769Z",
"updatedAt": "2022-06-24T03:01:43.769Z"
}
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 |
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-email | missing posted email |
invalid posted email | |
invalid-organization-name | missing posted name |
invalid-organization-name-length | posted name too short |
posted name too long | |
invalid-profile | missing posted profileid |
invalid posted profileid | |
ineligible posted profileid missing fields |
NodeJS source (view on github)
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.pin || !req.body.pin.length) {
throw new Error('invalid-pin')
}
if (req.body.pin.match(/^[a-z0-9]+$/i) === null) {
throw new Error('invalid-pin')
}
if (!req.body.profileid || !req.body.profileid.length) {
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 requireProfileFields = global.membershipProfileFields
for (const field of requireProfileFields) {
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 (global.minimumOrganizationPINLength > req.body.pin.length ||
global.maximumOrganizationPINLength < req.body.pin.length) {
throw new Error('invalid-pin-length')
}
if (!req.body.email || !req.body.email.length || req.body.email.indexOf('@') < 1) {
throw new Error('invalid-organization-email')
}
const organizationInfo = {
appid: req.appid || global.appid,
ownerid: req.query.accountid,
name: req.body.name,
pin: req.body.pin,
email: req.body.email
}
try {
const organization = await organizations.Storage.Organization.create(organizationInfo)
const membershipInfo = {
appid: req.appid || global.appid,
organizationid: organization.dataValues.organizationid,
accountid: req.query.accountid,
profileid: req.body.profileid
}
await organizations.Storage.Membership.create(membershipInfo)
req.query.organizationid = organization.dataValues.organizationid
return global.api.user.organizations.Organization.get(req)
} catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
throw new Error('duplicate-pin')
}
throw new Error('unknown-error')
}
}
}
Test source (view on github)
/* 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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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: '',
pin: '12345'
}
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',
pin: '12345'
}
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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
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.fullName,
'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,
pin: '12345'
}
req.filename = __filename
req.saveResponse = true
const organization = await req.post()
assert.strictEqual(organization.object, 'organization')
})
})
})