Skip to content

Commit e2d7eb1

Browse files
author
EliuX
committed
feat: Check whether time entry is running or not when start and stop
1 parent 0fbfe4c commit e2d7eb1

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

time_tracker_api/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create(self, project):
2828
raise NotImplementedError # pragma: no cover
2929

3030
@abc.abstractmethod
31-
def update(self, id, data):
31+
def update(self, id, data, description=None):
3232
raise NotImplementedError # pragma: no cover
3333

3434
@abc.abstractmethod

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def current_user_id():
2121
def find_running(self):
2222
pass
2323

24+
@abc.abstractmethod
25+
def stop(self, id: str):
26+
pass
27+
28+
@abc.abstractmethod
29+
def restart(self, id: str):
30+
pass
31+
2432

2533
container_definition = {
2634
'id': 'time_entry',
@@ -169,6 +177,20 @@ def check_whether_current_user_owns_item(cls, data: dict):
169177
raise CustomError(HTTPStatus.FORBIDDEN,
170178
"The current user is not the owner of this time entry")
171179

180+
@classmethod
181+
def checks_owner_and_is_not_stopped(cls, data: dict):
182+
cls.check_whether_current_user_owns_item(data)
183+
184+
if data.get('end_date') is not None:
185+
raise CustomError(HTTPStatus.UNPROCESSABLE_ENTITY, "The specified time entry is already stopped")
186+
187+
@classmethod
188+
def checks_owner_and_is_not_started(cls, data: dict):
189+
cls.check_whether_current_user_owns_item(data)
190+
191+
if data.get('end_date') is None:
192+
raise CustomError(HTTPStatus.UNPROCESSABLE_ENTITY, "The specified time entry is already running")
193+
172194
def get_all(self, conditions: dict = {}) -> list:
173195
event_ctx = self.create_event_context("read-many")
174196
conditions.update({"owner_id": event_ctx.user_id})
@@ -182,11 +204,23 @@ def create(self, data: dict):
182204
event_ctx = self.create_event_context("create")
183205
return self.repository.create(data, event_ctx)
184206

185-
def update(self, id, data: dict):
186-
event_ctx = self.create_event_context("update")
207+
def update(self, id, data: dict, description=None):
208+
event_ctx = self.create_event_context("update", description)
187209
return self.repository.partial_update(id, data, event_ctx,
188210
peeker=self.check_whether_current_user_owns_item)
189211

212+
def stop(self, id):
213+
event_ctx = self.create_event_context("update", "Stop time entry")
214+
return self.repository.partial_update(id, {
215+
'end_date': current_datetime_str()
216+
}, event_ctx, peeker=self.checks_owner_and_is_not_stopped)
217+
218+
def restart(self, id):
219+
event_ctx = self.create_event_context("update", "Restart time entry")
220+
return self.repository.partial_update(id, {
221+
'end_date': None
222+
}, event_ctx, peeker=self.checks_owner_and_is_not_started)
223+
190224
def delete(self, id):
191225
event_ctx = self.create_event_context("delete")
192226
self.repository.delete(id, event_ctx, peeker=self.check_whether_current_user_owns_item)

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,26 @@ def delete(self, id):
163163

164164
@ns.route('/<string:id>/stop')
165165
@ns.response(HTTPStatus.NOT_FOUND, 'Running time entry not found')
166-
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
166+
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, '"The specified time entry is already stopped')
167167
@ns.param('id', 'The unique identifier of a running time entry')
168168
class StopTimeEntry(Resource):
169169
@ns.doc('stop_time_entry')
170170
@ns.marshal_with(time_entry)
171171
def post(self, id):
172172
"""Stop a running time entry"""
173-
return time_entries_dao.update(id, {
174-
'end_date': current_datetime_str()
175-
})
173+
return time_entries_dao.stop(id)
176174

177175

178176
@ns.route('/<string:id>/restart')
179177
@ns.response(HTTPStatus.NOT_FOUND, 'Stopped time entry not found')
180-
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
178+
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The specified time entry is already running')
181179
@ns.param('id', 'The unique identifier of a stopped time entry')
182180
class RestartTimeEntry(Resource):
183181
@ns.doc('restart_time_entry')
184182
@ns.marshal_with(time_entry)
185183
def post(self, id):
186184
"""Restart a time entry"""
187-
return time_entries_dao.update(id, {
188-
'end_date': None
189-
})
185+
return time_entries_dao.restart(id)
190186

191187

192188
@ns.route('/running')

0 commit comments

Comments
 (0)