diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt index 41666ef..bf8b674 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt @@ -178,12 +178,15 @@ class GroupSessionController { observations.add(observation) } GroupSessionManager.invalidate() - GlobalScope.launch { - if (::mailer.isInitialized) - mailer.sendObservationData(observations) - else - logger.error("Mailer has not been initialized.") - } + if(this::environment.isInitialized && environment.getProperty("smtp.autosendoncomplete")!!.toBoolean()) + GlobalScope.launch { + if (::mailer.isInitialized) + mailer.sendObservationData(observations) + else + logger.error("Mailer has not been initialized.") + } + else + logger.debug("Environment or Mailer is unavailable") websocketMessenger.convertAndSend("/ws/status", mapOf("status" to "complete")) return mapOf("success" to "The submission was successfully completed.") } else if (!GroupSessionManager.dataComplete()) { diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt index 3ccdb41..e66ccd6 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsController.kt @@ -19,6 +19,7 @@ import uk.co.neviyn.observationdatabase.AfiPieChartDataset import uk.co.neviyn.observationdatabase.AverageData import uk.co.neviyn.observationdatabase.ChartData import uk.co.neviyn.observationdatabase.ChartDataset +import uk.co.neviyn.observationdatabase.Email import uk.co.neviyn.observationdatabase.NameValue import uk.co.neviyn.observationdatabase.NewObservation import uk.co.neviyn.observationdatabase.NewSite @@ -50,6 +51,9 @@ class ObservationsController { lateinit var observationRepository: ObservationRepository @Autowired lateinit var personRepository: PersonRepository + @Autowired + lateinit var mailer: Email + /** * Returns a list of all current sites. @@ -272,4 +276,22 @@ class ObservationsController { } return AfiPieChart(AfiPieChartDataset(monitoring, knowledge, control, conservatism, teamwork)) } + + /** + * Send current observation as a csv file to the configured email address + */ + @PostMapping("/observations/email") + fun sendObservationsByEmail(@Valid @RequestBody observationsRequest: ObservationsRequest): Boolean { + logger.info("Sending Observation data by email") + if (::mailer.isInitialized) { + val data = getObservations(observationsRequest) + if(data.isEmpty()) + return false + mailer.sendObservationData(data) + return true + } + else + logger.error("Mailer has not been initialized.") + return false + } } \ No newline at end of file diff --git a/backend/src/main/resources/META-INF/spring-configuration-metadata.json b/backend/src/main/resources/META-INF/spring-configuration-metadata.json index 328f28f..238310f 100644 --- a/backend/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/backend/src/main/resources/META-INF/spring-configuration-metadata.json @@ -27,6 +27,10 @@ { "name": "smtp.toaddress", "type": "java.lang.String" + }, + { + "name": "smtp.autosendoncomplete", + "type": "java.lang.Boolean" } ] } \ No newline at end of file diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 34d00d7..cdefd4b 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -11,4 +11,5 @@ smtp.server=x smtp.port=587 smtp.username=none smtp.password=none -smtp.toaddress=none \ No newline at end of file +smtp.toaddress=none +smtp.autosendoncomplete=false \ No newline at end of file diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt index 375aeed..037e263 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt @@ -15,6 +15,7 @@ import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.junit.MockitoJUnitRunner import org.springframework.web.server.ResponseStatusException +import uk.co.neviyn.observationdatabase.Email import uk.co.neviyn.observationdatabase.NameValue import uk.co.neviyn.observationdatabase.NewObservation import uk.co.neviyn.observationdatabase.NewSite @@ -47,6 +48,8 @@ class ObservationsControllerTest { lateinit var observationRepository: ObservationRepository @Mock lateinit var personRepository: PersonRepository + @Mock + lateinit var mailer: Email @Test fun testGetSites() {