Search in sources :

Example 6 with Organization

use of org.c4sg.entity.Organization in project c4sg-services by Code4SocialGood.

the class BadgeServiceImpl method saveBadge.

@Override
public Badge saveBadge(Integer userId, Integer projectId) {
    /* Volunteer Recognition  - Give badge to the volunteer(accepted applicant)
		 * Send out email to the volunteer
		 * Save it to the database
		*/
    System.out.println("save Badge called...");
    User user = userDAO.findById(userId);
    requireNonNull(user, "Invalid User Id");
    Project project = projectDAO.findById(projectId);
    requireNonNull(project, "Invalid Project Id");
    Integer orgId = project.getOrganization().getId();
    Organization org = organizationDAO.findOne(orgId);
    List<User> users = userDAO.findByOrgId(orgId);
    User orgUser = users.get(0);
    isBadgeGiven(userId, projectId);
    Badge heroBadge = new Badge();
    heroBadge.setUser(user);
    heroBadge.setProject(project);
    heroBadge = badgeDAO.save(heroBadge);
    System.out.println("Hero Badge Given to" + userId + "," + projectId);
    // send email to the volunteer
    Map<String, Object> contextVolunteer = new HashMap<String, Object>();
    contextVolunteer.put("heroesLink", urlService.getHeroUrl());
    contextVolunteer.put("org", org);
    contextVolunteer.put("orgUser", orgUser);
    contextVolunteer.put("project", project);
    contextVolunteer.put("projectLink", urlService.getProjectUrl(project.getId()));
    asyncEmailService.sendWithContext(Constants.C4SG_ADDRESS, user.getEmail(), orgUser.getEmail(), Constants.SUBJECT_HERO_USER, Constants.TEMPLATE_HERO_USER, contextVolunteer);
    System.out.println("Hero Badge email sent: Project=" + project.getId() + " ; ApplicantEmail=" + user.getEmail());
    return heroBadge;
}
Also used : Project(org.c4sg.entity.Project) User(org.c4sg.entity.User) Organization(org.c4sg.entity.Organization) HashMap(java.util.HashMap) Badge(org.c4sg.entity.Badge)

Example 7 with Organization

use of org.c4sg.entity.Organization in project c4sg-services by Code4SocialGood.

the class EmailServiceTest method testSendVolunteerApplicantEmail.

@Test
public void testSendVolunteerApplicantEmail() throws MessagingException {
    Organization org = new Organization();
    org.setName("Test Organization");
    org.setContactEmail("test@c4sg.org");
    org.setContactName("John Sloan");
    org.setContactPhone("9898989899");
    User user = new User();
    user.setEmail("test@c4sg.org");
    user.setFirstName("John");
    user.setLastName("Sloan");
    user.setChatUsername("jsloan");
    Project project = new Project();
    project.setId(110);
    project.setName("Test Project");
    Map<String, Object> mailContext = new HashMap<String, Object>();
    mailContext.put("org", org);
    mailContext.put("user", user);
    mailContext.put("projectLink", "http://codeforsocialgood.org");
    mailContext.put("project", project);
    mailService.sendWithContext(from, user.getEmail(), "", "Test Email", "applicant-application", mailContext);
    // received message
    MimeMessage[] receivedMessages = mailer.getReceivedMessages();
    assertEquals(1, receivedMessages.length);
    MimeMessage msg = receivedMessages[0];
    assertThat(from, is(msg.getFrom()[0].toString()));
    assertThat("Test Email", is(msg.getSubject()));
}
Also used : Project(org.c4sg.entity.Project) Organization(org.c4sg.entity.Organization) User(org.c4sg.entity.User) HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with Organization

use of org.c4sg.entity.Organization in project c4sg-services by Code4SocialGood.

the class UserServiceImpl method createUser.

@Override
public UserDTO createUser(CreateUserDTO createUserDTO) {
    User user = userMapper.getUserEntityFromCreateUserDto(createUserDTO);
    if ((user != null) && !StringUtils.isEmpty(user.getCountry())) {
        try {
            Map<String, BigDecimal> geoCode = geocodeService.getGeoCode(user.getState(), user.getCountry());
            user.setLatitude(geoCode.get("lat"));
            user.setLongitude(geoCode.get("lng"));
        } catch (Exception e) {
            // Don't throw exception, geocoding error won't present user from being created.
            System.out.println("Error getting geocode: " + e.toString());
        }
    }
    // Set user status to "N" for new user
    user.setStatus("N");
    User userEntity = userDAO.save(user);
    // If the user is organization user:
    // 1. Creates an empty organization.
    // 2. Create a relationship between user and organization. The relationship is one to one.
    String role = createUserDTO.getRole();
    if ((role != null) && role.equals("O")) {
        Organization organization = new Organization();
        organization.setStatus("N");
        Organization organizationEntity = organizationDAO.save(organization);
        organizationService.saveUserOrganization(userEntity.getId(), organizationEntity.getId());
    }
    return userMapper.getUserDtoFromEntity(userEntity);
}
Also used : User(org.c4sg.entity.User) Organization(org.c4sg.entity.Organization) BigDecimal(java.math.BigDecimal) NotFoundException(org.c4sg.exception.NotFoundException) UserServiceException(org.c4sg.exception.UserServiceException)

Example 9 with Organization

use of org.c4sg.entity.Organization in project c4sg-services by Code4SocialGood.

the class OrganizationServiceImpl method updateOrganization.

public OrganizationDTO updateOrganization(int id, OrganizationDTO organizationDTO) {
    Organization organization = organizationDAO.findOne(id);
    if (organization == null) {
        System.out.println("Organization does not exist.");
    } else {
        organization = organizationMapper.getOrganizationEntityFromDto(organizationDTO);
        try {
            Map<String, BigDecimal> geoCode = geocodeService.getGeoCode(organization.getState(), organization.getCountry());
            organization.setLatitude(geoCode.get("lat"));
            organization.setLongitude(geoCode.get("lng"));
        } catch (Exception e) {
            // throw new NotFoundException("Error getting geocode");
            System.out.println("Error getting geocode: " + e.toString());
        }
        organizationDAO.save(organization);
        String newStatus = organization.getStatus();
        // Notify admin users of new organization, or update for a declined organization
        if (newStatus.equals(Constants.ORGANIZATION_STATUS_PENDIONG_REVIEW) || newStatus.equals(Constants.ORGANIZATION_STATUS_DECLINED)) {
            String toAddress = null;
            List<User> users = userDAO.findByKeyword(null, "A", "A", null);
            if (users != null && !users.isEmpty()) {
                User adminUser = users.get(0);
                toAddress = adminUser.getEmail();
            }
            Map<String, Object> context = new HashMap<String, Object>();
            context.put("organization", organization);
            asyncEmailService.sendWithContext(Constants.C4SG_ADDRESS, toAddress, "", Constants.SUBJECT_NEW_ORGANIZATION_REVIEW, Constants.TEMPLATE_NEW_ORGANIZATION_REVIEW, context);
            System.out.println("New organization email sent: Organization=" + organization.getId() + " ; Email=" + toAddress);
        }
    }
    return organizationMapper.getOrganizationDtoFromEntity(organization);
}
Also used : Organization(org.c4sg.entity.Organization) UserOrganization(org.c4sg.entity.UserOrganization) User(org.c4sg.entity.User) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) UserOrganizationException(org.c4sg.exception.UserOrganizationException)

Example 10 with Organization

use of org.c4sg.entity.Organization in project c4sg-services by Code4SocialGood.

the class OrganizationServiceImpl method deleteOrganization.

public void deleteOrganization(int id) {
    Organization organization = organizationDAO.findOne(id);
    if (organization != null) {
        organization.setStatus(Constants.ORGANIZATION_STATUS_DELETED);
        // TODO Delete logo from S3 by frontend
        organizationDAO.save(organization);
        List<ProjectDTO> projects = projectService.findByOrganization(id, null);
        for (ProjectDTO project : projects) {
            projectService.deleteProject(project.getId());
        }
        organizationDAO.deleteUserOrganizations(id);
    // TODO: Local or Timezone?
    // TODO: Format date
    // organization.setDeleteTime(LocalDateTime.now().toString());
    // organization.setDeleteBy(user.getUsername());
    }
}
Also used : ProjectDTO(org.c4sg.dto.ProjectDTO) Organization(org.c4sg.entity.Organization) UserOrganization(org.c4sg.entity.UserOrganization)

Aggregations

Organization (org.c4sg.entity.Organization)10 User (org.c4sg.entity.User)6 UserOrganization (org.c4sg.entity.UserOrganization)6 HashMap (java.util.HashMap)4 Project (org.c4sg.entity.Project)3 BigDecimal (java.math.BigDecimal)2 UserOrganizationException (org.c4sg.exception.UserOrganizationException)2 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1 MimeMessage (javax.mail.internet.MimeMessage)1 ProjectDTO (org.c4sg.dto.ProjectDTO)1 Badge (org.c4sg.entity.Badge)1 UserProject (org.c4sg.entity.UserProject)1 NotFoundException (org.c4sg.exception.NotFoundException)1 UserServiceException (org.c4sg.exception.UserServiceException)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 Async (org.springframework.scheduling.annotation.Async)1