Organizations module API explorer

/api/administrator/organizations/memberships (GET)

Account information like email addresses is generated with faker-js it is not real user information.

await global.api.administrator.organizations.Memberships.get(req)

Returns array

[
  {
    "membershipid": "mmbr_31aa751d28b0c3c1",
    "object": "membership",
    "appid": "tests_1656039697",
    "accountid": "acct_dbadf5530d19da27",
    "organizationid": "orgn_e1eb2c21ee584a20",
    "invitationid": "invt_9e2feb6e5f75566c",
    "profileid": "prof_3d8b683e05307067",
    "createdAt": "2022-06-24T03:01:38.400Z",
    "updatedAt": "2022-06-24T03:01:38.400Z",
    "displayName": "Darrell Cassin",
    "displayEmail": "Darrell_Cassin88@hotmail.com"
  },
  {
    "membershipid": "mmbr_7f0fda18b2c9b284",
    "object": "membership",
    "appid": "tests_1656039697",
    "accountid": "acct_dbadf5530d19da27",
    "organizationid": "orgn_5be3d42448627a76",
    "invitationid": "invt_4ae618ba1a612ccc",
    "profileid": "prof_3d8b683e05307067",
    "createdAt": "2022-06-24T03:01:38.314Z",
    "updatedAt": "2022-06-24T03:01:38.314Z",
    "displayName": "Darrell Cassin",
    "displayEmail": "Darrell_Cassin88@hotmail.com"
  },
  {
    "membershipid": "mmbr_af5897b296576897",
    "object": "membership",
    "appid": "tests_1656039697",
    "accountid": "acct_dbadf5530d19da27",
    "organizationid": "orgn_1878e6f5aac026e8",
    "invitationid": "invt_4c205b347eaafb49",
    "profileid": "prof_3d8b683e05307067",
    "createdAt": "2022-06-24T03:01:38.216Z",
    "updatedAt": "2022-06-24T03:01:38.216Z",
    "displayName": "Darrell Cassin",
    "displayEmail": "Darrell_Cassin88@hotmail.com"
  }
]

Receives

API routes may receive parameters from the URL and POST supporting simple and multipart:

Field Value Required Type
accountid string optional URL
all boolean optional URL
limit integer optional URL
offset integer optional URL
organizationid string optional URL

NodeJS source (view on github)

const organizations = require('../../../../../index.js')

module.exports = {
  get: async (req) => {
    req.query = req.query || {}
    const where = {
      appid: req.appid || global.appid
    }
    if (req.query.accountid) {
      where.accountid = req.query.accountid
    } else if (req.query.organizationid) {
      where.organizationid = req.query.organizationid
    }
    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'],
        order: [
          ['createdAt', 'DESC']
        ],
        offset,
        limit
      })
    }
    if (!membershipids || !membershipids.length) {
      return null
    }
    const items = []
    for (const membershipInfo of membershipids) {
      req.query.membershipid = membershipInfo.dataValues.membershipid
      const membership = await global.api.administrator.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/administrator/organizations/memberships', function () {
  const cachedResponses = {}
  const cachedMemberships = []
  const accountMemberships = []
  const organizationMemberships = []
  before(async () => {
    await DashboardTestHelper.setupBeforeEach()
    await TestHelper.setupBeforeEach()
    const administrator = await TestHelper.createOwner()
    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: `1234${i}`
      })
      cachedMemberships.unshift(owner.membership.membershipid)
      global.userProfileFields = ['contact-email', 'full-name']
      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
      })
      await TestHelper.createInvitation(owner)
      await TestHelper.acceptInvitation(user, owner)
      cachedMemberships.unshift(user.membership.membershipid)
    }
    global.userProfileFields = ['contact-email', 'full-name']
    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
    })
    await TestHelper.createOrganization(user, {
      email: user.profile.displayEmail,
      name: 'My organization 1',
      profileid: user.profile.profileid,
      pin: '5454'
    })
    accountMemberships.unshift(user.membership.membershipid)
    cachedMemberships.unshift(user.membership.membershipid)
    organizationMemberships.unshift(user.membership.membershipid)
    for (let i = 0, len = 3; i < len; i++) {
      global.userProfileFields = ['contact-email', 'full-name']
      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.createInvitation(user)
      await TestHelper.acceptInvitation(member, user)
      organizationMemberships.unshift(member.membership.membershipid)
      cachedMemberships.unshift(user.membership.membershipid)
    }
    for (let i = 0, len = 3; 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: `7789${i}`
      })
      cachedMemberships.unshift(owner.membership.membershipid)
      await TestHelper.createInvitation(owner)
      await TestHelper.acceptInvitation(user, owner)
      accountMemberships.unshift(user.membership.membershipid)
      cachedMemberships.unshift(user.membership.membershipid)
    }
    const req1 = TestHelper.createRequest('/api/administrator/organizations/memberships?offset=1')
    req1.account = administrator.account
    req1.session = administrator.session
    cachedResponses.offset = await req1.get()
    const req2 = TestHelper.createRequest('/api/administrator/organizations/memberships?limit=1')
    req2.account = administrator.account
    req2.session = administrator.session
    cachedResponses.limit = await req2.get()
    const req3 = TestHelper.createRequest('/api/administrator/organizations/memberships?all=true')
    req3.account = administrator.account
    req3.session = administrator.session
    cachedResponses.all = await req3.get()
    const req4 = TestHelper.createRequest(`/api/administrator/organizations/memberships?accountid=${user.account.accountid}&all=true`)
    req4.account = administrator.account
    req4.session = administrator.session
    cachedResponses.accountid = await req4.get()
    const req5 = TestHelper.createRequest(`/api/administrator/organizations/memberships?organizationid=${user.organization.organizationid}&all=true`)
    req5.account = administrator.account
    req5.session = administrator.session
    cachedResponses.organizationid = await req5.get()
    const req6 = TestHelper.createRequest(`/api/administrator/organizations/memberships?accountid=${user.account.accountid}`)
    req6.account = administrator.account
    req6.session = administrator.session
    req6.filename = __filename
    req6.saveResponse = true
    cachedResponses.returns = await req6.get()
    global.pageSize = 3
    cachedResponses.pageSize = await req6.get()
  })
  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 accountid (string)', async () => {
      const membershipsNow = cachedResponses.accountid
      assert.strictEqual(membershipsNow.length, accountMemberships.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 membershipsNow = cachedResponses.pageSize
      assert.strictEqual(membershipsNow.length, global.pageSize)
    })
  })
})