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)