mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-14 00:33:17 +01:00
Add goals feature for personal goals with filters
This commit is contained in:
161
database/factories/GoalFactory.php
Normal file
161
database/factories/GoalFactory.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\GoalComparison;
|
||||
use App\Enums\GoalPeriod;
|
||||
use App\Enums\GoalType;
|
||||
use App\Enums\TimeEntryType;
|
||||
use App\Enums\Weekday;
|
||||
use App\Models\Goal;
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Service\Dto\GoalFiltersDto;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<Goal>
|
||||
*/
|
||||
class GoalFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$filters = new GoalFiltersDto;
|
||||
$filters->timeEntryType = TimeEntryType::Work;
|
||||
|
||||
return [
|
||||
'name' => $this->faker->sentence(3),
|
||||
'type' => GoalType::Personal,
|
||||
'comparison' => $this->faker->randomElement(GoalComparison::cases()),
|
||||
'target_seconds' => $this->faker->numberBetween(1, 40) * 3600,
|
||||
'period' => $this->faker->randomElement(GoalPeriod::cases()),
|
||||
'filters' => $filters,
|
||||
'timezone' => 'UTC',
|
||||
'week_start' => Weekday::Monday,
|
||||
'archived_at' => null,
|
||||
'organization_id' => Organization::factory(),
|
||||
// The member has to belong to the organization of the goal
|
||||
'member_id' => fn (array $attributes) => Member::factory()->state([
|
||||
'organization_id' => $attributes['organization_id'],
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Goal in the given organization, for a new member of that organization unless forMember() is used.
|
||||
*/
|
||||
public function forOrganization(Organization $organization): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'organization_id' => $organization->getKey(),
|
||||
'member_id' => Member::factory()->forOrganization($organization),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Personal goal of the given member.
|
||||
*/
|
||||
public function forMember(Member $member): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'type' => GoalType::Personal,
|
||||
'member_id' => $member->getKey(),
|
||||
'organization_id' => $member->organization_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization goal that counts the time of the given member.
|
||||
*/
|
||||
public function organizationGoalForMember(Member $member): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'type' => GoalType::Organization,
|
||||
'member_id' => $member->getKey(),
|
||||
'organization_id' => $member->organization_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Organization goal that counts the time of every member.
|
||||
*/
|
||||
public function forEveryMember(): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'type' => GoalType::Organization,
|
||||
'member_id' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function type(GoalType $type): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'type' => $type,
|
||||
]);
|
||||
}
|
||||
|
||||
public function archived(): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'archived_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function timezone(string $timezone): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'timezone' => $timezone,
|
||||
]);
|
||||
}
|
||||
|
||||
public function weekStart(Weekday $weekStart): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'week_start' => $weekStart,
|
||||
]);
|
||||
}
|
||||
|
||||
public function atLeast(int $targetSeconds): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'comparison' => GoalComparison::AtLeast,
|
||||
'target_seconds' => $targetSeconds,
|
||||
]);
|
||||
}
|
||||
|
||||
public function lessThan(int $targetSeconds): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'comparison' => GoalComparison::LessThan,
|
||||
'target_seconds' => $targetSeconds,
|
||||
]);
|
||||
}
|
||||
|
||||
public function period(GoalPeriod $period): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'period' => $period,
|
||||
]);
|
||||
}
|
||||
|
||||
public function filters(GoalFiltersDto $filters): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'filters' => $filters,
|
||||
]);
|
||||
}
|
||||
|
||||
public function randomCreatedAt(): self
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'created_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
56
database/migrations/2026_09_02_000001_create_goals_table.php
Normal file
56
database/migrations/2026_09_02_000001_create_goals_table.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('goals', function (Blueprint $table): void {
|
||||
$table->uuid('id')->primary();
|
||||
$table->string('name');
|
||||
// personal: a goal a member set for themselves, organization: a goal the organization set (team goals extension)
|
||||
$table->string('type');
|
||||
$table->string('comparison');
|
||||
$table->unsignedBigInteger('target_seconds');
|
||||
$table->string('period');
|
||||
$table->jsonb('filters');
|
||||
// Timezone and week start define the periods of the goal, they are pinned on the goal like on a report
|
||||
$table->string('timezone');
|
||||
$table->string('week_start');
|
||||
$table->dateTime('archived_at')->nullable();
|
||||
$table->uuid('organization_id');
|
||||
$table->foreign('organization_id')
|
||||
->references('id')
|
||||
->on('organizations')
|
||||
->restrictOnDelete()
|
||||
->cascadeOnUpdate();
|
||||
// Member whose time entries count towards the goal, null means every member of the organization
|
||||
// (organization goals only). Personal goals always have a member.
|
||||
$table->uuid('member_id')->nullable();
|
||||
$table->foreign('member_id')
|
||||
->references('id')
|
||||
->on('members')
|
||||
->restrictOnDelete()
|
||||
->cascadeOnUpdate();
|
||||
$table->timestamps();
|
||||
$table->index(['organization_id', 'member_id']);
|
||||
$table->index(['organization_id', 'type']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('goals');
|
||||
}
|
||||
};
|
||||
@@ -4,11 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Enums\GoalPeriod;
|
||||
use App\Enums\Role;
|
||||
use App\Enums\TimeEntryType;
|
||||
use App\Events\DatabaseSeederAfterSeed;
|
||||
use App\Events\DatabaseSeederBeforeDelete;
|
||||
use App\Models\Audit;
|
||||
use App\Models\Client;
|
||||
use App\Models\Goal;
|
||||
use App\Models\Member;
|
||||
use App\Models\Organization;
|
||||
use App\Models\OrganizationInvitation;
|
||||
@@ -19,6 +22,7 @@ use App\Models\Tag;
|
||||
use App\Models\Task;
|
||||
use App\Models\TimeEntry;
|
||||
use App\Models\User;
|
||||
use App\Service\Dto\GoalFiltersDto;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Laravel\Passport\AuthCode;
|
||||
@@ -114,13 +118,28 @@ class DatabaseSeeder extends Seeder
|
||||
Tag::factory()->forOrganization($organizationAcme)->create([
|
||||
'name' => 'Code Review',
|
||||
]);
|
||||
Tag::factory()->forOrganization($organizationAcme)->create([
|
||||
$tagMeeting = Tag::factory()->forOrganization($organizationAcme)->create([
|
||||
'name' => 'Meeting',
|
||||
]);
|
||||
Tag::factory()->forOrganization($organizationAcme)->create([
|
||||
'name' => 'Research',
|
||||
]);
|
||||
|
||||
$workFilters = new GoalFiltersDto;
|
||||
$workFilters->timeEntryType = TimeEntryType::Work;
|
||||
Goal::factory()->forMember($userAcmeOwnerMember)->atLeast(6 * 3600)->period(GoalPeriod::Day)->filters($workFilters)->create([
|
||||
'name' => 'Track at least 6 hours per day',
|
||||
]);
|
||||
$meetingFilters = new GoalFiltersDto;
|
||||
$meetingFilters->setTagIds([$tagMeeting->getKey()]);
|
||||
$meetingFilters->timeEntryType = TimeEntryType::Work;
|
||||
Goal::factory()->forMember($userAcmeOwnerMember)->lessThan(5 * 3600)->period(GoalPeriod::Week)->filters($meetingFilters)->create([
|
||||
'name' => 'Less than 5 hours of meetings per week',
|
||||
]);
|
||||
Goal::factory()->forMember($userAcmeEmployeeMember)->atLeast(30 * 3600)->period(GoalPeriod::Week)->filters($workFilters)->create([
|
||||
'name' => 'Track 30 hours per week',
|
||||
]);
|
||||
|
||||
TimeEntry::factory()
|
||||
->count(10)
|
||||
->forMember($userAcmeAdminMember)
|
||||
@@ -219,6 +238,7 @@ class DatabaseSeeder extends Seeder
|
||||
// Application tables
|
||||
DB::table((new Audit)->getTable())->delete();
|
||||
DB::table((new Report)->getTable())->delete();
|
||||
DB::table((new Goal)->getTable())->delete();
|
||||
DB::table((new TimeEntry)->getTable())->delete();
|
||||
DB::table((new Task)->getTable())->delete();
|
||||
DB::table((new Tag)->getTable())->delete();
|
||||
|
||||
Reference in New Issue
Block a user