Source: managers/retention-policies.ts

  1. /**
  2. * @fileoverview Manager for the Retention Policies Resource
  3. */
  4. // -----------------------------------------------------------------------------
  5. // Requirements
  6. // -----------------------------------------------------------------------------
  7. import BoxClient from '../box-client';
  8. import urlPath from '../util/url-path';
  9. import {UserMini} from "../schemas";
  10. // -----------------------------------------------------------------------------
  11. // Typedefs
  12. // -----------------------------------------------------------------------------
  13. /**
  14. * Enum of valid retention policy types, which specify how long the policy should
  15. * remain in effect.
  16. * @readonly
  17. * @enum {RetentionPolicyType}
  18. */
  19. enum RetentionPolicyType {
  20. FINITE = 'finite',
  21. INDEFINITE = 'indefinite',
  22. }
  23. /**
  24. * Enum of valid retention types.
  25. * @readonly
  26. * @enum {RetentionType}
  27. */
  28. enum RetentionType {
  29. /**
  30. * You can modify the retention policy. For example, you can add or remove folders,
  31. * shorten or lengthen the policy duration, or delete the assignment.
  32. * Use this type if your retention policy is not related to any regulatory purposes.
  33. */
  34. MODIFIABLE = 'modifiable',
  35. /**
  36. * You can modify the retention policy only in a limited way: add a folder, lengthen the duration,
  37. * retire the policy, change the disposition action or notification settings.
  38. * You cannot perform other actions, such as deleting the assignment or shortening the policy duration.
  39. * Use this type to ensure compliance with regulatory retention policies.
  40. */
  41. NON_MODIFIABLE = 'non_modifiable',
  42. }
  43. /**
  44. * Enum of valid retention policy disposition actions, which specify what should
  45. * be done when the retention period is over
  46. * @readonly
  47. * @enum {RetentionPolicyDispositionAction}
  48. */
  49. enum RetentionPolicyDispositionAction {
  50. PERMANENTLY_DELETE = 'permanently_delete',
  51. REMOVE_RETENTION = 'remove_retention',
  52. }
  53. /**
  54. * Enum of valid policy assignment types, which specify what object the policy applies to
  55. * @readonly
  56. * @enum {RetentionPolicyAssignmentType}
  57. */
  58. enum RetentionPolicyAssignmentType {
  59. FOLDER = 'folder',
  60. ENTERPRISE = 'enterprise',
  61. METADATA = 'metadata_template',
  62. }
  63. /**
  64. * Metadata template fields to filter on for assigning a retention policy
  65. * @typedef {Object} MetadataFilterField
  66. * @property {string} field The field to filter on
  67. * @property {string|int} value The value to filter against
  68. */
  69. type MetadataFilterField = {
  70. field: string;
  71. value: string | number;
  72. };
  73. // -----------------------------------------------------------------------------
  74. // Private
  75. // -----------------------------------------------------------------------------
  76. const BASE_PATH = '/retention_policies',
  77. ASSIGNMENTS_PATH = '/retention_policy_assignments',
  78. FILE_VERSION_RETENTIONS_PATH = '/file_version_retentions',
  79. ASSIGNMENTS_SUBRESOURCE = 'assignments',
  80. FILES_UNDER_RETENTION_SUBRESOURCE = 'files_under_retention',
  81. FILES_VERSIONS_UNDER_RETENTION_SUBRESOURCE = 'file_versions_under_retention';
  82. // -----------------------------------------------------------------------------
  83. // Public
  84. // -----------------------------------------------------------------------------
  85. /**
  86. * Simple manager for interacting with all Retention Policies endpoints and actions.
  87. *
  88. * @constructor
  89. * @param {BoxClient} client - The Box API Client that is responsible for making calls to the API
  90. * @returns {void}
  91. */
  92. class RetentionPolicies {
  93. client: BoxClient;
  94. policyTypes!: typeof RetentionPolicyType;
  95. dispositionActions!: typeof RetentionPolicyDispositionAction;
  96. assignmentTypes!: typeof RetentionPolicyAssignmentType;
  97. retentionTypes!: typeof RetentionType;
  98. constructor(client: BoxClient) {
  99. this.client = client;
  100. }
  101. /**
  102. * Used to create a single retention policy for an enterprise
  103. *
  104. * API Endpoint: '/retention_policies'
  105. * Method: POST
  106. *
  107. * @param {string} name - The name of the retention policy to be created
  108. * @param {RetentionPolicyType} type - The type of policy to create
  109. * @param {RetentionPolicyDispositionAction} action - The disposition action for the new policy
  110. * @param {Object} [options] - Additional parameters
  111. * @param {boolean} [options.are_owners_notified] - Whether or not owner and co-owners of a file are notified when the policy nears expiration
  112. * @param {boolean} [options.can_owner_extend_retention] - Whether or not the owner of a file will be allowed to extend the retention
  113. * @param {UserMini[]} [options.custom_notification_recipients] - A list of users notified when the retention policy duration is about to end
  114. * @param {string} [options.description] - The additional text description of the retention policy
  115. * @param {int} [options.retention_length] - For finite policies, the number of days to retain the content
  116. * @param {RetentionType} [options.retention_type] - The type of retention for the new policy
  117. * @param {Function} [callback] - Passed the new policy information if it was acquired successfully, error otherwise
  118. * @returns {Promise<Object>} A promise resolving to the new policy object
  119. */
  120. create(
  121. name: string,
  122. type: RetentionPolicyType,
  123. action: RetentionPolicyDispositionAction,
  124. options?: {
  125. are_owners_notified? : boolean;
  126. can_owner_extend_retention?: boolean;
  127. custom_notification_recipients?: UserMini[];
  128. description?: string;
  129. retention_length?: number;
  130. retention_type?: RetentionType;
  131. },
  132. callback?: Function
  133. ) {
  134. var apiPath = urlPath(BASE_PATH),
  135. params = {
  136. body: {
  137. policy_name: name,
  138. policy_type: type,
  139. disposition_action: action,
  140. },
  141. };
  142. Object.assign(params.body, options);
  143. return this.client.wrapWithDefaultHandler(this.client.post)(
  144. apiPath,
  145. params,
  146. callback
  147. );
  148. }
  149. /**
  150. * Fetches details about a specific retention policy
  151. *
  152. * API Endpoint: '/retention_policies/:policyID'
  153. * Method: GET
  154. *
  155. * @param {string} policyID - The Box ID of the retention policy being requested
  156. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  157. * @param {Function} [callback] - Passed the policy information if it was acquired successfully, error otherwise
  158. * @returns {Promise<Object>} A promise resolving to the policy object
  159. */
  160. get(policyID: string, options?: Record<string, any>, callback?: Function) {
  161. var apiPath = urlPath(BASE_PATH, policyID),
  162. params = {
  163. qs: options,
  164. };
  165. return this.client.wrapWithDefaultHandler(this.client.get)(
  166. apiPath,
  167. params,
  168. callback
  169. );
  170. }
  171. /**
  172. * Update or modify a retention policy.
  173. *
  174. * API Endpoint: '/retention_policies/:policyID'
  175. * Method: PUT
  176. *
  177. * @param {string} policyID - The Box ID of the retention policy to update
  178. * @param {Object} updates - The information to be updated
  179. * @param {string} [updates.policy_name] - The name of the retention policy
  180. * @param {RetentionPolicyDispositionAction} [updates.disposition_action] - The disposition action for the updated policy
  181. * @param {boolean} [updates.are_owners_notified] - Whether or not owner and co-owners of a file are notified when the policy nears expiration
  182. * @param {boolean} [updates.can_owner_extend_retention] - Whether or not the owner of a file will be allowed to extend the retention
  183. * @param {UserMini[]} [updates.custom_notification_recipients] - A list of users notified when the retention policy duration is about to end
  184. * @param {string} [updates.description] - The additional text description of the retention policy
  185. * @param {int} [updates.retention_length] - For finite policies, the number of days to retain the content
  186. * @param {RetentionType} [updates.retention_type] - The type of retention. The only possible value here is non_modifiable. You can convert a modifiable policy to non_modifiable, but not the other way around.
  187. * @param {string} [updates.status] - Used to retire a retention policy if status is set to retired
  188. * @param {Function} [callback] - Passed the updated policy information if it was acquired successfully, error otherwise
  189. * @returns {Promise<Object>} A promise resolving to the updated policy object
  190. */
  191. update(
  192. policyID: string,
  193. updates: {
  194. policy_name?: string;
  195. disposition_action?: RetentionPolicyDispositionAction;
  196. are_owners_notified? : boolean;
  197. can_owner_extend_retention?: boolean;
  198. custom_notification_recipients?: UserMini[];
  199. description?: string;
  200. retention_length?: number;
  201. retention_type?: RetentionType;
  202. status?: string;
  203. },
  204. callback?: Function
  205. ) {
  206. var apiPath = urlPath(BASE_PATH, policyID),
  207. params = {
  208. body: updates,
  209. };
  210. return this.client.wrapWithDefaultHandler(this.client.put)(
  211. apiPath,
  212. params,
  213. callback
  214. );
  215. }
  216. /**
  217. * Fetches a list of retention policies for the enterprise
  218. *
  219. * API Endpoint: '/retention_policies
  220. * Method: GET
  221. *
  222. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  223. * @param {string} [options.policy_name] - A full or partial name to filter the retention policies by
  224. * @param {RetentionPolicyType} [options.policy_type] - A policy type to filter the retention policies by
  225. * @param {string} [options.created_by_user_id] - A user id to filter the retention policies by
  226. * @param {Function} [callback] - Passed the policy objects if they were acquired successfully, error otherwise
  227. * @returns {Promise<Object>} A promise resolving to the collection of policies
  228. */
  229. getAll(
  230. options?: {
  231. policy_name?: string;
  232. policy_type?: RetentionPolicyType;
  233. created_by_user_id?: string;
  234. },
  235. callback?: Function
  236. ) {
  237. var apiPath = urlPath(BASE_PATH),
  238. params = {
  239. qs: options,
  240. };
  241. return this.client.wrapWithDefaultHandler(this.client.get)(
  242. apiPath,
  243. params,
  244. callback
  245. );
  246. }
  247. /**
  248. * Fetch a list of assignments for a given retention policy
  249. *
  250. * API Endpoint: '/retention_policies/:policyID/assignments'
  251. * Method: GET
  252. *
  253. * @param {string} policyID - The Box ID of the retention policy to get assignments for
  254. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  255. * @param {RetentionPolicyAssignmentType} [options.type] - The type of the retention policy assignment to retrieve
  256. * @param {Function} [callback] - Passed the assignment objects if they were acquired successfully, error otherwise
  257. * @returns {Promise<Object>} A promise resolving to the collection of policy assignments
  258. */
  259. getAssignments(
  260. policyID: string,
  261. options?: {
  262. type?: RetentionPolicyAssignmentType;
  263. },
  264. callback?: Function
  265. ) {
  266. var apiPath = urlPath(BASE_PATH, policyID, ASSIGNMENTS_SUBRESOURCE),
  267. params = {
  268. qs: options,
  269. };
  270. return this.client.wrapWithDefaultHandler(this.client.get)(
  271. apiPath,
  272. params,
  273. callback
  274. );
  275. }
  276. /**
  277. * Assign a retention policy to a folder or the entire enterprise.
  278. *
  279. * API Endpoint: '/retention_policy_assignments
  280. * Method: POST
  281. *
  282. * @param {string} policyID - The ID of the policy to assign
  283. * @param {RetentionPolicyAssignmentType} assignType - The type of object the policy will be assigned to
  284. * @param {string} assignID - The Box ID of the object to assign the retention policy to
  285. * @param {Object} [options] - Optional parameters for the request
  286. * @param {MetadataFilterField[]} [options.filter_fields] - Metadata fields to filter against, if assigning to a metadata template
  287. * @param {string} [options.start_date_field] - Id of Metadata field which will be used to specify the start date for the retention policy, or set this to "upload_date" as value to use the date when the file was uploaded to Box
  288. * @param {Function} [callback] - Passed the new assignment object if successful, error otherwise
  289. * @returns {Promise<Object>} A promise resolving to the created assignment object
  290. */
  291. assign(
  292. policyID: string,
  293. assignType: RetentionPolicyAssignmentType,
  294. assignID: string,
  295. options?:
  296. | {
  297. filter_fields?: MetadataFilterField[];
  298. start_date_field?: string;
  299. }
  300. | Function
  301. | null,
  302. callback?: Function
  303. ) {
  304. // Shuffle optional arguments
  305. if (typeof options === 'function') {
  306. callback = options;
  307. options = null;
  308. }
  309. var apiPath = urlPath(ASSIGNMENTS_PATH),
  310. params = {
  311. body: {
  312. policy_id: policyID,
  313. assign_to: {
  314. type: assignType,
  315. id: assignID,
  316. },
  317. },
  318. };
  319. Object.assign(params.body, options);
  320. return this.client.wrapWithDefaultHandler(this.client.post)(
  321. apiPath,
  322. params,
  323. callback
  324. );
  325. }
  326. /**
  327. * Fetch a specific policy assignment
  328. *
  329. * API Endpoint: '/retention_policy_assignments/:assignmentID'
  330. * Method: GET
  331. *
  332. * @param {string} assignmentID - The Box ID of the policy assignment object to fetch
  333. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  334. * @param {Function} [callback] - Passed the assignment object if it was acquired successfully, error otherwise
  335. * @returns {Promise<Object>} A promise resolving to the assignment object
  336. */
  337. getAssignment(
  338. assignmentID: string,
  339. options?: Record<string, any>,
  340. callback?: Function
  341. ) {
  342. var apiPath = urlPath(ASSIGNMENTS_PATH, assignmentID),
  343. params = {
  344. qs: options,
  345. };
  346. return this.client.wrapWithDefaultHandler(this.client.get)(
  347. apiPath,
  348. params,
  349. callback
  350. );
  351. }
  352. /**
  353. * Delete a specific policy assignment.
  354. *
  355. * API Endpoint: '/retention_policy_assignments/:assignmentID'
  356. * Method: DELETE
  357. *
  358. * @param {string} assignmentID - The Box ID of the policy assignment object to delete
  359. * @param {Function} [callback] - Empty response body passed if successful.
  360. * @returns {Promise<void>} A promise resolving to nothing
  361. */
  362. deleteAssignment(assignmentID: string, callback?: Function) {
  363. var apiPath = urlPath(ASSIGNMENTS_PATH, assignmentID)
  364. return this.client.wrapWithDefaultHandler(this.client.del)(
  365. apiPath,
  366. null,
  367. callback
  368. );
  369. }
  370. /**
  371. * Get the specific retention record for a retained file version. To use this feature,
  372. * you must have the manage retention policies scope enabled for your API key
  373. * via your application management console.
  374. *
  375. * API Endpoint: '/file_version_retentions/:retentionID'
  376. * Method: GET
  377. *
  378. * @param {string} retentionID - The ID for the file retention record to retrieve
  379. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  380. * @param {Function} [callback] - Pass the file version retention record if successful, error otherwise
  381. * @returns {Promise<Object>} A promise resolving to the retention record
  382. */
  383. getFileVersionRetention(
  384. retentionID: string,
  385. options?: Record<string, any>,
  386. callback?: Function
  387. ) {
  388. var apiPath = urlPath(FILE_VERSION_RETENTIONS_PATH, retentionID),
  389. params = {
  390. qs: options,
  391. };
  392. return this.client.wrapWithDefaultHandler(this.client.get)(
  393. apiPath,
  394. params,
  395. callback
  396. );
  397. }
  398. /**
  399. * Get a list of retention records for a retained file versions in an enterprise.
  400. * To use this feature, you must have the manage retention policies scope enabled
  401. * for your API key via your application management console.
  402. *
  403. * API Endpoint: '/file_version_retentions'
  404. * Method: GET
  405. *
  406. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  407. * @param {string} [options.file_id] - A file id to filter the file version retentions by
  408. * @param {string} [options.file_version_id] - A file version id to filter the file version retentions by
  409. * @param {string} [options.policy_id] - A policy id to filter the file version retentions by
  410. * @param {RetentionPolicyDispositionAction} [options.disposition_action] - The disposition action of the retention policy to filter by
  411. * @param {string} [options.disposition_before] - Filter by retention policies which will complete before a certain time
  412. * @param {string} [options.disposition_after] - Filter by retention policies which will complete after a certain time
  413. * @param {int} [options.limit] - The maximum number of items to return in a page
  414. * @param {string} [options.marker] - Paging marker, left blank to begin paging from the beginning
  415. * @param {Function} [callback] - Pass the file version retention record if successful, error otherwise
  416. * @returns {Promise<Object>} A promise resolving to the collection of retention records
  417. */
  418. getAllFileVersionRetentions(
  419. options?: {
  420. file_id?: string;
  421. file_version_id?: string;
  422. policy_id?: string;
  423. disposition_action?: RetentionPolicyDispositionAction;
  424. disposition_before?: string;
  425. disposition_after?: string;
  426. limit?: number;
  427. marker?: string;
  428. },
  429. callback?: Function
  430. ) {
  431. var apiPath = urlPath(FILE_VERSION_RETENTIONS_PATH),
  432. params = {
  433. qs: options,
  434. };
  435. return this.client.wrapWithDefaultHandler(this.client.get)(
  436. apiPath,
  437. params,
  438. callback
  439. );
  440. }
  441. /**
  442. * Get files under retention by each assignment
  443. * To use this feature, you must have the manage retention policies scope enabled
  444. * for your API key via your application management console.
  445. *
  446. * API Endpoint: '/retention_policy_assignments/:assignmentID/files_under_retention'
  447. * Method: GET
  448. *
  449. * @param {string} assignmentID - The Box ID of the policy assignment object to fetch
  450. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  451. * @param {int} [options.limit] - The maximum number of items to return in a page
  452. * @param {string} [options.marker] - Paging marker, left blank to begin paging from the beginning
  453. * @param {Function} [callback] - Pass the file version retention record if successful, error otherwise
  454. * @returns {Promise<Object>} A promise resolving to the collection of retention records
  455. */
  456. getFilesUnderRetentionForAssignment(
  457. assignmentID: string,
  458. options?: {
  459. limit?: number;
  460. marker?: string;
  461. },
  462. callback?: Function
  463. ) {
  464. var apiPath = urlPath(
  465. ASSIGNMENTS_PATH,
  466. assignmentID,
  467. FILES_UNDER_RETENTION_SUBRESOURCE
  468. ),
  469. params = {
  470. qs: options,
  471. };
  472. return this.client.wrapWithDefaultHandler(this.client.get)(
  473. apiPath,
  474. params,
  475. callback
  476. );
  477. }
  478. /**
  479. * Get file versions under retention by each assignment
  480. * To use this feature, you must have the manage retention policies scope enabled
  481. * for your API key via your application management console.
  482. *
  483. * API Endpoint: '/retention_policy_assignments/:assignmentID/file_versions_under_retention'
  484. * Method: GET
  485. *
  486. * @param {string} assignmentID - The Box ID of the policy assignment object to fetch
  487. * @param {Object} [options] - Additional options for the request. Can be left null in most cases.
  488. * @param {int} [options.limit] - The maximum number of items to return in a page
  489. * @param {string} [options.marker] - Paging marker, left blank to begin paging from the beginning
  490. * @param {Function} [callback] - Pass the file version retention record if successful, error otherwise
  491. * @returns {Promise<Object>} A promise resolving to the collection of retention records
  492. */
  493. getFileVersionsUnderRetentionForAssignment(
  494. assignmentID: string,
  495. options?: {
  496. limit?: number;
  497. market?: string;
  498. },
  499. callback?: Function
  500. ) {
  501. var apiPath = urlPath(
  502. ASSIGNMENTS_PATH,
  503. assignmentID,
  504. FILES_VERSIONS_UNDER_RETENTION_SUBRESOURCE
  505. ),
  506. params = {
  507. qs: options,
  508. };
  509. return this.client.wrapWithDefaultHandler(this.client.get)(
  510. apiPath,
  511. params,
  512. callback
  513. );
  514. }
  515. }
  516. /**
  517. * Enum of valid retention policy types, which specify how long the policy should
  518. * remain in effect.
  519. * @readonly
  520. * @enum {RetentionPolicyType}
  521. */
  522. RetentionPolicies.prototype.policyTypes = RetentionPolicyType;
  523. /**
  524. * Enum of valid retention policy disposition actions, which specify what should
  525. * be done when the retention period is over
  526. * @readonly
  527. * @enum {RetentionPolicyDispositionAction}
  528. */
  529. RetentionPolicies.prototype.dispositionActions =
  530. RetentionPolicyDispositionAction;
  531. /**
  532. * Enum of valid policy assignment types, which specify what object the policy applies to
  533. * @readonly
  534. * @enum {RetentionPolicyAssignmentType}
  535. */
  536. RetentionPolicies.prototype.assignmentTypes = RetentionPolicyAssignmentType;
  537. /**
  538. * Enum of valid retention types. Could be either modifiable or non-modifiable.
  539. * @readonly
  540. * @enum {RetentionType}
  541. */
  542. RetentionPolicies.prototype.retentionTypes = RetentionType;
  543. export = RetentionPolicies;