Skip to content

Commit

Permalink
Removed option to update billable rate without updating time entries
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Jul 15, 2024
1 parent be50397 commit 1dc35f1
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 58 deletions.
8 changes: 3 additions & 5 deletions app/Http/Controllers/Api/V1/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ public function update(Organization $organization, Member $member, MemberUpdateR
{
$this->checkPermission($organization, 'members:update', $member);

if ($request->has('billable_rate')) {
if ($request->has('billable_rate') && $member->billable_rate !== $request->getBillableRate()) {
$member->billable_rate = $request->getBillableRate();

$billableRateService->updateTimeEntriesBillableRateForMember($member);
}
if ($request->has('role') && $member->role !== $request->getRole()->value) {
$newRole = $request->getRole();
Expand All @@ -95,10 +97,6 @@ public function update(Organization $organization, Member $member, MemberUpdateR
}
$member->save();

if ($request->getBillableRateUpdateTimeEntries()) {
$billableRateService->updateTimeEntriesBillableRateForMember($member);
}

return new MemberResource($member);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V1/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public function update(Organization $organization, OrganizationUpdateRequest $re
$this->checkPermission($organization, 'organizations:update');

$organization->name = $request->input('name');
$oldBillableRate = $organization->billable_rate;
$organization->billable_rate = $request->getBillableRate();
$organization->save();

if ($request->getBillableRateUpdateTimeEntries()) {
if ($oldBillableRate !== $request->getBillableRate()) {
$billableRateService->updateTimeEntriesBillableRateForOrganization($organization);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V1/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ public function update(Organization $organization, Project $project, ProjectUpda
if ($request->has('is_archived')) {
$project->archived_at = $request->getIsArchived() ? Carbon::now() : null;
}
$oldBillableRate = $project->billable_rate;
$project->billable_rate = $request->getBillableRate();
$project->client_id = $request->input('client_id');
$project->save();

if ($request->getBillableRateUpdateTimeEntries()) {
if ($oldBillableRate !== $request->getBillableRate()) {
$billableRateService->updateTimeEntriesBillableRateForProject($project);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V1/ProjectMemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public function store(Organization $organization, Project $project, ProjectMembe
public function update(Organization $organization, ProjectMember $projectMember, ProjectMemberUpdateRequest $request, BillableRateService $billableRateService): JsonResource
{
$this->checkPermission($organization, 'project-members:update', projectMember: $projectMember);
$oldBillableRate = $projectMember->billable_rate;
$projectMember->billable_rate = $request->getBillableRate();
$projectMember->save();

if ($request->getBillableRateUpdateTimeEntries()) {
if ($oldBillableRate !== $request->getBillableRate()) {
$billableRateService->updateTimeEntriesBillableRateForProjectMember($projectMember);
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V1/TimeEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public function update(Organization $organization, TimeEntry $timeEntry, TimeEnt

$timeEntry->fill($request->validated());
$timeEntry->description = $request->input('description', $timeEntry->description) ?? '';
$timeEntry->setComputedAttributeValue('billable_rate');
$timeEntry->save();

return new TimeEntryResource($timeEntry);
Expand Down
9 changes: 0 additions & 9 deletions app/Http/Requests/V1/Member/MemberUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public function rules(): array
'integer',
'min:0',
],
'billable_rate_update_time_entries' => [
'boolean',
],
];
}

Expand All @@ -45,12 +42,6 @@ public function getBillableRate(): ?int
return $input !== null && $input !== 0 ? (int) $this->input('billable_rate') : null;
}

public function getBillableRateUpdateTimeEntries(): bool
{
return $this->has('billable_rate_update_time_entries') &&
$this->boolean('billable_rate_update_time_entries');
}

public function getRole(): Role
{
return Role::from($this->input('role'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function rules(): array
'integer',
'min:0',
],
'billable_rate_update_time_entries' => [
'boolean',
],
];
}

Expand All @@ -43,10 +40,4 @@ public function getBillableRate(): ?int

return $input !== null && $input !== 0 ? (int) $this->input('billable_rate') : null;
}

public function getBillableRateUpdateTimeEntries(): bool
{
return $this->has('billable_rate_update_time_entries') &&
$this->boolean('billable_rate_update_time_entries');
}
}
9 changes: 0 additions & 9 deletions app/Http/Requests/V1/Project/ProjectUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public function rules(): array
'integer',
'min:0',
],
'billable_rate_update_time_entries' => [
'boolean',
],
];
}

Expand All @@ -81,10 +78,4 @@ public function getBillableRate(): ?int

return $input !== null && $input !== 0 ? (int) $this->input('billable_rate') : null;
}

public function getBillableRateUpdateTimeEntries(): bool
{
return $this->has('billable_rate_update_time_entries') &&
$this->boolean('billable_rate_update_time_entries');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public function rules(): array
'integer',
'min:0',
],
'billable_rate_update_time_entries' => [
'boolean',
],
];
}

Expand All @@ -38,10 +35,4 @@ public function getBillableRate(): ?int

return $input !== null && $input !== 0 ? (int) $this->input('billable_rate') : null;
}

public function getBillableRateUpdateTimeEntries(): bool
{
return $this->has('billable_rate_update_time_entries') &&
$this->boolean('billable_rate_update_time_entries');
}
}
2 changes: 2 additions & 0 deletions app/Jobs/Test/TestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestJob implements ShouldQueue
private User $user;

private string $message;

private bool $fail;

/**
Expand All @@ -37,6 +38,7 @@ public function __construct(User $user, string $message, bool $fail = false)

/**
* Execute the job.
*
* @throws Exception
*/
public function handle(): void
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Endpoint/Api/V1/MemberEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ public function test_update_member_succeeds_if_data_is_valid(): void
$data = $this->createUserWithPermission([
'members:update',
]);
$member = Member::factory()->forOrganization($data->organization)->role(Role::Admin)->create();
$member = Member::factory()->forOrganization($data->organization)->withBillableRate()->role(Role::Admin)->create();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);

// Act
$response = $this->putJson(route('api.v1.members.update', [$data->organization->id, $member]), [
'billable_rate' => 10001,
'billable_rate' => $member->billable_rate,
'role' => Role::Employee->value,
]);

// Assert
$response->assertStatus(200);
$oldBillableRate = $member->billable_rate;
$member->refresh();
$this->assertSame(10001, $member->billable_rate);
$this->assertSame($oldBillableRate, $member->billable_rate);
$this->assertSame(Role::Employee->value, $member->role);
}

Expand All @@ -124,7 +125,6 @@ public function test_update_member_can_update_billable_rate_of_member_and_update
// Act
$response = $this->putJson(route('api.v1.members.update', [$data->organization->getKey(), $data->member]), [
'billable_rate' => 10001,
'billable_rate_update_time_entries' => true,
]);

// Assert
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Endpoint/Api/V1/OrganizationEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public function test_update_endpoint_can_update_billable_rate_of_organization_an
$response = $this->withoutExceptionHandling()->putJson(route('api.v1.organizations.update', [$data->organization->getKey()]), [
'name' => $organizationFake->name,
'billable_rate' => $organizationFake->billable_rate,
'billable_rate_update_time_entries' => true,
]);

// Assert
Expand Down
7 changes: 3 additions & 4 deletions tests/Unit/Endpoint/Api/V1/ProjectEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public function test_update_endpoint_updates_project(): void
$this->assertFalse($project->is_archived);
}

public function test_update_endpoint_can_update_projects_billable_rate(): void
public function test_update_endpoint_does_not_update_billable_rates_of_time_entries_if_billable_rate_is_unchanged(): void
{
// Arrange
$data = $this->createUserWithPermission([
Expand All @@ -523,15 +523,15 @@ public function test_update_endpoint_can_update_projects_billable_rate(): void
'name' => $projectFake->name,
'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'billable_rate' => 10002,
'billable_rate' => $project->billable_rate,
]);

// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(Project::class, [
'name' => $projectFake->name,
'color' => $projectFake->color,
'billable_rate' => 10002,
'billable_rate' => $project->billable_rate,
]);
}

Expand All @@ -556,7 +556,6 @@ public function test_update_endpoint_can_update_projects_billable_rate_and_updat
'color' => $projectFake->color,
'is_billable' => $projectFake->is_billable,
'billable_rate' => 10003,
'billable_rate_update_time_entries' => true,
]);

// Assert
Expand Down
8 changes: 3 additions & 5 deletions tests/Unit/Endpoint/Api/V1/ProjectMemberEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,27 @@ public function test_update_endpoint_fails_if_user_has_no_permission_to_update_p
$response->assertForbidden();
}

public function test_update_endpoint_updates_project_member(): void
public function test_update_endpoint_updates_project_member_with_unchanged_billable_rate(): void
{
// Arrange
$data = $this->createUserWithPermission([
'project-members:update',
]);
$project = Project::factory()->forOrganization($data->organization)->create();
$billableRate = 1001;
$projectMember = ProjectMember::factory()->forProject($project)->create();
$this->assertBillableRateServiceIsUnused();
Passport::actingAs($data->user);

// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $billableRate,
'billable_rate' => $projectMember->billable_rate,
]);

// Assert
$response->assertStatus(200);
$this->assertDatabaseHas(ProjectMember::class, [
'id' => $projectMember->getKey(),
'billable_rate' => $billableRate,
'billable_rate' => $projectMember->billable_rate,
'member_id' => $projectMember->member_id,
]);
}
Expand All @@ -326,7 +325,6 @@ public function test_update_endpoints_can_update_billable_rate_and_update_time_e
// Act
$response = $this->putJson(route('api.v1.project-members.update', [$data->organization->getKey(), $projectMember->getKey()]), [
'billable_rate' => $billableRate,
'billable_rate_update_time_entries' => true,
]);

// Assert
Expand Down

0 comments on commit 1dc35f1

Please sign in to comment.