/api/user/organizations/membership (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.Membership.get(req)Returns object
{
"membershipid": "mmbr_753d028b9fc85ca6",
"object": "membership",
"appid": "tests_1656039708",
"accountid": "acct_f2cf30a05ece8b90",
"organizationid": "orgn_b738843e40f4904f",
"invitationid": "invt_472b0d242eeccab2",
"profileid": "prof_2ce3a9411844d85c",
"createdAt": "2022-06-24T03:01:48.390Z",
"updatedAt": "2022-06-24T03:01:48.390Z",
"displayName": "Dale Frami",
"displayEmail": "Dale_Frami@gmail.com"
}
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 must be organization member or owner |
invalid-membershipid | missing querystring membershipid |
invalid querystring membershipid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const organizations = require('../../../../../index.js')
module.exports = {
get: async (req) => {
if (!req.query || !req.query.membershipid) {
throw new Error('invalid-membershipid')
}
let membership = await dashboard.StorageCache.get(req.query.membershipid)
if (!membership) {
const membershipInfo = await organizations.Storage.Membership.findOne({
where: {
membershipid: req.query.membershipid,
appid: req.appid || global.appid
}
})
if (!membershipInfo) {
throw new Error('invalid-membershipid')
}
membership = {}
for (const field of membershipInfo._options.attributes) {
membership[field] = membershipInfo.get(field)
}
await dashboard.StorageCache.set(req.query.membershipid, membership)
}
if (membership.accountid !== req.account.accountid) {
req.query.organizationid = membership.organizationid
const organization = await global.api.user.organizations.Organization.get(req)
if (!organization) {
throw new Error('invalid-membershipid')
}
if (organization.ownerid !== req.account.accountid) {
req.query.organizationid = organization.organizationid
const membership = await global.api.user.organizations.OrganizationMembership.get(req)
if (!membership) {
throw new Error('invalid-account')
}
}
}
req.query.profileid = membership.profileid
const profile = await global.api.administrator.Profile.get(req)
const requireProfileFields = global.membershipProfileFields
for (const field of requireProfileFields) {
if (field === 'full-name') {
membership.fullName = profile.fullName
} else {
const displayName = global.profileFieldMap[field]
membership[displayName] = profile[displayName]
}
}
return membership
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/organizations/membership', () => {
describe('exceptions', () => {
describe('invalid-membershipid', () => {
it('missing querystring membershipid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/membership')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-membershipid')
})
it('invalid querystring membershipid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/membership?membershipid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-membershipid')
})
})
describe('invalid-account', () => {
it('accessing account must be organization member or 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.acceptInvitation(user, owner)
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/membership?membershipid=${user.membership.membershipid}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.get()
} 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/membership?membershipid=${user.membership.membershipid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const membership = await req.get()
assert.strictEqual(membership.object, 'membership')
})
})
})