Skip to content

Commit

Permalink
fix(Boards): #MAG-383 fix get boards now handle board with one folder (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rseuret authored and JinDouang committed Apr 4, 2024
1 parent 99ffc4e commit 15d7c08
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 50 deletions.
2 changes: 2 additions & 0 deletions src/main/java/fr/cgi/magneto/core/constants/Mongo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ public class Mongo {
public static final String PIPELINE = "$pipeline";

public static final String RESTRICT_SEARCH_WITH_MATCH = "restrictSearchWithMatch";
public static final String IFNULL = "$ifNull";
public static final String GT = "$gt";
}
129 changes: 79 additions & 50 deletions src/main/java/fr/cgi/magneto/service/impl/DefaultBoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,20 @@ public Future<JsonObject> updateLayoutCards(BoardPayload updateBoard, Board curr
}

@Override
public Future<JsonObject> restoreBoards(String userId, List<String> boardIds){
public Future<JsonObject> restoreBoards(String userId, List<String> boardIds) {
Promise<JsonObject> promise = Promise.promise();
Future<JsonObject> preDeleteBoardsFuture = preDeleteBoards(userId,boardIds,true);
Future<JsonObject> preDeleteBoardsFuture = preDeleteBoards(userId, boardIds, true);
preDeleteBoardsFuture.compose(r -> this.handleInsertSharedArrayFromFolder(boardIds))
.onSuccess(success -> promise.complete(preDeleteBoardsFuture.result()))
.onFailure(promise::fail);

return promise.future();
}

private Future<Void> handleInsertSharedArrayFromFolder(List<String> boardIds) {
Promise<Void> promise= Promise.promise();
List<Future<Void>> futures= new ArrayList<>();
boardIds.forEach(id ->{
private Future<Void> handleInsertSharedArrayFromFolder(List<String> boardIds) {
Promise<Void> promise = Promise.promise();
List<Future<Void>> futures = new ArrayList<>();
boardIds.forEach(id -> {
futures.add(insertSharedArrayFromFolder(id));
});
FutureHelper.all(futures)
Expand Down Expand Up @@ -517,40 +517,7 @@ Mongo.MAP, new JsonObject()
.put(Field.NBCARDS, new JsonObject().put(Mongo.SIZE, String.format("$%s", Field.CARDIDS)))
.put(Field.NBCARDSSECTIONS, 1)
.put(Field.MODIFICATIONDATE, 1)
.put(Field.FOLDERID, new JsonObject().put(Mongo.FILTER,
new JsonObject()
.put(Mongo.INPUT, String.format("$%s", Field.FOLDERS))
.put(Mongo.AS, Field.FOLDER)
.put(Mongo.COND,
new JsonObject().put(Mongo.AND, new JsonArray()
.add(new JsonObject().put(Mongo.OR, new JsonArray()
.add(new JsonObject().put(Mongo.EQ, new JsonArray() //user is owner
.add(String.format("$$%s.%s", Field.FOLDER, Field.OWNERID))
.add(user.getUserId())))
.add(new JsonObject() //share field exists and is not empty
.put(Field.FOLDER, new JsonObject()
.put(Field.SHARED, new JsonObject()
.put(Mongo.OR, new JsonArray() //shared with user or user group
.add(new JsonObject()
.put(Mongo.IN, new JsonArray()
.add(Field.USERID)
.add(new JsonArray().add(user.getUserId())))
)
.add(new JsonObject()
.put(Mongo.IN, new JsonArray()
.add(Field.GROUPID)
.add(new JsonArray().add(user.getGroupsIds())))
)
)
)
)
)
))
.add(new JsonObject().put(Mongo.EQ, new JsonArray()
.add(String.format("$$%s.%s", Field.FOLDER, Field.DELETED))
.add(String.format("$%s", Field.DELETED)))))
)
))
.put(Field.FOLDERID, getFolderFiltersForGetBoards(user))
.put(Field.DESCRIPTION, 1)
.put(Field.OWNERID, 1)
.put(Field.OWNERNAME, 1)
Expand Down Expand Up @@ -595,7 +562,7 @@ Mongo.MAP, new JsonObject()
.put(Field.DISPLAY_NB_FAVORITES, 1));
if (getCount) {
query = query.count();
}else{
} else {
if (page != null) {
query.page(page);
}
Expand All @@ -604,6 +571,60 @@ Mongo.MAP, new JsonObject()
return query.getAggregate();
}

private static JsonObject getFolderFiltersForGetBoards(UserInfos user) {
JsonObject sharedCheck = new JsonObject() //share field exists and is not empty
.put(Mongo.GT, new JsonArray().add(
new JsonObject().put(Mongo.SIZE,
new JsonObject().put(Mongo.FILTER,
new JsonObject().put(Mongo.INPUT, String.format("$$%s.%s", Field.FOLDER, Field.SHARED))
.put(Mongo.AS, Field.ALLFOLDERS)
.put(Mongo.COND, new JsonObject().put(Mongo.OR,
new JsonArray()
.add(new JsonObject().put(Mongo.EQ, new JsonArray()
.add(String.format("$$%s.%s", Field.ALLFOLDERS, Field.USERID))
.add(user.getUserId())
)
).add(new JsonObject().put(Mongo.IN, new JsonArray()
.add(String.format("$$%s.%s", Field.ALLFOLDERS, Field.GROUPID))
.add(user.getGroupsIds())
)
)
)))))
.add(0)
);

JsonObject checkNull = new JsonObject().put(Mongo.IFNULL, new JsonArray().add(String.format("$$%s.%s", Field.FOLDER, Field.SHARED)).add(false));
JsonObject checkEmpty = new JsonObject().put(Mongo.NE,
new JsonArray().add(new JsonObject().put(String.format("$%s", Field.SIZE),
new JsonObject().put(Mongo.IFNULL,
new JsonArray().add(String.format("$$%s.%s", Field.FOLDER, Field.SHARED)).add(false))))
.add(0));
JsonObject andCheckShared = new JsonObject().put(Mongo.AND, new JsonArray().add(checkNull).add(checkEmpty).add(sharedCheck));
return new JsonObject().put(Mongo.FILTER,
new JsonObject()
.put(Mongo.INPUT, String.format("$%s", Field.FOLDERS))
.put(Mongo.AS, Field.FOLDER)
.put(Mongo.COND,
new JsonObject().put(Mongo.AND, new JsonArray()
.add(new JsonObject().put(Mongo.OR, new JsonArray()
.add(new JsonObject().put(Mongo.EQ, new JsonArray() //user is owner
.add(String.format("$$%s.%s", Field.FOLDER, Field.OWNERID))
.add(user.getUserId())))
.add(andCheckShared)
))
.add(new JsonObject().put(Mongo.EQ, new JsonArray()
.add(String.format("$$%s.%s", Field.FOLDER, Field.DELETED))
.add(String.format("$%s", Field.DELETED)))))
)
);
}

/**
* génerate the match to get boards from main page
*
* @param user
* @param query
*/
private static void filterBoardWithoutFolder(UserInfos user, MongoQuery query) {
JsonObject folderIdMatch = new JsonObject().putNull(String.format("%s.%s", Field.FOLDERID, Field._ID));
JsonObject userRequest = new JsonObject()
Expand All @@ -613,12 +634,20 @@ private static void filterBoardWithoutFolder(UserInfos user, MongoQuery query) {
.put(Field.USERID, user.getUserId()))));

JsonObject checkGroupRequest = new JsonObject()
.put(String.format("%s.%s.%s",Field.FOLDERID,Field.SHARED, Field.GROUPID), new JsonObject().put(Mongo.NIN, user.getGroupsIds()));
.put(String.format("%s.%s.%s", Field.FOLDERID, Field.SHARED, Field.GROUPID), new JsonObject().put(Mongo.NIN, user.getGroupsIds()));

JsonObject notUserFolder = new JsonObject()
.put(String.format("%s.%s", Field.FOLDERID, Field.OWNERID),
new JsonObject().put(Mongo.NE, user.getUserId()));
JsonObject andCondition = new JsonObject().put(Mongo.AND, new JsonArray().add(userRequest).add(checkGroupRequest).add(notUserFolder));
JsonObject folderNotShared = new JsonObject().put(String.format("%s.%s", Field.FOLDERID, Field.SHARED),
new JsonObject().put(Mongo.EXISTS, true).put(Mongo.NE, new JsonArray()));

JsonObject folderNotOwned = new JsonObject().put(Field.OWNERID, String.format("$%s.$%s", Field.FOLDERID, Field.OWNERID));

JsonObject filterPreviousBoardsInForeignFolders = new JsonObject().put(Mongo.AND, new JsonArray().add(folderNotShared).add(folderNotOwned));
JsonObject andCondition = new JsonObject().put(Mongo.AND,
new JsonArray().add(userRequest).add(checkGroupRequest).add(notUserFolder).add(filterPreviousBoardsInForeignFolders));

query.matchOr(new JsonArray().add(folderIdMatch).add(andCondition));
}

Expand Down Expand Up @@ -713,8 +742,8 @@ public Future<List<String>> getAllDocumentIds(String boardId, UserInfos user) {
}

@Override
public Future<List<String> > shareBoard(List<String> ids, JsonObject share, boolean checkOldRights) {
Promise<List<String> > promise = Promise.promise();
public Future<List<String>> shareBoard(List<String> ids, JsonObject share, boolean checkOldRights) {
Promise<List<String>> promise = Promise.promise();
List<Future<JsonObject>> futures = new ArrayList<>();
ids.forEach(id -> futures.add(this.shareService.upsertSharedArray(id, share, this.collection, checkOldRights)));
FutureHelper.all(futures)
Expand All @@ -725,9 +754,9 @@ public Future<List<String> > shareBoard(List<String> ids, JsonObject share, bool

@Override
public Future<List<String>> shareBoard(List<String> ids, List<SharedElem> share, List<SharedElem> deletedShared, boolean b) {
Promise<List<String> > promise = Promise.promise();
Promise<List<String>> promise = Promise.promise();
List<Future<JsonObject>> futures = new ArrayList<>();
ids.forEach(id -> futures.add(this.shareService.upsertSharedArray(id, share, deletedShared,this.collection, true)));
ids.forEach(id -> futures.add(this.shareService.upsertSharedArray(id, share, deletedShared, this.collection, true)));
FutureHelper.all(futures)
.onSuccess(success -> promise.complete(ids))
.onFailure(error -> promise.fail(error.getMessage()));
Expand All @@ -739,8 +768,7 @@ public Future<List<String>> getOwnedBoardsIds(List<String> boardsIds, String own
Promise<List<String>> promise = Promise.promise();
JsonObject query = new MongoQuery(this.collection)
.match(new JsonObject()
.put(Field._ID, new JsonObject().put(Mongo.IN, boardsIds)).put(Field.OWNERID,ownerId)).getAggregate()
;
.put(Field._ID, new JsonObject().put(Mongo.IN, boardsIds)).put(Field.OWNERID, ownerId)).getAggregate();
mongoDb.command(query.toString(), MongoDbResult.validResultHandler(either -> {
if (either.isLeft()) {
log.error("[Magneto@%s::getBoards] Failed to get boards", this.getClass().getSimpleName(),
Expand All @@ -752,7 +780,7 @@ public Future<List<String>> getOwnedBoardsIds(List<String> boardsIds, String own
.getJsonArray(Field.FIRSTBATCH, new JsonArray());
promise.complete(result.stream().filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.map(board->board.getString(Field._ID))
.map(board -> board.getString(Field._ID))
.collect(Collectors.toList()));
}
}));
Expand Down Expand Up @@ -794,6 +822,7 @@ Mongo.MAP, new JsonObject()
.put(Field.DISPLAY_NB_FAVORITES, 1));
return query.getAggregate();
}

@Override
public Future<List<Board>> getBoardsWithNbCards(List<String> resultIds) {
Promise<List<Board>> promise = Promise.promise();
Expand Down

0 comments on commit 15d7c08

Please sign in to comment.