/api/user/organizations/memberships (GET)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.Memberships.get(req)Returns array
[
{
"membershipid": "mmbr_331f4b981592433f",
"object": "membership",
"appid": "tests_1656039709",
"accountid": "acct_05d7723fb3e1abe5",
"organizationid": "orgn_aa4e5852ce73bd0d",
"invitationid": null,
"profileid": "prof_bd3a2f4f5cf7ba46",
"createdAt": "2022-06-24T03:01:49.365Z",
"updatedAt": "2022-06-24T03:01:49.365Z",
"displayName": "Ivan Konopelski",
"displayEmail": "Ivan_Konopelski@gmail.com"
},
{
"membershipid": "mmbr_77e871830ad556f3",
"object": "membership",
"appid": "tests_1656039709",
"accountid": "acct_05d7723fb3e1abe5",
"organizationid": "orgn_d42ee8970aa209c0",
"invitationid": "invt_632c52c10a83b9f3",
"profileid": "prof_bd3a2f4f5cf7ba46",
"createdAt": "2022-06-24T03:01:49.353Z",
"updatedAt": "2022-06-24T03:01:49.353Z",
"displayName": "Ivan Konopelski",
"displayEmail": "Ivan_Konopelski@gmail.com"
},
{
"membershipid": "mmbr_1feb34a6e13c621a",
"object": "membership",
"appid": "tests_1656039709",
"accountid": "acct_05d7723fb3e1abe5",
"organizationid": "orgn_0d175154a8bcc7f5",
"invitationid": "invt_245370e8b9911101",
"profileid": "prof_bd3a2f4f5cf7ba46",
"createdAt": "2022-06-24T03:01:49.266Z",
"updatedAt": "2022-06-24T03:01:49.266Z",
"displayName": "Ivan Konopelski",
"displayEmail": "Ivan_Konopelski@gmail.com"
}
]
Receives
API routes may receive parameters from the URL and POST supporting simple and multipart:
Field | Value | Required | Type |
---|---|---|---|
all | boolean | optional | URL |
limit | integer | optional | URL |
offset | integer | optional | URL |
organizationid | string | optional | URL |
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 | ineligible accessing account |
invalid-accountid | missing querystring accountid |
invalid querystring accountid |
NodeJS source (view on github)
const organizations = require('../../../../../index.js')
module.exports = {
get: 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')
}
const where = {
appid: req.appid || global.appid
}
if (req.query.organizationid) {
const membership = await global.api.user.organizations.OrganizationMembership.get(req)
if (!membership) {
throw new Error('invalid-organizationid')
}
where.organizationid = req.query.organizationid
} else {
where.accountid = req.query.accountid
}
let membershipids
if (req.query.all) {
membershipids = await organizations.Storage.Membership.findAll({
where,
attributes: ['membershipid'],
order: [
['createdAt', 'DESC']
]
})
} else {
const offset = req.query.offset ? parseInt(req.query.offset, 10) : 0
const limit = req.query.limit ? parseInt(req.query.limit, 10) : global.pageSize
membershipids = await organizations.Storage.Membership.findAll({
where,
attributes: ['membershipid'],
offset,
limit,
order: [
['createdAt', 'DESC']
]
})
}
if (!membershipids || !membershipids.length) {
return null
}
const items = []
for (const membershipInfo of membershipids) {
req.query.membershipid = membershipInfo.dataValues.membershipid
const membership = await global.api.user.organizations.Membership.get(req)
items.push(membership)
}
return items
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const DashboardTestHelper = require('@layeredapps/dashboard/test-helper.js')
describe('/api/user/organizations/memberships', function () {
const cachedResponses = {}
const cachedMemberships = []
const organizationMemberships = []
before(async () => {
await DashboardTestHelper.setupBeforeEach()
await TestHelper.setupBeforeEach()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-email', 'display-name']
await TestHelper.createProfile(user, {
'display-name': user.profile.fullName,
'display-email': user.profile.contactEmail
})
for (let i = 0, len = global.pageSize + 1; i < len; i++) {
global.userProfileFields = ['contact-email', 'full-name']
const owner = await TestHelper.createUser()
global.userProfileFields = ['display-email', 'display-name']
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${i}`
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
cachedMemberships.unshift(user.membership.membershipid)
}
await TestHelper.createOrganization(user, {
email: user.profile.displayEmail,
name: 'My organization',
profileid: user.profile.profileid,
pin: '12345'
})
cachedMemberships.unshift(user.membership.membershipid)
organizationMemberships.unshift(user.membership.membershipid)
for (let i = 0, len = 3; i < len; i++) {
await TestHelper.createInvitation(user)
const member = await TestHelper.createUser()
global.userProfileFields = ['display-email', 'display-name']
await TestHelper.createProfile(member, {
'display-name': member.profile.fullName,
'display-email': member.profile.contactEmail
})
await TestHelper.acceptInvitation(member, user)
organizationMemberships.unshift(member.membership.membershipid)
}
const req1 = TestHelper.createRequest(`/api/user/organizations/memberships?accountid=${user.account.accountid}&offset=1`)
req1.account = user.account
req1.session = user.session
cachedResponses.offset = await req1.get()
const req2 = TestHelper.createRequest(`/api/user/organizations/memberships?accountid=${user.account.accountid}&limit=1`)
req2.account = user.account
req2.session = user.session
cachedResponses.limit = await req2.get()
const req3 = TestHelper.createRequest(`/api/user/organizations/memberships?accountid=${user.account.accountid}&all=true`)
req3.account = user.account
req3.session = user.session
cachedResponses.all = await req3.get()
const req4 = TestHelper.createRequest(`/api/user/organizations/memberships?accountid=${user.account.accountid}&organizationid=${user.organization.organizationid}&all=true`)
req4.account = user.account
req4.session = user.session
cachedResponses.organizationid = await req4.get()
const req5 = TestHelper.createRequest(`/api/user/organizations/memberships?accountid=${user.account.accountid}`)
req5.account = user.account
req5.session = user.session
req5.filename = __filename
req5.saveResponse = true
cachedResponses.returns = await req5.get()
global.pageSize = 3
cachedResponses.pageSize = await req5.get()
})
describe('exceptions', () => {
describe('invalid-accountid', () => {
it('missing querystring accountid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/memberships')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
it('invalid querystring accountid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/memberships?accountid=invalid')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-accountid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', 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/memberships?accountid=${owner.account.accountid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.get()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('receives', () => {
it('optional querystring offset (integer)', async () => {
const offset = 1
const membershipsNow = cachedResponses.offset
for (let i = 0, len = global.pageSize; i < len; i++) {
assert.strictEqual(membershipsNow[i].membershipid, cachedMemberships[offset + i])
}
})
it('optional querystring limit (integer)', async () => {
const limit = 1
const membershipsNow = cachedResponses.limit
assert.strictEqual(membershipsNow.length, limit)
})
it('optional querystring all (boolean)', async () => {
const membershipsNow = cachedResponses.all
assert.strictEqual(membershipsNow.length, cachedMemberships.length)
})
it('optional querystring organizationid (string)', async () => {
const membershipsNow = cachedResponses.organizationid
assert.strictEqual(membershipsNow.length, organizationMemberships.length)
})
})
describe('returns', () => {
it('array', async () => {
const membershipsNow = cachedResponses.returns
assert.strictEqual(membershipsNow.length, global.pageSize)
})
})
describe('configuration', () => {
it('environment PAGE_SIZE', async () => {
global.pageSize = 3
const codesNow = cachedResponses.pageSize
assert.strictEqual(codesNow.length, global.pageSize)
})
})
})