Creating an activity

The API schema (file activityAPI-1.0.swagger.yaml) defines a POST operation on the /activities endpoint. This HTTP operation creates a new activity. Gosu handler class ExampleActivitiesApiHandler provides the functionality for the example Activities API. You need to update the stub createActivity method in this class to perform the following work:
  • Accept a body argument that provides the details of the activity to create.
  • Load the provided activityPattern by code and validate it.
  • Load the account using the provided accounNumber.
  • Check that the account permits additional activities to be added to it.
  • Create a transaction for the result in a new bundle.
  • Use the already defined mapper for ActivityDetail to produce the method response.

Adding functionality to handler method createActivity

Updating class ExampleActivitiesApiHandler to provide the functionality for creating a new activity requires that you update the createActivity method. This class exists in the following directory in the Studio Project window:
  • configuration > gsrc > mc > activityapi
The following code sample illustrates an updated createActivity method , as well as, the use of several private functions to perform associated work.
function createActivity(body : JsonObject) : TransformResult {
  var activityPattern = loadActivityPattern(body.get("activityPattern") as String)
  var accountNumber = body.get("accountNumber") as String

  if (accountNumber == null) {
    throw new UnsupportedOperationException("Only linking through accountNumber is currently supported")
  }
  var account = loadAccount(accountNumber)
  if (account.AccountStatus == AccountStatus.TC_WITHDRAWN) {
    throw new BadInputException("The associated account has been withdrawn")
  }

  var activity : Activity
  gw.transaction.Transaction.runWithNewBundle(\b -> {
    activity = activityPattern.createAccountActivity(b, activityPattern, account,
          body.get("subject") as String,
          body.get("description") as String,
          null,
          body.get("property") as Priority,
          body.get("mandatory") as Boolean,
          body.get("targetDate") as Date,
          body.get("escalationDate") as Date)
    })

  var mapper = JsonConfigAccess.getMapper("mc.activityapi.activityAPI-1.0", "ActivityDetail")
  return mapper.transformObject(activity)
}   

private function loadActivityPattern(activityPatternCode : String) : ActivityPattern {
  var activityPattern = ActivityPattern. finder.getActivityPatternByCode(activityPatternCode)
  if (activityPattern ==  null) {
    throw new BadInputException( "No activity pattern was found with code " + activityPatternCode)
  }

  return activityPattern
}

//-----------------------PRIVATE FUNCTIONS--------------------------
private function loadAccount(accountNumber : String) : Account {
  var account = Account. finder.findAccountByAccountNumber(accountNumber)
  if (account ==  null) {
    throw new BadInputException( "No account exists with account number " + accountNumber)
  }
  return account
}

Next steps

After adding the mapper file in Studio and updating the API handler file, do the following:
  • Recompile the PolicyCenter application
  • Restart the application server

Testing your work

To test your work, you need to create a POST /activities request with a request body that contains the necessary activity information. To determine what information to include in the request body, review the definition for NewActivity in the JSON mapping file.

The following example JSON indicates the information that you need to include in the request body. Replace the placeholder values in the example with actual values.
{
  "accountNumber": "string",
  "activityPattern": "string",
  "description": "string",
  "escalationDate": "2019-11-14T18:06:32.150Z",
  "jobNumber": "string",
  "mandatory": true,
  "priority": "string",
  "subject": "string",
  "targetDate": "2019-11-14T18:06:32.150Z"
}

In testing your work, you can use Swagger UI or Postman to create and send the request to the application server, for example.