From ae16b63e7560dadd9873bfedcd2a588b76a0d51a Mon Sep 17 00:00:00 2001 From: Alaudidae Lark Date: Sun, 7 May 2017 18:36:15 +0530 Subject: [PATCH] fixed issues --- src/App.hs | 13 +++++++------ src/Models.hs | 8 ++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/App.hs b/src/App.hs index 5b0d3ac..0d1d596 100644 --- a/src/App.hs +++ b/src/App.hs @@ -37,7 +37,6 @@ couponServer pool = CouponValid_from =. couponValid_from newCoupon, CouponValid_till =. couponValid_till newCoupon] -- TODO Add more upsert cols - -- when (isNothing exists) $ void $ insert newCoupon return NoContent couponGet :: Text -> IO (Maybe Coupon) @@ -47,6 +46,8 @@ couponServer pool = couponDel :: Text -> IO NoContent couponDel code = flip runSqlPersistMPool pool $ do + deleteWhere [CustomerCouponCode ==. code] + deleteWhere [ProductCouponCode ==. code] deleteWhere [CouponCode ==. code] return NoContent @@ -74,11 +75,11 @@ computeBill c b = do productLimit <- couponApplicable "Coupon Expired/Not yet valid"] let rejLookup = zip validityList rejectionStrings let rsl = map snd $ filter (not.fst) rejLookup - let rejectionString = intercalate "\n" rsl + let rejectionString = intercalate " " rsl liftIO $ mapM_ print rejLookup if and validityList then do upsProdCoupon (couponValue c) - upsert (newCustCoupon b) [CustomerCouponCused +=. 1] + upsertBy (newUCust b) (newCustCoupon b) [CustomerCouponCused +=. 1] updateWhere [CouponCode ==. couponCode c] [CouponUsed +=. 1] return $ Applied couponAmount else return $ Rejected rejectionString @@ -92,15 +93,15 @@ computeBill c b = do productLimit <- couponApplicable custCoupon = do cm <- selectFirst [CustomerCouponCode ==. couponCode c, CustomerCouponEmail ==. customer b] [] return $ customerLimit $ entityVal <$> cm - upsProdCoupon (ProductFlat cf) = do upsert (newProdCoupon cf b) [ProductCouponPused +=. 1] + upsProdCoupon (ProductFlat cf) = do upsertBy (newUprod cf) (newProdCoupon cf b) [ProductCouponPused +=. 1] return () upsProdCoupon _ = return () priceLimit = couponMin_price c < billAmount b couponUsageLimit = couponUsed c < couponUsage_limit c couponTime ct = couponValid_from c < ct && couponValid_till c > ct - prodLimit (Just pc) = productCouponPused pc > couponProduct_limit c + prodLimit (Just pc) = productCouponPused pc < couponProduct_limit c prodLimit _ = True - customerLimit (Just cp) = customerCouponCused cp > couponCustomer_limit c + customerLimit (Just cp) = customerCouponCused cp < couponCustomer_limit c customerLimit _ = True billAmount :: BillCoupon -> Int diff --git a/src/Models.hs b/src/Models.hs index 2c4b97b..9d9a042 100644 --- a/src/Models.hs +++ b/src/Models.hs @@ -55,9 +55,13 @@ Coupon json newProdCoupon :: CouponForProduct -> BillCoupon -> ProductCoupon newProdCoupon c p = ProductCoupon { productCouponProduct = couponProductName c, productCouponCode = coupon p, - productCouponPused = 1} + productCouponPused = 0} newCustCoupon :: BillCoupon -> CustomerCoupon newCustCoupon b = CustomerCoupon { customerCouponEmail = customer b, customerCouponCode = coupon b, - customerCouponCused = 1} + customerCouponCused = 0} + +newUCust b = UniqueEmail (customer b) + +newUprod c = UniqueProduct (couponProductName c)