10 require_once(__DIR__ .
'/../../../../../app/web/shared/library/hcuCommon.i');
11 require_once(__DIR__ .
'/../../../../../app/web/shared/library/db.postgres.i');
12 require_once(__DIR__ .
'/../../../../../app/web/shared/library/hcuEnv.i');
14 $sysenv = LoadSystemEnv(
"eforms");
15 $dbh = GetDBH($sysenv[
'db']);
18 if (! UserInputOkay()) {
19 echo
"Exiting program." . PHP_EOL . PHP_EOL;
24 $ids = GetTestLoans($dbh);
25 }
catch (Exception $e) {
26 echo
"Exception thrown: {$e->getMessage()}" . PHP_EOL;
29 foreach ($ids as $loanid) {
32 $userid = GetUserId($dbh, $loanid);
33 echo
"deleting loan id $loanid user $userid" . PHP_EOL;
34 DeleteUserRecords($dbh, $userid);
35 DeleteLoanRecords($dbh, $loanid);
37 }
catch (Exception $e) {
38 echo
"Exception thrown: {$e->getMessage()}" . PHP_EOL;
43 echo PHP_EOL .
"All done. Type 'php generate-loans.php' to generate test loan records." . PHP_EOL . PHP_EOL;
51 function UserInputOkay()
55 while ($line !==
'y') {
62 Local test loan records cleanup script. 63 This will DELETE any local loan records and associated user records 64 with a loan ID >= 1000. Enter 'y' to continue (not 'Y' or yes, 'y'.) 65 Enter 'q' to exit without changes. 68 $handle = fopen (
"php://stdin",
"r");
69 $line = trim(fgets($handle));
83 function GetTestLoans($db)
87 $sql =
"select loanid from lnappuserresponse where loanid >= $1";
88 $result = pg_query_params($db, $sql, [$test_loans]);
91 throw new Exception(
"Exception getting test loans: " . pg_last_error());
94 $res = pg_fetch_all($result);
96 for ($i = 0; $i < count($res); $i++) {
97 $loan_ids[] = $res[$i][
'loanid'];
110 function GetUserId($db, $loanid)
112 $sql =
"select userid from lnappuserresponse where loanid = $1";
113 $result = pg_query_params($db, $sql, [$loanid]);
116 throw new Exception(
"Exception getting iser id for loan id $loanid: " . pg_last_error());
119 $res = pg_fetch_row($result);
121 return (isset($res[0]))? $res[0] : 0;
131 function DeleteUserRecords($db, $userid)
133 $sql =
"delete from lnappuser_questselect where userid = $1";
134 if (! pg_query_params($db, $sql, [$userid])) {
135 throw new Exception(
"Could not delete loan app question select: " . pg_last_error());
138 $sql =
"delete from lnappuser where userid = $1";
139 if (! pg_query_params($db, $sql, [$userid])) {
140 throw new Exception(
"Could not delete loan app user record: " . pg_last_error());
151 function DeleteLoanRecords($db, $loanid)
153 foreach ([
'lnappschemadetail',
'lnappschemamaster',
'lnappuserresponse'] as $table) {
155 $sql =
"delete from $table where loanid = $1";
157 if (! pg_query_params($db, $sql, [$loanid])) {
158 throw new Exception(
"Could not delete loan app record for table $table: " . pg_last_error());