diff --git a/Backend.Tests/Controllers/UserEditControllerTests.cs b/Backend.Tests/Controllers/UserEditControllerTests.cs index 6bdbdce3b2..7fc4cc49a4 100644 --- a/Backend.Tests/Controllers/UserEditControllerTests.cs +++ b/Backend.Tests/Controllers/UserEditControllerTests.cs @@ -21,6 +21,7 @@ public class UserEditControllerTests private string _projId; private IPermissionService _permissionService; private IUserService _userService; + private User _jwtAuthenticatedUser; [SetUp] public void Setup() @@ -36,7 +37,11 @@ public void Setup() { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; - + _jwtAuthenticatedUser = new User { Username = "user", Password = "pass" }; + _userService.Create(_jwtAuthenticatedUser); + _jwtAuthenticatedUser = _userService.Authenticate( + _jwtAuthenticatedUser.Username, _jwtAuthenticatedUser.Password).Result; + _userEditController.ControllerContext.HttpContext.Request.Headers["UserId"] = _jwtAuthenticatedUser.Id; } private UserEdit RandomUserEdit() @@ -89,8 +94,8 @@ public void TestGetUserEdit() public void TestCreateUserEdit() { var userEdit = new UserEdit { ProjectId = _projId }; - var id = (_userEditController.Post(_projId).Result as ObjectResult).Value as string; - userEdit.Id = id; + var withUser = (_userEditController.Post(_projId).Result as ObjectResult).Value as WithUser; + userEdit.Id = withUser.UpdatedUser.WorkedProjects[_projId]; Assert.Contains(userEdit, _userEditRepo.GetAllUserEdits(_projId).Result); } diff --git a/Backend/Controllers/UserEditController.cs b/Backend/Controllers/UserEditController.cs index eccecfd830..aad741d996 100644 --- a/Backend/Controllers/UserEditController.cs +++ b/Backend/Controllers/UserEditController.cs @@ -105,7 +105,7 @@ public async Task Get(string projectId, string userEditId) /// Creates a /// POST: v1/projects/{projectId}/useredits - /// Id of create UserEdit + /// UpdatedUser [HttpPost] public async Task Post(string projectId) { @@ -114,9 +114,21 @@ public async Task Post(string projectId) return new ForbidResult(); } + // Generate the new userEdit var userEdit = new UserEdit { ProjectId = projectId }; await _repo.Create(userEdit); - return new OkObjectResult(userEdit.Id); + // Update current user + var currentUserId = _permissionService.GetUserId(HttpContext); + var currentUser = await _userService.GetUser(currentUserId); + currentUser.WorkedProjects.Add(projectId, userEdit.Id); + await _userService.Update(currentUserId, currentUser); + // Generate the JWT based on the new userEdit + currentUser = await _userService.MakeJwt(currentUser); + await _userService.Update(currentUserId, currentUser); + + var output = new WithUser() { UpdatedUser = currentUser }; + + return new OkObjectResult(output); } /// Adds a goal to with specified id diff --git a/Backend/Models/User.cs b/Backend/Models/User.cs index b9a1c3f9b2..7e10ef38e2 100644 --- a/Backend/Models/User.cs +++ b/Backend/Models/User.cs @@ -166,4 +166,12 @@ public class Credentials public string Username { get; set; } public string Password { get; set; } } + + /// Contains UpdatedUser for Axios interceptor. + public class WithUser + { + public User UpdatedUser; + + public WithUser() { } + } } diff --git a/src/backend/index.tsx b/src/backend/index.tsx index d9745a6dcc..0ede4a7935 100644 --- a/src/backend/index.tsx +++ b/src/backend/index.tsx @@ -20,10 +20,10 @@ const backendServer = axios.create({ backendServer.interceptors.response.use( (resp) => { - if (resp.data.__UpdatedUser) { - LocalStorage.setCurrentUser(resp.data.__UpdatedUser); + if (resp.data.updatedUser) { + LocalStorage.setCurrentUser(resp.data.updatedUser); } - delete resp.data.__UpdatedUser; + delete resp.data.updatedUser; return resp; }, (err) => { @@ -458,7 +458,7 @@ function goalNameToGoalTypeId(goalName: string): string { return goalType.toString(); } -export async function createUserEdit(): Promise { +export async function createUserEdit(): Promise { let resp = await backendServer.post( `projects/${LocalStorage.getProjectId()}/useredits`, "", diff --git a/src/components/GoalTimeline/GoalsActions.tsx b/src/components/GoalTimeline/GoalsActions.tsx index baa130a62e..608dec2fc7 100644 --- a/src/components/GoalTimeline/GoalsActions.tsx +++ b/src/components/GoalTimeline/GoalsActions.tsx @@ -66,30 +66,17 @@ export function asyncLoadExistingUserEdits( }; } -function asyncCreateNewUserEditsObject(projectId: string) { - return async () => { - await Backend.createUserEdit() - .then(async (userEditId: string) => { - let updatedUser: User = updateUserIfExists(projectId, userEditId); - await Backend.updateUser(updatedUser); - }) - .catch((err) => { - console.log(err); - }); - }; -} - export function asyncGetUserEdits() { return async (dispatch: ThunkDispatch) => { - const user: User | null = LocalStorage.getCurrentUser(); - if (user) { - const projectId: string = LocalStorage.getProjectId(); - const userEditId: string | undefined = getUserEditId(user); + const user = LocalStorage.getCurrentUser(); + const projectId = LocalStorage.getProjectId(); + if (user && projectId) { + const userEditId = getUserEditId(user); if (userEditId !== undefined) { dispatch(asyncLoadExistingUserEdits(projectId, userEditId)); } else { - dispatch(asyncCreateNewUserEditsObject(projectId)); + dispatch(Backend.createUserEdit); } } }; @@ -99,7 +86,7 @@ export function asyncAddGoalToHistory(goal: Goal) { return async (dispatch: ThunkDispatch) => { const user = LocalStorage.getCurrentUser(); if (user) { - let userEditId: string | undefined = getUserEditId(user); + const userEditId = getUserEditId(user); if (userEditId !== undefined) { dispatch(loadGoalData(goal)).then( (returnedGoal) => (goal = returnedGoal) @@ -200,25 +187,6 @@ export function getUserEditId(user: User): string | undefined { } } -function updateUserIfExists(projectId: string, userEditId: string): User { - let currentUser: User | null = LocalStorage.getCurrentUser(); - let updatedUser: User = new User("", "", ""); - if (currentUser) { - updatedUser = updateUserWithUserEditId(currentUser, projectId, userEditId); - LocalStorage.setCurrentUser(updatedUser); - } - return updatedUser; -} - -function updateUserWithUserEditId( - user: User, - projectId: string, - userEditId: string -): User { - user.workedProjects[projectId] = userEditId; - return user; -} - export function getIndexInHistory(history: Goal[], currentGoal: Goal): number { for (let i = 0; i < history.length; i++) { if (history[i].hash === currentGoal.hash) { diff --git a/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx b/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx index 308dd354b6..c5ba7b5375 100644 --- a/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx +++ b/src/components/GoalTimeline/tests/GoalTimelineActions.test.tsx @@ -41,7 +41,7 @@ jest.mock("../../../backend", () => { return Promise.resolve(mockUserEdit); }), createUserEdit: jest.fn(() => { - return Promise.resolve(mockUserEditId); + return Promise.resolve({}); }), updateUser: jest.fn((_user: User) => { return Promise.resolve(mockUser);