Skip to content

Commit

Permalink
Migrate remaining usage of QueuedOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
antweb committed Nov 1, 2024
1 parent 87434df commit 8b87835
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<QueuedOrder> orders = dataStore.select(QueuedOrder.class)
.where(QueuedOrder.ERROR.isNull())
.and(QueuedOrder.LOCKED.eq(false))
.get().toList();
List<QueuedOrder> orders = db.getQueuedOrderQueries().selectUnlockedWithoutError().executeAsList();

try {
int i = 0;
Expand All @@ -630,27 +627,23 @@ 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",
new JSONObject(qo.getPayload()),
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?;
Expand All @@ -27,3 +32,15 @@ VALUES(
?,
?
);

lock:
UPDATE QueuedOrder
SET
locked = TRUE
WHERE id = ?;

updateError:
UPDATE QueuedOrder
SET
error = ?
WHERE id = ?;
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ VALUES (
?,
?
);

updateOrderCode:
UPDATE Receipt
SET
order_code = ?
WHERE id = ?;

0 comments on commit 8b87835

Please sign in to comment.