UNPKG

balena-sdk

Version:
191 lines (187 loc) 6.92 kB
"use strict"; /* Copyright 2020 Balena Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const errors = tslib_1.__importStar(require("balena-errors")); const util_1 = require("../util"); const RESOURCE = 'invitee__is_invited_to__organization'; const getOrganizationInviteModel = function (deps, opts, getOrganization) { const { request, pine } = deps; const { apiUrl } = opts; const exports = { /** * @summary Get all invites * @name getAll * @public * @function * @memberof balena.models.organization.invite * * @description * This method returns all invites. * * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - invites * @returns {Promise} * * @example * balena.models.organization.invite.getAll().then(function(invites) { * console.log(invites); * }); */ getAll(options) { return pine.get({ resource: RESOURCE, options, }); }, /** * @summary Get all invites by organization * @name getAllByOrganization * @public * @function * @memberof balena.models.organization.invite * * @description * This method returns all invites for a specific organization. * * @param {String|Number} handleOrId - organization handle (string), or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - invites * @returns {Promise} * * @example * balena.models.organization.invite.getAllByOrganization('MyOrg').then(function(invites) { * console.log(invites); * }); * * @example * balena.models.organization.invite.getAllByOrganization(123).then(function(invites) { * console.log(invites); * }); */ async getAllByOrganization(handleOrId, options) { const { id } = await getOrganization(handleOrId, { $select: 'id', }); return await exports.getAll((0, util_1.mergePineOptions)({ $filter: { is_invited_to__organization: id } }, options)); }, /** * @summary Creates a new invite for an organization * @name create * @public * @function * @memberof balena.models.organization.invite * * @description This method invites a user by their email to an organization. * * @param {String|Number} handleOrId - organization handle (string), or id (number) * @param {Object} options - invite creation parameters * @param {String} options.invitee - the email of the invitee * @param {String} [options.roleName="developer"] - the role name to be granted to the invitee * @param {String} [message=null] - the message to send along with the invite * * @fulfil {String} - organization invite * @returns {Promise} * * @example * balena.models.organization.invite.create('MyOrg', { invitee: "invitee@example.org", roleName: "developer", message: "join my org" }).then(function(invite) { * console.log(invite); * }); */ async create(handleOrId, { invitee, roleName, message }) { var _a; const [{ id }, roles] = await Promise.all([ getOrganization(handleOrId, { $select: 'id' }), roleName ? pine.get({ resource: 'organization_membership_role', options: { $top: 1, $select: ['id'], $filter: { name: roleName, }, }, }) : undefined, ]); const body = { is_invited_to__organization: id, // @ts-expect-error this doesn't actually exist in the model and is a hooks thing :( invitee, message, }; if (roles) { const roleId = (_a = roles[0]) === null || _a === void 0 ? void 0 : _a.id; // Throw if the user provided a roleName, but we didn't find that role if (!roleId && roleName) { throw new errors.BalenaOrganizationMembershipRoleNotFound(roleName); } body.organization_membership_role = roleId; } return await pine.post({ resource: RESOURCE, body, }); }, /** * @summary Revoke an invite * @name revoke * @public * @function * @memberof balena.models.organization.invite * * @param {Number} id - organization invite id * @returns {Promise} * * @example * balena.models.organization.invite.revoke(123); */ async revoke(id) { await pine.delete({ resource: RESOURCE, id }); }, /** * @summary Accepts an invite * @name accept * @public * @function * @memberof balena.models.organization.invite * * @description This method adds the calling user to the organization. * * @param {String} invitationToken - invite token * @returns {Promise} * * @example * balena.models.organization.invite.accept("qwerty-invitation-token"); */ async accept(invitationToken) { try { await request.send({ method: 'POST', url: `/org/v1/invitation/${invitationToken}`, baseUrl: apiUrl, }); } catch (err) { if (err.statusCode === 401) { throw new errors.BalenaNotLoggedIn(); } throw err; } }, }; return exports; }; exports.default = getOrganizationInviteModel;