Skip to content

Commit

Permalink
TIM-97: Add new function getAllEntriesAction()
Browse files Browse the repository at this point in the history
 - function takes a array of parameters and returns entries filtered by parameter
  • Loading branch information
tivvie authored and CybotTM committed Apr 30, 2024
1 parent 7682745 commit a1b7405
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Netresearch\TimeTrackerBundle\Controller;

use Netresearch\TimeTrackerBundle\Entity\Entry;
use Netresearch\TimeTrackerBundle\Response\Error;
use Netresearch\TimeTrackerBundle\Entity\User;
use Netresearch\TimeTrackerBundle\Helper\TimeHelper;
use Netresearch\TimeTrackerBundle\Model\JsonResponse;
use Netresearch\TimeTrackerBundle\Model\Response;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;

class InterpretationController extends BaseController
Expand Down Expand Up @@ -455,4 +457,116 @@ private function normalizeData(array $data)
return $normalized;
}

/**
* Retrieves filtered time tracker entries based on request parameters.
* Applies pagination.
*
* @param Request $request
* @return \Netresearch\TimeTrackerBundle\Model\JsonResponse|Error
* @throws \Exception
*/
public function getAllEntriesAction(Request $request)
{
if (false === $this->isPl($request)) {
return $this->getFailedAuthorizationResponse();
}

$project = (int) $request->get('project_id');
$datestart = $request->get('datestart');
$dateend = $request->get('dateend');
$customer = (int) $request->get('customer_id');
$activity = (int) $request->get('activity_id');
$maxResults = (int) $request->get('maxResults');
$page = (int) $request->get('page');

//prepare data
if ($page < 0) {
$message = $this->get('translator')->trans('page can not be negative.');
return new Error($message, 400);
}
$maxResults = $maxResults > 0 ? $maxResults : 50; //cant be lower than 0

$searchArray = [
'maxResults' => $maxResults, //default 50
'page' => $page
];
// parameter has to exist in request and has to be different from null -> return true -> set array key
($activity ?? null) ? $searchArray['activity'] = $activity : null;
($project ?? null) ? $searchArray['project'] = $project : null;
($datestart ?? null) ? $searchArray['datestart'] = $datestart : null;
($dateend ?? null) ? $searchArray['dateend'] = $dateend : null;
($customer ?? null) ? $searchArray['customer'] = $customer : null;

$repository = $this->getDoctrine()->getRepository('NetresearchTimeTrackerBundle:Entry');
try {
$paginator = new Paginator($repository->queryByFilterArray($searchArray));
} catch (\Exception $e) {
$response = new Response($this->translate($e->getMessage()));
$response->setStatusCode(406);
}

// get data
$entries = $paginator->getQuery()->getResult();
$entryList = array();
foreach ($entries as $entry) {
$flatEntry = $entry->toArray();
unset($flatEntry['class']);
$flatEntry['date'] = $entry->getDay() ? $entry->getDay()->format('Y-m-d') : null;

// add id suffix to if parameter
$flatEntry['user_id'] = $flatEntry['user'];
$flatEntry['project_id'] = $flatEntry['project'];
$flatEntry['customer_id'] = $flatEntry['customer'];
$flatEntry['activity_id'] = $flatEntry['activity'];
$flatEntry['worklog_id'] = $flatEntry['worklog'];
// unset old keys
unset($flatEntry['user']);
unset($flatEntry['project']);
unset($flatEntry['customer']);
unset($flatEntry['activity']);
unset($flatEntry['worklog']);

// build result
$entryList[] = $flatEntry;
}

// build url
parse_str($request->getQueryString(), $queryString);
unset($queryString['page']);
$queryString = '?' . http_build_query($queryString);
$route = $request->getUriForPath($request->getPathInfo()). $queryString;

// negative firstResult are interpreted as 0
$total = $paginator->count();

//self
$self = $route . '&' . http_build_query(['page' => $page]);

// returns null for empty Paginator, else returns last page for given $maxResults
$lastPage = ceil($total / $maxResults) - 1;
$last = $total
? $route . '&' . http_build_query(['page' => $lastPage])
: null;

// returns the last previous page with data, or null if you are on page 0 or there is no data
$prev = $page && $total
? $route . '&' . http_build_query(['page' => min($page - 1, $lastPage)])
: null;

//null when query would return empty data
$next = $page < $lastPage
? $route . '&' . http_build_query(['page' => $page + 1])
: null;

$links = [
'links' => [
'self' => $self,
'last' => $last,
'prev' => $prev,
'next' => $next,
],
];
$entryList = array_merge($links, ['data' => $entryList]);
return new JsonResponse($entryList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ interpretation_entries:
path: /interpretation/entries
defaults: { _controller: NetresearchTimeTrackerBundle:Interpretation:getLastEntries }

interpretation_all_entries:
path: /interpretation/allEntries
defaults: { _controller: NetresearchTimeTrackerBundle:Interpretation:getAllEntries }

timetracking_save:
path: /tracking/save
defaults: { _controller: NetresearchTimeTrackerBundle:Crud:save }
Expand Down
147 changes: 147 additions & 0 deletions web/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,148 @@ paths:
properties:
$ref: '#/components/schemas/ChartDataSuccessResponse'

/interpretation/allEntries:
get:
summary: Get all entries filtered by parameter. Not set parameter will not be searched (e.g. not declaring a project will return all projects). Data is sorted by descending id.
tags:
- Entries
parameters:
- $ref: '#/components/parameters/datestart'
- $ref: '#/components/parameters/dateend'
- in: query
name: project_id
description: Project ID
schema:
type: integer
example: 1
- in: query
name: customer_id
description: Customer ID
schema:
type: integer
example: 1
- in: query
name: activity_id
description: Activity ID
schema:
type: integer
example: 1
- in: query
name: maxResults
description: Number of returned entries, default = 50
schema:
type: integer
example: 50
- in: query
name: page
description: Offset
schema:
type: integer
example: 0
responses:
'406':
description: Error Not Acceptable
content:
application/json:
schema:
type: string
example: Error for databasequery
'403':
description: Error Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/ForbiddenResponse'
'401':
description: Error Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/UnauthorizedResponse'
'400':
description: invalid parameter value
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: 'page can not be negative.'
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
links:
type: object
properties:
self:
type: string
last:
type: string
prev:
type: string
description: url of previous entries set or null if no previous entries
next:
type: string
description: url of next entries set or null if no more entries
data:
type: array
items:
type: object
properties:
id:
type: integer
date:
type: string
format: date
example: "2000-01-30"
start:
type: string
format: time
example: "08:00"
end:
type: string
format: time
example: "10:40"
description:
type: string
example: "Backen"
ticket:
type: string
example: "FG-33"
duration:
type: integer
description: The duration in minutes
example: 160
durationString:
type: string
description: The duration in string format (hh:mm)
example: "02:40"
user_id:
type: integer
example: 2
project_id:
type: integer
example: 12
nullable: true
customer_id:
type: integer
example: 1
activity_id:
type: integer
example: 1
nullable: true
description: Activity record ID, see /getActivities
worklog_id:
type: integer
nullable: true
description: ID of Jira work log entry
example: null

/login:
post:
summary: login
Expand Down Expand Up @@ -2425,20 +2567,23 @@ components:
activity:
in: query
name: activity
description: Activity ID
schema:
type: integer
example: 2

customer:
in: query
name: customer
description: Customer ID
schema:
type: integer
example: 1

dateend:
in: query
name: dateend
description: Entries untill this date
schema:
type: string
format: date
Expand All @@ -2447,6 +2592,7 @@ components:
datestart:
in: query
name: datestart
description: Entries from this date onwards
schema:
type: string
format: date
Expand All @@ -2462,6 +2608,7 @@ components:
project:
in: query
name: project
description: Project ID
schema:
type: integer
example: 2
Expand Down

0 comments on commit a1b7405

Please sign in to comment.