-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix buggy site information deduplication behavior (#2711)
Site information is currently mistakenly saved on every submission, regardless of whether any information actually changed. This PR fixes the issue and adds tests to prevent it from occurring again.
- Loading branch information
1 parent
3e3798d
commit 5f0a637
Showing
17 changed files
with
283 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Http\Submission\Traits; | ||
|
||
use App\Models\Site; | ||
use App\Models\SiteInformation; | ||
|
||
trait UpdatesSiteInformation | ||
{ | ||
/** | ||
* Saves a new site information record if $newInformation is different from the most recent | ||
* information recorded for the site. | ||
*/ | ||
protected function updateSiteInfoIfChanged(Site $site, SiteInformation $newInformation): void | ||
{ | ||
if ($site->information()->count() === 0) { | ||
// No existing information, so save whatever we're given, regardless of whether it's all null. | ||
$site->information()->save($newInformation); | ||
return; | ||
} | ||
|
||
$all_attributes_null = true; | ||
foreach ($newInformation->getFillable() as $attribute) { | ||
$all_attributes_null = $all_attributes_null && $newInformation->getAttribute($attribute) === null; | ||
} | ||
|
||
if ($all_attributes_null) { | ||
// Don't save information which is all null unless this is the first information we've received. | ||
return; | ||
} | ||
|
||
foreach ($newInformation->getFillable() as $attribute) { | ||
if ($site->mostRecentInformation?->getAttribute($attribute) !== $newInformation->getAttribute($attribute)) { | ||
// Something changed, so we can go ahead and save the new information. | ||
$site->information()->save($newInformation); | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.