Skip to content

Commit

Permalink
Revise query
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jul 2, 2024
1 parent cc89fbf commit 6eda7d6
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,13 @@ func (c *Client) ActivateClient(ctx context.Context, projectID types.ID, key str

// DeactivateClient deactivates the client of the given refKey and updates document statuses as detached.
func (c *Client) DeactivateClient(ctx context.Context, refKey types.ClientRefKey) (*database.ClientInfo, error) {
update := bson.A{
res := c.collection(ColClients).FindOneAndUpdate(ctx, bson.M{
"project_id": refKey.ProjectID,
"_id": refKey.ClientID,
}, bson.A{
bson.M{
"$set": bson.M{
"status": "deactivated",
"status": database.ClientDeactivated,
"updated_at": gotime.Now(),
"documents": bson.M{
"$arrayToObject": bson.M{
Expand All @@ -536,11 +539,11 @@ func (c *Client) DeactivateClient(ctx context.Context, refKey types.ClientRefKey
"k": "$$doc.k",
"v": bson.M{
"$cond": bson.M{
"if": bson.M{"$eq": bson.A{"$$doc.v.status", "attached"}},
"if": bson.M{"$eq": bson.A{"$$doc.v.status", database.DocumentAttached}},
"then": bson.M{
"client_seq": 0,
"server_seq": 0,
"status": "detached",
"status": database.DocumentDetached,
},
"else": "$$doc.v",
},
Expand All @@ -551,16 +554,7 @@ func (c *Client) DeactivateClient(ctx context.Context, refKey types.ClientRefKey
},
},
},
}

filter := bson.M{
"project_id": refKey.ProjectID,
"_id": refKey.ClientID,
}

opts := options.FindOneAndUpdate().SetReturnDocument(options.After)

res := c.collection(ColClients).FindOneAndUpdate(ctx, filter, update, opts)
}, options.FindOneAndUpdate().SetReturnDocument(options.After))

clientInfo := database.ClientInfo{}
if err := res.Decode(&clientInfo); err != nil {
Expand Down

0 comments on commit 6eda7d6

Please sign in to comment.