class MonitoringOfficerRegistrationController {

    @PostMapping("/{inviteHash}/register")
    public String submitDetails(Model model,
                                    @PathVariable("inviteHash") String inviteHash,
                                    @ModelAttribute(FORM_ATTR_NAME) @Validated({Default.class, PhoneNumberValidationGroup.class}) RegistrationForm monitoringOfficerRegistrationForm,
                                    BindingResult bindingResult,
                                    ValidationHandler validationHandler,
                                    UserResource loggedInUser) {

        Supplier<String> failureView = () -> doViewYourDetails(model, inviteHash, loggedInUser);

        if (loggedInUser != null) {
            return failureView.get();
        }
        else {
            return validationHandler.failNowOrSucceedWith(failureView, () -> {
                ServiceResult<Void> result = monitoringOfficerService.activateAndUpdateMonitoringOfficer(inviteHash,
                                                                                                         monitoringOfficerRegistrationForm);
                result.getErrors().forEach(error -> {
                    if (StringUtils.hasText(error.getFieldName())) {
                        bindingResult.rejectValue(error.getFieldName(), "registration." + error.getErrorKey());
                    } else {
                        bindingResult.reject("registration." + error.getErrorKey());
                    }
                });
                return validationHandler.
                        failNowOrSucceedWith(failureView,
                                () -> format("redirect:/monitoring-officer/%s/register/account-created", inviteHash));
            });
        }
    }

}