Skip to content

Commit

Permalink
Fixes #65: Add custom endpoint to get Online Applications Campaign
Browse files Browse the repository at this point in the history
Remove entrypoint code and add tests

Update Application Form to use REST endpoint
  • Loading branch information
lschaefer-sugarcrm committed Jul 2, 2018
1 parent 68f3840 commit 06c9be3
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 268 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class OnlineApplicationsAPI extends SugarApi
{
/**
* Registers a custom API endpoint at /Campaigns/GetOnlineApplicationsCampaignId.
* The endpoint returns ID of the most recently updated Campaign with the name 'Online Applications'
* @return array The definition of the custom endpoint
*/
public function registerApiRest()
{
return array(
//GET
'onlineApplicationsGet' => array(

//request type
'reqType' => 'GET',

//set authentication
'noLoginRequired' => true,

//endpoint path
'path' => array('Campaigns', 'getOnlineApplicationsCampaignId'),

//method to call
'method' => 'getOnlineApplicationsCampaignId',

//short help string to be displayed in the help documentation
'shortHelp' => 'Get the ID of the most recently updated Campaign with the name \'Online Applications\'',

//long help to be displayed in the help documentation
'longHelp' => 'custom/modules/Campaigns/clients/base/api/help/GetOnlineApplicationsCampaignId.html',
),
);
}

/**
* Get the ID of the most recently updated Campaign with the name 'Online Applications'
*
* @return an array containing the ID of the most recently updated Campaign with the name 'Online Applications'
*/
public function getOnlineApplicationsCampaignId()
{
// Get the most recently updated Campaign with the name 'Online Applications'
$q = $this->getSugarQuery();
$q->select(array('id', 'name'));
$q->from($this->getNewCampaignBean(), array('team_security' => false));
$q->where()->equals('name', 'Online Applications');
$q->orderBy('date_modified', 'desc');
$q->limit(1);

$results = $q->execute();

if(sizeof($results) != 1 || !$results[0]['id']){
throw new \Exception("Unable to find ID for the Campaign named Online Applications");
}

return array('id' => $results[0]['id']);
}

/**
* Get Sugar Query.
* This function exists so we can mock the SugarQuery in automated tests.
*
* @return \SugarQuery
*/
protected function getSugarQuery()
{
return new \SugarQuery();
}

/**
* Get a new Campaign Bean.
* This function exists so we can mock the Campaigns Bean in automated tests.
*
* @return null|\SugarBean
*/
protected function getNewCampaignBean()
{
return \BeanFactory::newBean('Campaigns');
}

}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h2>Overview</h2>
<span class="lead">
A custom GET endpoint. Returns the ID of the most recently updated Campaign with the name 'Online Applications'.
</span>

<h2>Result</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
id
</td>
<td>
String
</td>
<td>
The ID of the most recently updated Campaign with the name 'Online Applications'.
</td>
</tr>
</tbody>
</table>

<h3>Output Example</h3>
<pre class="pre-scrollable">
{
"id": "f56b6d18-52e1-11e8-a080-34363bc46900"
}
</pre>
Loading

0 comments on commit 06c9be3

Please sign in to comment.