|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | +import { |
| 3 | + create, |
| 4 | + fetch, |
| 5 | + deleteEntity, |
| 6 | + fetchAll |
| 7 | +} from '../../entity' |
| 8 | +import { TeamUsers } from './teamUsers' |
| 9 | +import { StackRoleMappings } from './stackRoleMappings' |
| 10 | +import error from '../../core/contentstackError' |
| 11 | + |
| 12 | +export function Teams (http, data) { |
| 13 | + this.organizationUid = data.organizationUid |
| 14 | + this.urlPath = `/organizations/${this.organizationUid}/teams` |
| 15 | + if (data && data.uid) { |
| 16 | + Object.assign(this, cloneDeep(data)) |
| 17 | + |
| 18 | + this.urlPath = `/organizations/${this.organizationUid}/teams/${this.uid}` |
| 19 | + |
| 20 | + /** |
| 21 | + * @description The update call on team will allow to update details of team. |
| 22 | + * @memberof Teams |
| 23 | + * @func update |
| 24 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 25 | + * @example |
| 26 | + * import * as contentstack from '@contentstack/management' |
| 27 | + * const client = contentstack.client() |
| 28 | + * const updateData = { |
| 29 | + * name: 'updatedname', |
| 30 | + * users: [ |
| 31 | + * { |
| 32 | + * email: 'abc@abc.com' |
| 33 | + * } |
| 34 | + * ], |
| 35 | + * organizationRole: 'blt09e5dfced326aaea', |
| 36 | + * stackRoleMapping: [] |
| 37 | + * } |
| 38 | + * client.organization(s'organizationUid').teams('teamUid').update(updateData) |
| 39 | + * .then((response) => console.log(response)) |
| 40 | + * |
| 41 | + */ |
| 42 | + this.update = async (updateData) => { |
| 43 | + try { |
| 44 | + const response = await http.put(this.urlPath, updateData) |
| 45 | + if (response.data) { |
| 46 | + return response.data |
| 47 | + } |
| 48 | + } catch (err) { |
| 49 | + throw error(err) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @description The delete call on team will delete the existing team. |
| 55 | + * @memberof Teams |
| 56 | + * @func delete |
| 57 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 58 | + * @example |
| 59 | + * import * as contentstack from '@contentstack/management' |
| 60 | + * const client = contentstack.client() |
| 61 | + * client.organization('organizationUid').teams('teamUid').delete() |
| 62 | + * .then((response) => console.log(response)) |
| 63 | + * |
| 64 | + */ |
| 65 | + this.delete = deleteEntity(http) |
| 66 | + |
| 67 | + /** |
| 68 | + * @description The fetch call on team will delete the existing team. |
| 69 | + * @memberof Teams |
| 70 | + * @func fetch |
| 71 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 72 | + * @example |
| 73 | + * import * as contentstack from '@contentstack/management' |
| 74 | + * const client = contentstack.client() |
| 75 | + * client.organization('organizationUid').teams('teamUid').fetch() |
| 76 | + * .then((response) => console.log(response)) |
| 77 | + * |
| 78 | + */ |
| 79 | + this.fetch = fetch(http, 'team') |
| 80 | + |
| 81 | + /** |
| 82 | + * @description The users call on team will get users details. |
| 83 | + * @memberof Teams |
| 84 | + * @func users |
| 85 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 86 | + * @example |
| 87 | + * import * as contentstack from '@contentstack/management' |
| 88 | + * const client = contentstack.client() |
| 89 | + * client.organization('organizationUid').teams('teamUid').teamUsers().fetchAll() |
| 90 | + * .then((response) => console.log(response)) |
| 91 | + * |
| 92 | + */ |
| 93 | + this.teamUsers = (userId = null) => { |
| 94 | + data.organizationUid = this.organizationUid |
| 95 | + data.teamUid = this.uid |
| 96 | + if (userId) { |
| 97 | + data.userId = userId |
| 98 | + } |
| 99 | + return new TeamUsers(http, data) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @description The stackRoleMappings call on team will get the stack role Mapping. |
| 104 | + * @memberof Teams |
| 105 | + * @func users |
| 106 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 107 | + * @example |
| 108 | + * import * as contentstack from '@contentstack/management' |
| 109 | + * const client = contentstack.client() |
| 110 | + * client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll() |
| 111 | + * .then((response) => console.log(response)) |
| 112 | + * |
| 113 | + */ |
| 114 | + this.stackRoleMappings = (stackApiKey = null) => { |
| 115 | + data.organizationUid = this.organizationUid |
| 116 | + data.teamUid = this.uid |
| 117 | + if (stackApiKey) { |
| 118 | + data.stackApiKey = stackApiKey |
| 119 | + } |
| 120 | + return new StackRoleMappings(http, data) |
| 121 | + } |
| 122 | + } else { |
| 123 | + /** |
| 124 | + * @description The fetch call on team will delete the existing team. |
| 125 | + * @memberof Teams |
| 126 | + * @func create |
| 127 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 128 | + * @example |
| 129 | + * import * as contentstack from '@contentstack/management' |
| 130 | + * const client = contentstack.client() |
| 131 | + * const team = { |
| 132 | + * name: 'name', |
| 133 | + * organizationUid: 'organization_uid', |
| 134 | + * users: [], |
| 135 | + * stackRoleMapping: [], |
| 136 | + * organizationRole: 'organizationRole' |
| 137 | + * } |
| 138 | + * client.organization('organizationUid').teams().create(team) |
| 139 | + * .then((response) => console.log(response)) |
| 140 | + * |
| 141 | + */ |
| 142 | + this.create = create({ http }) |
| 143 | + |
| 144 | + /** |
| 145 | + * @description The fetchAll on team will allow to fetch details of all teams. |
| 146 | + * @memberof Teams |
| 147 | + * @func fetchAll |
| 148 | + * @returns {Promise<Teams.Teams>} Response Object. |
| 149 | + * @example |
| 150 | + * import * as contentstack from '@contentstack/management' |
| 151 | + * const client = contentstack.client() |
| 152 | + * |
| 153 | + * client.organization('organizationUid').teams().fetchAll() |
| 154 | + * .then((response) => console.log(response)) |
| 155 | + */ |
| 156 | + this.fetchAll = fetchAll(http, TeamsCollection, { api_version: 1.1 }) |
| 157 | + } |
| 158 | +} |
| 159 | +export function TeamsCollection (http, teamsData) { |
| 160 | + const obj = cloneDeep(teamsData.teams) || [] |
| 161 | + const teamsCollection = obj.map((team) => { |
| 162 | + return new Teams(http, team) |
| 163 | + }) |
| 164 | + return teamsCollection |
| 165 | +} |
0 commit comments