diff --git a/core/app/Http/Controllers/Me/EventsController.php b/core/app/Http/Controllers/Me/EventsController.php index 29b483e..ff71a68 100644 --- a/core/app/Http/Controllers/Me/EventsController.php +++ b/core/app/Http/Controllers/Me/EventsController.php @@ -6,14 +6,17 @@ use App\Delivery\EventActions; use App\Enums\EventState; use App\Http\Controllers\Api\EventsController as ApiPresenter; use App\Http\Controllers\Controller; +use App\Ingest\EventUpserter; use App\Models\Event; +use App\Models\Source; use App\Models\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Support\Str; class EventsController extends Controller { - public function __construct(private readonly EventActions $actions) {} + public function __construct(private readonly EventActions $actions, private readonly EventUpserter $upserter) {} public function index(Request $request): JsonResponse { @@ -44,6 +47,53 @@ class EventsController extends Controller return response()->json(ApiPresenter::present($model->fresh())); } + public function store(Request $request): JsonResponse + { + /** @var User $user */ + $user = $request->attributes->get('user'); + $data = $request->validate([ + 'title' => ['required', 'string', 'max:200'], + 'subtitle' => ['sometimes', 'nullable', 'string', 'max:200'], + 'due_date' => ['required', 'date_format:Y-m-d'], + 'due_time' => ['sometimes', 'nullable', 'date_format:H:i'], + ]); + + $source = Source::firstOrCreate(['name' => 'manual'], ['token_hash' => hash('sha256', Str::random(64))]); + $dueAt = $data['due_date'].(empty($data['due_time']) ? '' : 'T'.$data['due_time']); + + [$result] = $this->upserter->upsert($source, [[ + 'user' => $user->login, + 'source_ref' => 'manual:'.Str::ulid(), + 'topic' => '', + 'due_at' => $dueAt, + 'fire_at' => \Carbon\CarbonImmutable::createFromFormat('Y-m-d', $data['due_date'])->subDays(30)->format('Y-m-d'), + 'after_due' => 'keep', + 'payload' => array_filter([ + 'title' => $data['title'], + 'subtitle' => $data['subtitle'] ?? null, + 'done_label' => 'Сделано', + ], fn ($v) => $v !== null && $v !== ''), + ]]); + + $event = Event::with('source')->findOrFail($result['id']); + + return response()->json(ApiPresenter::present($event) + ['source' => $event->source->name], 201); + } + + public function archive(Request $request): JsonResponse + { + /** @var User $user */ + $user = $request->attributes->get('user'); + $events = $user->events()->with('source') + ->whereIn('state', [EventState::Done->value, EventState::Expired->value]) + ->orderByDesc('updated_at')->limit(100)->get(); + + return response()->json(['events' => $events->map(fn (Event $e) => ApiPresenter::present($e) + [ + 'source' => $e->source->name, + 'closed_as' => $e->state === EventState::Expired ? 'Истекло' : ($e->payload['done_label'] ?? 'Сделано'), + ])->values()]); + } + private function own(Request $request, int $id): Event { /** @var User $user */ diff --git a/core/routes/web.php b/core/routes/web.php index 40cc23a..d397c66 100644 --- a/core/routes/web.php +++ b/core/routes/web.php @@ -18,8 +18,10 @@ Route::middleware('remote.user')->group(function () { Route::patch('/me', [ProfileController::class, 'update']); Route::post('/me/heartbeat', [ProfileController::class, 'heartbeat']); Route::get('/me/events', [MeEvents::class, 'index']); + Route::post('/me/events', [MeEvents::class, 'store']); Route::post('/me/events/{event}/ack', [MeEvents::class, 'ack']); Route::post('/me/events/{event}/done', [MeEvents::class, 'done']); + Route::get('/me/archive', [MeEvents::class, 'archive']); Route::get('/me/channels', [ChannelsController::class, 'index']); Route::post('/me/channels', [ChannelsController::class, 'store']); Route::delete('/me/channels/{channel}', [ChannelsController::class, 'destroy'])->whereNumber('channel'); diff --git a/core/tests/Feature/ManualEventsTest.php b/core/tests/Feature/ManualEventsTest.php new file mode 100644 index 0000000..a25ea88 --- /dev/null +++ b/core/tests/Feature/ManualEventsTest.php @@ -0,0 +1,88 @@ + 'nikita']; + + protected function setUp(): void + { + parent::setUp(); + config(['hado.default_tz' => 'UTC']); + CarbonImmutable::setTestNow('2026-09-04T12:00:00Z'); + } + + protected function tearDown(): void + { + CarbonImmutable::setTestNow(); + parent::tearDown(); + } + + public function test_creates_manual_event_grouped_by_date(): void + { + $this->postJson('/me/events', ['title' => 'Продлить страховку', 'subtitle' => 'раз в год', 'due_date' => '2026-09-20'], $this->as) + ->assertCreated() + ->assertJsonPath('state', 'preparing') + ->assertJsonPath('source', 'manual') + ->assertJsonPath('payload.title', 'Продлить страховку') + ->assertJsonPath('payload.done_label', 'Сделано') + ->assertJsonPath('fire_at', '2026-08-21'); + + $this->postJson('/me/events', ['title' => 'Далеко', 'due_date' => '2026-12-15'], $this->as)->assertJsonPath('state', 'scheduled'); + $this->postJson('/me/events', ['title' => 'Сегодня', 'due_date' => '2026-09-04', 'due_time' => '19:40'], $this->as) + ->assertJsonPath('state', 'today')->assertJsonPath('due_at', '2026-09-04T19:40'); + $this->postJson('/me/events', ['title' => 'Прошло', 'due_date' => '2026-09-01'], $this->as)->assertJsonPath('state', 'overdue'); + + $this->assertSame(4, Event::count()); + $this->assertSame('manual', Source::sole()->name); + $this->assertSame(AfterDue::Keep, Event::first()->after_due); + $this->assertSame(DueMode::Local, Event::first()->due_mode); + } + + public function test_validation(): void + { + $this->postJson('/me/events', ['due_date' => '2026-09-20'], $this->as)->assertStatus(422)->assertJsonValidationErrors(['title']); + $this->postJson('/me/events', ['title' => 'x'], $this->as)->assertStatus(422)->assertJsonValidationErrors(['due_date']); + $this->postJson('/me/events', ['title' => 'x', 'due_date' => '2026-09-20', 'due_time' => '25:00'], $this->as)->assertStatus(422)->assertJsonValidationErrors(['due_time']); + } + + public function test_manual_source_cannot_be_used_through_client_api(): void + { + $this->postJson('/me/events', ['title' => 'x', 'due_date' => '2026-09-20'], $this->as)->assertCreated(); + $this->putJson('/api/events', ['events' => []], ['Authorization' => 'Bearer manual'])->assertStatus(401); + } + + public function test_archive_lists_done_and_expired_newest_first(): void + { + $this->getJson('/me', $this->as); + $user = User::sole(); + $source = Source::create(['name' => 'docs', 'token_hash' => hash('sha256', 'x')]); + $make = fn (string $topic, EventState $state, string $updatedAt, ?string $doneLabel = null) => tap(Event::create([ + 'source_id' => $source->id, 'user_id' => $user->id, 'source_ref' => 'r', 'topic' => $topic, + 'due_mode' => DueMode::Local, 'due_date' => '2026-08-28', 'due_time' => '24:00:00', 'fire_on' => '2026-08-28', + 'after_due' => AfterDue::Expire, 'payload' => array_filter(['title' => "E $topic", 'done_label' => $doneLabel]), + 'state' => $state, 'quiet_until' => CarbonImmutable::parse('2000-01-01Z'), + ]), fn (Event $e) => Event::whereKey($e->id)->update(['updated_at' => $updatedAt])); + $make('a', EventState::Done, '2026-09-01 10:00:00', 'Поздравил'); + $make('b', EventState::Expired, '2026-09-02 10:00:00'); + $make('c', EventState::Withdrawn, '2026-09-03 10:00:00'); + $make('d', EventState::Today, '2026-09-03 10:00:00'); + + $r = $this->getJson('/me/archive', $this->as)->assertOk()->assertJsonCount(2, 'events'); + $this->assertSame(['b', 'a'], array_column($r->json('events'), 'topic')); + $this->assertSame(['Истекло', 'Поздравил'], array_column($r->json('events'), 'closed_as')); + } +}