refactor: time tracker component to combine conditions for retrieving remote log and handle API request

This commit is contained in:
Kifungo A
2024-10-14 15:06:27 +02:00
parent ad84931eb7
commit 1446e8e0c2
@@ -488,19 +488,29 @@ export class TimeTrackerComponent implements OnInit, AfterViewInit {
} else {
if (!this._isOffline) {
try {
timelog =
// Combine all conditions into a single descriptive boolean
const isRetrieveRemoteLog =
!isRemoteTimerRunning ||
isRemote ||
this._remoteSleepLock ||
(this.isRemoteTimer && (this._isSpecialLogout || this.quitApp))
? this._timeTrackerStatus.remoteTimer.lastLog
: await this.preventDuplicateApiRequest(
{
...params,
...lastTimer
},
(payload) => this.timeTrackerService.toggleApiStop(payload)
);
(this.isRemoteTimer && (this._isSpecialLogout || this.quitApp));
if (isRetrieveRemoteLog) {
// Retrieve remote timer log
timelog = this._timeTrackerStatus.remoteTimer.lastLog;
} else {
// Create request parameters only when necessary
const requestParams = {
...params,
...lastTimer
};
// Execute API request to stop timer and store the result in timelog
timelog = await this.preventDuplicateApiRequest(requestParams, (payload) =>
this.timeTrackerService.toggleApiStop(payload)
);
}
} catch (error) {
// Handle any error during the process
lastTimer.isStoppedOffline = true;
await this.electronService.ipcRenderer.invoke('MARK_AS_STOPPED_OFFLINE');
this._loggerService.error(error);