Source: managers/terms-of-service.ts

  1. /**
  2. * @fileoverview Manager for the Box Terms of Service Resource
  3. */
  4. // -----------------------------------------------------------------------------
  5. // Requirements
  6. // -----------------------------------------------------------------------------
  7. import httpStatusCodes from 'http-status';
  8. import BoxClient from '../box-client';
  9. import errors from '../util/errors';
  10. import urlPath from '../util/url-path';
  11. // -----------------------------------------------------------------------------
  12. // Typedefs
  13. // -----------------------------------------------------------------------------
  14. /**
  15. * Enum value of scope of the custom terms of services set to either managed by an enterprise or enternal to an enterprise
  16. *
  17. * @readonly
  18. * @enum {TermsOfServicesType}
  19. */
  20. enum TermsOfServicesType {
  21. MANAGED = 'managed',
  22. EXTERNAL = 'external',
  23. }
  24. /**
  25. * Enum value of status of the custom terms of services, either currently enabled or currently disabled
  26. *
  27. * @readonly
  28. * @enum {TermsOfServicesStatus}
  29. */
  30. enum TermsOfServicesStatus {
  31. ENABLED = 'enabled',
  32. DISABLED = 'disabled',
  33. }
  34. // -----------------------------------------------------------------------------
  35. // Private
  36. // -----------------------------------------------------------------------------
  37. // Base path for all terms of service endpoints
  38. const BASE_PATH = '/terms_of_services',
  39. USER_STATUSES_PATH = '/terms_of_service_user_statuses';
  40. // ------------------------------------------------------------------------------
  41. // Public
  42. // ------------------------------------------------------------------------------
  43. /**
  44. * Simple manager for interacting with all 'Terms of Services' and 'Terms of Service User Statuses' endpoints and actions.
  45. *
  46. * @param {BoxClient} client The Box API Client that is responsible for making calls to the API
  47. * @constructor
  48. */
  49. class TermsOfService {
  50. client: BoxClient;
  51. type!: typeof TermsOfServicesType;
  52. status!: typeof TermsOfServicesStatus;
  53. constructor(client: BoxClient) {
  54. // Attach the client, for making API calls
  55. this.client = client;
  56. }
  57. /**
  58. * Creates a custom terms of services with user specified values
  59. *
  60. * API Endpoint: '/terms_of_services'
  61. * Method: POST
  62. *
  63. * @param {TermsOfServicesType} termsOfServicesType - Determine if the custom terms of service is scoped internall or externally to an enterprise
  64. * @param {TermsOfServicesStatus} termsOfServicesStatus - Determine if the custom terms of service is enabled or disabled
  65. * @param {string} termsOfServicesText - Text field for message associated with custom terms of services
  66. * @param {Function} [callback] - Passed the terms of services information if successful, error otherwise
  67. * @returns {Promise<Object>} A promise resolving to the terms of services object
  68. */
  69. create(
  70. termsOfServicesType: TermsOfServicesType,
  71. termsOfServicesStatus: TermsOfServicesStatus,
  72. termsOfServicesText: string,
  73. callback?: Function
  74. ) {
  75. var params = {
  76. body: {
  77. status: termsOfServicesStatus,
  78. tos_type: termsOfServicesType,
  79. text: termsOfServicesText,
  80. },
  81. };
  82. var apiPath = urlPath(BASE_PATH);
  83. return this.client.wrapWithDefaultHandler(this.client.post)(
  84. apiPath,
  85. params,
  86. callback
  87. );
  88. }
  89. /**
  90. * Updates a custom terms of services with new specified values
  91. *
  92. * API Endpoint: '/terms_of_services/:termsOfServicesID'
  93. * Method: PUT
  94. *
  95. * @param {string} termsOfServicesID - The id of the custom terms of services to update
  96. * @param {Object} updates - Fields ot the Terms of Service to update
  97. * @param {TermsOfServicesStatus} [updates.status] - Determine if the custom terms of service is scoped internall or externally to an enterprise
  98. * @param {string} [updates.text] - Text field for message associated with custom terms of services
  99. * @param {Function} [callback] - Passed the terms of services updated information if successful, error otherwise
  100. * @returns {Promise<Object>} A promise resolving to the terms of services object
  101. */
  102. update(
  103. termsOfServicesID: string,
  104. updates: {
  105. status?: TermsOfServicesStatus;
  106. text?: string;
  107. },
  108. callback?: Function
  109. ) {
  110. var params = {
  111. body: updates,
  112. };
  113. var apiPath = urlPath(BASE_PATH, termsOfServicesID);
  114. return this.client.wrapWithDefaultHandler(this.client.put)(
  115. apiPath,
  116. params,
  117. callback
  118. );
  119. }
  120. /**
  121. * Gets a specific custom terms of services with specified ID
  122. *
  123. * API Endpoint: '/terms_of_services/:termsOfServicesID'
  124. * Method: GET
  125. *
  126. * @param {string} termsOfServicesID - The id of the custom terms of services to retrieve
  127. * @param {Object} [options] - Additional options. Can be left null in most cases.
  128. * @param {string} [options.fields] - Comma-separated list of fields to return on the collaboration objects
  129. * @param {Function} [callback] - Passed the terms of services information with specified ID if successful, error otherwise
  130. * @returns {Promise<Object>} A promise resolving to the terms of services object
  131. */
  132. get(
  133. termsOfServicesID: string,
  134. options?: {
  135. fields?: string;
  136. },
  137. callback?: Function
  138. ) {
  139. var params = {
  140. qs: options,
  141. };
  142. var apiPath = urlPath(BASE_PATH, termsOfServicesID);
  143. return this.client.wrapWithDefaultHandler(this.client.get)(
  144. apiPath,
  145. params,
  146. callback
  147. );
  148. }
  149. /**
  150. * Gets custom terms of services for the user's enterprise
  151. *
  152. * API Endpoint: '/terms_of_services'
  153. * Method: GET
  154. *
  155. * @param {Object} [options] - Additional options. Can be left null in most cases.
  156. * @param {TermsOfServiceType} [options.tos_type] - Optional, indicates whether the terms of service is set for external or managed under enterprise
  157. * @param {string} [options.fields] - Comma-separated list of fields to return on the collaboration objects
  158. * @param {Function} [callback] - Passed the terms of services information if successful, error otherwise
  159. * @returns {Promise<Object>} A promise resolving to the terms of services object
  160. */
  161. getAll(
  162. options?: {
  163. tos_type?: TermsOfServicesType;
  164. fields?: string;
  165. },
  166. callback?: Function
  167. ) {
  168. var params = {
  169. qs: options,
  170. };
  171. var apiPath = urlPath(BASE_PATH);
  172. return this.client.wrapWithDefaultHandler(this.client.get)(
  173. apiPath,
  174. params,
  175. callback
  176. );
  177. }
  178. /**
  179. * Accepts/rejects custom terms of services for the user
  180. *
  181. * API Endpoint: '/terms_of_service_user_statuses'
  182. * Method: POST
  183. *
  184. * @param {string} termsOfServicesID - Terms of services ID to retrieve user statuses on
  185. * @param {boolean} isAccepted - Determines wehether the terms of services has been accepted or rejected
  186. * @param {Object} [options] - Additional options. Can be left null in most cases.
  187. * @param {string} [options.user_id] - Optional, user id to retrieve terms of service status on, default is current user
  188. * @param {Function} [callback] - Passed the terms of service user status information if successful, error otherwise
  189. * @returns {Promise<Object>} A promise resolving to the terms of service user status
  190. */
  191. createUserStatus(
  192. termsOfServicesID: string,
  193. isAccepted: boolean,
  194. options?: {
  195. user_id?: string;
  196. },
  197. callback?: Function
  198. ) {
  199. var params = {
  200. body: {
  201. tos: {
  202. id: termsOfServicesID,
  203. type: 'terms_of_service',
  204. },
  205. is_accepted: isAccepted,
  206. } as Record<string, any>,
  207. };
  208. if (options && options.user_id) {
  209. params.body.user = { id: options.user_id, type: 'user' };
  210. }
  211. var apiPath = urlPath(USER_STATUSES_PATH);
  212. return this.client.wrapWithDefaultHandler(this.client.post)(
  213. apiPath,
  214. params,
  215. callback
  216. );
  217. }
  218. /**
  219. * Gets a terms os service status given the terms of services id
  220. *
  221. * API Endpoint: '/terms_of_service_user_statuses'
  222. * Method: GET
  223. *
  224. * @param {string} termsOfServicesID - The ID of the terms of services to retrieve status on
  225. * @param {Object} [options] - Additional options. Can be left null in most cases
  226. * @param {string} [options.user_id] - Optional, the id of the user to retrieve status of custom terms and service on
  227. * @param {Function} [callback] - Passed the terms of service user status information if successful, error otherwise
  228. * @returns {Promise<Object>} A promise resolving to the terms of service user status
  229. */
  230. getUserStatus(
  231. termsOfServicesID: string,
  232. options?: {
  233. user_id?: string;
  234. },
  235. callback?: Function
  236. ) {
  237. var params = {
  238. qs: {
  239. tos_id: termsOfServicesID,
  240. },
  241. };
  242. if (options) {
  243. Object.assign(params.qs, options);
  244. }
  245. var apiPath = urlPath(USER_STATUSES_PATH);
  246. return this.client
  247. .get(apiPath, params)
  248. .then((response: any /* FIXME */) => {
  249. if (response.statusCode !== 200) {
  250. throw errors.buildUnexpectedResponseError(response);
  251. }
  252. return response.body.entries[0] ?? {};
  253. })
  254. .asCallback(callback);
  255. }
  256. /**
  257. * Accepts/rejects custom terms of services for the user
  258. *
  259. * API Endpoint: '/terms_of_service_user_statuses'
  260. * Method: PUT
  261. *
  262. * @param {string} termsOfServiceUserStatusID - Terms of service user status object ID
  263. * @param {boolean} isAccepted - Determines wehether the terms of services has been accepted or rejected
  264. * @param {Function} [callback] - Passed the terms of service user status updated information if successful, error otherwise
  265. * @returns {Promise<Object>} A promise resolving to the updated terms of service user status
  266. */
  267. updateUserStatus(
  268. termsOfServiceUserStatusID: string,
  269. isAccepted: boolean,
  270. callback?: Function
  271. ) {
  272. var params = {
  273. body: {
  274. is_accepted: isAccepted,
  275. },
  276. };
  277. var apiPath = urlPath(USER_STATUSES_PATH, termsOfServiceUserStatusID);
  278. return this.client.wrapWithDefaultHandler(this.client.put)(
  279. apiPath,
  280. params,
  281. callback
  282. );
  283. }
  284. /**
  285. * Creates a user status for terms of service, if already exists then update existing user status for terms of service
  286. *
  287. * API Endpoint: '/terms_of_service_user_statuses'
  288. * Method: POST/PUT
  289. *
  290. * @param {string} termsOfServicesID - Terms of services ID to retrieve user statuses on
  291. * @param {boolean} isAccepted - Determines wehether the terms of services has been accepted or rejected
  292. * @param {Object} [options] - Additional options. Can be left null in most cases.
  293. * @param {string} [options.user_id] - Optional, user id to retrieve terms of service status on, default is current user
  294. * @param {Function} [callback] - Passed the terms of service user status information if successful, error otherwise
  295. * @returns {Promise<Object>} A promise resolving to the terms of service user status
  296. */
  297. setUserStatus(
  298. termsOfServicesID: string,
  299. isAccepted: boolean,
  300. options?: {
  301. user_id?: string;
  302. },
  303. callback?: Function
  304. ) {
  305. var params = {
  306. body: {
  307. tos: {
  308. id: termsOfServicesID,
  309. type: 'terms_of_service',
  310. },
  311. is_accepted: isAccepted,
  312. } as Record<string, any>,
  313. };
  314. if (options && options.user_id) {
  315. params.body.user = { id: options.user_id, type: 'user' };
  316. }
  317. var apiPath = urlPath(USER_STATUSES_PATH);
  318. return this.client
  319. .post(apiPath, params)
  320. .then((response: any /* FIXME */) => {
  321. switch (response.statusCode) {
  322. // 200/201 - A user status has been successfully updated/created on terms of service
  323. // return the terms of service user status object
  324. case httpStatusCodes.OK:
  325. case httpStatusCodes.CREATED:
  326. return response.body;
  327. // 409 - Conflict
  328. // Terms of Service already exists. Update the existing terms of service object
  329. case httpStatusCodes.CONFLICT:
  330. var getOptions = Object.assign({ fields: 'id' }, options);
  331. return this.getUserStatus(termsOfServicesID, getOptions).then(
  332. (userStatus: any /* FIXME */) =>
  333. this.updateUserStatus(userStatus.id, isAccepted)
  334. );
  335. default:
  336. throw errors.buildUnexpectedResponseError(response);
  337. }
  338. })
  339. .asCallback(callback);
  340. }
  341. }
  342. /**
  343. * Enum value of scope of the custom terms of services set to either managed by an enterprise or enternal to an enterprise
  344. *
  345. * @readonly
  346. * @enum {TermsOfServicesType}
  347. */
  348. TermsOfService.prototype.type = TermsOfServicesType;
  349. /**
  350. * Enum value of status of the custom terms of services, either currently enabled or currently disabled
  351. *
  352. * @readonly
  353. * @enum {TermsOfServicesStatus}
  354. */
  355. TermsOfService.prototype.status = TermsOfServicesStatus;
  356. /**
  357. * @module box-node-sdk/lib/managers/terms-of-service
  358. * @see {@Link TermsOfService}
  359. */
  360. export = TermsOfService;