From 8b87835850796f7a24fde9ef76afb84b28b93e42 Mon Sep 17 00:00:00 2001 From: Anton Weber Date: Sun, 27 Oct 2024 17:17:36 +0100 Subject: [PATCH] Migrate remaining usage of QueuedOrder --- .../libpretixsync/sync/SyncManager.java | 19 ++++++------------- .../libpretixsync/sqldelight/QueuedOrder.sq | 17 +++++++++++++++++ .../libpretixsync/sqldelight/Receipt.sq | 6 ++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java index 5e0ba71d..f4162fdb 100644 --- a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java +++ b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java @@ -7,6 +7,7 @@ import eu.pretix.libpretixsync.sqldelight.ClosingExtensionsKt; import eu.pretix.libpretixsync.sqldelight.QueuedCall; import eu.pretix.libpretixsync.sqldelight.QueuedCheckIn; +import eu.pretix.libpretixsync.sqldelight.QueuedOrder; import eu.pretix.libpretixsync.sqldelight.SyncDatabase; import eu.pretix.libpretixsync.utils.JSONUtils; import io.requery.sql.StatementExecutionException; @@ -22,7 +23,6 @@ import eu.pretix.libpretixsync.SentryInterface; import eu.pretix.libpretixsync.config.ConfigStore; import eu.pretix.libpretixsync.db.Answer; -import eu.pretix.libpretixsync.db.QueuedOrder; import eu.pretix.libpretixsync.db.Receipt; import eu.pretix.libpretixsync.db.ReceiptLine; import eu.pretix.libpretixsync.db.ReceiptPayment; @@ -617,10 +617,7 @@ protected void uploadReceipts(ProgressFeedback feedback) throws SyncException { protected void uploadOrders(ProgressFeedback feedback) throws SyncException { sentry.addBreadcrumb("sync.queue", "Start order upload"); - List orders = dataStore.select(QueuedOrder.class) - .where(QueuedOrder.ERROR.isNull()) - .and(QueuedOrder.LOCKED.eq(false)) - .get().toList(); + List orders = db.getQueuedOrderQueries().selectUnlockedWithoutError().executeAsList(); try { int i = 0; @@ -630,8 +627,7 @@ protected void uploadOrders(ProgressFeedback feedback) throws SyncException { } i++; - qo.setLocked(true); - dataStore.update(qo, QueuedOrder.LOCKED); + db.getQueuedOrderQueries().lock(qo.getId()); Long startedAt = System.currentTimeMillis(); PretixApi.ApiResponse resp = api.postResource( api.eventResourceUrl(qo.getEvent_slug(), "orders") + "?pdf_data=true&force=true", @@ -639,18 +635,15 @@ protected void uploadOrders(ProgressFeedback feedback) throws SyncException { qo.getIdempotency_key() ); if (resp.getResponse().code() == 201) { - Receipt r = qo.getReceipt(); - r.setOrder_code(resp.getData().getString("code")); - dataStore.update(r, Receipt.ORDER_CODE); - dataStore.delete(qo); + db.getReceiptQueries().updateOrderCode(resp.getData().getString("code"), qo.getReceipt()); + db.getQueuedOrderQueries().delete(qo.getId()); (new OrderSyncAdapter(db, fileStorage, qo.getEvent_slug(), null, true, true, api, configStore.getSyncCycleId(), null)).standaloneRefreshFromJSON(resp.getData()); if (connectivityFeedback != null) { connectivityFeedback.recordSuccess(System.currentTimeMillis() - startedAt); } } else if (resp.getResponse().code() == 400) { // TODO: User feedback or log in some way? - qo.setError(resp.getData().toString()); - dataStore.update(qo); + db.getQueuedOrderQueries().updateError(resp.getData().toString(), qo.getId()); } } } catch (JSONException e) { diff --git a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/QueuedOrder.sq b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/QueuedOrder.sq index d9113ee6..6427199c 100644 --- a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/QueuedOrder.sq +++ b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/QueuedOrder.sq @@ -3,6 +3,11 @@ SELECT * FROM QueuedOrder WHERE id = ?; +selectUnlockedWithoutError: +SELECT * +FROM QueuedOrder +WHERE locked = FALSE AND error IS NULL; + delete: DELETE FROM QueuedOrder WHERE id = ?; @@ -27,3 +32,15 @@ VALUES( ?, ? ); + +lock: +UPDATE QueuedOrder +SET + locked = TRUE +WHERE id = ?; + +updateError: +UPDATE QueuedOrder +SET + error = ? +WHERE id = ?; diff --git a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Receipt.sq b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Receipt.sq index 007d6d00..303d1c72 100644 --- a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Receipt.sq +++ b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/Receipt.sq @@ -56,3 +56,9 @@ VALUES ( ?, ? ); + +updateOrderCode: +UPDATE Receipt +SET + order_code = ? +WHERE id = ?;