diff --git a/core/api_coupon.go b/core/api_coupon.go index 1d1f4a8..23b99b7 100644 --- a/core/api_coupon.go +++ b/core/api_coupon.go @@ -197,7 +197,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) @@ -274,8 +279,8 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. round, _ = coupon.MakeCouponRoundHash(code) } - // 1. 내가 이 라운드의 쿠폰을 쓴 적이 있나 - already, err := mongoClient.Exists(CollectionCouponUse, bson.M{ + // 쿠폰 사용 유무 검사 + alreadyused, err := mongoClient.Exists(CollectionCouponUse, bson.M{ "_id": acc, "rounds": round, }) @@ -285,7 +290,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. return } - if already { + if alreadyused { // 이미 이 라운드의 쿠폰을 사용한 적이 있다. w.WriteHeader(http.StatusConflict) return @@ -311,7 +316,8 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. } else { // 2. 쿠폰을 하나 꺼냄 matched, _, err := mongoClient.Update(CollectionCoupon, bson.M{ - "_id": roundObj, + "_id": roundObj, + "remains": key, }, bson.M{ "$pull": bson.M{"remains": key}, }) @@ -339,6 +345,12 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http. } } + if coupon.Expire < time.Now().Unix() { + // 쿠폰 만료시간 경과 + w.WriteHeader(http.StatusInternalServerError) + return + } + if len(coupon.Effect) == 0 { // 쿠폰이 없네? w.WriteHeader(http.StatusBadRequest)