/api/user/organizations/create-membership (POST)
await global.api.user.organizations.CreateMembership.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 querystring invitationid | |
invalid-invitationid | missing querystring invitationid |
invalid-invitation | querystring invitationid is not open invitation |
invalid posted secret-code | |
invalid-secret-code | missing posted secret-code |
accessing account is organization member | |
invalid-account | accessing account is organization owner |
invalid posted profileid | |
invalid-profileid | missing posted profileid |
invalid-profile | ineligible posted profileid is missing fields |
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
profileid | string | required | POST |
secret-code | 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.invitationid) {
throw new Error('invalid-invitationid')
}
if (!req.body) {
throw new Error('invalid-secret-code')
}
req.body['secret-code'] = req.body['secret-code'].trim ? req.body['secret-code'].trim() : req.body['secret-code']
if (!req.body['secret-code'] || !req.body['secret-code'].length) {
throw new Error('invalid-secret-code')
}
const secretCodeHash = await dashboard.Hash.sha512Hash(req.body['secret-code'], req.alternativesha512, req.alternativeDashboardEncryptionKey)
const invitation = await global.api.user.organizations.OpenInvitation.get(req)
if (!invitation) {
throw new Error('invalid-invitationid')
}
if (invitation.accepted) {
throw new Error('invalid-invitation')
}
const invitationsecretCodeHash = await organizations.StorageObject.getProperty(`${req.appid}/invitation/${req.query.invitationid}`, 'secretCodeHash')
if (invitationsecretCodeHash !== secretCodeHash) {
throw new Error('invalid-secret-code')
}
req.query.organizationid = invitation.organizationid
const organization = await global.api.user.organizations.OpenInvitationOrganization.get(req)
if (!organization) {
throw new Error('invalid-organizationid')
}
if (req.account.accountid === organization.ownerid) {
throw new Error('invalid-account')
}
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')
}
}
let membership
try {
membership = await global.api.user.organizations.OrganizationMembership.get(req)
} catch (error) {
}
if (membership) {
throw new Error('invalid-account')
}
await organizations.StorageObject.setProperty(`${req.appid}/invitation/${req.query.invitationid}`, 'accepted', dashboard.Timestamp.now)
const membershipid = `membership_${await dashboard.UUID.generateID()}`
const membershipInfo = {
object: 'membership',
membershipid: membershipid,
organizationid: invitation.organizationid,
accountid: req.account.accountid,
created: dashboard.Timestamp.now,
invitationid: req.query.invitationid,
profileid: req.body.profileid
}
await organizations.Storage.write(`${req.appid}/membership/${membershipid}`, membershipInfo)
await organizations.StorageObject.setProperty(`${req.appid}/invitation/${req.query.invitationid}`, 'membershipid', membershipid)
await organizations.StorageList.addMany({
[`${req.appid}/memberships`]: membershipid,
[`${req.appid}/account/memberships/${req.account.accountid}`]: membershipid,
[`${req.appid}/account/organizations/${req.account.accountid}`]: organization.organizationid,
[`${req.appid}/account/invitations/${req.account.accountid}`]: req.query.invitationid,
[`${req.appid}/organization/memberships/${organization.organizationid}`]: membershipid
})
await organizations.Storage.write(`${req.appid}/map/organizationid/membershipid/${req.account.accountid}/${organization.organizationid}`, membershipid)
return membershipInfo
}
}
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-membership', () => {
describe('exceptions', () => {
describe('invalid-invitationid', () => {
it('missing querystring invitationid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/create-membership')
req.account = user.account
req.session = user.session
req.body = {
'secret-code': 'a code',
profileid: user.profile.profileid,
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitationid')
})
it('invalid querystring invitationid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/create-membership?invitationid=invalid')
req.account = user.account
req.session = user.session
req.body = {
'secret-code': 'a code',
profileid: user.profile.profileid,
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitationid')
})
})
describe('invalid-invitation', () => {
it('querystring invitationid has been used', 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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.acceptInvitation(user, owner)
const user2 = await TestHelper.createUser()
await TestHelper.createProfile(user2, {
'display-name': user2.profile.firstName,
'display-email': user2.profile.contactEmail
})
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user2.account
req.session = user2.session
req.body = {
'secret-code': owner.invitation.secretCode,
email: user2.profile.contactEmail,
name: user2.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitation')
})
it('posted secret-code has been used', 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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.acceptInvitation(user, owner)
const user2 = await TestHelper.createUser()
await TestHelper.createProfile(user2, {
'display-name': user2.profile.firstName,
'display-email': user2.profile.contactEmail
})
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user2.account
req.session = user2.session
req.body = {
'secret-code': owner.invitation.secretCode,
email: user2.profile.contactEmail,
name: user2.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitation')
})
})
describe('invalid-secret-code', () => {
it('missing posted secret-code', 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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': '',
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-secret-code')
})
it('invalid posted secret-code', 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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': 'invalid',
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-secret-code')
})
})
describe('invalid-account', () => {
it('accessing account 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
})
await TestHelper.createInvitation(owner)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = owner.account
req.session = owner.session
req.body = {
'secret-code': owner.invitation.secretCode,
email: owner.profile.contactEmail,
name: owner.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
it('accessing account is organization 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.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
global.userProfileFields = ['display-email', 'display-name']
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
await TestHelper.createInvitation(owner)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-profileid', () => {
it('missing 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
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: '',
email: user.profile.displayEmail,
name: user.profile.firstName
}
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()
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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: 'invalid',
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profileid')
})
})
describe('invalid-profile', () => {
it('ineligible posted profileid is missing fields', async () => {
const owner = await TestHelper.createUser()
global.userProfileFields = global.membershipProfileFields = ['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
})
await TestHelper.createInvitation(owner)
const user = await TestHelper.createUser()
global.userProfileFields = global.membershipProfileFields = ['display-email']
await TestHelper.createProfile(user, {
'display-email': user.profile.contactEmail
})
global.userProfileFields = global.membershipProfileFields = ['display-name', 'display-email']
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid,
email: user.profile.displayEmail,
name: user.profile.firstName
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-profile')
})
})
describe('invalid-invitation', () => {
it('querystring invitationid is not open invitation', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
const user2 = 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.createProfile(user2, {
'display-name': user2.profile.firstName,
'display-email': user2.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user2, owner)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid
}
let errorMessage
try {
await req.post()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitation')
})
})
})
describe('receives', () => {
it('required posted secret-code', 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)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid
}
const membership = await req.post()
assert.strictEqual(membership.object, 'membership')
})
it('required posted profileid', 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)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid
}
const membership = await req.post()
assert.strictEqual(membership.profileid, user.profile.profileid)
})
})
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)
const req = TestHelper.createRequest(`/api/user/organizations/create-membership?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
req.body = {
'secret-code': owner.invitation.secretCode,
profileid: user.profile.profileid
}
req.filename = __filename
req.saveResponse = true
const membership = await req.post()
assert.strictEqual(membership.object, 'membership')
})
})
})