add find by user method in json scheduled node repo

This commit is contained in:
Yisroel Baum 2026-05-01 09:03:31 +03:00
parent 07e34ffd46
commit f2840a3eb1
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -2,12 +2,19 @@
namespace App\ScheduledNode;
use App\Node\NodeRepository;
use App\Plan\PlanRepository;
use App\User\User;
use DateTimeImmutable;
class JsonScheduledNodeRepository implements ScheduledNodeRepository
{
private string $filePath;
public function __construct()
{
public function __construct(
private PlanRepository $planRepo,
private NodeRepository $nodeRepo,
) {
$this->filePath = __DIR__ . '/../../data/scheduledNodes.json';
}
@ -66,4 +73,34 @@ class JsonScheduledNodeRepository implements ScheduledNodeRepository
return $maxId + 1;
}
public function findByUser(User $user): array
{
$allScheduledNodes = $this->readScheduledNodes();
$planIds = array_map(
function ($plan) {
return $plan->getId();
},
$this->planRepo->findByUser($user)
);
$usersScheduledNodes = array_filter(
$allScheduledNodes,
function ($node) use ($planIds) {
return in_array($node['planId'], $planIds);
}
);
return array_map(
function ($data) {
return new ScheduledNode(
id: $data['id'],
date: new DateTimeImmutable($data['date']),
plan: $this->planRepo->find($data['planId']),
node: $this->nodeRepo->find($data['nodeId']),
completed: $data['completed']
);
},
$usersScheduledNodes
);
}
}