public void ClearUserId(string userId) { // Replace invalid characters with empty strings. Can't pass it as a parameter, as string parameters get wrapped in '\"' when inserted var sanitisedUserId = Regex.Replace(userId, @"[^\w\.@-]", ""); // Get full ID var idForDynamicField = _dbSession.GetIdForDynamicField <User>(sanitisedUserId); var fullId = _dbSession.GetFullId <User>(userId); // Form a patch query var queryString = $@"FROM INDEX '{new BacklogItems_ForList().IndexName}' AS i WHERE i.{nameof(BacklogItemIndexedForList.MentionedUser)}_{idForDynamicField} != null UPDATE {{ i.{nameof(BacklogItem.Comments)}.forEach(comment => {{ let mentionedUsers = comment.{nameof(Comment.MentionedUserIds)}; if (mentionedUsers != null) Object.keys(mentionedUsers).forEach(key => {{ if (mentionedUsers[key].toLowerCase() == '{fullId}'.toLowerCase()) delete mentionedUsers[key]; }}); return comment; }}); }}" ; var query = new IndexQuery { Query = queryString }; // Add the patch to a collection _patchOperations.AddDeferredPatchQuery(query); }
public void ClearUserId(string userId) { // Replace invalid characters with empty strings. Can't pass it as a parameter, as string parameters get wrapped in '\"' when inserted var sanitisedUserId = Regex.Replace(userId, @"[^\w\.@-]", ""); // Get full ID var idForDynamicField = _dbSession.GetIdForDynamicField <User>(sanitisedUserId); var fullId = _dbSession.GetFullId <User>(userId); // Form a patch query var queryString = $@"FROM INDEX '{new BacklogItems_ForList().IndexName}' AS i WHERE i.{nameof(BacklogItemIndexedForList.ModifiedBy)}_{idForDynamicField} != null OR i.{nameof(BacklogItemIndexedForList.AssignedUserId)} == $userId UPDATE {{ if (i.{nameof(BacklogItemIndexedForList.Assignee)}.{nameof(UserReference.Id)}.toLowerCase() == $userId) {{ i.{nameof(BacklogItemIndexedForList.Assignee)} = null; }} i.{nameof(BacklogItem.Comments)}.forEach(comment => {{ if (comment.{nameof(Comment.Author)}.{nameof(UserReference.Id)}.toLowerCase() == $userId) {{ comment.{nameof(Comment.Author)}.{nameof(UserReference.Id)} = null; }} }}); i.{nameof(BacklogItem.ModifiedBy)}.forEach(modif => {{ if (modif.{nameof(BacklogItemHistoryRecord.ActionedBy)}.{nameof(UserReference.Id)}.toLowerCase() == $userId) modif.{nameof(BacklogItemHistoryRecord.ActionedBy)}.{nameof(UserReference.Id)} = null; }}); }}" ; var query = new IndexQuery { Query = queryString, QueryParameters = new Parameters { { "userId", fullId.ToLower() } } }; // Add the patch to a collection _patchOperations.AddDeferredPatchQuery(query); }
protected string GetFullId(string id) => id.Contains('/') ? id // Assume it's already a full ID with a prefix : DbSession.GetFullId <TEntity>(id);
/// <summary> /// Get a special form of the ID for using in the dynamic fields /// </summary> /// <remarks> /// Full ID can't be used due to https://issues.hibernatingrhinos.com/issue/RavenDB-15235. /// Short ID can't be used due to https://issues.hibernatingrhinos.com/issue/RavenDB-15234. /// </remarks> /// <typeparam name="T"> The entity type (e.g. class `Users`) </typeparam> /// <param name="session"> Session to resolve conventions for converting the ID </param> /// <param name="shortId"> The short ID (e.g. '1-A') </param> /// <returns> A special ID for dynamic fields (e.g. 'users1-A') </returns> internal static string GetIdForDynamicField <T>(this IAsyncDocumentSession session, string shortId) where T : IEntity => session.GetFullId <T>(shortId).Replace("/", "").ToLower();
protected string GetFullId(string id) => DbSession.GetFullId <TEntity>(id);