/api/user/organizations/organization-membership (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.OrganizationMembership.get(req)Returns object
{
"membershipid": "mmbr_4d8467e07622a039",
"object": "membership",
"appid": "tests_1656039711",
"accountid": "acct_d3a8a97572a33add",
"organizationid": "orgn_69cd96fca66917d4",
"invitationid": "invt_20860ae7bc0da791",
"profileid": "prof_6a67e9f0a1fcb2ae",
"createdAt": "2022-06-24T03:01:51.513Z",
"updatedAt": "2022-06-24T03:01:51.513Z"
}
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 |
invalid-organizationid | missing querystring organizationid |
invalid querystring organizationid |
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.organizationid) {
throw new Error('invalid-organizationid')
}
const organization = await global.api.user.organizations.Organization.get(req)
if (!organization) {
throw new Error('invalid-organizationid')
}
let membership = await dashboard.StorageCache.get(`membership_${req.account.accountid}_${req.query.organizationid}`)
if (!membership) {
const membershipInfo = await organizations.Storage.Membership.findOne({
where: {
organizationid: req.query.organizationid,
accountid: req.account.accountid,
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(`membership_${req.account.accountid}_${req.query.organizationid}`, membership)
}
return membership
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/organizations/organization-membership', () => {
describe('exceptions', () => {
describe('invalid-organizationid', () => {
it('missing querystring organizationid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/organization-membership')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organizationid')
})
it('invalid querystring organizationid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/organization-membership?organizationid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-organizationid')
})
})
describe('invalid-account', () => {
it('accessing account must be 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.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/organization-membership?organizationid=${owner.organization.organizationid}`)
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/organization-membership?organizationid=${owner.organization.organizationid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const membership = await req.get()
assert.strictEqual(membership.object, 'membership')
})
})
})