Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

fix(bulk): keep payer and category rows in step with bulk delete and set-category

Miguel Ribeirob3a4d9879848
diff --git a/endpoints/subscriptions/bulkedit.php b/endpoints/subscriptions/bulkedit.php
index b1c71ec..1005521 100644
--- a/endpoints/subscriptions/bulkedit.php
+++ b/endpoints/subscriptions/bulkedit.php
@@ -2,6 +2,7 @@
require_once '../../includes/connect_endpoint.php';
require_once '../../includes/validate_endpoint.php';
require_once '../../includes/logo_cleanup.php';
+require_once '../../includes/subscription_categories.php';
$postData = file_get_contents("php://input");
$data = json_decode($postData, true);
@@ -65,6 +66,14 @@ if (isset($updates[$action])) {
if (!$stmt->execute()) {
bulkEditFail($i18n);
}
+
+ // The subscription_categories rows are the full list the filter and the
+ // statistics read, category_id is only the first of them: replace them.
+ if ($action === 'set_category') {
+ foreach ($ids as $subscriptionId) {
+ saveSubscriptionCategories($db, $subscriptionId, [$value]);
+ }
+ }
} elseif ($action === 'delete') {
// Same clean up as endpoints/subscription/delete.php, for each subscription.
$logos = [];
@@ -76,6 +85,14 @@ if (isset($updates[$action])) {
$logos[] = $row['logo_variant'];
}
+ // Subscription ids can be reused by SQLite, so the payer and category rows
+ // of a deleted subscription must not be left behind for the next one.
+ foreach (['subscription_payers', 'subscription_categories'] as $table) {
+ $stmt = $db->prepare("DELETE FROM $table WHERE subscription_id IN (SELECT id FROM subscriptions WHERE id IN ($idList) AND user_id = :userId)");
+ $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+ $stmt->execute();
+ }
+
$stmt = $db->prepare("DELETE FROM subscriptions WHERE id IN ($idList) AND user_id = :userId");
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
if (!$stmt->execute()) {
diff --git a/tests/cases/subscription_payers_test.php b/tests/cases/subscription_payers_test.php
index b78a6e5..7a25f52 100644
--- a/tests/cases/subscription_payers_test.php
+++ b/tests/cases/subscription_payers_test.php
@@ -203,3 +203,15 @@ wallos_test('the statistics split a price evenly between its categories, so they
assert_contains('$fullPrice / count($subscriptionCategoryIds)', $source, 'category cost divides by the number of categories');
assert_contains('array_intersect($subscriptionCategoryIds, $filteredCategoryIds)) / count($subscriptionCategoryIds)', $source, 'a category filter counts that category\'s share');
});
+
+/* Bulk actions (endpoints/subscriptions/bulkedit.php) have to keep the junction tables in step. */
+
+wallos_test('bulk delete removes the payer and category rows too, and bulk set-category replaces the category list', function () {
+ $source = file_get_contents(WALLOS_ROOT . '/endpoints/subscriptions/bulkedit.php');
+ assert_contains("foreach (['subscription_payers', 'subscription_categories'] as \$table)", $source,
+ 'a deleted subscription leaves no payer or category rows behind for a reused id');
+ assert_true(strpos($source, "DELETE FROM \$table") < strpos($source, 'DELETE FROM subscriptions WHERE id IN'),
+ 'the junction rows are removed while the subscriptions can still be found');
+ assert_contains('saveSubscriptionCategories($db, $subscriptionId, [$value])', $source,
+ 'set-category writes the list the filter and the statistics read');
+});