SWM-99-degree / jaribean-fastapi

0 stars 1 forks source link

[FEAT] api/matching/status 추가 #62

Open psy-choi opened 10 months ago

psy-choi commented 10 months ago

목적

해결

@app.get("/api/matching/status")
def checkingMatchingProcessing(payload : dict() = Depends(verify_jwt_token)):
    userId = payload["userId"]
    new_set = Redis.MessageSet("matching:" + userId)
    if new_set.exist():
        return CheckingResponse(1, "매칭 요청이 진행중입니다.", "ENQUEUED")

    current_time = datetime.datetime.now()
    ten_minutes_ago = current_time - datetime.timedelta(minutes=10)
    collection = mongodb.client["jariBeanProd"]["matching"]
    query = {
        "matchingTime": {"$lt": current_time, "$gte": ten_minutes_ago},
        "userId": userId,
        "status" : "PROCESS"
    }
    data = collection.find_one(query)
    if data == None:
        return CheckingResponse(1, "매칭이 진행중입니다.", "PROCESSING", str(data._id), str(data.cafe._id))
    return CheckingResponse(1, "매칭이 진행중이지 않습니다.", "NORMAL")
def CheckingResponse(code : int, msg : str, status : str, matchingId : str = None, cafeId : str = None):
    return {"code" : code, "msg" : msg, "data" : {"code":code, "msg": msg, "status": status, "matchingId" : matchingId, "cafeId" : str}}

1) 만일 redis에 없을 경우에는 ENQUEUED로 응답 2) 만일 MongoDB 에 매칭이 없는 경우에는 PROCESS로 응답(더불어서 매칭의 요소도 함께 전달) 3) 만일 둘 다 해당하지 않을 경우에는 NORMAL로 요청