Dashboard API explorer

/api/administrator/set-owner-account (PATCH)

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

await global.api.administrator.SetOwnerAccount.patch(req)

Returns object

{
  "accountid": "acct_b4058bdd77ebf2b6",
  "object": "account",
  "appid": "tests_1656038572",
  "profileid": "prof_bd2f29bc0fbdd499",
  "sessionKeyNumber": 1,
  "lastSignedInAt": "2022-06-24T02:42:52.000Z",
  "owner": true,
  "ownerSince": "2022-06-24T02:42:52.000Z",
  "administrator": true,
  "administratorSince": "2022-06-24T02:42:52.000Z",
  "createdAt": "2022-06-24T02:42:52.865Z",
  "updatedAt": "2022-06-24T02:42:52.898Z"
}

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
ineligible querystring account is already owner
invalid-accountid missing querystring accountid
invalid querystring accountid
requires accessing account is owner
querystring account is not owner
querystring account is not deleted

NodeJS source (view on github)

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

module.exports = {
  /**
   * Transfer the ownership by PATCHing the session, then
   * completing an authorization and PATCHing again to apply
   * the queued change
   */
  patch: async (req) => {
    if (!req.query || !req.query.accountid) {
      throw new Error('invalid-accountid')
    }
    if (!req.account.ownerSince || req.query.accountid === req.account.accountid) {
      throw new Error('invalid-account')
    }
    const account = await global.api.administrator.Account.get(req)
    if (!account) {
      throw new Error('invalid-accountid')
    }
    if (account.deletedAt) {
      throw new Error('invalid-account')
    }
    await dashboard.Storage.Account.update({
      ownerSince: new Date(),
      administratorSince: account.administratorSince || new Date()
    }, {
      where: {
        accountid: req.query.accountid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.Storage.Account.update({
      ownerSince: null
    }, {
      where: {
        accountid: req.account.accountid,
        appid: req.appid || global.appid
      }
    })
    await dashboard.StorageCache.remove(req.query.accountid)
    await dashboard.StorageCache.remove(req.account.accountid)
    return global.api.administrator.Account.get(req)
  }
}

Test source (view on github)

/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../test-helper.js')

describe('/api/administrator/set-owner-account', () => {
  describe('exceptions', () => {
    describe('invalid-accountid', () => {
      it('missing querystring accountid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/set-owner-account')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })

      it('invalid querystring accountid', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/set-owner-account?accountid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch()
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-accountid')
      })
    })

    describe('invalid-account', () => {
      it('ineligible accessing account', async () => {
        const owner = await TestHelper.createOwner()
        const administrator = await TestHelper.createAdministrator(owner)
        const user = await TestHelper.createUser()
        const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${user.account.accountid}`)
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })

      it('ineligible querystring account is already owner', async () => {
        const owner = await TestHelper.createOwner()
        const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${owner.account.accountid}`)
        req.account = owner.account
        req.session = owner.session
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-account')
      })
    })
  })

  describe('requires', () => {
    it('accessing account is owner', async () => {
      const owner = await TestHelper.createOwner()
      const administrator = await TestHelper.createAdministrator(owner)
      const user = await TestHelper.createUser()
      const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${user.account.accountid}`)
      req.account = administrator.account
      req.session = administrator.session
      let errorMessage
      try {
        await req.patch(req)
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-account')
    })

    it('querystring account is not owner', async () => {
      const owner = await TestHelper.createOwner()
      const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${owner.account.accountid}`)
      req.account = owner.account
      req.session = owner.session
      let errorMessage
      try {
        await req.patch(req)
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-account')
    })

    it('querystring account is not deleted', async () => {
      const owner = await TestHelper.createOwner()
      const user = await TestHelper.createUser()
      await TestHelper.setDeleted(user)
      const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${user.account.accountid}`)
      req.account = owner.account
      req.session = owner.session
      let errorMessage
      try {
        await req.patch(req)
      } catch (error) {
        errorMessage = error.message
      }
      assert.strictEqual(errorMessage, 'invalid-account')
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const owner = await TestHelper.createOwner()
      const user = await TestHelper.createUser()
      const req = TestHelper.createRequest(`/api/administrator/set-owner-account?accountid=${user.account.accountid}`)
      req.account = owner.account
      req.session = owner.session
      req.filename = __filename
      req.saveResponse = true
      const accountNow = await req.patch()
      assert.notStrictEqual(accountNow.owner, undefined)
      assert.notStrictEqual(accountNow.owner, null)
      const req2 = TestHelper.createRequest(`/api/administrator/account?accountid=${owner.account.accountid}`)
      req2.account = req.account
      req2.session = req.session
      const ownerAccountNow = await req2.get()
      assert.strictEqual(ownerAccountNow.owner, undefined)
    })
  })
})