[이민권] Unlink 수정
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -434,6 +433,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
sType := queryvals.Get("stype")
|
sType := queryvals.Get("stype")
|
||||||
sId := queryvals.Get("sid")
|
sId := queryvals.Get("sid")
|
||||||
sk := queryvals.Get("sk")
|
sk := queryvals.Get("sk")
|
||||||
|
targetType := queryvals.Get("ttype")
|
||||||
|
|
||||||
authInfo := sh.auths.Find(sk)
|
authInfo := sh.auths.Find(sk)
|
||||||
if authInfo == nil {
|
if authInfo == nil {
|
||||||
@ -457,38 +457,39 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
numRecord, err := sh.mongoClient.Collection(CollectionAccount).CountDocuments(context.Background(), bson.M{
|
accDocs, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{
|
||||||
"accid": authInfo.Accid,
|
"accid": authInfo.Accid,
|
||||||
}, options.Count().SetLimit(2))
|
}, options.Find().SetProjection(bson.M{
|
||||||
|
"_id": 1,
|
||||||
|
}))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("unlink failed, fail to count accounts :", err)
|
logger.Error("unlink failed, fail to count accounts :", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRecord <= 1 {
|
if len(accDocs) <= 1 {
|
||||||
logger.Println("unlink failed. At least one link must be maintained. :", r.URL.Query())
|
logger.Println("unlink failed. At least one link must be maintained. :", r.URL.Query())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sType, sId, err = sh.getProviderInfo(sType, sId)
|
var ids primitive.A
|
||||||
if err != nil {
|
for _, accDoc := range accDocs {
|
||||||
logger.Error("getProviderInfo failed :", err)
|
ids = append(ids, accDoc["_id"].(primitive.ObjectID))
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := sh.mongoClient.FindOneAndDelete(CollectionLink, bson.M{
|
link, err := sh.mongoClient.FindOneAndDelete(CollectionLink, bson.M{
|
||||||
"platform": sType,
|
"platform": targetType,
|
||||||
"uid": sId,
|
"_id": bson.M{"$in": ids},
|
||||||
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("link failed. FindOneAndDelete link err:", err)
|
logger.Error("unlink failed. FindOneAndDelete link err:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{
|
preid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{
|
||||||
"_id": link["_id"].(primitive.ObjectID),
|
"_id": link["_id"].(primitive.ObjectID),
|
||||||
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -496,10 +497,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if preid == nil {
|
||||||
// newid가 있어야 한다. 그래야 기존 서비스 계정이 없는 상태이다.
|
|
||||||
if newid == nil {
|
|
||||||
// 이미 계정이 있네?
|
|
||||||
logger.Println("unlink failed. service account not found:", r.URL.Query())
|
logger.Println("unlink failed. service account not found:", r.URL.Query())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user