filter mails by property contact alias
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.db.repo.InboundEmailRepo
|
||||
import com.android.trisolarisserver.models.property.Role
|
||||
import com.android.trisolarisserver.security.MyPrincipal
|
||||
import org.springframework.core.io.FileSystemResource
|
||||
import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.UUID
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/properties/{propertyId}/inbound-emails")
|
||||
class InboundEmails(
|
||||
private val propertyAccess: PropertyAccess,
|
||||
private val inboundEmailRepo: InboundEmailRepo
|
||||
) {
|
||||
|
||||
@GetMapping("/{emailId}/file")
|
||||
fun downloadEmailPdf(
|
||||
@PathVariable propertyId: UUID,
|
||||
@PathVariable emailId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?
|
||||
): ResponseEntity<FileSystemResource> {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
propertyAccess.requireAnyRole(propertyId, principal.userId, Role.ADMIN, Role.MANAGER)
|
||||
|
||||
val email = inboundEmailRepo.findByIdAndPropertyId(emailId, propertyId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Email not found")
|
||||
val path = email.rawPdfPath ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Email PDF missing")
|
||||
val file = Paths.get(path)
|
||||
if (!Files.exists(file)) {
|
||||
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Email PDF missing")
|
||||
}
|
||||
val resource = FileSystemResource(file)
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"email-${emailId}.pdf\"")
|
||||
.contentLength(resource.contentLength())
|
||||
.body(resource)
|
||||
}
|
||||
|
||||
private fun requirePrincipal(principal: MyPrincipal?) {
|
||||
if (principal == null) {
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing principal")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,11 +41,13 @@ class Orgs(
|
||||
}
|
||||
val org = Organization().apply {
|
||||
name = request.name
|
||||
emailAliases = request.emailAliases?.toMutableSet() ?: mutableSetOf()
|
||||
}
|
||||
val saved = orgRepo.save(org)
|
||||
return OrgResponse(
|
||||
id = saved.id ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Org id missing"),
|
||||
name = saved.name ?: ""
|
||||
name = saved.name ?: "",
|
||||
emailAliases = saved.emailAliases.toSet()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -63,7 +65,8 @@ class Orgs(
|
||||
}
|
||||
return OrgResponse(
|
||||
id = org.id ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Org id missing"),
|
||||
name = org.name ?: ""
|
||||
name = org.name ?: "",
|
||||
emailAliases = org.emailAliases.toSet()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,8 @@ class Properties(
|
||||
timezone = request.timezone ?: "Asia/Kolkata",
|
||||
currency = request.currency ?: "INR",
|
||||
active = request.active ?: true,
|
||||
otaAliases = request.otaAliases?.toMutableSet() ?: mutableSetOf()
|
||||
otaAliases = request.otaAliases?.toMutableSet() ?: mutableSetOf(),
|
||||
emailAddresses = request.emailAddresses?.toMutableSet() ?: mutableSetOf()
|
||||
)
|
||||
val saved = propertyRepo.save(property)
|
||||
return saved.toResponse()
|
||||
@@ -211,6 +212,9 @@ class Properties(
|
||||
if (request.otaAliases != null) {
|
||||
property.otaAliases = request.otaAliases.toMutableSet()
|
||||
}
|
||||
if (request.emailAddresses != null) {
|
||||
property.emailAddresses = request.emailAddresses.toMutableSet()
|
||||
}
|
||||
|
||||
return propertyRepo.save(property).toResponse()
|
||||
}
|
||||
@@ -249,7 +253,8 @@ private fun Property.toResponse(): PropertyResponse {
|
||||
timezone = timezone,
|
||||
currency = currency,
|
||||
active = active,
|
||||
otaAliases = otaAliases.toSet()
|
||||
otaAliases = otaAliases.toSet(),
|
||||
emailAddresses = emailAddresses.toSet()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@ package com.android.trisolarisserver.controller.dto
|
||||
import java.util.UUID
|
||||
|
||||
data class OrgCreateRequest(
|
||||
val name: String
|
||||
val name: String,
|
||||
val emailAliases: Set<String>? = null
|
||||
)
|
||||
|
||||
data class OrgResponse(
|
||||
val id: UUID,
|
||||
val name: String
|
||||
val name: String,
|
||||
val emailAliases: Set<String>
|
||||
)
|
||||
|
||||
data class PropertyCreateRequest(
|
||||
@@ -18,7 +20,8 @@ data class PropertyCreateRequest(
|
||||
val timezone: String? = null,
|
||||
val currency: String? = null,
|
||||
val active: Boolean? = null,
|
||||
val otaAliases: Set<String>? = null
|
||||
val otaAliases: Set<String>? = null,
|
||||
val emailAddresses: Set<String>? = null
|
||||
)
|
||||
|
||||
data class PropertyUpdateRequest(
|
||||
@@ -28,7 +31,8 @@ data class PropertyUpdateRequest(
|
||||
val timezone: String? = null,
|
||||
val currency: String? = null,
|
||||
val active: Boolean? = null,
|
||||
val otaAliases: Set<String>? = null
|
||||
val otaAliases: Set<String>? = null,
|
||||
val emailAddresses: Set<String>? = null
|
||||
)
|
||||
|
||||
data class PropertyResponse(
|
||||
@@ -40,7 +44,8 @@ data class PropertyResponse(
|
||||
val timezone: String,
|
||||
val currency: String,
|
||||
val active: Boolean,
|
||||
val otaAliases: Set<String>
|
||||
val otaAliases: Set<String>,
|
||||
val emailAddresses: Set<String>
|
||||
)
|
||||
|
||||
data class UserResponse(
|
||||
|
||||
Reference in New Issue
Block a user