Skip to content

Commit

Permalink
fix: update home date in assignDevice + prefer addToSet instead of pu…
Browse files Browse the repository at this point in the history
…sh in a transaction, because idempotent #10

Signed-off-by: Stefano Cappa <[email protected]>
  • Loading branch information
Ks89 committed Aug 29, 2024
1 parent e365375 commit 08269e6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/assign_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (handler *AssignDevice) PutAssignDeviceToHomeRoom(c *gin.Context) {
return nil, errClean
}

// 5. assign device with id = `deviceID` to room with id = `assignDeviceReq.RoomID` of home with id = `assignDeviceReq.HomeID`
// 5. assign device with id = `deviceID` to room with id = `roomObjID` of home with id = `homeObjID`
filterHome := bson.D{bson.E{Key: "_id", Value: homeObjID}}
arrayFiltersRoom := options.ArrayFilters{Filters: bson.A{bson.M{"x._id": roomObjID}}}
opts := options.UpdateOptions{
Expand All @@ -173,8 +173,8 @@ func (handler *AssignDevice) PutAssignDeviceToHomeRoom(c *gin.Context) {
},
"$set": bson.M{
"rooms.$[x].modifiedAt": time.Now(),
"modifiedAt": time.Now(),
},
// TODO I should update `modifiedAt` of both `home` and `room` documents
}
_, errUpdate := handler.collHomes.UpdateOne(sessionCtx, filterHome, update, &opts)
if errUpdate != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (handler *Register) insertDevice(device *models.Device, profile *models.Pro
_, errUpd := handler.collProfiles.UpdateOne(
sessionCtx,
bson.M{"_id": profile.ID},
bson.M{"$push": bson.M{"devices": device.ID}},
bson.M{"$addToSet": bson.M{"devices": device.ID}},
)
if errUpd != nil {
return nil, customerrors.Wrap(http.StatusInternalServerError, errUpd, "Cannot update profile with the new device")
Expand Down
1 change: 0 additions & 1 deletion testuutils/db_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func AssignDeviceToHomeAndRoom(ctx context.Context, collectionHomes *mongo.Colle
"$set": bson.M{
"rooms.$[x].modifiedAt": time.Now(),
},
// TODO I should update `modifiedAt` of both `home` and `room` documents
}
_, err = collectionHomes.UpdateOne(ctx, filterHome, update, &opts)
return err
Expand Down

0 comments on commit 08269e6

Please sign in to comment.