Skip to content
Merged

Dev #3217

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions app/Imports/MatchmakingProfileImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ public function model(array $row): ?Model
]);
if (!empty($countryValue)) {
$countryIso = $this->findCountryIso($countryValue);
Log::info('[MatchmakingProfileImport] Country lookup', [
'input' => $countryValue,
'result' => $countryIso,
]);
} else {
Log::info('[MatchmakingProfileImport] No country value found in row');
}

// Parse website - ensure it has protocol
Expand Down Expand Up @@ -388,15 +394,16 @@ public function model(array $row): ?Model
}
}

// Build the profile data
// Build the profile data - include all fields, even if null
// This ensures we can clear fields that should be empty
$profileData = [
'type' => $type,
'email' => $email,
'organisation_name' => $organisationName,
'organisation_type' => $organisationType,
'organisation_mission' => $organisationMission,
'website' => $website,
'country' => $countryIso,
'country' => $countryIso, // This can be null to clear the field
'support_activities' => $supportActivities,
'interested_in_school_collaboration' => $interestedInCollaboration,
'target_school_types' => $targetSchoolTypes,
Expand All @@ -414,23 +421,46 @@ public function model(array $row): ?Model
$profileData['slug'] = $slug;
}

// Remove null values to use database defaults, but keep empty arrays
$profileData = array_filter($profileData, function ($value) {
return $value !== null;
});
// For updates, we want to include all fields (even null) to properly update
// For new records, we can filter out nulls to use defaults
if ($existingProfile) {
// Keep all fields for updates, but convert empty strings to null for consistency
$profileData = array_map(function ($value) {
return $value === '' ? null : $value;
}, $profileData);
} else {
// For new records, remove null values to use database defaults
$profileData = array_filter($profileData, function ($value) {
return $value !== null;
});
}

try {
if ($existingProfile) {
// Update existing profile - use fill() and save() to ensure model events fire
// Log before update to see what we're updating
Log::info('[MatchmakingProfileImport] Before update', [
'id' => $existingProfile->id,
'current_country' => $existingProfile->country,
'new_country' => $countryIso,
'current_email' => $existingProfile->email,
'new_email' => $email,
]);

$existingProfile->fill($profileData);
$existingProfile->save();

// Refresh to get updated values
$existingProfile->refresh();

Log::info('[MatchmakingProfileImport] Updated existing profile', [
'id' => $existingProfile->id,
'slug' => $existingProfile->slug,
'email' => $existingProfile->email,
'organisation_name' => $existingProfile->organisation_name,
'country' => $existingProfile->country,
'updated_fields' => array_keys($profileData),
'profile_data' => $profileData,
]);

return $existingProfile;
Expand Down
Loading