Skip to content

Commit

Permalink
update and settings controller changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-addison-h committed May 30, 2024
1 parent f6f3ab6 commit 5ade11a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
77 changes: 73 additions & 4 deletions app/femr/ui/controllers/SettingsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@
import com.google.inject.Inject;
import controllers.AssetsFinder;
import femr.business.services.core.ISessionService;
import femr.business.services.core.IUserService;
import femr.common.dtos.CurrentUser;
import femr.ui.views.html.home.index;
import femr.common.dtos.ServiceResponse;
import femr.common.models.UserItem;
import femr.ui.models.settings.EditViewModel;
import femr.ui.views.html.settings.index;

import femr.ui.views.html.sessions.create;
import femr.util.stringhelpers.StringUtils;
import play.data.FormFactory;
import play.mvc.Controller;
import play.mvc.Result;
import play.data.Form;

import java.util.ArrayList;

public class SettingsController extends Controller {
private final AssetsFinder assetsFinder;
private ISessionService sessionService;
private final FormFactory formFactory;
private final ISessionService sessionService;
private final IUserService userService;

@Inject
public SettingsController(AssetsFinder assetsFinder, ISessionService sessionService) {

public SettingsController(AssetsFinder assetsFinder,
FormFactory formFactory,
ISessionService sessionService,
IUserService userService) {
this.assetsFinder = assetsFinder;
this.formFactory = formFactory;
this.sessionService = sessionService;
this.userService = userService;
}

public Result index() {
Expand All @@ -31,4 +45,59 @@ public Result index() {

return ok(create.render(null, 0, null, assetsFinder, new ArrayList<String>()));
}

public Result update() {
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();
if (currentUser == null) {
return unauthorized();
}

Form<EditViewModel> editViewModelForm = formFactory.form(EditViewModel.class).bindFromRequest();
if (editViewModelForm.hasErrors()) {
return badRequest(index.render(currentUser, assetsFinder));
}

EditViewModel viewModel = editViewModelForm.get();
ServiceResponse<UserItem> userServiceResponse = userService.retrieveUser(currentUser.getId());
if (userServiceResponse.hasErrors()) {
return internalServerError();
}

UserItem userItem = userServiceResponse.getResponseObject();
userItem.setFirstName(viewModel.getFirstName());
userItem.setLastName(viewModel.getLastName());
userItem.setLanguageCode(viewModel.getLanguageCode());

if (StringUtils.isNotNullOrWhiteSpace(viewModel.getPasswordReset()) && viewModel.getPasswordReset().equals("on")) {
userItem.setPasswordReset(true);
} else {
userItem.setPasswordReset(false);
}

String newPassword = null;
if (StringUtils.isNotNullOrWhiteSpace(viewModel.getNewPassword()) && viewModel.getNewPassword().equals(viewModel.getNewPasswordVerify())) {
newPassword = viewModel.getNewPassword();
}

ServiceResponse<UserItem> updateResponse = userService.updateUser(userItem, newPassword);
if (updateResponse.hasErrors()) {
return internalServerError();
}

return redirect("/settings");
}



private EditViewModel convertToEditViewModel(UserItem userItem) {
EditViewModel editViewModel = new EditViewModel();
editViewModel.setUserId(userItem.getId());
editViewModel.setFirstName(userItem.getFirstName());
editViewModel.setLastName(userItem.getLastName());
editViewModel.setEmail(userItem.getEmail());
editViewModel.setPasswordReset(Boolean.toString(userItem.isPasswordReset()));
editViewModel.setLanguageCode(userItem.getLanguageCode());
return editViewModel;
}

}
5 changes: 4 additions & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ GET /search/typeahead/medicationsWithID @femr.ui.controll
GET /search/typeahead/medicationAdministrations @femr.ui.controllers.SearchController.typeaheadMedicationAdministrationsJSONGet()

#Settings
#GET /settings @femr.ui.controllers.SettingsController.index()
#GET /settings @femr.ui.controllers.SettingsController.index()
POST /settings/update @femr.ui.controllers.SettingsController.update()


#Photo
GET /photo/patient/:id @femr.ui.controllers.PhotoController.GetPatientPhoto(id: Integer, showDefault: Boolean ?= false)
GET /photo/encounter/:id @femr.ui.controllers.PhotoController.GetPhoto(id: Int)
Expand Down

0 comments on commit 5ade11a

Please sign in to comment.