UNPKG

@contentstack/management

Version:

The Content Management API is used to manage the content of your Contentstack account

196 lines (190 loc) 7 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import cloneDeep from 'lodash/cloneDeep'; import { create, fetch, deleteEntity, fetchAll } from '../../entity'; import { TeamUsers } from './teamUsers'; import { StackRoleMappings } from './stackRoleMappings'; import error from '../../core/contentstackError'; export function Teams(http, data) { var _this = this; this.organizationUid = data.organizationUid; this.urlPath = "/organizations/".concat(this.organizationUid, "/teams"); if (data && data.uid) { Object.assign(this, cloneDeep(data)); this.urlPath = "/organizations/".concat(this.organizationUid, "/teams/").concat(this.uid); /** * @description The update call allows you to update details of a team. * @memberof Teams * @func update * @async * @param {Object} updateData - Team update data. * @prop {string} updateData.name - Team name. * @prop {Array<Object>} updateData.users - Array of user objects with email property. * @prop {string} updateData.organizationRole - Organization role UID. * @prop {Array} updateData.stackRoleMapping - Stack role mappings. * @returns {Promise<Object>} Response Object. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const updateData = { * name: 'updatedname', * users: [ * { * email: 'abc@abc.com' * } * ], * organizationRole: 'blt09e5dfced326aaea', * stackRoleMapping: [] * } * client.organization('organizationUid').teams('teamUid').update(updateData) * .then((response) => console.log(response)) * */ this.update = /*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(updateData) { var response, _t; return _regeneratorRuntime.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 1; return http.put(_this.urlPath, updateData); case 1: response = _context.sent; if (!response.data) { _context.next = 2; break; } return _context.abrupt("return", response.data); case 2: _context.next = 4; break; case 3: _context.prev = 3; _t = _context["catch"](0); throw error(_t); case 4: case "end": return _context.stop(); } }, _callee, null, [[0, 3]]); })); return function (_x) { return _ref.apply(this, arguments); }; }(); /** * @description The delete call deletes an existing team. * @memberof Teams * @func delete * @returns {Promise<Object>} Response Object. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * client.organization('organizationUid').teams('teamUid').delete() * .then((response) => console.log(response)) * */ this["delete"] = deleteEntity(http); /** * @description The fetch call fetches the existing team details. * @memberof Teams * @func fetch * @returns {Promise<Teams>} Promise for Teams instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * client.organization('organizationUid').teams('teamUid').fetch() * .then((response) => console.log(response)) * */ this.fetch = fetch(http, 'team'); /** * @description The teamUsers call returns a TeamUsers instance for managing team users. * @memberof Teams * @func teamUsers * @returns {TeamUsers} Instance of TeamUsers. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * client.organization('organizationUid').teams('teamUid').teamUsers().fetchAll() * .then((response) => console.log(response)) * */ this.teamUsers = function () { var userId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; data.organizationUid = _this.organizationUid; data.teamUid = _this.uid; if (userId) { data.userId = userId; } return new TeamUsers(http, data); }; /** * @description The stackRoleMappings call returns a StackRoleMappings instance for managing stack role mappings. * @memberof Teams * @func stackRoleMappings * @returns {StackRoleMappings} Instance of StackRoleMappings. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll() * .then((response) => console.log(response)) * */ this.stackRoleMappings = function () { var stackApiKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; data.organizationUid = _this.organizationUid; data.teamUid = _this.uid; if (stackApiKey) { data.stackApiKey = stackApiKey; } return new StackRoleMappings(http, data); }; } else { /** * @description The create call creates a new team. * @memberof Teams * @func create * @returns {Promise<Teams>} Promise for Teams instance * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * const team = { * name: 'name', * organizationUid: 'organization_uid', * users: [], * stackRoleMapping: [], * organizationRole: 'organizationRole' * } * client.organization('organizationUid').teams().create(team) * .then((response) => console.log(response)) * */ this.create = create({ http: http }); /** * @description The fetchAll call allows you to fetch details of all teams. * @memberof Teams * @func fetchAll * @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance. * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * * client.organization('organizationUid').teams().fetchAll() * .then((response) => console.log(response)) */ this.fetchAll = fetchAll(http, TeamsCollection, { api_version: 1.1 }); } } export function TeamsCollection(http, teamsData) { var obj = cloneDeep(teamsData.teams) || []; var teamsCollection = obj.map(function (team) { return new Teams(http, team); }); return teamsCollection; }