Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion cypress/e2e/1_feature_tests/5_3_Manage_Beneficiaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,15 @@ describe('Manage beneficiaries', () => {
clickMergeButton();
verifyBeneficiaryRowLevel(TEST_LASTNAME1,0);
verifyBeneficiaryRowLevel(TEST_LASTNAME2,1);


// Verify history logs for merge operation
cy.getBeneficiaryIdFromRow(TEST_LASTNAME1).then(id1 => {
cy.checkHistoryLog('people', id1, 'merged to family');
});
cy.getBeneficiaryIdFromRow(TEST_LASTNAME2).then(id2 => {
cy.checkHistoryLog('people', id2, 'merged to family');
});

//cleanup
fullDeleteOfMergedUsers();
});
Expand All @@ -266,6 +274,12 @@ describe('Manage beneficiaries', () => {
clickDetachButton();
verifyBeneficiaryRowLevel(TEST_LASTNAME1,0);
verifyBeneficiaryRowLevel(TEST_LASTNAME2,0);

// Verify history log for detach operation
cy.getBeneficiaryIdFromRow(TEST_LASTNAME2).then(id2 => {
cy.checkHistoryLog('people', id2, 'detached from family');
});

//cleanup
fullDeleteTestedBeneficiaries([TEST_FIRSTNAME1,TEST_FIRSTNAME2]);
});
Expand Down
26 changes: 26 additions & 0 deletions cypress/support/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,29 @@ Cypress.Commands.add("testauth0user", (email) => {
expect(response.body).to.contain("true");
});
});

Cypress.Commands.add("checkHistoryLog", (tablename, recordId, expectedChange) => {
cy.request({
method: "POST",
url: "/ajax.php?file=testhistorycheck",
body: {
tablename: tablename,
record_id: recordId,
expected_change: expectedChange
},
form: true
}).then(response => {
expect(response.status).to.eq(200);
const body = typeof response.body === 'string' ? JSON.parse(response.body) : response.body;
expect(body.found).to.eq(true);
expect(body.count).to.be.greaterThan(0);
});
});

Cypress.Commands.add("getBeneficiaryIdFromRow", (lastname) => {
return cy.getRowWithText(lastname).then($row => {
const id = $row.closest('tr').attr('data-id');
expect(id).to.not.be.undefined;
return parseInt(id);
});
});
27 changes: 26 additions & 1 deletion include/people.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ function () use ($cmsmain, $data) {
}
}
});
simpleBulkSaveChangeHistory('people', $ids, 'merged to family (head: '.$oldest.')');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should maybe be simpleBulkSaveChangeHistory('people', $ids, 'parent_id; merged to family', [], ['int'=> <parent_id>);

$success = true;
$message = 'The merge has be successfully applied';
$redirect = true;
Expand All @@ -464,6 +465,7 @@ function () use ($cmsmain, $data) {
db_query('UPDATE people SET parent_id = NULL WHERE id = :id', ['id' => $id]);
}
});
simpleBulkSaveChangeHistory('people', $ids, 'detached from family');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should maybe be simpleBulkSaveChangeHistory('people', $ids, 'parent_id; detached from family', ['int'=> <parent_id>, []);

$redirect = true;
$success = true;
$message = ($success) ? 'Selected people have been detached' : 'Something went wrong';
Expand All @@ -482,7 +484,30 @@ function () use ($cmsmain, $data) {
$ids = json_decode((string) $_POST['ids']);
// list($success, $message, $redirect, $aftermove) = listMove($table, $ids, true, 'correctdrops');
// Refactored list move method to use a transaction block and bulk insert for the correctdrops method
[$success, $message, $redirect, $aftermove] = listBulkMove($table, $ids, true, 'bulkcorrectdrops', true);
[$success, $message, $redirect, $aftermove, $parentChanges] = listBulkMove($table, $ids, true, 'bulkcorrectdrops', true);

// Log history for drag & drop family operations
if (!empty($parentChanges)) {
$addedToFamily = [];
$removedFromFamily = [];

foreach ($parentChanges as $change) {
if (is_null($change['old_parent_id']) && !is_null($change['new_parent_id'])) {
// Added to family
$addedToFamily[] = $change['id'];
} elseif (!is_null($change['old_parent_id']) && is_null($change['new_parent_id'])) {
// Removed from family
$removedFromFamily[] = $change['id'];
}
}

if (!empty($addedToFamily)) {
simpleBulkSaveChangeHistory('people', $addedToFamily, 'added to family via drag & drop');
}
if (!empty($removedFromFamily)) {
simpleBulkSaveChangeHistory('people', $removedFromFamily, 'removed from family via drag & drop');
}
}

break;

Expand Down
36 changes: 36 additions & 0 deletions library/ajax/testhistorycheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

// allowed databases
$devdbs = ['dropapp_dev', 'dropapp_staging'];
// confirmed testusers
$testusers = ['admin@admin.co', 'madmin@admin.co', 'coordinator@coordinator.co', 'user@user.co'];

$return = [];

// Test if user is a testusers and the database is a dev database
if (!(in_array($settings['db_database'], $devdbs) && in_array($_SESSION['user']['email'], $testusers))) {
$msg = 'You do not have access to check test data!';
trigger_error($msg, E_USER_ERROR);

echo json_encode(['error' => 'No permission']);
} else {
$recordId = $_POST['record_id'];
$tablename = $_POST['tablename'];
$expectedChange = $_POST['expected_change'];

// Query history table for matching record
$historyEntries = db_array(
'SELECT * FROM history WHERE tablename = :tablename AND record_id = :record_id AND changes LIKE :changes ORDER BY changedate DESC',
[
'tablename' => $tablename,
'record_id' => $recordId,
'changes' => '%'.$expectedChange.'%',
]
);

if (count($historyEntries) > 0) {
echo json_encode(['found' => true, 'count' => count($historyEntries), 'entries' => $historyEntries]);
} else {
echo json_encode(['found' => false, 'count' => 0]);
}
}
7 changes: 5 additions & 2 deletions library/lib/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function listBulkMove($table, $ids, $regardparent = true, $hook = '', $updatetra

$i = 1;
$return = '';
$hookIds = db_transaction(function () use ($ids, $hasParent, $table, $hook, $i, $updatetransactions) {
$parentChanges = [];
$hookIds = db_transaction(function () use ($ids, $hasParent, $table, $hook, $i, $updatetransactions, &$parentChanges) {
$hookIds = [];
$seq = [];
foreach ($ids as $line) {
Expand All @@ -72,6 +73,8 @@ function listBulkMove($table, $ids, $regardparent = true, $hook = '', $updatetra
if ($updatetransactions && null != $new_parent_id) {
db_query('UPDATE transactions SET people_id = :parent_id WHERE people_id = :id', ['parent_id' => $new_parent_id, 'id' => $id]);
}
// Track parent_id changes for history logging
$parentChanges[] = ['id' => $id, 'old_parent_id' => $old_parent_id, 'new_parent_id' => $new_parent_id];
}
}

Expand All @@ -88,7 +91,7 @@ function listBulkMove($table, $ids, $regardparent = true, $hook = '', $updatetra
$aftermove = $hook($hookIds);
}

return [true, $return, false, $aftermove];
return [true, $return, false, $aftermove, $parentChanges];
}

function listRealDelete($table, $ids, $uri = false)
Expand Down