[이민권] 쿠폰

- 유효번호 쿠폰 사용 안 되는 이슈 수정
- 유효번호 쿠폰이 사용 불가여도 사용 처리 되는 이슈 수정
This commit is contained in:
2024-02-26 15:51:49 +09:00
parent 231b1a35d5
commit 2d060fe117

View File

@ -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 {
@ -300,6 +322,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
// 쿠폰을 하나 꺼냄
matched, _, err := mongoClient.Update(CollectionCoupon, bson.M{
"_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)