Rewrite processLogEntries for v2 shape; add golden-file test
This commit is contained in:
+39
-38
@@ -129,57 +129,58 @@ function prepareInsertStatement($pdo) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an array of log entries retrieved from the Torn API.
|
||||
* Process an array of log entries retrieved from the Torn v2 API.
|
||||
*
|
||||
* Goes through each log entry and checks if it's a vault deposit or withdrawal.
|
||||
* If it is, it checks if the entry already exists in the database. If it
|
||||
* doesn't, it inserts the entry into the database.
|
||||
* For each entry, extracts the v2 shape (`id`, `timestamp`,
|
||||
* `details.title`, `data.deposited` | `data.withdrawn`) and inserts it
|
||||
* via the prepared statement, which uses `ON CONFLICT(id) DO NOTHING`
|
||||
* for idempotency.
|
||||
*
|
||||
* @param array $logEntries The array of log entries to process
|
||||
* @param array $logEntries The array of v2 log entries to process
|
||||
* @param string $user The user whose log entries are being processed
|
||||
* @param PDOStatement $checkStmt A prepared statement to check if an entry
|
||||
* already exists in the database
|
||||
* @param PDOStatement $insertStmt A prepared statement to insert a new entry
|
||||
* into the database
|
||||
* @param boolean $debug Whether to output debug information (default: false)
|
||||
* @param PDOStatement $insertStmt A prepared statement for the idempotent
|
||||
* insert (`INSERT … ON CONFLICT(id) DO NOTHING`)
|
||||
*
|
||||
* @throws LogEntryIncompleteException If a vault entry is missing timestamp
|
||||
* or details.title
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function processLogEntries($logEntries, $user, $checkStmt, $insertStmt, $debug) {
|
||||
foreach ($logEntries as $key =>$entry) {
|
||||
if ($debug) {
|
||||
$logMessage = "Raw entry:\n" . print_r($entry, true);
|
||||
file_put_contents(__DIR__ . '/debug.log', $logMessage, FILE_APPEND);
|
||||
function processLogEntries($logEntries, $user, $insertStmt) {
|
||||
foreach ($logEntries as $entry) {
|
||||
$id = $entry['id'] ?? null;
|
||||
|
||||
if (!$id) {
|
||||
consoleLog('Skipping entry with no id: ' . print_r($entry, true));
|
||||
continue;
|
||||
}
|
||||
|
||||
$timestamp = $entry['timestamp'];
|
||||
$description = $entry['title'];
|
||||
$amount = $entry['log'] === 5850 ? $entry['data']['deposited'] : -$entry['data']['withdrawn'];
|
||||
$timestamp = $entry['timestamp'] ?? null;
|
||||
$description = $entry['details']['title'] ?? null;
|
||||
$hasDeposit = isset($entry['data']['deposited']);
|
||||
$hasWithdraw = isset($entry['data']['withdrawn']);
|
||||
|
||||
if ($debug) {
|
||||
$logMessage = "Vault entry:\n\tUser: $user,\n\tTimestamp: $timestamp,\n\tDescription: $description,\n\tAmount: $amount\n";
|
||||
file_put_contents(__DIR__ . '/debug.log', $logMessage, FILE_APPEND);
|
||||
if ($hasDeposit) {
|
||||
$amount = (int)$entry['data']['deposited'];
|
||||
} elseif ($hasWithdraw) {
|
||||
$amount = -((int)$entry['data']['withdrawn']);
|
||||
} else {
|
||||
consoleLog('Skipping entry ' . $id . ' with no deposited/withdrawn: ' . print_r($entry, true));
|
||||
continue;
|
||||
}
|
||||
|
||||
$checkStmt->execute(
|
||||
[
|
||||
':user' => $user,
|
||||
':timestamp' => $timestamp,
|
||||
':amount' => $amount
|
||||
]
|
||||
);
|
||||
|
||||
if ($checkStmt->fetchColumn() == 0) {
|
||||
$insertStmt->execute(
|
||||
[
|
||||
':id' => $key,
|
||||
':user' => $user,
|
||||
':timestamp' => $timestamp,
|
||||
':description' => $description,
|
||||
':amount' => $amount
|
||||
]
|
||||
if ($timestamp === null || $description === null) {
|
||||
throw new LogEntryIncompleteException(
|
||||
"Entry $id missing timestamp or details.title."
|
||||
);
|
||||
}
|
||||
|
||||
$insertStmt->bindValue(':id', $id);
|
||||
$insertStmt->bindValue(':user', $user);
|
||||
$insertStmt->bindValue(':timestamp', $timestamp);
|
||||
$insertStmt->bindValue(':description', $description);
|
||||
$insertStmt->bindValue(':amount', $amount);
|
||||
$insertStmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user