Source:
<code>public Response createUser(StaffMemberPayload payload) { DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO(); Defaults defaults = defaultsDAO.getDefaults(); if (defaults.getUserDefaultContactType() == null) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity("userDefaultContactType not set in Defaults").build(); } if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail(), payload.getRole())) { return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build(); } Role role = Role.valueOf(payload.getRole()); if (role != Role.MANAGER && role != Role.TEACHER) { return Response.status(Status.BAD_REQUEST).entity(String.format("Unsupported role %s", payload.getRole())).build(); } String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail())); Email email = commonController.findEmailByAddress(address); if (email != null) { return Response.status(Status.CONFLICT).entity(getMessage("error.emailInUse")).build(); } User loggedUser = sessionController.getUser(); Person person = personController.createPerson(null, null, null, null, Boolean.FALSE); <START> StaffMember staffMember = userController.createStaffMember(loggedUser.getOrganization(), payload.getFirstName(), payload.getLastName(), role, person); <END> userController.addUserEmail(staffMember, defaults.getUserDefaultContactType(), address, Boolean.TRUE); payload.setIdentifier(staffMember.getId().toString()); return Response.ok(payload).build(); }</code><technical_language>Add check for null organization, in case (administrator operate w/o organization mandatory)</technical_language>

Target:
public Response createUser(StaffMemberPayload payload) { DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO(); Defaults defaults = defaultsDAO.getDefaults(); if (defaults.getUserDefaultContactType() == null) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity("userDefaultContactType not set in Defaults").build(); } if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail(), payload.getRole())) { return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build(); } Role role = Role.valueOf(payload.getRole()); if (role != Role.MANAGER && role != Role.TEACHER) { return Response.status(Status.BAD_REQUEST).entity(String.format("Unsupported role %s", payload.getRole())).build(); } String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail())); Email email = commonController.findEmailByAddress(address); if (email != null) { return Response.status(Status.CONFLICT).entity(getMessage("error.emailInUse")).build(); } User loggedUser = sessionController.getUser(); if (loggedUser.getOrganization() == null) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Current user lacks organization").build(); } Person person = personController.createPerson(null, null, null, null, Boolean.FALSE); StaffMember staffMember = userController.createStaffMember(loggedUser.getOrganization(), payload.getFirstName(), payload.getLastName(), role, person); userController.addUserEmail(staffMember, defaults.getUserDefaultContactType(), address, Boolean.TRUE); payload.setIdentifier(staffMember.getId().toString()); return Response.ok(payload).build(); }