From 8fe5605109f0ead6484fe9559aaf4ae38672abe3 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@leastauthority.com>
Date: Thu, 17 Oct 2019 19:33:14 +0530
Subject: [PATCH] persistence: new function that checks if a voucher is unpaid

---
 src/PaymentServer/Persistence.hs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/PaymentServer/Persistence.hs b/src/PaymentServer/Persistence.hs
index 19bda64..26f7df1 100644
--- a/src/PaymentServer/Persistence.hs
+++ b/src/PaymentServer/Persistence.hs
@@ -134,10 +134,16 @@ instance VoucherDatabase Sqlite.Connection where
 instance FromRow Fingerprint where
   fromRow = Sqlite.field
 
--- | Paid but not redeemed
+
+-- | All paid but not redeemed vouchers
 getUnredeemedVouchers :: Sqlite.Connection -> IO [Voucher]
 getUnredeemedVouchers dbConn =
-  Sqlite.query_ dbConn "SELECT DISTINCT NAME FROM vouchers INNER JOIN redeemed_new WHERE vouchers.id != redeemed.voucher_id"
+  Sqlite.query_ dbConn "SELECT DISTINCT name FROM vouchers INNER JOIN redeemed WHERE vouchers.id != redeemed.voucher_id"
+
+isVoucherUnpaid :: Sqlite.Connection -> Voucher -> IO Bool
+isVoucherUnpaid dbConn voucher = do
+  results <- Sqlite.query dbConn "SELECT DISTINCT name FROM vouchers INNER JOIN redeemed WHERE vouchers.id != redeemed.voucher_id AND vouchers.name = ?" (Sqlite.Only voucher) :: IO [Voucher]
+  return (results == [])
 
 getVoucherFingerprint :: Sqlite.Connection -> Voucher -> IO [Fingerprint]
 getVoucherFingerprint dbConn voucher = do
-- 
GitLab