Source: managers/collaborations.ts

  1. /**
  2. * @fileoverview Manager for the Box Collaboration Resource
  3. */
  4. // ------------------------------------------------------------------------------
  5. // Requirements
  6. // ------------------------------------------------------------------------------
  7. import BoxClient from "../box-client";
  8. import urlPath from "../util/url-path";
  9. import {
  10. Collaboration,
  11. CollaborationAccesibleBy,
  12. CollaborationRole,
  13. CollaborationStatus,
  14. CollaborationUpdate
  15. } from "../schemas";
  16. // -----------------------------------------------------------------------------
  17. // Typedefs
  18. // -----------------------------------------------------------------------------
  19. type ItemType = "file" | "folder";
  20. // ------------------------------------------------------------------------------
  21. // Private
  22. // ------------------------------------------------------------------------------
  23. const BASE_PATH = "/collaborations";
  24. // ------------------------------------------------------------------------------
  25. // Public
  26. // ------------------------------------------------------------------------------
  27. /**
  28. * Simple manager for interacting with all 'Collaboration' endpoints and actions.
  29. *
  30. * @constructor
  31. * @param {BoxClient} client - The Box API Client that is responsible for making calls to the API
  32. * @returns {void}
  33. */
  34. class Collaborations {
  35. client: BoxClient;
  36. constructor(client: BoxClient) {
  37. this.client = client;
  38. }
  39. /**
  40. * Requests a collaboration object with a given ID.
  41. *
  42. * API Endpoint: '/collaborations/:collaborationID'
  43. * Method: GET
  44. *
  45. * @param {string} collaborationID - Box ID of the collaboration being requested
  46. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  47. * @param {Function} [callback] - Passed the collaboration information if it was acquired successfully
  48. * @returns {Promise<Collaboration>} A promise resolving to the collaboration object
  49. */
  50. get(
  51. collaborationID: string,
  52. options?: Record<string, any>,
  53. callback?: Function
  54. ): Promise<Collaboration> {
  55. const params = {
  56. qs: options
  57. };
  58. const apiPath = urlPath(BASE_PATH, collaborationID);
  59. return this.client.wrapWithDefaultHandler(this.client.get)(
  60. apiPath,
  61. params,
  62. callback
  63. );
  64. }
  65. /**
  66. * Gets a user's pending collaborations
  67. *
  68. * API Endpoint: '/collaborations'
  69. * Method: GET
  70. *
  71. * @param {Function} [callback] - Called with a collection of pending collaborations if successful
  72. * @returns {Promise<Collaborations>} A promise resolving to the collection of pending collaborations
  73. */
  74. getPending(callback?: Function): Promise<Collaborations> {
  75. const params = {
  76. qs: {
  77. status: "pending"
  78. }
  79. };
  80. return this.client.wrapWithDefaultHandler(this.client.get)(
  81. BASE_PATH,
  82. params,
  83. callback
  84. );
  85. }
  86. private updateInternal(
  87. collaborationID: string,
  88. updates: CollaborationUpdate | { status: CollaborationStatus },
  89. callback?: Function
  90. ): Promise<Collaboration> {
  91. const params = {
  92. body: updates
  93. };
  94. const apiPath = urlPath(BASE_PATH, collaborationID);
  95. return this.client.wrapWithDefaultHandler(this.client.put)(
  96. apiPath,
  97. params,
  98. callback
  99. );
  100. }
  101. /**
  102. * Update some information about a given collaboration.
  103. *
  104. * API Endpoint: '/collaborations/:collaborationID'
  105. * Method: PUT
  106. *
  107. * @param {string} collaborationID - Box ID of the collaboration being requested
  108. * @param {CollaborationUpdate} updates - Fields of the collaboration to be updated
  109. * @param {Function} [callback] - Passed the updated collaboration information if it was acquired successfully
  110. * @returns {Promise<Collaboration>} A promise resolving to the updated collaboration object
  111. */
  112. update(
  113. collaborationID: string,
  114. updates: CollaborationUpdate,
  115. callback?: Function
  116. ): Promise<Collaboration> {
  117. return this.updateInternal(collaborationID, updates, callback);
  118. }
  119. /**
  120. * Update the status of a pending collaboration.
  121. *
  122. * API Endpoint: '/collaborations/:collaborationID'
  123. * Method: PUT
  124. *
  125. * @param {string} collaborationID - Box ID of the collaboration being requested
  126. * @param {CollaborationStatus} newStatus - The new collaboration status ('accepted'/'rejected')
  127. * @param {Function} [callback] - Passed the updated collaboration information if it was acquired successfully
  128. * @returns {Promise<Collaboration>} A promise resolving to the accepted collaboration object
  129. */
  130. respondToPending(
  131. collaborationID: string,
  132. newStatus: CollaborationStatus,
  133. callback?: Function
  134. ): Promise<Collaboration> {
  135. return this.updateInternal(
  136. collaborationID,
  137. {
  138. status: newStatus
  139. },
  140. callback);
  141. }
  142. /**
  143. * Invite a collaborator to a folder. You'll have to create the 'accessible_by' input object
  144. * yourself, but the method allows for multiple types of collaborator invites. See
  145. * {@link http://developers.box.com/docs/#collaborations-add-a-collaboration} for formatting
  146. * help.
  147. *
  148. * API Endpoint: '/collaborations
  149. * Method: POST
  150. *
  151. * @param {CollaborationAccesibleBy} accessibleBy - The accessible_by object expected by the API
  152. * @param {string} itemID - Box ID of the item to which the user should be invited
  153. * @param {CollaborationRole} role - The role which the invited collaborator should have
  154. * @param {Object} [options] - Optional parameters for the collaboration
  155. * @param {ItemType} [options.type=folder] - Type of object to be collaborated
  156. * @param {boolean} [options.notify] - Determines if the user or group will receive email notifications
  157. * @param {boolean} [options.can_view_path] - Whether view path collaboration feature is enabled or not
  158. * @param {boolean} [options.is_access_only] - WARN: Feature not yet available.
  159. * Do not display collaborated items on collaborator's All Files Pages
  160. * and suppress notifications sent to collaborators regarding access-only content.
  161. * This feature is going to be released in Q4. Watch our
  162. * [announcements](https://developer.box.com/changelog/) to learn about its availability.
  163. * @param {Function} [callback] - Called with the new collaboration if successful
  164. * @returns {Promise<Collaboration>} A promise resolving to the created collaboration object
  165. */
  166. create(
  167. accessibleBy: CollaborationAccesibleBy,
  168. itemID: string,
  169. role: CollaborationRole,
  170. options?:
  171. | {
  172. type?: ItemType;
  173. notify?: boolean;
  174. can_view_path?: boolean;
  175. is_access_only?: boolean;
  176. }
  177. | Function,
  178. callback?: Function
  179. ): Promise<Collaboration> {
  180. const defaultOptions = {
  181. type: "folder"
  182. };
  183. if (typeof options === "function") {
  184. callback = options;
  185. options = {};
  186. }
  187. options = Object.assign({}, defaultOptions, options);
  188. const params: {
  189. body: Record<string, any>;
  190. qs?: Record<string, any>;
  191. } = {
  192. body: {
  193. item: {
  194. type: options.type,
  195. id: itemID
  196. },
  197. accessible_by: accessibleBy,
  198. role
  199. }
  200. };
  201. if (typeof options.can_view_path === "boolean") {
  202. params.body.can_view_path = options.can_view_path;
  203. }
  204. if (typeof options.notify === "boolean") {
  205. params.qs = {
  206. notify: options.notify
  207. };
  208. }
  209. if(typeof options.is_access_only === 'boolean') {
  210. params.body.is_access_only = options.is_access_only;
  211. }
  212. return this.client.wrapWithDefaultHandler(this.client.post)(
  213. BASE_PATH,
  214. params,
  215. callback
  216. );
  217. }
  218. /**
  219. * Invite a user to collaborate on an item via their user ID.
  220. *
  221. * API Endpoint: '/collaborations
  222. * Method: POST
  223. *
  224. * @param {int | string} userID - The ID of the user you'll invite as a collaborator
  225. * @param {string} itemID - Box ID of the item to which the user should be invited
  226. * @param {CollaborationRole} role - The role which the invited collaborator should have
  227. * @param {Object} [options] - Optional parameters for the collaboration
  228. * @param {ItemType} [options.type=folder] - Type of object to be collaborated
  229. * @param {boolean} [options.notify] - Determines if the user will receive email notifications
  230. * @param {boolean} [options.can_view_path] - Whether view path collaboration feature is enabled or not
  231. * @param {Function} [callback] - Called with the new collaboration if successful
  232. * @returns {Promise<Collaboration>} A promise resolving to the created collaboration object
  233. */
  234. createWithUserID(
  235. userID: number | string,
  236. itemID: string,
  237. role: CollaborationRole,
  238. options?:
  239. | {
  240. type?: ItemType;
  241. notify?: boolean;
  242. can_view_path?: boolean;
  243. }
  244. | Function,
  245. callback?: Function
  246. ): Promise<Collaboration> {
  247. if (typeof options === "function") {
  248. callback = options;
  249. options = {};
  250. }
  251. const accessibleBy: CollaborationAccesibleBy = {
  252. type: "user",
  253. id: `${userID}`
  254. };
  255. return this.create(accessibleBy, itemID, role, options, callback);
  256. }
  257. /**
  258. * Invite a user to collaborate on an item via their user login email address.
  259. *
  260. * API Endpoint: '/collaborations
  261. * Method: POST
  262. *
  263. * @param {string} email - The collaborator's email address
  264. * @param {string} itemID - Box ID of the item to which the user should be invited
  265. * @param {CollaborationRole} role - The role which the invited collaborator should have
  266. * @param {Object} [options] - Optional parameters for the collaboration
  267. * @param {ItemType} [options.type=folder] - Type of object to be collaborated
  268. * @param {boolean} [options.notify] - Determines if the user will receive email notifications
  269. * @param {boolean} [options.can_view_path] - Whether view path collaboration feature is enabled or not
  270. * @param {Function} [callback] - Called with the new collaboration if successful
  271. * @returns {Promise<Collaboration>} A promise resolving to the created collaboration object
  272. */
  273. createWithUserEmail(
  274. email: string,
  275. itemID: string,
  276. role: CollaborationRole,
  277. options?:
  278. | {
  279. type?: ItemType;
  280. notify?: boolean;
  281. can_view_path?: boolean;
  282. }
  283. | Function,
  284. callback?: Function
  285. ): Promise<Collaboration> {
  286. if (typeof options === "function") {
  287. callback = options;
  288. options = {};
  289. }
  290. const accessibleBy: CollaborationAccesibleBy = {
  291. type: "user",
  292. login: email
  293. };
  294. return this.create(accessibleBy, itemID, role, options, callback);
  295. }
  296. /**
  297. * Invite a group to collaborate on an item via their group ID.
  298. *
  299. * API Endpoint: '/collaborations
  300. * Method: POST
  301. *
  302. * @param {int | string} groupID - The ID of the group you'll invite as a collaborator
  303. * @param {string} itemID - Box ID of the item to which the group should be invited
  304. * @param {CollaborationRole} role - The role which the invited collaborator should have
  305. * @param {Object} [options] - Optional parameters for the collaboration
  306. * @param {ItemType} [options.type=folder] - Type of object to be collaborated
  307. * @param {boolean} [options.notify] - Determines if the group will receive email notifications
  308. * @param {boolean} [options.can_view_path] - Whether view path collaboration feature is enabled or not
  309. * @param {Function} [callback] - Called with the new collaboration if successful
  310. * @returns {Promise<Collaboration>} A promise resolving to the created collaboration object
  311. */
  312. createWithGroupID(
  313. groupID: number | string,
  314. itemID: string,
  315. role: CollaborationRole,
  316. options?:
  317. | {
  318. type?: ItemType;
  319. notify?: boolean;
  320. can_view_path?: boolean;
  321. }
  322. | Function,
  323. callback?: Function
  324. ): Promise<Collaboration> {
  325. if (typeof options === "function") {
  326. callback = options;
  327. options = {};
  328. }
  329. const accessibleBy: CollaborationAccesibleBy = {
  330. type: "group",
  331. id: `${groupID}`
  332. };
  333. return this.create(accessibleBy, itemID, role, options, callback);
  334. }
  335. /**
  336. * Delete a given collaboration.
  337. *
  338. * API Endpoint: '/collaborations/:collaborationID'
  339. * Method: DELETE
  340. *
  341. * @param {string} collaborationID - Box ID of the collaboration being requested
  342. * @param {Function} [callback] - Empty response body passed if successful.
  343. * @returns {Promise<void>} A promise resolving to nothing
  344. */
  345. delete(collaborationID: string, callback?: Function): Promise<void> {
  346. const apiPath = urlPath(BASE_PATH, collaborationID);
  347. return this.client.wrapWithDefaultHandler(this.client.del)(
  348. apiPath,
  349. null,
  350. callback
  351. );
  352. }
  353. }
  354. /**
  355. * @module box-node-sdk/lib/managers/collaborations
  356. * @see {@Link Collaborations}
  357. */
  358. export = Collaborations;