From 17f6d557a15ac648df79c2684b7a68bc3f34fae7 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 12 Aug 2024 08:58:07 -0500 Subject: [PATCH 1/4] Add Persistent dependencies --- zgo-backend.cabal | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/zgo-backend.cabal b/zgo-backend.cabal index 416f405..afc8823 100644 --- a/zgo-backend.cabal +++ b/zgo-backend.cabal @@ -33,6 +33,7 @@ library Xero ZGoBackend ZGoTx + DB hs-source-dirs: src build-depends: @@ -47,6 +48,10 @@ library , configurator , containers , crypto-rng + , esqueleto + , persistent + , persistent-mysql + , persistent-template , ghc-prim , hexstring , http-conduit From 9b48ce2b6811cdca01360ba7d631dbf00bda22c7 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 12 Aug 2024 08:58:45 -0500 Subject: [PATCH 2/4] Remove comments from QuasiQuote --- src/DB.hs | 220 +++++++++++++++++++++++++++--------------------------- 1 file changed, 110 insertions(+), 110 deletions(-) diff --git a/src/DB.hs b/src/DB.hs index bea93e1..44d8f42 100644 --- a/src/DB.hs +++ b/src/DB.hs @@ -39,161 +39,161 @@ share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| ZgoBlock - block_id Int --Block Id number - block_confs Int default=0 -- Block confirnations - block_network String default="" -- Block Network - block_height Int default=0 -- Block Height - block_time Int default=0 -- Block time + block_id Int + block_confs Int default=0 + block_network String default="" + block_height Int default=0 + block_time Int default=0 UniqueBlock block_id deriving Show Eq ZgoBlockTx - blocktx_block_id Int --'Block Tx parent - blocktx_id Int -- Block Tx record Id - blocktx_txid String -- Block Tx Id + blocktx_block_id Int + blocktx_id Int + blocktx_txid String UniqueBlockTx blocktx_block_id blocktx_id deriving Show Eq ZgoCountry - country_code String -- Two Character Country Code - country_name String -- Country unique name + country_code String + country_name String UniqueCountryCode country_code deriving Show Eq ZgoItem - item_owner_id Int -- Owner Id - item_id String -- Item Id - item_name String -- Item name', - item_description T.Text default="" -- Item description - item_cost Float default=0.0 -- Item Unit price + item_owner_id Int + item_id String + item_name String + item_description T.Text default="" + item_cost Float default=0.0 UniqueItemId item_owner item_id deriving Show Eq ZgoLanguages - encode_id String -- Language encoding id - view_name String default-"" -- View name that use this language' - view_element String default="" -- 'Element name - view_element_text T.Text default="" -- Text to be displayed' + encode_id String + view_name String default-"" + view_element String default="" + view_element_text T.Text default="" UniqueLanguage encode_id view_name view_element deriving Show Eq ZgoOrders - order_id Int -- Order Id - order_zaddress T.Text -- Order zcash address - order_session T.Text -- Order session id - order_timestamp Int -- Order timestamp - order_closed Word8 -- Order closed flag - order_currency String -- Order currency - order_price Float default=0.0 -- Order ZEC price - order_total Float default=0.0 -- Order total amount - order_totalzec Float default=0.0 -- Order total in ZEC - order_paid Word8 default=0 -- Order paid status - order_token String default="" -- Order token - order_extinv String default="" -- Order External Invoice - order_shortcode String default='' -- Order invoice shortcode - order_taxamount Float default=0.0 -- Order tax amount - order_tipamount Float default=0.0 -- Order tip amount - order_vatamount Float default=0.0 -- Orcer VAT amount' + order_id Int + order_zaddress T.Text + order_session T.Text + order_timestamp Int + order_closed Word8 + order_currency String + order_price Float default=0.0 + order_total Float default=0.0 + order_totalzec Float default=0.0 + order_paid Word8 default=0 + order_token String default="" + order_extinv String default="" + order_shortcode String default='' + order_taxamount Float default=0.0 + order_tipamount Float default=0.0 + order_vatamount Float default=0.0 UniqueOrderId order_id deriving Show Eq ZgoOrderLines - orderline_orderid Int default=0 -- Orderlines Order Id - orderline_line Int default=0 -- Orderlines Line number - orderline_qty Int default=0 -- Orderline item qty - orderline_name T.Text default="" -- Orderline item description - orderline_cost Float default=0.0 -- Orderline item cost + orderline_orderid Int default=0 + orderline_line Int default=0 + orderline_qty Int default=0 + orderline_name T.Text default="" + orderline_cost Float default=0.0 UniqueOrderLine orderline_orderid orderline_line deriving Show Eq ZgoOwner - owner_id Int --'Owner ID number - owner_co_name String default="" -- Company''s name - owner_firstname String -- Owner's first name - owner_lastname String -- Owner's last name - owner_email String -- Owner's email address - owner_street String -- Owner's street - owner_city String -- Owner's city - owner_state String -- Owner's state - owner_zipcode String -- Owner's zip code - owner_phone String -- Owner's phone number - owner_country String -- Owner's country - owner_website String -- Owner's website URL - owner_currency String -- Owner's currency', - owner_zcaddress String -- Owner's zcash address - owner_usezats Word8 -- Use zats for display - owner_crmtoken T.Text -- Owner's crm token - owner_usetax Word8 -- Use tax flag - owner_taxvalue Float -- Tax rate to use - owner_usevat Word8 -- Use vat flag - owner_vat Float -- Vat rate to use - owner_payconf Word8 -- Confirm payment flag - owner_viewkey T.Text -- Viewing Key for payment confirmation - owner_invoices Word8 -- Use zgo invoices - owner_tips Word8 -- Activate tips - owner_paid Word8 -- True if account is paid - owner_expiration Int -- Expiration date-time + owner_id Int + owner_co_name String default="" + owner_firstname String + owner_lastname String + owner_email String + owner_street String + owner_city String + owner_state String + owner_zipcode String + owner_phone String + owner_country String + owner_website String + owner_currency String + owner_zcaddress String + owner_usezats Word8 + owner_crmtoken T.Text + owner_usetax Word8 + owner_taxvalue Float + owner_usevat Word8 + owner_vat Float + owner_payconf Word8 + owner_viewkey T.Text + owner_invoices Word8 + owner_tips Word8 + owner_paid Word8 + owner_expiration Int UniqueOwnerId owner_id UniqueOwnerZcashAddr owner_zcaddress deriving Show Eq ZgoPayment - payment_id Int -- Payment Id - payment_delta Int -- Payment delta - payment_done Word8 -- Payment done flag - payment_zaddress T.Text -- Payment zcash address - payment_session String default="" -- Payment session - payment_blocktime Int default=0 -- Payment blocktime - payment_amount Float default=0.0 -- Payment amount - payment_txid String default ="" -- Payment transaction id - payment_memo T.Text default="" -- Payment Memo + payment_id Int + payment_delta Int + payment_done Word8 + payment_zaddress T.Text + payment_session String default="" + payment_blocktime Int default=0 + payment_amount Float default=0.0 + payment_txid String default ="" + payment_memo T.Text default="" UniquePaymentId payment_id deriving Show Eq ZgoPrice - price_id Int -- Price Id - price_delta Int default=0 -- Price delta - price_done Word8 default=0 -- Price done flag - price_zaddress T.Text default="" -- Price zcash address - price_session String default="" -- Price session - price_blocktime Int default=0 -- Price blocktime - price_amount Float default=0.0 -- Price amount - price_txid String default="" -- Price transaction id - price_memo T.Text default="" -- Price Memo + price_id Int + price_delta Int default=0 + price_done Word8 default=0 + price_zaddress T.Text default="" + price_session String default="" + price_blocktime Int default=0 + price_amount Float default=0.0 + price_txid String default="" + price_memo T.Text default="" UniquePriceId price_id deriving Show Eq ZgoProsession - pro_id Int -- Prosession record id - pro_zaddress T.Text default="" -- Prosession zcash address - pro_expiration Int default=0 -- Prosession expiration date/time - pro_closed Word8 default=0 -- Prosession closed flag + pro_id Int + pro_zaddress T.Text default="" + pro_expiration Int default=0 + pro_closed Word8 default=0 UniqueProSesId pro_id UniqueProSesZad pro_zaddress deriving Show Eq ZgoTransaction - tx_id Int -- Transaction ID - tx_zaddress T.Text default="" -- Transaction Zcash Address - tx_expiration Int default=0 -- Transaction Expiration Date/Time - tx_closed Word8 default=0 -- Transaction Closed flag + tx_id Int + tx_zaddress T.Text default="" + tx_expiration Int default=0 + tx_closed Word8 default=0 UniqueTxId tx_id deriving Show Eq ZgoUser - user_id Int -- User Id - user_zaddress T.Text -- User zcash address, - user_session String -- User session id - user_blocktime Int -- User block time - user_pin String -- User pin - user_validated Word8 default=0 -- User validated flag + user_id Int + user_zaddress T.Text + user_session String + user_blocktime Int + user_pin String + user_validated Word8 default=0 UniqueUserId user_id UniqueUserZad user_zaddress deriving Show Eq ZgoXero - xero_id Int -- Xero Id - xero_client_id String default="" -- Xero client id - xero_client_secret String default="" -- Xero client secret word + xero_id Int + xero_client_id String default="" + xero_client_secret String default="" UniqueXeroId xero_id UniqueXeroClientId xero_client_id deriving Show Eq ZgoXeroToken - xerotoken_id Int -- Xero account id - xerotoken_zaddress T.Text -- Xero zcash address - xerotoken_access_token T.Text -- Xero account access token - xerotoken_expires Int -- Xero token expiration date/time - xerotoken_refreshtoken String -- Xero refresh token - xerotoken_accexpires Int -- Xero account expire date/time - xerotoken_accCode String -- Xero account code - xerotoken_refexpires Int -- Xero reference expire date/time + xerotoken_id Int + xerotoken_zaddress T.Text + xerotoken_access_token T.Text + xerotoken_expires Int + xerotoken_refreshtoken String + xerotoken_accexpires Int + xerotoken_accCode String + xerotoken_refexpires Int UniqueXeroTokenId xerotoken_id deriving Show Eq - \ No newline at end of file + |] From e24581eb5d575314f65a5b77d6730c11d398f5fd Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 12 Aug 2024 09:23:36 -0500 Subject: [PATCH 3/4] Remove ID fields Persistent adds them automatically --- src/DB.hs | 33 +-------------------------------- zgo-backend.cabal | 1 + 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/src/DB.hs b/src/DB.hs index 44d8f42..0cfd7f6 100644 --- a/src/DB.hs +++ b/src/DB.hs @@ -27,30 +27,26 @@ import qualified Data.ByteString as BS import Data.HexString import Data.List (group, sort) import Data.Maybe (catMaybes, fromJust, isJust) -import Data.Pool (Pool) import qualified Data.Text as T import qualified Data.Text.Encoding as TE import Data.Word import Database.Esqueleto.Experimental -import qualified Database.Persist.Sqlite as PS +import qualified Database.Persist.MySQL as PM import Database.Persist.TH share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| ZgoBlock - block_id Int block_confs Int default=0 block_network String default="" block_height Int default=0 block_time Int default=0 - UniqueBlock block_id deriving Show Eq ZgoBlockTx blocktx_block_id Int blocktx_id Int blocktx_txid String - UniqueBlockTx blocktx_block_id blocktx_id deriving Show Eq ZgoCountry country_code String @@ -59,21 +55,17 @@ share deriving Show Eq ZgoItem item_owner_id Int - item_id String item_name String item_description T.Text default="" item_cost Float default=0.0 - UniqueItemId item_owner item_id deriving Show Eq ZgoLanguages encode_id String view_name String default-"" view_element String default="" view_element_text T.Text default="" - UniqueLanguage encode_id view_name view_element deriving Show Eq ZgoOrders - order_id Int order_zaddress T.Text order_session T.Text order_timestamp Int @@ -89,18 +81,14 @@ share order_taxamount Float default=0.0 order_tipamount Float default=0.0 order_vatamount Float default=0.0 - UniqueOrderId order_id deriving Show Eq ZgoOrderLines orderline_orderid Int default=0 - orderline_line Int default=0 orderline_qty Int default=0 orderline_name T.Text default="" orderline_cost Float default=0.0 - UniqueOrderLine orderline_orderid orderline_line deriving Show Eq ZgoOwner - owner_id Int owner_co_name String default="" owner_firstname String owner_lastname String @@ -126,11 +114,8 @@ share owner_tips Word8 owner_paid Word8 owner_expiration Int - UniqueOwnerId owner_id - UniqueOwnerZcashAddr owner_zcaddress deriving Show Eq ZgoPayment - payment_id Int payment_delta Int payment_done Word8 payment_zaddress T.Text @@ -139,10 +124,8 @@ share payment_amount Float default=0.0 payment_txid String default ="" payment_memo T.Text default="" - UniquePaymentId payment_id deriving Show Eq ZgoPrice - price_id Int price_delta Int default=0 price_done Word8 default=0 price_zaddress T.Text default="" @@ -151,42 +134,29 @@ share price_amount Float default=0.0 price_txid String default="" price_memo T.Text default="" - UniquePriceId price_id deriving Show Eq ZgoProsession - pro_id Int pro_zaddress T.Text default="" pro_expiration Int default=0 pro_closed Word8 default=0 - UniqueProSesId pro_id - UniqueProSesZad pro_zaddress deriving Show Eq ZgoTransaction - tx_id Int tx_zaddress T.Text default="" tx_expiration Int default=0 tx_closed Word8 default=0 - UniqueTxId tx_id deriving Show Eq ZgoUser - user_id Int user_zaddress T.Text user_session String user_blocktime Int user_pin String user_validated Word8 default=0 - UniqueUserId user_id - UniqueUserZad user_zaddress deriving Show Eq ZgoXero - xero_id Int xero_client_id String default="" xero_client_secret String default="" - UniqueXeroId xero_id - UniqueXeroClientId xero_client_id deriving Show Eq ZgoXeroToken - xerotoken_id Int xerotoken_zaddress T.Text xerotoken_access_token T.Text xerotoken_expires Int @@ -194,6 +164,5 @@ share xerotoken_accexpires Int xerotoken_accCode String xerotoken_refexpires Int - UniqueXeroTokenId xerotoken_id deriving Show Eq |] diff --git a/zgo-backend.cabal b/zgo-backend.cabal index afc8823..eb24a91 100644 --- a/zgo-backend.cabal +++ b/zgo-backend.cabal @@ -59,6 +59,7 @@ library , jwt , megaparsec , memory + , monad-logger , mongoDB , network , quickcheck-instances From 8be9215f044ceb983658d6394bf66c13bc168052 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Mon, 12 Aug 2024 10:42:30 -0500 Subject: [PATCH 4/4] Correct types --- src/DB.hs | 205 +++++++++++++++++++++++++++++------------------------- 1 file changed, 110 insertions(+), 95 deletions(-) diff --git a/src/DB.hs b/src/DB.hs index 0cfd7f6..b6c1098 100644 --- a/src/DB.hs +++ b/src/DB.hs @@ -22,6 +22,7 @@ import Control.Exception (throwIO) import Control.Monad (forM_, when) import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Logger (NoLoggingT, runNoLoggingT) +import Data.Aeson import Data.Bifunctor (bimap) import qualified Data.ByteString as BS import Data.HexString @@ -29,140 +30,154 @@ import Data.List (group, sort) import Data.Maybe (catMaybes, fromJust, isJust) import qualified Data.Text as T import qualified Data.Text.Encoding as TE +import Data.Time (UTCTime(..)) +import Data.UUID (UUID) import Data.Word import Database.Esqueleto.Experimental import qualified Database.Persist.MySQL as PM import Database.Persist.TH +newtype UuidDB = UuidDB + { getUUID :: UUID + } deriving newtype (Eq, Show, Read, ToJSON, FromJSON) + +derivePersistFieldJSON "UuidDB" + +newtype HexStringDB = HexStringDB + { getHex :: HexString + } deriving newtype (Eq, Show, Read) + +derivePersistField "HexStringDB" + share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| - ZgoBlock - block_confs Int default=0 - block_network String default="" - block_height Int default=0 - block_time Int default=0 + ZgoBlock json + confs Int default=0 + network String default="" + height Int default=0 + time Int default=0 deriving Show Eq - ZgoBlockTx + ZgoBlockTx blocktx_block_id Int blocktx_id Int blocktx_txid String deriving Show Eq - ZgoCountry - country_code String - country_name String - UniqueCountryCode country_code + ZgoCountry json + code T.Text + name T.Text + UniqueCountryCode code deriving Show Eq - ZgoItem - item_owner_id Int - item_name String - item_description T.Text default="" - item_cost Float default=0.0 + ZgoItem json + owner_id Int + name T.Text + description T.Text default="" + cost Double default=0.0 deriving Show Eq ZgoLanguages - encode_id String - view_name String default-"" - view_element String default="" - view_element_text T.Text default="" + encode_id T.Text + name T.Text default-"" + element T.Text default="" + element_text T.Text default="" deriving Show Eq - ZgoOrders - order_zaddress T.Text - order_session T.Text - order_timestamp Int - order_closed Word8 - order_currency String - order_price Float default=0.0 - order_total Float default=0.0 - order_totalzec Float default=0.0 - order_paid Word8 default=0 - order_token String default="" - order_extinv String default="" - order_shortcode String default='' - order_taxamount Float default=0.0 - order_tipamount Float default=0.0 - order_vatamount Float default=0.0 + ZgoOrder json + owner ZgoOwnerId + session ZgoUserId + timestamp Int + closed Word8 + currency T.Text + price Double + total Double + totalzec Double + paid Bool + token T.Text default="" + externalInvoice T.Text default="" + shortcode T.Text + taxamount Double + tipamount Double + vatamount Double deriving Show Eq ZgoOrderLines - orderline_orderid Int default=0 - orderline_qty Int default=0 - orderline_name T.Text default="" - orderline_cost Float default=0.0 + orderId ZgoOrderId + qty Int default=0 + name T.Text default="" + cost Double default=0.0 deriving Show Eq ZgoOwner - owner_co_name String default="" - owner_firstname String - owner_lastname String - owner_email String - owner_street String - owner_city String - owner_state String - owner_zipcode String - owner_phone String - owner_country String - owner_website String - owner_currency String - owner_zcaddress String - owner_usezats Word8 - owner_crmtoken T.Text - owner_usetax Word8 - owner_taxvalue Float - owner_usevat Word8 - owner_vat Float - owner_payconf Word8 - owner_viewkey T.Text - owner_invoices Word8 - owner_tips Word8 - owner_paid Word8 - owner_expiration Int + co_name T.Text + firstname T.Text + lastname T.Text + email T.Text + street T.Text + city T.Text + state T.Text + zipcode T.Text + phone T.Text + country T.Text + website T.Text + currency T.Text + zcaddress T.Text + usezats Bool + crmtoken T.Text + usetax Bool + taxvalue Double + usevat Bool + vat Double + payconf Bool + viewkey T.Text + invoices Bool + tips Bool + paid Bool + expiration UTCTime deriving Show Eq ZgoPayment - payment_delta Int - payment_done Word8 - payment_zaddress T.Text - payment_session String default="" - payment_blocktime Int default=0 - payment_amount Float default=0.0 - payment_txid String default ="" - payment_memo T.Text default="" + delta Int + done Bool + zaddress T.Text + session UuidDB + blocktime Int default=0 + amount Double default=0.0 + txid HexStringDB + memo T.Text deriving Show Eq ZgoPrice price_delta Int default=0 - price_done Word8 default=0 + price_done Bool default=0 price_zaddress T.Text default="" price_session String default="" price_blocktime Int default=0 - price_amount Float default=0.0 + price_amount Double default=0.0 price_txid String default="" price_memo T.Text default="" deriving Show Eq ZgoProsession - pro_zaddress T.Text default="" - pro_expiration Int default=0 - pro_closed Word8 default=0 + zaddress T.Text default="" + expiration UTCTime + closed Bool deriving Show Eq ZgoTransaction - tx_zaddress T.Text default="" - tx_expiration Int default=0 - tx_closed Word8 default=0 + zaddress T.Text default="" + expiration Int default=0 + closed Word8 default=0 deriving Show Eq - ZgoUser - user_zaddress T.Text - user_session String - user_blocktime Int - user_pin String - user_validated Word8 default=0 + ZgoUser json + owner ZgoOwnerId + session UuidDB + blocktime Int + pin T.Text + validated Bool deriving Show Eq ZgoXero - xero_client_id String default="" - xero_client_secret String default="" + client_id T.Text default="" + client_secret T.Text default="" deriving Show Eq ZgoXeroToken - xerotoken_zaddress T.Text - xerotoken_access_token T.Text - xerotoken_expires Int - xerotoken_refreshtoken String - xerotoken_accexpires Int - xerotoken_accCode String - xerotoken_refexpires Int + zaddress T.Text + access_token T.Text + expires Int + refreshtoken T.Text + accexpires Int + accCode T.Text + refexpires Int deriving Show Eq |]