From 2d060fe1170b68fc428d6ba362eb6c077abb2826 Mon Sep 17 00:00:00 2001 From: mklee Date: Mon, 26 Feb 2024 15:51:49 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=9D=B4=EB=AF=BC=EA=B6=8C]=20=EC=BF=A0?= =?UTF-8?q?=ED=8F=B0=20-=20=EC=9C=A0=ED=9A=A8=EB=B2=88=ED=98=B8=20?= =?UTF-8?q?=EC=BF=A0=ED=8F=B0=20=EC=82=AC=EC=9A=A9=20=EC=95=88=20=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95=20-=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EB=B2=88=ED=98=B8=20=EC=BF=A0=ED=8F=B0?= =?UTF-8?q?=EC=9D=B4=20=EC=82=AC=EC=9A=A9=20=EB=B6=88=EA=B0=80=EC=97=AC?= =?UTF-8?q?=EB=8F=84=20=EC=82=AC=EC=9A=A9=20=EC=B2=98=EB=A6=AC=20=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/api_coupon.go | 62 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/core/api_coupon.go b/core/api_coupon.go index 74fd4dd..1dc0c7c 100644 --- a/core/api_coupon.go +++ b/core/api_coupon.go @@ -202,7 +202,12 @@ func downloadCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r roundnum := binary.BigEndian.Uint32(roundObj[:]) var coupons []string for _, uid := range coupon.Remains { - coupons = append(coupons, makeCouponKey(roundnum, []byte(uid))) + decUid, err := hex.DecodeString(uid) + if err != nil { + logger.Println("downloadCoupons Fail", err) + continue + } + coupons = append(coupons, makeCouponKey(roundnum, decUid)) } enc := json.NewEncoder(w) @@ -279,6 +284,23 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. round, _ = coupon.MakeCouponRoundHash(code) } + // 쿠폰 사용 유무 검사 + alreadyused, err := mongoClient.Exists(CollectionCouponUse, bson.M{ + "_id": acc, + "rounds": round, + }) + if err != nil { + logger.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + if alreadyused { + // 이미 이 라운드의 쿠폰을 사용한 적이 있다. + w.WriteHeader(http.StatusConflict) + return + } + var coupon couponDoc roundObj, _ := primitive.ObjectIDFromHex(round + round + round) if len(key) == 0 { @@ -299,7 +321,8 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. } else { // 쿠폰을 하나 꺼냄 matched, _, err := mongoClient.Update(CollectionCoupon, bson.M{ - "_id": roundObj, + "_id": roundObj, + "remains": key, }, bson.M{ "$pull": bson.M{"remains": key}, }) @@ -333,41 +356,6 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. return } - alreadyused := false - - // 무한 쿠폰인지 일회용 쿠폰인지 구분 - // 쿠폰 사용 유무 검사 - if coupon.Total == -1 { - already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ - "_id": acc, - "rounds": round, - }) - if err != nil { - logger.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - alreadyused = already - } else { - already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ - "rounds": round, - }) - if err != nil { - logger.Println(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - alreadyused = already - } - - if alreadyused { - // 이미 이 라운드의 쿠폰을 사용한 적이 있다. - w.WriteHeader(http.StatusConflict) - return - } - if len(coupon.Effect) == 0 { // 쿠폰이 없네? w.WriteHeader(http.StatusBadRequest)