Skip to content

Commit

Permalink
Temp functions, button to fill all empty guids. (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Feb 26, 2021
1 parent e408c91 commit 949c439
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Backend.Tests/Mocks/WordRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ public Task<Word> Add(Word word)
_words.Add(word.Clone());
return Task.FromResult(word.Clone());
}

public Task<bool> PopulateAllGuids() // ToDo: Remove after used on live server.
{
return Task.FromResult(false);
}
}
}
9 changes: 9 additions & 0 deletions Backend/Controllers/WordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public WordController(IWordRepository repo, IWordService wordService, IProjectSe
[HttpGet]
public async Task<IActionResult> Get(string projectId)
{
// ToDo: Remove this if-statement after used on live server.
if (projectId == "populateguids")
{
Console.WriteLine("Starting to populate guids...");
await _wordRepo.PopulateAllGuids();
Console.WriteLine("Done populating guids! Please verify success in mongo.");
return new OkResult();
}

if (!await _permissionService.HasProjectPermission(HttpContext, Permission.WordEntry))
{
return new ForbidResult();
Expand Down
1 change: 1 addition & 0 deletions Backend/Interfaces/IWordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IWordRepository
Task<List<Word>> GetAllWords(string projectId);
Task<Word?> GetWord(string projectId, string wordId);
Task<Word> Create(Word word);
Task<bool> PopulateAllGuids(); // ToDo: Remove after used on live server.
Task<Word> Add(Word word);
Task<bool> DeleteAllWords(string projectId);
Task<List<Word>> GetFrontier(string projectId);
Expand Down
93 changes: 93 additions & 0 deletions Backend/Services/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,65 @@ internal static void PopulateWordGuids(Word word)
}
}

/// <remarks> This method should be removed once all legacy data has been converted. </remarks>
private async Task<bool> PopulateGuidsAndUpdateWord(Word word, Guid? guid = null)
{
if ((word.Guid is null || Guid.Empty.Equals(word.Guid)) && guid != null)
{
word.Guid = guid;
}
PopulateWordGuids(word);
return await UpdateWord(word);
}

/// <remarks> This method should be removed once all legacy data has been converted. </remarks>
private async Task<bool> PopulateGuidInHistory(Word word)
{
PopulateWordGuids(word);
var idsToUpdate = new List<string>(word.History) { word.Id };
var success = true;
foreach (var priorId in idsToUpdate)
{
var priorWord = await GetWord(word.ProjectId, priorId);
if (priorWord != null)
{
success &= await PopulateGuidsAndUpdateWord(priorWord, word.Guid);
}
}
return success;
}

/// <remarks> This method should be removed once all legacy data has been converted. </remarks>
public async Task<bool> PopulateAllGuids()
{
var success = true;

// Update frontier words and their predecessors.
var frontierWords = await _wordDatabase.Frontier.Find(w => true).ToListAsync();
foreach (var w in frontierWords)
{
success &= await PopulateGuidInHistory(w);
success &= await UpdateFrontier(w);
}

// Update deleted words and their predecessors.
// Note: this could error if a deleted word has another deleted word in its history.
var deletedWords = await _wordDatabase.Words.Find(w => w.Accessibility == State.Deleted).ToListAsync();
foreach (var w in deletedWords)
{
success &= await PopulateGuidInHistory(w);
}

// Catch stragglers.
var allWords = await _wordDatabase.Words.Find(w => true).ToListAsync();
foreach (var w in allWords)
{
success &= await PopulateGuidsAndUpdateWord(w);
}

return success;
}

/// <summary> Adds a <see cref="Word"/> only to the WordsCollection </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
Expand Down Expand Up @@ -141,5 +200,39 @@ public async Task<bool> DeleteFrontier(string projectId, string wordId)
var deleted = await _wordDatabase.Frontier.DeleteOneAsync(filter);
return deleted.DeletedCount > 0;
}

/// <summary> Updates <see cref="Word"/> in the Frontier collection with same wordId and projectId </summary>
/// <returns> A bool: success of operation </returns>
public async Task<bool> UpdateFrontier(Word word)
{
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.And(
filterDef.Eq(x => x.ProjectId, word.ProjectId),
filterDef.Eq(x => x.Id, word.Id));

var deleted = (await _wordDatabase.Frontier.DeleteOneAsync(filter)).DeletedCount > 0;
if (deleted)
{
await AddFrontier(word);
}
return deleted;
}

/// <summary> Updates <see cref="Word"/> in the Words collection with same wordId and projectId </summary>
/// <returns> A bool: success of operation </returns>
private async Task<bool> UpdateWord(Word word)
{
var filterDef = new FilterDefinitionBuilder<Word>();
var filter = filterDef.And(
filterDef.Eq(x => x.ProjectId, word.ProjectId),
filterDef.Eq(x => x.Id, word.Id));

var deleted = (await _wordDatabase.Words.DeleteOneAsync(filter)).DeletedCount > 0;
if (deleted)
{
await Add(word);
}
return deleted;
}
}
}
8 changes: 8 additions & 0 deletions src/backend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ export async function getAllUsersInCurrentProject(): Promise<User[]> {
return resp.data;
}

// ToDo: Remove this function after used on live server.
export async function populateAllGuids() {
const resp = await backendServer.get("projects/populateguids/words", {
headers: authHeader(),
});
return resp.data;
}

export async function getUser(id: string): Promise<User> {
let resp = await backendServer.get(`users/${id}`, { headers: authHeader() });
return resp.data;
Expand Down
8 changes: 7 additions & 1 deletion src/components/SiteSettings/SiteSettingsComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Grid } from "@material-ui/core";
import { Button, Grid } from "@material-ui/core";
import { List, People } from "@material-ui/icons";
import React from "react";
import { Translate } from "react-localize-redux";

import { populateAllGuids } from "backend";
import BaseSettingsComponent from "components/BaseSettings/BaseSettingsComponent";
import ProjectManagement from "components/SiteSettings/ProjectManagement/ProjectManagement";
import UserManagement from "components/SiteSettings//UserManagement/UserManagement";

export default function SiteSettingsComponent() {
return (
<Grid container justify="center" spacing={6}>
{/* ToDo: Remove this Button after used on live server. */}
<Button onClick={populateAllGuids} variant="contained" color="primary">
Populate Guids
</Button>

{/* Project List */}
<BaseSettingsComponent
icon={<List />}
Expand Down

0 comments on commit 949c439

Please sign in to comment.