Skip to content

Commit 26bcd50

Browse files
tivviecweiske
authored andcommitted
TIM-123: Add new route get single entry by id: /tracking/entry/{id}
1 parent 11e8384 commit 26bcd50

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

src/Netresearch/TimeTrackerBundle/Controller/CrudController.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,58 @@ class CrudController extends BaseController
2323
{
2424
const LOG_FILE = 'trackingsave.log';
2525

26+
public function getAction(Request $request)
27+
{
28+
if (!$this->checkLogin($request)) {
29+
return $this->getFailedLoginResponse();
30+
}
31+
32+
// type and syntax check in routing.yml
33+
$entryId = $request->get('id');
34+
35+
$doctrine = $this->getDoctrine();
36+
/** @var Entry $entry */
37+
$entry = $doctrine->getRepository('NetresearchTimeTrackerBundle:Entry')
38+
->find($entryId);
39+
40+
if (!$entry) {
41+
$message = $this->get('translator')->trans('No entry for id.');
42+
return new Error($message, 404);
43+
}
44+
45+
// check for role, dev can only return their own tickets
46+
$ticketUserId = $entry->getUser() ? $entry->getUser()->getId() : null;
47+
$currentUserId = $this->getUserId($request);
48+
if ($this->isDEV($request) && ($ticketUserId != $currentUserId)) {
49+
$message = $this->get('translator')->trans('You are not allowed to view this entry.');
50+
return new Error($message, 403);
51+
}
52+
53+
// format output like /interpretation/allEntries
54+
$date = $entry->getDay() ? $entry->getDay()->format('Y-m-d') : null;
55+
$entry = $entry->toArray();
56+
57+
// unset unneded keys
58+
unset($entry['class']);
59+
$entry['date'] = $date;
60+
61+
// add id suffix to id parameter
62+
$entry['user_id'] = $entry['user'];
63+
$entry['project_id'] = $entry['project'];
64+
$entry['customer_id'] = $entry['customer'];
65+
$entry['activity_id'] = $entry['activity'];
66+
$entry['worklog_id'] = $entry['worklog'];
67+
68+
// unset old keys
69+
unset($entry['user']);
70+
unset($entry['project']);
71+
unset($entry['customer']);
72+
unset($entry['activity']);
73+
unset($entry['worklog']);
74+
75+
return new JsonResponse(array('data' => $entry));
76+
}
77+
2678
public function deleteAction(Request $request)
2779
{
2880
if (!$this->checkLogin($request)) {

src/Netresearch/TimeTrackerBundle/Resources/config/routing.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ interpretation_all_entries:
4242
path: /interpretation/allEntries
4343
defaults: { _controller: NetresearchTimeTrackerBundle:Interpretation:getAllEntries }
4444

45+
timetracking_get:
46+
path: /tracking/entry/{id}
47+
defaults: { _controller: NetresearchTimeTrackerBundle:Crud:get }
48+
requirements: { id: \d+}
49+
4550
timetracking_save:
4651
path: /tracking/save
4752
defaults: { _controller: NetresearchTimeTrackerBundle:Crud:save }

tests/src/Netresearch/TimeTrackerBundle/Controller/CrudControllerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,53 @@
77

88
class CrudControllerTest extends BaseTest
99
{
10+
public function testGetAction()
11+
{
12+
$this->client->request('GET', '/tracking/entry/1');
13+
14+
$expectedJson = [
15+
'data' => [
16+
'date' => '1000-01-30',
17+
'start' => '08:00',
18+
'end' => '08:50',
19+
'description' => '/interpretation/entries',
20+
'ticket' => 'testGetLastEntriesAction',
21+
'duration' => 50,
22+
'durationString' => '00:50',
23+
'extTicket' => "",
24+
'user_id' => 1,
25+
'project_id' => 1,
26+
'customer_id' => 1,
27+
'activity_id' => 1,
28+
'worklog_id' => null,
29+
],
30+
];
31+
32+
$this->assertStatusCode(200);
33+
$this->assertJsonStructure($expectedJson);
34+
}
35+
36+
public function testGetActionNoEntry()
37+
{
38+
$this->client->request('GET', '/tracking/entry/100');
39+
40+
$expectedJson = ['message' => 'Kein Eintrag für ID.'];
41+
42+
$this->assertStatusCode(404);
43+
$this->assertJsonStructure($expectedJson);
44+
}
45+
46+
public function testGetActionNotAllowed()
47+
{
48+
$this->logInSession('developer');
49+
$this->client->request('GET', '/tracking/entry/1');
50+
51+
$expectedJson = ['message' => 'Sie sind nicht berechtigt, diesen Eintrag anzuzeigen.'];
52+
53+
$this->assertStatusCode(403);
54+
$this->assertJsonStructure($expectedJson);
55+
}
56+
1057
public function testSaveAction()
1158
{
1259
$parameter = [

web/api.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,111 @@ paths:
18821882
type: object
18831883
$ref: '#/components/schemas/TicketSystem'
18841884

1885+
/tracking/entry/{id}:
1886+
get:
1887+
summary: get an entry by id
1888+
tags:
1889+
- Entries
1890+
parameters:
1891+
- in: path
1892+
name: id
1893+
required: true
1894+
description: The id of the entry
1895+
schema:
1896+
type: integer
1897+
example: 1
1898+
responses:
1899+
'500':
1900+
description: Error
1901+
'401':
1902+
description: Error Unauthorized
1903+
content:
1904+
application/json:
1905+
schema:
1906+
$ref: '#/components/schemas/UnauthorizedResponse'
1907+
'403':
1908+
description: Error
1909+
content:
1910+
application/json:
1911+
schema:
1912+
type: object
1913+
properties:
1914+
message:
1915+
type: string
1916+
example: You are not allowed to view this entry.
1917+
'404':
1918+
description: Error
1919+
content:
1920+
application/json:
1921+
schema:
1922+
type: object
1923+
properties:
1924+
message:
1925+
type: string
1926+
example: No entry for id.
1927+
'200':
1928+
description: OK
1929+
content:
1930+
application/json:
1931+
schema:
1932+
type: object
1933+
properties:
1934+
data:
1935+
type: object
1936+
properties:
1937+
id:
1938+
type: integer
1939+
date:
1940+
type: string
1941+
format: date
1942+
example: "2000-01-30"
1943+
start:
1944+
type: string
1945+
format: time
1946+
example: "08:00"
1947+
end:
1948+
type: string
1949+
format: time
1950+
example: "10:40"
1951+
description:
1952+
type: string
1953+
example: "Backen"
1954+
ticket:
1955+
type: string
1956+
example: "FG-33"
1957+
duration:
1958+
type: integer
1959+
description: The duration in minutes
1960+
example: 160
1961+
durationString:
1962+
type: string
1963+
description: The duration in string format (hh:mm)
1964+
example: "02:40"
1965+
extTicket:
1966+
type: string
1967+
description: String of the orginal jira key
1968+
example: ""
1969+
user_id:
1970+
type: integer
1971+
example: 2
1972+
project_id:
1973+
type: integer
1974+
example: 12
1975+
nullable: true
1976+
customer_id:
1977+
type: integer
1978+
example: 1
1979+
activity_id:
1980+
type: integer
1981+
example: 1
1982+
nullable: true
1983+
description: Activity record ID, see /getActivities
1984+
worklog_id:
1985+
type: integer
1986+
nullable: true
1987+
description: ID of Jira work log entry
1988+
example: null
1989+
18851990
/tracking/save:
18861991
post:
18871992
summary: Create an entry

0 commit comments

Comments
 (0)