Skip to content

Commit

Permalink
Fix billiable rate in updateMultiple time entries (ST-396)
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Sep 3, 2024
1 parent e538fec commit 9df91f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Http/Controllers/Api/V1/TimeEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public function updateMultiple(Organization $organization, TimeEntryUpdateMultip
$error = new Collection();

foreach ($ids as $id) {
/** @var TimeEntry|null $timeEntry */
$timeEntry = $timeEntries->firstWhere('id', $id);
if ($timeEntry === null) {
// Note: ID wrong or time entry in different organization
Expand All @@ -316,6 +317,7 @@ public function updateMultiple(Organization $organization, TimeEntryUpdateMultip
if ($overwriteClient) {
$timeEntry->client()->associate($client);
}
$timeEntry->setComputedAttributeValue('billable_rate');
$timeEntry->save();
$success->push($id);
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,55 @@ public function test_update_multiple_updates_all_time_entries_and_fails_for_time
]);
}

public function test_update_multiple_refreshes_billable_rate_on_updates_time_entries(): void
{
// Arrange
$data = $this->createUserWithPermission([
'time-entries:update:all',
]);

$oldProject = Project::factory()->forOrganization($data->organization)->billable()->create();
$timeEntry1 = TimeEntry::factory()->forMember($data->member)->forProject($oldProject)->billable()->create();
$timeEntry2 = TimeEntry::factory()->forMember($data->member)->forProject($oldProject)->notBillable()->create();
$project = Project::factory()->billable()->forOrganization($data->organization)->create();
Passport::actingAs($data->user);

// Act
$response = $this->patchJson(route('api.v1.time-entries.update-multiple', [$data->organization->getKey()]), [
'ids' => [
$timeEntry1->getKey(),
$timeEntry2->getKey(),
],
'changes' => [
'project_id' => $project->getKey(),
],
]);

// Assert
$response->assertValid();
$response->assertStatus(200);
$response->assertExactJson([
'success' => [
$timeEntry1->getKey(),
$timeEntry2->getKey(),
],
'error' => [
],
]);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry1->getKey(),
'project_id' => $project->getKey(),
'billable' => true,
'billable_rate' => $project->billable_rate,
]);
$this->assertDatabaseHas(TimeEntry::class, [
'id' => $timeEntry2->getKey(),
'project_id' => $project->getKey(),
'billable' => false,
'billable_rate' => null,
]);
}

public function test_update_multiple_ignores_other_fields_in_changes(): void
{
// Arrange
Expand Down

0 comments on commit 9df91f4

Please sign in to comment.