UNPKG

balena-sdk

Version:
164 lines (160 loc) 5.33 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 getOrganizationModel = function (deps, opts) { const { pine, sdkInstance } = deps; /* eslint-disable @typescript-eslint/no-require-imports */ const membershipModel = require('./organization-membership').default(deps, (...args) => get(...args)); const inviteModel = require('./organization-invite').default(deps, opts, (...args) => get(...args)); /* eslint-enable @typescript-eslint/no-require-imports */ /** * @summary Creates a new organization * @name create * @public * @function * @memberof balena.models.organization * * @description This method creates a new organization with the current user as an administrator. * * @param {Object} options - Organization parameters to use. * @param {String} options.name - Required: the name of the organization that will be created. * @param {String} [options.handle] - The handle of the organization that will be created. * * @fulfil {String} - Organization * @returns {Promise} * * @example * balena.models.organization.create({ name:'MyOrganization' }).then(function(organization) { * console.log(organization); * }); * * @example * balena.models.organization.create({ * name:'MyOrganization', * logo_image: new File( * imageContent, * 'img.jpeg' * ); * }) * .then(function(organization) { * console.log(organization); * }); */ const create = function (organization) { return pine.post({ resource: 'organization', body: organization, }); }; /** * @summary Get all Organizations * @name getAll * @public * @function * @memberof balena.models.organization * * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - organizations * @returns {Promise} * * @example * balena.models.organization.getAll().then(function(organizations) { * console.log(organizations); * }); */ const getAll = function (options) { return pine.get({ resource: 'organization', options: (0, util_1.mergePineOptions)({ $orderby: { name: 'asc' }, }, options), }); }; /** * @summary Get a single organization * @name get * @public * @function * @memberof balena.models.organization * * @param {String|Number} handleOrId - organization handle (string) or id (number). * @param {Object} [options={}] - extra pine options to use * @fulfil {Object} - organization * @returns {Promise} * * @example * balena.models.organization.get('myorganization').then(function(organization) { * console.log(organization); * }); * * @example * balena.models.organization.get(123).then(function(organization) { * console.log(organization); * }); */ const get = async function (handleOrId, options) { if (handleOrId == null) { throw new errors.BalenaInvalidParameterError('handleOrId', handleOrId); } const organization = await pine.get({ resource: 'organization', id: (0, util_1.isId)(handleOrId) ? handleOrId : { handle: handleOrId }, options, }); if (organization == null) { throw new errors.BalenaOrganizationNotFound(handleOrId); } return organization; }; /** * @summary Remove an Organization * @name remove * @public * @function * @memberof balena.models.organization * * @param {String|Number} handleOrId - organization handle (string) or id (number). * @returns {Promise} * * @example * balena.models.organization.remove(123); */ const remove = async function (handleOrId) { const id = (await sdkInstance.models.organization.get(handleOrId, { $select: 'id' })).id; await pine.delete({ resource: 'organization', id, }); }; return { create, getAll, get, remove, /** * @namespace balena.models.organization.membership * @memberof balena.models.organization */ membership: membershipModel, /** * @namespace balena.models.organization.invite * @memberof balena.models.organization */ invite: inviteModel, }; }; exports.default = getOrganizationModel;