Skip to content

Commit

Permalink
Merge pull request #2490 from objectcomputing/feature-2448/backend-fo…
Browse files Browse the repository at this point in the history
…r-volunteering

Add API for managing volunteering
  • Loading branch information
timyates authored Jun 12, 2024
2 parents 5e318bb + 1ce89e0 commit bf06d6c
Show file tree
Hide file tree
Showing 25 changed files with 2,106 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public enum Permission {
CAN_VIEW_SETTINGS("View settings", "Settings"),
CAN_VIEW_ALL_PULSE_RESPONSES("View pulse responses", "Reporting"),
CAN_MANAGE_CERTIFICATIONS("Manage certifications", "Certifications"),
CAN_MANAGE_EARNED_CERTIFICATIONS("Manage earned certifications", "Certifications");
CAN_MANAGE_EARNED_CERTIFICATIONS("Manage earned certifications", "Certifications"),
CAN_ADMINISTER_VOLUNTEERING_ORGANIZATIONS("Update volunteering organizations", "Volunteering"),
CAN_ADMINISTER_VOLUNTEERING_RELATIONSHIPS("Update volunteering relationships", "Volunteering"),
CAN_ADMINISTER_VOLUNTEERING_EVENTS("Update volunteering events", "Volunteering");

private final String description;
private final String category;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.objectcomputing.checkins.services.volunteering;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.objectcomputing.checkins.converter.LocalDateConverter;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.TypeDef;
import io.micronaut.data.model.DataType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.time.LocalDate;
import java.util.Objects;
import java.util.UUID;

@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Introspected
@ToString
@Table(name = "volunteering_event")
public class VolunteeringEvent {

@Id
@Column(name = "event_id")
@AutoPopulated
@TypeDef(type = DataType.STRING)
@Schema(description = "id of the volunteering event")
private UUID id;

@Column(name = "relationship_id")
@NotNull
@Schema(description = "id of the Volunteering relationship")
private UUID relationshipId;

@NotNull
@Column(name = "event_date")
@TypeDef(type = DataType.DATE, converter = LocalDateConverter.class)
@Schema(description = "when the volunteering event occurred")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate eventDate;

@Column(name = "hours")
@Schema(description = "number of hours spent volunteering")
@TypeDef(type = DataType.INTEGER)
private int hours;

@Nullable
@Column(name = "notes")
@TypeDef(type = DataType.STRING)
@Schema(description = "notes about the volunteering event")
private String notes;

public VolunteeringEvent(UUID relationshipId, LocalDate eventDate, int hours, String notes) {
this(null, relationshipId, eventDate, hours, notes);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
VolunteeringEvent that = (VolunteeringEvent) o;
return hours == that.hours && Objects.equals(id, that.id) && Objects.equals(relationshipId, that.relationshipId) && Objects.equals(eventDate, that.eventDate) && Objects.equals(notes, that.notes);
}

@Override
public int hashCode() {
return Objects.hash(id, relationshipId, eventDate, hours, notes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.objectcomputing.checkins.services.volunteering;

import com.objectcomputing.checkins.services.permissions.Permission;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Delete;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Put;
import io.micronaut.http.annotation.Status;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

import java.util.List;
import java.util.UUID;

import static io.micronaut.http.HttpStatus.CREATED;

@ExecuteOn(TaskExecutors.BLOCKING)
@Secured(SecurityRule.IS_AUTHENTICATED)
@Tag(name = "volunteering")
@Controller("/services/volunteer/event")
class VolunteeringEventController {

private final VolunteeringService volunteeringService;

VolunteeringEventController(VolunteeringService volunteeringService) {
this.volunteeringService = volunteeringService;
}

/**
* List all volunteering events.
* If memberId is provided, restrict to events for that member.
* If relationshipId is provided, restrict to events for that relationship.
* If includeInactive is true, include inactive organizations and relationships in the results.
*
* @param memberId the id of the member
* @param organizationId the id of the organization
* @param includeDeactivated whether to include deactivated relationships or organizations
* @return list of {@link VolunteeringEvent}
*/
@Get("/{?memberId,organizationId,includeDeactivated}")
List<VolunteeringEvent> findEvents(@Nullable UUID memberId, @Nullable UUID organizationId, @Nullable Boolean includeDeactivated) {
return volunteeringService.listEvents(memberId, organizationId, Boolean.TRUE.equals(includeDeactivated));
}

/**
* Create a new volunteering event.
* Requires you to be the creator of the relationship, or have the {@link Permission#CAN_ADMINISTER_VOLUNTEERING_EVENTS} permission.
*
* @param event the event to create
* @return the created {@link VolunteeringEvent}
*/
@Post
@Status(CREATED)
VolunteeringEvent create(@Valid @Body VolunteeringEventDTO event) {
return volunteeringService.create(new VolunteeringEvent(
event.getRelationshipId(),
event.getEventDate(),
event.getHours(),
event.getNotes()
));
}

/**
* Update an existing volunteering event.
* Requires you to be the creator of the relationship, or have the {@link Permission#CAN_ADMINISTER_VOLUNTEERING_EVENTS} permission.
*
* @param id the id of the relationship to update
* @param event the relationship to update
* @return the updated {@link VolunteeringEvent}
*/
@Put("/{id}")
VolunteeringEvent update(@NotNull UUID id, @Valid @Body VolunteeringEventDTO event) {
return volunteeringService.update(new VolunteeringEvent(
id,
event.getRelationshipId(),
event.getEventDate(),
event.getHours(),
event.getNotes()
));
}

/**
* Delete a volunteering event.
* Requires you to be the creator of the relationship, or have the {@link Permission#CAN_ADMINISTER_VOLUNTEERING_EVENTS} permission.
*
* @param id the id of the event to delete
*/
@Delete("/{id}")
@Status(HttpStatus.OK)
void delete(UUID id) {
volunteeringService.deleteEvent(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.objectcomputing.checkins.services.volunteering;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.micronaut.core.annotation.Introspected;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Setter
@Getter
@AllArgsConstructor
@Introspected
public class VolunteeringEventDTO {

@NotNull
@Schema(description = "id of the Volunteering relationship")
private UUID relationshipId;

@NotNull
@Schema(description = "when the volunteering event occurred")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate eventDate;

@NotNull
@Schema(description = "number of hours spent volunteering")
private Integer hours;

@Nullable
@Schema(description = "notes about the volunteering event")
private String notes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.objectcomputing.checkins.services.volunteering;

import io.micronaut.data.annotation.Query;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;
import jakarta.validation.constraints.NotNull;

import java.util.List;
import java.util.UUID;

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface VolunteeringEventRepository extends CrudRepository<VolunteeringEvent, UUID> {

@Query("""
SELECT event.*
FROM volunteering_event AS event
JOIN volunteering_relationship AS rel USING(relationship_id)
JOIN volunteering_organization AS org USING(organization_id)
WHERE rel.member_id::uuid = :memberId
AND (rel.is_active = TRUE OR :includeDeactivated = TRUE)
AND (org.is_active = TRUE OR :includeDeactivated = TRUE)
ORDER BY event.event_date, org.name, event.hours DESC""")
List<VolunteeringEvent> findByMemberId(@NotNull UUID memberId, boolean includeDeactivated);

@Query("""
SELECT event.*
FROM volunteering_event AS event
JOIN volunteering_relationship AS rel USING(relationship_id)
JOIN volunteering_organization AS org USING(organization_id)
WHERE org.organization_id::uuid = :organizationId
AND (rel.is_active = TRUE OR :includeDeactivated = TRUE)
AND (org.is_active = TRUE OR :includeDeactivated = TRUE)
ORDER BY event.event_date, org.name, event.hours DESC""")
List<VolunteeringEvent> findByOrganizationId(@NotNull UUID organizationId, boolean includeDeactivated);

@Query("""
SELECT event.*
FROM volunteering_event AS event
JOIN volunteering_relationship AS rel USING(relationship_id)
JOIN volunteering_organization AS org USING(organization_id)
WHERE rel.member_id::uuid = :memberId
AND org.organization_id::uuid = :organizationId
AND (rel.is_active = TRUE OR :includeDeactivated = TRUE)
AND (org.is_active = TRUE OR :includeDeactivated = TRUE)
ORDER BY event.event_date, org.name, event.hours DESC""")
List<VolunteeringEvent> findByMemberIdAndOrganizationId(@NotNull UUID memberId, @NotNull UUID organizationId, boolean includeDeactivated);

@Query("""
SELECT event.*
FROM volunteering_event AS event
JOIN volunteering_relationship AS rel USING(relationship_id)
JOIN volunteering_organization AS org USING(organization_id)
WHERE (rel.is_active = TRUE OR :includeDeactivated = TRUE)
AND (org.is_active = TRUE OR :includeDeactivated = TRUE)
ORDER BY event.event_date, org.name, event.hours DESC""")
List<VolunteeringEvent> findAll(boolean includeDeactivated);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.objectcomputing.checkins.services.volunteering;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.TypeDef;
import io.micronaut.data.model.DataType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.UUID;

@Setter
@Getter
@Entity
@Introspected
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "volunteering_organization")
public class VolunteeringOrganization {

@Id
@Column(name = "organization_id")
@AutoPopulated
@TypeDef(type = DataType.STRING)
@Schema(description = "id of the volunteering organization")
private UUID id;

@Column(name = "name")
@NotBlank
@Schema(description = "name of the volunteering organization")
private String name;

@Column(name = "description")
@NotBlank
@Schema(description = "description of the volunteering organization")
private String description;

@Column(name = "website")
@NotBlank
@Schema(description = "website for the volunteering organization")
private String website;

@Column(name = "is_active")
@Schema(description = "whether the Volunteering Organization is active")
private boolean active = true;

public VolunteeringOrganization(String name, String description, String website) {
this(null, name, description, website, true);
}

public VolunteeringOrganization(String name, String description, String website, boolean active) {
this(null, name, description, website, active);
}
}
Loading

0 comments on commit bf06d6c

Please sign in to comment.