Give tasks one assignee and members, and schedule template tasks to due dates
- A task now has one optional assignee (tasks.assignee_user_id), whose calendar row it is on and whose days its estimate books, and members (task_members) who see it among their own jobs and in the weekly sheet but get no bookings. A migration moves existing assignees over: the one with most booked time becomes the assignee, the others members. - Adding tasks from a workflow template asks for each step's assignee and, by default, schedules tasks with a due date to end on it, booking the estimate backwards from the due date (not before today); what does not fit runs past the due date and is reported as late. - The task calendar leaves quick sale tasks out of the unscheduled list, shows a bar's job details in a popover on hover and focus, and shows the day load on its own grid line. - The template table stacks its cells on narrow screens to reflow at 320 px. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -13,7 +13,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model of a task, used only inside the Jobs Infrastructure
|
||||
* layer. Assignees are in task_assignees, day bookings in task_bookings.
|
||||
* layer. Members are in task_members, the assignee's day bookings in
|
||||
* task_bookings.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $customer_job_id
|
||||
@@ -25,10 +26,11 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property CarbonImmutable|null $planned_from
|
||||
* @property CarbonImmutable|null $planned_to
|
||||
* @property string|null $estimated_hours
|
||||
* @property int|null $assignee_user_id
|
||||
* @property int $created_by
|
||||
*/
|
||||
#[Table('tasks')]
|
||||
#[Fillable(['customer_job_id', 'work_type_id', 'title', 'description', 'status', 'due_on', 'planned_from', 'planned_to', 'estimated_hours', 'created_by'])]
|
||||
#[Fillable(['customer_job_id', 'work_type_id', 'title', 'description', 'status', 'due_on', 'planned_from', 'planned_to', 'estimated_hours', 'assignee_user_id', 'created_by'])]
|
||||
#[UseFactory(TaskFactory::class)]
|
||||
class TaskModel extends Model
|
||||
{
|
||||
@@ -47,6 +49,7 @@ class TaskModel extends Model
|
||||
'due_on' => 'immutable_date',
|
||||
'planned_from' => 'immutable_date',
|
||||
'planned_to' => 'immutable_date',
|
||||
'assignee_user_id' => 'integer',
|
||||
'created_by' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* The jobs a user is involved in: responsible, contact person, biller,
|
||||
* member, or assignee of one of the job's tasks. Used for "my jobs" and
|
||||
* member, or assignee or member of one of the job's tasks. Used for "my jobs" and
|
||||
* for what a subcontractor may see.
|
||||
*/
|
||||
final class JobInvolvement
|
||||
@@ -27,10 +27,11 @@ final class JobInvolvement
|
||||
->orWhere($table.'.contact_user_id', $userId)
|
||||
->orWhere($table.'.biller_user_id', $userId)
|
||||
->orWhereIn($table.'.id', DB::table('customer_job_members')->select('customer_job_id')->where('user_id', $userId))
|
||||
->orWhereIn($table.'.id', DB::table('tasks')->select('customer_job_id')->where('assignee_user_id', $userId))
|
||||
->orWhereIn($table.'.id', DB::table('tasks')
|
||||
->join('task_assignees', 'task_assignees.task_id', '=', 'tasks.id')
|
||||
->join('task_members', 'task_members.task_id', '=', 'tasks.id')
|
||||
->select('tasks.customer_job_id')
|
||||
->where('task_assignees.user_id', $userId)));
|
||||
->where('task_members.user_id', $userId)));
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
+49
-14
@@ -2,17 +2,26 @@
|
||||
|
||||
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
||||
use App\Modules\Jobs\Application\Contracts\TaskCalendarQueries;
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarAssignment;
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarTask;
|
||||
use App\Modules\Jobs\Domain\Enums\JobStatus;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\JobInvolvement;
|
||||
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
{
|
||||
public function __construct(
|
||||
private CatalogQueries $catalog,
|
||||
private CustomerQueries $customers,
|
||||
private UserAccountQueries $users,
|
||||
) {}
|
||||
|
||||
public function scheduledTasks(string $from, string $to, ?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null): array
|
||||
{
|
||||
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
|
||||
@@ -29,6 +38,7 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
{
|
||||
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
|
||||
->whereNull('tasks.planned_from')
|
||||
->whereNull('customer_jobs.quick_sale_year')
|
||||
->where('tasks.status', '!=', TaskStatus::Done->value)
|
||||
->where('customer_jobs.status', '!=', JobStatus::Archived->value)
|
||||
->orderByRaw('tasks.due_on IS NULL')
|
||||
@@ -72,7 +82,7 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
|
||||
if ($onlyAssigneeUserId !== null) {
|
||||
JobInvolvement::restrict($query, $onlyAssigneeUserId);
|
||||
$query->whereIn('tasks.id', DB::table('task_assignees')->select('task_id')->where('user_id', $onlyAssigneeUserId));
|
||||
$query->where('tasks.assignee_user_id', $onlyAssigneeUserId);
|
||||
}
|
||||
|
||||
return $query;
|
||||
@@ -84,8 +94,10 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
private function calendarTasks(Builder $query): array
|
||||
{
|
||||
$rows = $query->get([
|
||||
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.status', 'tasks.due_on', 'tasks.estimated_hours',
|
||||
'tasks.planned_from', 'tasks.planned_to', 'customer_jobs.number', 'customer_jobs.name',
|
||||
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.description', 'tasks.status', 'tasks.due_on', 'tasks.estimated_hours',
|
||||
'tasks.planned_from', 'tasks.planned_to', 'tasks.assignee_user_id', 'tasks.work_type_id',
|
||||
'customer_jobs.number', 'customer_jobs.name', 'customer_jobs.customer_id', 'customer_jobs.status as job_status',
|
||||
'customer_jobs.due_on as job_due_on', 'customer_jobs.responsible_user_id',
|
||||
]);
|
||||
$taskIds = $rows->pluck('id')->map(fn ($id): int => (int) $id)->all();
|
||||
|
||||
@@ -93,7 +105,6 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
return [];
|
||||
}
|
||||
|
||||
$assignees = DB::table('task_assignees')->whereIn('task_id', $taskIds)->orderBy('user_id')->get()->groupBy('task_id');
|
||||
$bookings = DB::table('task_bookings')
|
||||
->whereIn('task_id', $taskIds)
|
||||
->groupBy('task_id', 'user_id')
|
||||
@@ -101,6 +112,14 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
->get()
|
||||
->keyBy(fn ($row): string => $row->task_id.'-'.$row->user_id);
|
||||
|
||||
$customerNames = $this->customers->namesByIds(self::ids($rows->pluck('customer_id')->all()));
|
||||
$userNames = $this->users->namesByIds(self::ids($rows->pluck('responsible_user_id')->all()));
|
||||
$workTypeNames = [];
|
||||
|
||||
foreach (self::ids($rows->pluck('work_type_id')->all()) as $workTypeId) {
|
||||
$workTypeNames[$workTypeId] = $this->catalog->findWorkType($workTypeId)->name ?? null;
|
||||
}
|
||||
|
||||
return $rows->map(fn ($row): CalendarTask => new CalendarTask(
|
||||
(int) $row->id,
|
||||
(int) $row->customer_job_id,
|
||||
@@ -112,19 +131,35 @@ class EloquentTaskCalendarQueries implements TaskCalendarQueries
|
||||
$row->estimated_hours === null ? null : (string) (float) $row->estimated_hours,
|
||||
self::date($row->planned_from),
|
||||
self::date($row->planned_to),
|
||||
($assignees->get($row->id) ?? collect())->map(function ($assignee) use ($bookings, $row): CalendarAssignment {
|
||||
$booking = $bookings->get($row->id.'-'.$assignee->user_id);
|
||||
|
||||
return new CalendarAssignment(
|
||||
(int) $assignee->user_id,
|
||||
self::date($booking?->first_day),
|
||||
self::date($booking?->last_day),
|
||||
(int) ($booking->minutes ?? 0),
|
||||
);
|
||||
})->values()->all(),
|
||||
$row->assignee_user_id === null ? null : self::assignment((int) $row->assignee_user_id, $bookings->get($row->id.'-'.$row->assignee_user_id)),
|
||||
customerName: $customerNames[(int) $row->customer_id] ?? null,
|
||||
jobStatus: JobStatus::tryFrom((string) $row->job_status),
|
||||
jobDueOn: self::date($row->job_due_on),
|
||||
responsibleName: $row->responsible_user_id === null ? null : ($userNames[(int) $row->responsible_user_id] ?? null),
|
||||
workTypeName: $workTypeNames[(int) $row->work_type_id] ?? null,
|
||||
description: $row->description === null ? null : (string) $row->description,
|
||||
))->values()->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $values
|
||||
* @return list<int>
|
||||
*/
|
||||
private static function ids(array $values): array
|
||||
{
|
||||
return array_values(array_unique(array_map('intval', array_filter($values, fn ($value): bool => $value !== null))));
|
||||
}
|
||||
|
||||
private static function assignment(int $userId, mixed $booking): CalendarAssignment
|
||||
{
|
||||
return new CalendarAssignment(
|
||||
$userId,
|
||||
self::date($booking?->first_day),
|
||||
self::date($booking?->last_day),
|
||||
(int) ($booking->minutes ?? 0),
|
||||
);
|
||||
}
|
||||
|
||||
private static function date(mixed $value): ?string
|
||||
{
|
||||
return $value === null ? null : substr((string) $value, 0, 10);
|
||||
|
||||
@@ -60,9 +60,10 @@ class EloquentTaskQueries implements TaskQueries
|
||||
public function assignedOpenTaskIds(int $userId): array
|
||||
{
|
||||
return DB::table('tasks')
|
||||
->join('task_assignees', 'task_assignees.task_id', '=', 'tasks.id')
|
||||
->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id')
|
||||
->where('task_assignees.user_id', $userId)
|
||||
->where(fn ($query) => $query
|
||||
->where('tasks.assignee_user_id', $userId)
|
||||
->orWhereIn('tasks.id', DB::table('task_members')->select('task_id')->where('user_id', $userId)))
|
||||
->where('tasks.status', '!=', TaskStatus::Done->value)
|
||||
->whereNotIn('customer_jobs.status', self::lockedStatuses())
|
||||
->orderBy('tasks.id')
|
||||
@@ -139,11 +140,13 @@ class EloquentTaskQueries implements TaskQueries
|
||||
*/
|
||||
private function summaries(Collection $models): array
|
||||
{
|
||||
$assignees = DB::table('task_assignees')
|
||||
$members = DB::table('task_members')
|
||||
->whereIn('task_id', $models->pluck('id')->all())
|
||||
->get()
|
||||
->groupBy('task_id');
|
||||
$userNames = $this->users->namesByIds($assignees->flatten()->pluck('user_id')->map(fn ($id): int => (int) $id)->unique()->values()->all());
|
||||
$userNames = $this->users->namesByIds($members->flatten()->pluck('user_id')
|
||||
->merge($models->pluck('assignee_user_id')->filter())
|
||||
->map(fn ($id): int => (int) $id)->unique()->values()->all());
|
||||
|
||||
$workTypeNames = [];
|
||||
foreach ($models->pluck('work_type_id')->unique() as $workTypeId) {
|
||||
@@ -159,7 +162,8 @@ class EloquentTaskQueries implements TaskQueries
|
||||
dueOn: $model->due_on?->format('Y-m-d'),
|
||||
estimatedHours: $model->estimated_hours === null ? null : rtrim(rtrim($model->estimated_hours, '0'), '.'),
|
||||
workType: new NamedId($model->work_type_id, $workTypeNames[$model->work_type_id] ?? ''),
|
||||
assignees: ($assignees->get($model->id) ?? collect())
|
||||
assignee: $model->assignee_user_id === null ? null : new NamedId($model->assignee_user_id, $userNames[$model->assignee_user_id] ?? ''),
|
||||
members: ($members->get($model->id) ?? collect())
|
||||
->map(fn ($row): NamedId => new NamedId((int) $row->user_id, $userNames[(int) $row->user_id] ?? ''))
|
||||
->sortBy('name')
|
||||
->values()
|
||||
|
||||
@@ -41,13 +41,14 @@ class EloquentTaskRepository implements TaskRepository
|
||||
'planned_from' => $task->plannedFrom()?->format('Y-m-d'),
|
||||
'planned_to' => $task->plannedTo()?->format('Y-m-d'),
|
||||
'estimated_hours' => $task->estimatedHours()?->value,
|
||||
'assignee_user_id' => $task->assigneeUserId(),
|
||||
'created_by' => $task->createdBy(),
|
||||
])->save();
|
||||
|
||||
DB::table('task_assignees')->where('task_id', $model->id)->whereNotIn('user_id', $task->assigneeUserIds())->delete();
|
||||
DB::table('task_assignees')->insertOrIgnore(array_map(
|
||||
DB::table('task_members')->where('task_id', $model->id)->whereNotIn('user_id', $task->memberUserIds())->delete();
|
||||
DB::table('task_members')->insertOrIgnore(array_map(
|
||||
fn (int $userId): array => ['task_id' => $model->id, 'user_id' => $userId],
|
||||
$task->assigneeUserIds(),
|
||||
$task->memberUserIds(),
|
||||
));
|
||||
|
||||
return $this->toEntity($model->refresh());
|
||||
@@ -69,7 +70,8 @@ class EloquentTaskRepository implements TaskRepository
|
||||
$model->status,
|
||||
self::date($model->due_on),
|
||||
$model->estimated_hours === null ? null : new EstimatedHours((float) $model->estimated_hours),
|
||||
DB::table('task_assignees')->where('task_id', $model->id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all(),
|
||||
$model->assignee_user_id,
|
||||
DB::table('task_members')->where('task_id', $model->id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all(),
|
||||
$model->created_by,
|
||||
self::date($model->planned_from),
|
||||
self::date($model->planned_to),
|
||||
|
||||
Reference in New Issue
Block a user