balena-sdk
Version:
The Balena JavaScript SDK
191 lines (187 loc) • 6.97 kB
JavaScript
;
/*
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__application';
const getApplicationInviteModel = function (deps, opts, getApplication) {
const { request, pine } = deps;
const { apiUrl } = opts;
const exports = {
/**
* @summary Get all invites
* @name getAll
* @public
* @function
* @memberof balena.models.application.invite
*
* @description
* This method returns all invites.
*
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - invites
* @returns {Promise}
*
* @example
* balena.models.application.invite.getAll().then(function(invites) {
* console.log(invites);
* });
*/
getAll(options) {
return pine.get({
resource: RESOURCE,
options: options,
});
},
/**
* @summary Get all invites by application
* @name getAllByApplication
* @public
* @function
* @memberof balena.models.application.invite
*
* @description
* This method returns all invites for a specific application.
*
* @param {String|Number} slugOrUuidOrId - application slug (string), uuid (string) or id (number)
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - invites
* @returns {Promise}
*
* @example
* balena.models.application.invite.getAllByApplication('myorganization/myapp').then(function(invites) {
* console.log(invites);
* });
*
* @example
* balena.models.application.invite.getAllByApplication(123).then(function(invites) {
* console.log(invites);
* });
*/
async getAllByApplication(slugOrUuidOrId, options) {
const { id } = await getApplication(slugOrUuidOrId, {
$select: 'id',
});
return await exports.getAll((0, util_1.mergePineOptions)({ $filter: { is_invited_to__application: id } }, options));
},
/**
* @summary Creates a new invite for an application
* @name create
* @public
* @function
* @memberof balena.models.application.invite
*
* @description This method invites a user by their email to an application.
*
* @param {String|Number} slugOrUuidOrId - application slug (string), uuid (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} - application invite
* @returns {Promise}
*
* @example
* balena.models.application.invite.create('myorganization/myapp', { invitee: "invitee@example.org", roleName: "developer", message: "join my app" }).then(function(invite) {
* console.log(invite);
* });
*/
async create(slugOrUuidOrId, { invitee, roleName, message }) {
var _a;
const [{ id }, roles] = await Promise.all([
getApplication(slugOrUuidOrId, { $select: 'id' }),
roleName
? pine.get({
resource: 'application_membership_role',
options: {
$top: 1,
$select: 'id',
$filter: {
name: roleName,
},
},
})
: undefined,
]);
const body = {
is_invited_to__application: 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.BalenaApplicationMembershipRoleNotFound(roleName);
}
body.application_membership_role = roleId;
}
return await pine.post({
resource: RESOURCE,
body,
});
},
/**
* @summary Revoke an invite
* @name revoke
* @public
* @function
* @memberof balena.models.application.invite
*
* @param {Number} id - application invite id
* @returns {Promise}
*
* @example
* balena.models.application.invite.revoke(123);
*/
async revoke(id) {
await pine.delete({ resource: RESOURCE, id });
},
/**
* @summary Accepts an invite
* @name accept
* @public
* @function
* @memberof balena.models.application.invite
*
* @description This method adds the calling user to the application.
*
* @param {String} invitationToken - invite token
* @returns {Promise}
*
* @example
* balena.models.application.invite.accept("qwerty-invitation-token");
*/
async accept(invitationToken) {
try {
await request.send({
method: 'POST',
url: `/user/v1/invitation/${invitationToken}`,
baseUrl: apiUrl,
});
}
catch (err) {
if (err.statusCode === 401) {
throw new errors.BalenaNotLoggedIn();
}
throw err;
}
},
};
return exports;
};
exports.default = getApplicationInviteModel;