Skip to content

Commit

Permalink
feat: re-assignment of molecules functionality with audits is now ava…
Browse files Browse the repository at this point in the history
…ilable
  • Loading branch information
sriramkanakam87 committed Sep 16, 2024
1 parent 12c9e0e commit 824e06d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,54 +195,74 @@ function (Molecule $molecule, $record): HtmlString {
DB::transaction(function () use ($data, $records, $livewire) {
DB::beginTransaction();
try {
$current_sample_locations = [];
$current_sample_locations_subset = [];
$new_sample_locations = [];
$molecule_with_one_location = [];
$molecules_ids_with_multiple_locations = [];
foreach ($livewire->mountedTableBulkActionData['molecule_locations'] as $molecule) {
$molecues_with_muliple_locations = $livewire->mountedTableBulkActionData['molecule_locations'];

// Extract ids of all selected molecules
$moleculeIds = $records->pluck('id')->toArray();

// Extract ids of molecules with multiple locations
foreach ($molecues_with_muliple_locations as $molecule) {
array_push($molecules_ids_with_multiple_locations, $molecule['id']);
}
$moleculeIds = $records->pluck('id')->toArray();
$remaining_moleculeIds = array_diff($moleculeIds, $molecules_ids_with_multiple_locations);

// Detach molecules from Organism
// Extract ids of molecules with only one location
$molecule_ids_with_one_location = array_diff($moleculeIds, $molecules_ids_with_multiple_locations);

// Get the NEW organism
$newOrganism = Organism::findOrFail($data['org_id']);

// Get the NEW sample locations
$new_form_sample_locations = $livewire->mountedTableBulkActionData['locations'];
if ($new_form_sample_locations) {
$new_sample_locations = SampleLocation::findOrFail($new_form_sample_locations);
}

// Get the CURRENT organism
$currentOrganism = $this->getOwnerRecord();
$remaining_records = $records->whereIn('id', $remaining_moleculeIds);
$currentOrganism->auditDetach('molecules', $remaining_moleculeIds);
customAuditLog('cust_detach1', $remaining_records, 'organisms', $currentOrganism, []);

// Detach molecules from Sample Locations
// Handling Molecules with only ONE location
// Detach Molecules with only ONE location from CURRENT Organism
if ($molecule_ids_with_one_location) {
$molecule_with_one_location = $records->whereIn('id', $molecule_ids_with_one_location);
$currentOrganism->auditDetach('molecules', $molecule_ids_with_one_location);

foreach ($remaining_records as $record) {
$sample_locations = $record->sampleLocations()->where('organism_id', $this->getOwnerRecord()->id)->get();
foreach ($sample_locations as $location) {
$location->auditDetach('molecules', $record->id);
customAuditLog('cust_detach2', [$record], 'sampleLocations', $location, []);
// Detach molecules with only ONE location from CURRENT Sample Locations
foreach ($molecule_with_one_location as $record) {
$current_sample_locations = $record->sampleLocations()->where('organism_id', $this->getOwnerRecord()->id)->get();
foreach ($current_sample_locations as $location) {
$location->auditDetach('molecules', $record->id);
}
}
}
// foreach ($currentOrganism->sampleLocations() as $location) {
// if ($location->molecules()->whereIn('molecule_id', $remaining_moleculeIds)->count()) {
// $location->auditDetach('molecules', $remaining_moleculeIds);
// customAuditLog('cust_detach', $remaining_records, 'sampleLocations', $location, []);
// };
// }
foreach ($livewire->mountedTableBulkActionData['molecule_locations'] as $molecule) {
$sampleLocations = SampleLocation::findOrFail($molecule['sampleLocations']);
foreach ($sampleLocations as $location) {

// Handling Molecules with MULTIPLE locations
// Detach molecules from Sample Locations
foreach ($molecues_with_muliple_locations as $molecule) {
$current_sample_locations_subset = SampleLocation::findOrFail($molecule['sampleLocations']);
$current_sample_locations_multiple = $records->where('id', $molecule['id'])[0]->sampleLocations()->where('organism_id', $this->getOwnerRecord()->id)->get();
// dd($current_sample_locations_multiple, $current_sample_locations_subset);
if ($current_sample_locations_multiple->pluck('id') == $current_sample_locations_subset->pluck('id')) {
$currentOrganism->auditDetach('molecules', $molecule['id']);
}
foreach ($current_sample_locations_subset as $location) {
$location->auditDetach('molecules', $molecule['id']);
customAuditLog('cust_detach3', [$records->find($molecule['id'])], 'sampleLocations', $location, []);
customAuditLog('re-assign', [$records->find($molecule['id'])], 'sampleLocations', $current_sample_locations_subset, $new_sample_locations);
}
}
$newOrganism = Organism::findOrFail($data['org_id']);

$locations = $livewire->mountedTableBulkActionData['locations'];
if ($locations) {
$sampleLocations = SampleLocation::findOrFail($locations);
foreach ($sampleLocations as $location) {
$location->auditSyncWithoutDetaching('molecules', $moleculeIds);
customAuditLog('cust_sync', $records, 'sampleLocations', [], $location);
}
foreach ($new_sample_locations as $location) {
$location->auditSyncWithoutDetaching('molecules', $moleculeIds);
// customAuditLog('cust_sync', $records, 'sampleLocations', [], $location);
}
customAuditLog('re-assign', $molecule_with_one_location, 'sampleLocations', $current_sample_locations, $new_sample_locations);

$newOrganism->auditSyncWithoutDetaching('molecules', $moleculeIds);
customAuditLog('cust_sync', $records, 'organisms', [], $newOrganism);
customAuditLog('re-assign', $records, 'organisms', $currentOrganism, $newOrganism);

$currentOrganism->refresh();
$newOrganism->refresh();
Expand Down
41 changes: 26 additions & 15 deletions app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,34 @@ function customAuditLog($event_type, $model_objects, $column_name, $currentValue

function changeAudit(array $data): array
{
$whitelist = [
'id' => 'id',
'name' => 'name',
'identifier' => 'identifier',
];

$changed_data = [];
$changed_model = array_keys($data['old_values'])[0];

$changed_data['old_values'] = $data['old_values'][$changed_model] instanceof \Illuminate\Database\Eloquent\Model ? [$data['old_values'][$changed_model]->toArray()] : $data['old_values'][$changed_model];
$changed_data['new_values'] = $data['new_values'][$changed_model] instanceof \Illuminate\Database\Eloquent\Model ? [$data['new_values'][$changed_model]->toArray()] : $data['new_values'][$changed_model];
if (($data['event'] === 're-assign' || $data['event'] === 'detach' || $data['event'] === 'attach' || $data['event'] === 'sync') && $data['old_values'] && $data['new_values']) {
$whitelist = [
// 'id' => 'id',
'name' => 'name',
// 'identifier' => 'identifier',
];

if (! is_int($changed_data['old_values'])) {
foreach ($changed_data as $key_type => $changed_data_values) {
$data[$key_type][$changed_model] = [];
foreach ($changed_data_values as $key => $value) {
$data[$key_type][$changed_model][$key] = array_intersect_key($value, $whitelist);
$changed_data = [];

$changed_model = array_keys($data['old_values']) ? array_keys($data['old_values'])[0] : array_keys($data['new_values'])[0];
$changed_data['old_values'] = $data['old_values'][$changed_model] instanceof \Illuminate\Database\Eloquent\Model ? [$data['old_values'][$changed_model]->toArray()] : $data['old_values'][$changed_model];
$changed_data['new_values'] = $data['new_values'][$changed_model] instanceof \Illuminate\Database\Eloquent\Model ? [$data['new_values'][$changed_model]->toArray()] : $data['new_values'][$changed_model];

if (! is_int($changed_data['old_values'])) {
foreach ($changed_data as $key_type => $changed_data_values) {
$data[$key_type][$changed_model] = [];
foreach ($changed_data_values as $key => $value) {
$value = is_array($value) ? $value : $value->toArray();
if ($value) {
if (array_key_exists('identifier', $value)) {
$value['name'] = $value['name'].' ('.$value['id'].')'.' ('.$value['identifier'].')';
} else {
$value['name'] = $value['name'].' ('.$value['id'].')';
}
}
$data[$key_type][$changed_model][$key] = array_intersect_key($value, $whitelist);
}
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 824e06d

Please sign in to comment.