1 var REST = library.REST.REST();
  2 var kCheckoutActions = {
  3   checkOut: "checkout",
  4   cancel: "cancel",
  5 };
  6 
  7 /**
  8  * Check out an asset
  9  * @param theAsset
 10  */
 11 function checkOut(theAsset) {
 12   if (
 13     searchManager.filterByACL([theAsset.assetId]).length == 0 ||
 14     checkoutManager.checkout(theAsset) === -1
 15   ) {
 16     REST.pushError(REST.errors.e500, "Failed to checkout asset " + theAsset);
 17     return;
 18   }
 19   REST.push(theAsset, {}, false);
 20 }
 21 
 22 /**
 23  * Cancel the check out of the asset
 24  * @param theAsset
 25  */
 26 function cancel(theAsset) {
 27   if (
 28     searchManager.filterByACL([theAsset.assetId]).length == 0 ||
 29     !checkoutManager.cancelCheckout(theAsset)
 30   ) {
 31     REST.pushError(
 32       REST.errors.e500,
 33       "Failed to cancel checkout for " + theAsset
 34     );
 35     return;
 36   }
 37 
 38   REST.push(theAsset, {}, false);
 39 }
 40 
 41 /**
 42  * Either check out or cancel a check out of an asset
 43  * @param theAsset
 44  */
 45 function doAction(theAsset) {
 46   switch (context.getParameter("action", "").toLowerCase()) {
 47     case kCheckoutActions.checkOut:
 48       return checkOut(theAsset);
 49     case kCheckoutActions.cancel:
 50       return cancel(theAsset);
 51     default:
 52       return REST.pushError(
 53         REST.errors.e400,
 54         "This Endpoint requires the parameter 'action' to equal '" +
 55           kCheckoutActions.checkout +
 56           "' or '" +
 57           kCheckoutActions.cancel +
 58           "'"
 59       );
 60   }
 61 }
 62 
 63 /**
 64  * @name CheckOut
 65  * @class Check out an asset or cancel the check out of an asset
 66  * @description This endpoint can check out assets so other users cannot modify them, and it can also cancel the check out of assets
 67  * @param [action] either 'checkout' or 'cancel'. This action is applied to all assets defined by the ids array or the resolver.
 68  * @param [ids] The asset id's
 69  * @param [resolver] A resolver url
 70  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 71  * @param [fields] An array of field id's to collect the values for each asset
 72  * @returns [{assetInfo}, ... ]
 73  *
 74  * @example /wf/restapi/v2/checkOut
 75  *
 76  * Parameters:
 77  * action=checkout
 78  * ids=[201629401]
 79  * fields=["http://mediabeacon.com/ns/default/1.0/ checkoutUser"]
 80  *
 81  * Response:
 82  [
 83  {
 84     "id": 201629401,
 85     "fields": {
 86       "http://mediabeacon.com/ns/default/1.0/ checkoutUser": "john.doe"
 87     }
 88   }
 89  ]
 90  * @example /wf/restapi/v2/checkout
 91  *
 92  * Parameters:
 93  * action=cancel
 94  * ids=[201629401]
 95  * fields=["http://mediabeacon.com/ns/default/1.0/ checkoutUser"]
 96  *
 97  * Response:
 98  [
 99  {
100     "id": 201629401,
101     "fields": {
102       "http://mediabeacon.com/ns/default/1.0/ checkoutUser": null
103     }
104   }
105  ]
106  */
107 function main() {
108   if (
109     (!context.getUser().isGlobalAdmin() &&
110       !context.getUser().isPermissionEnabled("checkOut")) ||
111     (context.getUser().isGlobalAdmin() &&
112       !context.getUser().isPermissionEnabled("checkOutAdmin"))
113   ) {
114     REST.pushError(
115       REST.errors.e403,
116       "checkOut or checkOutAdmin permission is not enabled"
117     );
118     return REST.execute();
119   }
120   REST.setCallback(doAction);
121   return REST.execute();
122 }
123 
124 main();
125