Added config to auto send email on session completion. Added route for manual mail sending.

This commit is contained in:
neviyn 2019-07-23 09:44:25 +01:00
parent 93b0f68721
commit 449c4cf3b1
5 changed files with 40 additions and 7 deletions

View File

@ -178,12 +178,15 @@ class GroupSessionController {
observations.add(observation)
}
GroupSessionManager.invalidate()
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()) {

View File

@ -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
}
}

View File

@ -27,6 +27,10 @@
{
"name": "smtp.toaddress",
"type": "java.lang.String"
},
{
"name": "smtp.autosendoncomplete",
"type": "java.lang.Boolean"
}
]
}

View File

@ -12,3 +12,4 @@ smtp.port=587
smtp.username=none
smtp.password=none
smtp.toaddress=none
smtp.autosendoncomplete=false

View File

@ -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() {