/api/user/organizations/set-invitation-terminated (PATCH)
Account information like email addresses is generated with faker-js it is not real user information.
await global.api.user.organizations.SetInvitationTerminated.patch(req)Returns object
{
"invitationid": "invt_ae1b42f4fcf40d9c",
"organizationid": "orgn_54a9be3b6fd35fa5",
"accountid": "acct_c99501a2ec532480",
"object": "invitation",
"appid": "tests_1656039714",
"secretCode": "secret258",
"multi": true,
"acceptedAt": null,
"terminatedAt": "2022-06-24T03:01:54.303Z",
"createdAt": "2022-06-24T03:01:54.288Z",
"updatedAt": "2022-06-24T03:01:54.303Z"
}
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 is not organization owner |
invalid-invitation | invitation is not multi-use |
invitation is already terminated | |
invalid-invitationid | missing querystring invitationid |
invalid querystring invitationid |
NodeJS source (view on github)
const dashboard = require('@layeredapps/dashboard')
const organizations = require('../../../../../index.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.invitationid) {
throw new Error('invalid-invitationid')
}
const invitation = await global.api.user.organizations.Invitation.get(req)
if (!invitation) {
throw new Error('invalid-invitationid')
}
if (!invitation.multi || invitation.terminatedAt) {
throw new Error('invalid-invitation')
}
req.query.organizationid = invitation.organizationid
const organization = await global.api.user.organizations.Organization.get(req)
if (organization.ownerid !== req.account.accountid) {
throw new Error('invalid-account')
}
await organizations.Storage.Invitation.update({
terminatedAt: new Date()
}, {
where: {
invitationid: req.query.invitationid,
appid: req.appid || global.appid
}
})
await dashboard.StorageCache.remove(req.query.invitationid)
await dashboard.StorageCache.remove(invitation.secretCode)
return global.api.user.organizations.Invitation.get(req)
}
}
Test source (view on github)
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/organizations/set-invitation-terminated', () => {
describe('exceptions', () => {
describe('invalid-invitationid', () => {
it('missing querystring invitationid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/set-invitation-terminated')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitationid')
})
it('invalid querystring invitationid', async () => {
const owner = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/set-invitation-terminated?invitationid=invalid')
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitationid')
})
})
describe('invalid-account', () => {
it('accessing account is not organization 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)
const req = TestHelper.createRequest(`/api/user/organizations/set-invitation-terminated?invitationid=${owner.invitation.invitationid}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-invitation', () => {
it('invitation is not multi-use', 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, {
lifespan: 'single'
})
const req = TestHelper.createRequest(`/api/user/organizations/set-invitation-terminated?invitationid=${owner.invitation.invitationid}`)
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitation')
})
it('invitation is already terminated', 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.terminateInvitation(owner)
const req = TestHelper.createRequest(`/api/user/organizations/set-invitation-terminated?invitationid=${owner.invitation.invitationid}`)
req.account = owner.account
req.session = owner.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-invitation')
})
})
})
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)
const req = TestHelper.createRequest(`/api/user/organizations/set-invitation-terminated?invitationid=${owner.invitation.invitationid}`)
req.account = owner.account
req.session = owner.session
req.filename = __filename
req.saveResponse = true
const invitationNow = await req.patch()
assert.notStrictEqual(invitationNow.terminatedAt, null)
assert.notStrictEqual(invitationNow.terminatedAt, undefined)
})
})
})