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