/api/user/organizations/update-organization (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.UpdateOrganization.patch(req)Returns object
{
"organizationid": "orgn_0ca6b390f67ea84c",
"object": "organization",
"appid": "tests_1656039717",
"ownerid": "acct_73d8de2f9739e9c9",
"name": "Organization Name",
"pin": "12345",
"email": "test@test.com",
"createdAt": "2022-06-24T03:01:57.301Z",
"updatedAt": "2022-06-24T03:01:57.317Z"
}
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
string | required | POST | |
name | string | required | POST |
pin | 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 |
---|---|
duplicate-pin | duplicate posted pin |
invalid-account | accessing account is not organization owner |
invalid-organization-email | missing posted email |
invalid posted email | |
invalid-organization-name | missing posted name |
invalid-organization-name-length | posted name is too short |
posted name is too long | |
invalid-organizationid | missing querystring organizationid |
invalid querystring organizationid | |
invalid-pin | missing posted pin |
invalid posted pin is not alphanumeric | |
invalid-pin-length | posted pin is too short |
posted pin is too long |
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.name || !req.body.name.length) {
throw new Error('invalid-organization-name')
}
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')
}
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 (global.minimumOrganizationPINLength > req.body.pin.length ||
global.maximumOrganizationPINLength < req.body.pin.length) {
throw new Error('invalid-pin-length')
}
if (req.body.pin !== organization.pin) {
const existing = await organizations.Storage.Organization.findOne({
where: {
pin: req.body.pin,
appid: req.appid || global.appid
}
})
if (existing && existing.dataValues && existing.dataValues.organizationid) {
throw new Error('duplicate-pin')
}
}
try {
await organizations.Storage.Organization.update({
name: req.body.name,
email: req.body.email,
pin: req.body.pin
}, {
where: {
organizationid: req.query.organizationid,
appid: req.appid || global.appid
}
})
} catch (error) {
if (error.name === 'SequelizeUniqueConstraintError') {
throw new Error('duplicate-pin')
}
throw new Error('unknown-error')
}
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/update-organization', () => {
describe('exceptions', () => {
describe('invalid-organizationid', () => {
it('missing querystring organizationid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/update-organization')
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/update-organization?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-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/update-organization?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')
})
})
describe('invalid-pin', () => {
it('missing posted pin', 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/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'My organization',
email: owner.profile.displayEmail
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-pin')
})
it('invalid posted pin is not alphanumeric', 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/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'My organization',
email: owner.profile.displayEmail,
pin: '!@#$#@$#@$'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-pin')
})
})
describe('duplicate-pin', () => {
it('duplicate posted pin', async () => {
const owner = await TestHelper.createUser()
const other = 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'
})
await TestHelper.createProfile(other, {
'display-name': other.profile.fullName,
'display-email': other.profile.contactEmail
})
await TestHelper.createOrganization(other, {
email: other.profile.displayEmail,
name: 'Other organization',
profileid: other.profile.profileid,
pin: '54321'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'My organization',
email: owner.profile.displayEmail,
pin: '54321'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'duplicate-pin')
})
})
describe('invalid-pin-length', () => {
it('posted pin is 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'My organization',
email: owner.profile.displayEmail,
pin: '1'
}
global.minimumOrganizationPINLength = 10
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-pin-length')
})
it('posted pin is 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'My organization',
email: owner.profile.displayEmail,
pin: '1234567890123456789012345678901234567890'
}
global.maximumOrganizationPINLength = 10
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-pin-length')
})
})
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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: '',
email: owner.profile.contactEmail
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name')
})
})
describe('invalid-organization-name-length', () => {
it('posted name is 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: '12345',
email: owner.profile.contactEmail
}
global.minimumOrganizationNameLength = 100
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-name-length')
})
it('posted name is 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: '12345',
email: owner.profile.contactEmail
}
global.maximumOrganizationNameLength = 1
let errorMessage
try {
await req.patch(req)
} 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'New name',
email: ''
}
let errorMessage
try {
await req.patch(req)
} 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'New name',
email: 'invalid'
}
let errorMessage
try {
await req.patch(req)
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organization-email')
})
})
})
describe('receives', () => {
it('required 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Organization Name',
email: 'test@test.com',
pin: '12345'
}
const organizationNow = await req.patch()
assert.strictEqual(organizationNow.name, 'Organization Name')
})
it('required 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Organization Name',
email: 'test@test.com',
pin: '12345'
}
const organizationNow = await req.patch()
assert.strictEqual(organizationNow.email, 'test@test.com')
})
it('required posted pin', 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/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
name: 'Organization Name',
email: 'test@test.com',
pin: '54321'
}
const organizationNow = await req.patch()
assert.strictEqual(organizationNow.pin, '54321')
})
})
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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid,
pin: '12345'
})
const req = TestHelper.createRequest(`/api/user/organizations/update-organization?organizationid=${owner.organization.organizationid}`)
req.account = owner.account
req.session = owner.session
req.filename = __filename
req.saveResponse = true
req.body = {
name: 'Organization Name',
email: 'test@test.com',
pin: '12345'
}
const organizationNow = await req.patch()
assert.strictEqual(organizationNow.object, 'organization')
})
})
})