~cypheon/nextcloud-chores-app

ref: d19a6603c01e63c7545582e5ff313ef4aec0bf9f nextcloud-chores-app/lib/Service/WorklogService.php -rw-r--r-- 2.7 KiB
d19a6603 — Johann Rudloff Make overdue sidebar badge dynamic, switch badges to `CounterBubble` component 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace OCA\Chores\Service;

use \DateTimeImmutable;

use OCP\IDBConnection;

use OCA\Chores\Db\ChoreMapper;
use OCA\Chores\Db\WorklogItem;
use OCA\Chores\Db\WorkMapper;
use OCA\Chores\Db\MemberMapper;

class WorklogService {
  private $conn;
  private $choreMapper;
  private $memberMapper;
  private $workMapper;

  public function __construct(IDBConnection $db, WorkMapper $workMapper,
    MemberMapper $memberMapper, ChoreMapper $choreMapper) {
    $this->conn = $db;
    $this->choreMapper = $choreMapper;
    $this->workMapper = $workMapper;
    $this->memberMapper = $memberMapper;
  }

  public function getWorklog(int $teamId): array {
    return $this->workMapper->findAllByTeam($teamId);
  }

  public function submitWorkAndAddPoints(int $teamId, array $workItem,
    DateTimeImmutable $submitTimestamp) {
    if ($this->workMapper->itemExists($workItem["id"])) {
      // nothing to do here
      error_log("skip existing work item: " . $workItem["id"]);
      return;
    }

    $workTime = validateDate($workItem["work_time"]);
    $memberUserId = validateString($workItem["member"]);
    $choreId = intval($workItem["chore_id"]);

    $this->conn->beginTransaction();

    if (!$this->memberMapper->teamHasMember($teamId, $memberUserId)) {
      throw new \UnexpectedValueException("user is not a team member");
    }

    $chore = $this->choreMapper->findById($teamId, $choreId);
    $nextDueDate = $chore->getDueDate()->add(new \DateInterval("P1D"));
    try {
      $nextDueDate = $chore->getSchedule()->nextDueDate($workTime);
    } catch (\Exception $e) {
      error_log("failed to calculate new due date: $e");
    }

    $pointsForChore = $chore->getPoints();

    error_log("workitem = " . json_encode($workItem));
    error_log("next date: " . $nextDueDate->format('r'));

    try {
      $this->workMapper->insert(WorklogItem::fromParams([
        "id" => $workItem["id"],
        "teamId" => $teamId,
        "choreId" => $choreId,
        "doneTimestampDate" => $workTime,
        "submitTimestampDate" => $submitTimestamp,
        "memberUid" => $memberUserId,
        "points" => $pointsForChore,
      ]));
      $this->memberMapper->addPoints($memberUserId, $pointsForChore);
      $this->choreMapper->updateDueDate($choreId, $nextDueDate);
      $this->conn->commit();
    } catch (\Exception $e) {
      error_log("error, rollback: " . $e);
      $this->conn->rollback();
      throw $e;
    }
  }
}

function validateDate(string $date): \DateTimeImmutable {
  $parsed = new DateTimeImmutable($date);
  return $parsed;
}

function validateString($val): string {
  if (!is_string($val) or $val == "") {
    throw new \UnexpectedValueException("invalid string");
  }
  return $val;
}