class AcceptGrantsInviteController {

    @GetMapping("/{hash}")
    public String inviteEntryPage(
            @PathVariable final long projectId,
            @PathVariable final String hash,
            HttpServletResponse response,
            Model model,
            UserResource loggedInUser) {

        RestResult<String> result = find(inviteByHash(projectId, hash)).andOnSuccess((invite) -> {
            ValidationMessages errors = validateUserCanAcceptInvite(loggedInUser, invite);
            if (errors.hasErrors()) {
                return populateModelWithErrorsAndReturnErrorView(errors, model);
            }
            cookieUtil.saveToCookie(response, INVITE_HASH, hash);
            model.addAttribute("model", new GrantsInviteViewModel(invite.getApplicationId(),
                    projectId, invite.getProjectName(), invite.getGrantsInviteRole(), invite.userExists(),
                    loggedInUser != null));
            return restSuccess("project/grants-invite/accept-invite");
        });

        if (result.isFailure()) {
            return ACCEPT_INVITE_FAILURE;
        } else {
            return result.getSuccess();
        }
    }

}