more codes
This commit is contained in:
@@ -101,6 +101,18 @@ class EmailStorage(
|
||||
return path.toString()
|
||||
}
|
||||
|
||||
fun storeUploadedPdf(propertyId: UUID, originalName: String?, bytes: ByteArray): String {
|
||||
val dir = Paths.get(rootPath, propertyId.toString(), "manual")
|
||||
Files.createDirectories(dir)
|
||||
val safeName = (originalName ?: UUID.randomUUID().toString()).replace(Regex("[^A-Za-z0-9._-]"), "_")
|
||||
val fileName = "${safeName}_${OffsetDateTime.now().toEpochSecond()}.pdf"
|
||||
val path = dir.resolve(fileName).normalize()
|
||||
val tmp = dir.resolve("${fileName}.tmp").normalize()
|
||||
Files.write(tmp, bytes)
|
||||
atomicMove(tmp, path)
|
||||
return path.toString()
|
||||
}
|
||||
|
||||
private fun atomicMove(tmp: Path, target: Path) {
|
||||
try {
|
||||
Files.move(tmp, target, java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.android.trisolarisserver.component
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import java.awt.image.BufferedImage
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
import javax.imageio.ImageIO
|
||||
|
||||
@Component
|
||||
class RoomImageStorage(
|
||||
@Value("\${storage.rooms.root:/home/androidlover5842/docs/rooms}")
|
||||
private val rootPath: String
|
||||
) {
|
||||
fun store(propertyId: UUID, roomId: UUID, file: MultipartFile): StoredRoomImage {
|
||||
val contentType = file.contentType ?: ""
|
||||
if (!contentType.startsWith("image/")) {
|
||||
throw IllegalArgumentException("Only image files are allowed")
|
||||
}
|
||||
val bytes = file.bytes
|
||||
val originalName = file.originalFilename ?: UUID.randomUUID().toString()
|
||||
val ext = extensionFor(contentType, originalName)
|
||||
|
||||
val dir = Paths.get(rootPath, propertyId.toString(), roomId.toString())
|
||||
Files.createDirectories(dir)
|
||||
val base = UUID.randomUUID().toString() + "_" + OffsetDateTime.now().toEpochSecond()
|
||||
val originalPath = dir.resolve("$base.$ext")
|
||||
val originalTmp = dir.resolve("$base.$ext.tmp")
|
||||
Files.write(originalTmp, bytes)
|
||||
atomicMove(originalTmp, originalPath)
|
||||
|
||||
val image = readImage(bytes)
|
||||
?: throw IllegalArgumentException("Unsupported image")
|
||||
val thumb = resize(image, 320)
|
||||
val thumbExt = if (ext.lowercase() == "jpg") "jpg" else "png"
|
||||
val thumbPath = dir.resolve("${base}_thumb.$thumbExt")
|
||||
val thumbTmp = dir.resolve("${base}_thumb.$thumbExt.tmp")
|
||||
ByteArrayInputStream(render(thumb, thumbExt)).use { input ->
|
||||
Files.copy(input, thumbTmp)
|
||||
}
|
||||
atomicMove(thumbTmp, thumbPath)
|
||||
|
||||
return StoredRoomImage(
|
||||
originalPath = originalPath.toString(),
|
||||
thumbnailPath = thumbPath.toString(),
|
||||
contentType = contentType,
|
||||
sizeBytes = bytes.size.toLong()
|
||||
)
|
||||
}
|
||||
|
||||
private fun readImage(bytes: ByteArray): BufferedImage? {
|
||||
return ByteArrayInputStream(bytes).use { input -> ImageIO.read(input) }
|
||||
}
|
||||
|
||||
private fun resize(input: BufferedImage, maxSize: Int): BufferedImage {
|
||||
val width = input.width
|
||||
val height = input.height
|
||||
if (width <= maxSize && height <= maxSize) return input
|
||||
val scale = if (width > height) maxSize.toDouble() / width else maxSize.toDouble() / height
|
||||
val newW = (width * scale).toInt()
|
||||
val newH = (height * scale).toInt()
|
||||
val output = BufferedImage(newW, newH, BufferedImage.TYPE_INT_RGB)
|
||||
val g = output.createGraphics()
|
||||
g.drawImage(input, 0, 0, newW, newH, null)
|
||||
g.dispose()
|
||||
return output
|
||||
}
|
||||
|
||||
private fun render(image: BufferedImage, format: String): ByteArray {
|
||||
val out = java.io.ByteArrayOutputStream()
|
||||
ImageIO.write(image, format, out)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
private fun extensionFor(contentType: String, filename: String): String {
|
||||
return when {
|
||||
contentType.contains("png", true) -> "png"
|
||||
contentType.contains("jpeg", true) || contentType.contains("jpg", true) -> "jpg"
|
||||
filename.contains(".") -> filename.substringAfterLast('.').lowercase()
|
||||
else -> "png"
|
||||
}
|
||||
}
|
||||
|
||||
private fun atomicMove(tmp: Path, target: Path) {
|
||||
try {
|
||||
Files.move(tmp, target, java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
|
||||
} catch (_: Exception) {
|
||||
Files.move(tmp, target, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class StoredRoomImage(
|
||||
val originalPath: String,
|
||||
val thumbnailPath: String,
|
||||
val contentType: String,
|
||||
val sizeBytes: Long
|
||||
)
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.EmailStorage
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.db.repo.InboundEmailRepo
|
||||
import com.android.trisolarisserver.models.booking.InboundEmail
|
||||
import com.android.trisolarisserver.models.booking.InboundEmailStatus
|
||||
import com.android.trisolarisserver.models.property.Role
|
||||
import com.android.trisolarisserver.repo.PropertyRepo
|
||||
import com.android.trisolarisserver.security.MyPrincipal
|
||||
import com.android.trisolarisserver.service.EmailIngestionService
|
||||
import org.apache.pdfbox.pdmodel.PDDocument
|
||||
import org.apache.pdfbox.text.PDFTextStripper
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/properties/{propertyId}/inbound-emails")
|
||||
class InboundEmailManual(
|
||||
private val propertyAccess: PropertyAccess,
|
||||
private val propertyRepo: PropertyRepo,
|
||||
private val inboundEmailRepo: InboundEmailRepo,
|
||||
private val emailStorage: EmailStorage,
|
||||
private val emailIngestionService: EmailIngestionService
|
||||
) {
|
||||
|
||||
@PostMapping("/manual")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
fun uploadManualPdf(
|
||||
@PathVariable propertyId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
@RequestParam("file") file: MultipartFile
|
||||
): ManualInboundResponse {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
propertyAccess.requireAnyRole(propertyId, principal.userId, Role.ADMIN, Role.MANAGER)
|
||||
|
||||
if (file.isEmpty) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "File is empty")
|
||||
}
|
||||
val contentType = file.contentType
|
||||
if (contentType != null && !contentType.equals("application/pdf", ignoreCase = true)) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Only PDF is supported")
|
||||
}
|
||||
|
||||
val property = propertyRepo.findById(propertyId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
|
||||
val bytes = file.bytes
|
||||
val pdfPath = emailStorage.storeUploadedPdf(propertyId, file.originalFilename, bytes)
|
||||
val text = extractPdfText(bytes)
|
||||
|
||||
val inbound = InboundEmail(
|
||||
property = property,
|
||||
messageId = "manual-${UUID.randomUUID()}",
|
||||
subject = file.originalFilename ?: "manual-upload",
|
||||
fromAddress = null,
|
||||
receivedAt = OffsetDateTime.now(),
|
||||
status = InboundEmailStatus.PENDING,
|
||||
rawPdfPath = pdfPath
|
||||
)
|
||||
|
||||
inboundEmailRepo.save(inbound)
|
||||
emailIngestionService.ingestManualPdf(property, inbound, text)
|
||||
return ManualInboundResponse(inboundId = inbound.id!!)
|
||||
}
|
||||
|
||||
private fun extractPdfText(bytes: ByteArray): String {
|
||||
return try {
|
||||
PDDocument.load(bytes).use { doc ->
|
||||
PDFTextStripper().getText(doc)
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
private fun requirePrincipal(principal: MyPrincipal?) {
|
||||
if (principal == null) {
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing principal")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ManualInboundResponse(
|
||||
val inboundId: UUID
|
||||
)
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.component.RoomImageStorage
|
||||
import com.android.trisolarisserver.controller.dto.RoomImageResponse
|
||||
import com.android.trisolarisserver.models.room.RoomImage
|
||||
import com.android.trisolarisserver.models.property.Role
|
||||
import com.android.trisolarisserver.repo.RoomImageRepo
|
||||
import com.android.trisolarisserver.repo.RoomRepo
|
||||
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.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.multipart.MultipartFile
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.UUID
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/properties/{propertyId}/rooms/{roomId}/images")
|
||||
class RoomImages(
|
||||
private val propertyAccess: PropertyAccess,
|
||||
private val roomRepo: RoomRepo,
|
||||
private val roomImageRepo: RoomImageRepo,
|
||||
private val storage: RoomImageStorage,
|
||||
@org.springframework.beans.factory.annotation.Value("\${storage.rooms.publicBaseUrl}")
|
||||
private val publicBaseUrl: String
|
||||
) {
|
||||
|
||||
@GetMapping
|
||||
fun list(
|
||||
@PathVariable propertyId: UUID,
|
||||
@PathVariable roomId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?
|
||||
): List<RoomImageResponse> {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
ensureRoom(propertyId, roomId)
|
||||
return roomImageRepo.findByRoomIdOrderByCreatedAtDesc(roomId)
|
||||
.map { it.toResponse(publicBaseUrl) }
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
fun upload(
|
||||
@PathVariable propertyId: UUID,
|
||||
@PathVariable roomId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
@RequestParam("file") file: MultipartFile
|
||||
): RoomImageResponse {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
propertyAccess.requireAnyRole(propertyId, principal.userId, Role.ADMIN, Role.MANAGER)
|
||||
val room = ensureRoom(propertyId, roomId)
|
||||
|
||||
if (file.isEmpty) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "File is empty")
|
||||
}
|
||||
val stored = try {
|
||||
storage.store(propertyId, roomId, file)
|
||||
} catch (ex: IllegalArgumentException) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, ex.message ?: "Invalid image")
|
||||
}
|
||||
|
||||
val image = RoomImage(
|
||||
property = room.property,
|
||||
room = room,
|
||||
originalPath = stored.originalPath,
|
||||
thumbnailPath = stored.thumbnailPath,
|
||||
contentType = stored.contentType,
|
||||
sizeBytes = stored.sizeBytes
|
||||
)
|
||||
return roomImageRepo.save(image).toResponse(publicBaseUrl)
|
||||
}
|
||||
|
||||
@GetMapping("/{imageId}/file")
|
||||
fun file(
|
||||
@PathVariable propertyId: UUID,
|
||||
@PathVariable roomId: UUID,
|
||||
@PathVariable imageId: UUID,
|
||||
@RequestParam(required = false, defaultValue = "full") size: String,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?
|
||||
): ResponseEntity<FileSystemResource> {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
ensureRoom(propertyId, roomId)
|
||||
|
||||
val image = roomImageRepo.findByIdAndRoomIdAndPropertyId(imageId, roomId, propertyId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Image not found")
|
||||
val path = if (size.equals("thumb", true)) image.thumbnailPath else image.originalPath
|
||||
val file = Paths.get(path)
|
||||
if (!Files.exists(file)) {
|
||||
throw ResponseStatusException(HttpStatus.NOT_FOUND, "File missing")
|
||||
}
|
||||
val resource = FileSystemResource(file)
|
||||
val type = image.contentType
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType(type))
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"room-${imageId}.${file.fileName}\"")
|
||||
.contentLength(resource.contentLength())
|
||||
.body(resource)
|
||||
}
|
||||
|
||||
private fun ensureRoom(propertyId: UUID, roomId: UUID): com.android.trisolarisserver.models.room.Room {
|
||||
return roomRepo.findByIdAndPropertyId(roomId, propertyId)
|
||||
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room not found")
|
||||
}
|
||||
|
||||
private fun requirePrincipal(principal: MyPrincipal?) {
|
||||
if (principal == null) {
|
||||
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing principal")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun RoomImage.toResponse(baseUrl: String): RoomImageResponse {
|
||||
val id = id ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Image id missing")
|
||||
return RoomImageResponse(
|
||||
id = id,
|
||||
propertyId = property.id!!,
|
||||
roomId = room.id!!,
|
||||
url = "$baseUrl/properties/${property.id}/rooms/${room.id}/images/$id/file",
|
||||
thumbnailUrl = "$baseUrl/properties/${property.id}/rooms/${room.id}/images/$id/file?size=thumb",
|
||||
contentType = contentType,
|
||||
sizeBytes = sizeBytes,
|
||||
createdAt = createdAt.toString()
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import com.android.trisolarisserver.component.PropertyAccess
|
||||
import com.android.trisolarisserver.controller.dto.RoomAvailabilityRangeResponse
|
||||
import com.android.trisolarisserver.controller.dto.RoomAvailabilityResponse
|
||||
import com.android.trisolarisserver.controller.dto.RoomBoardResponse
|
||||
import com.android.trisolarisserver.controller.dto.RoomBoardStatus
|
||||
@@ -25,6 +26,8 @@ import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import java.util.UUID
|
||||
|
||||
@RestController
|
||||
@@ -106,6 +109,43 @@ class Rooms(
|
||||
}.sortedBy { it.roomTypeName }
|
||||
}
|
||||
|
||||
@GetMapping("/availability-range")
|
||||
fun roomAvailabilityRange(
|
||||
@PathVariable propertyId: UUID,
|
||||
@AuthenticationPrincipal principal: MyPrincipal?,
|
||||
@org.springframework.web.bind.annotation.RequestParam("from") from: String,
|
||||
@org.springframework.web.bind.annotation.RequestParam("to") to: String
|
||||
): List<RoomAvailabilityRangeResponse> {
|
||||
requirePrincipal(principal)
|
||||
propertyAccess.requireMember(propertyId, principal!!.userId)
|
||||
|
||||
val property = propertyRepo.findById(propertyId).orElseThrow {
|
||||
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
||||
}
|
||||
|
||||
val fromDate = parseDate(from)
|
||||
val toDate = parseDate(to)
|
||||
if (!toDate.isAfter(fromDate)) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date range")
|
||||
}
|
||||
val zone = ZoneId.of(property.timezone)
|
||||
val fromAt = fromDate.atStartOfDay(zone).toOffsetDateTime()
|
||||
val toAt = toDate.atStartOfDay(zone).toOffsetDateTime()
|
||||
|
||||
val rooms = roomRepo.findByPropertyIdOrderByRoomNumber(propertyId)
|
||||
val occupiedRoomIds = roomStayRepo.findOccupiedRoomIdsBetween(propertyId, fromAt, toAt).toHashSet()
|
||||
|
||||
val freeRooms = rooms.filter { it.active && !it.maintenance && !occupiedRoomIds.contains(it.id) }
|
||||
val grouped = freeRooms.groupBy { it.roomType.name }
|
||||
return grouped.entries.map { (typeName, roomList) ->
|
||||
RoomAvailabilityRangeResponse(
|
||||
roomTypeName = typeName,
|
||||
freeRoomNumbers = roomList.map { it.roomNumber },
|
||||
freeCount = roomList.size
|
||||
)
|
||||
}.sortedBy { it.roomTypeName }
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
fun createRoom(
|
||||
@@ -182,6 +222,14 @@ class Rooms(
|
||||
val privileged = setOf(Role.ADMIN, Role.MANAGER, Role.STAFF, Role.HOUSEKEEPING, Role.FINANCE)
|
||||
return roles.none { it in privileged }
|
||||
}
|
||||
|
||||
private fun parseDate(value: String): LocalDate {
|
||||
return try {
|
||||
LocalDate.parse(value.trim())
|
||||
} catch (_: Exception) {
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date format")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Room.toRoomResponse(): RoomResponse {
|
||||
|
||||
@@ -25,6 +25,23 @@ data class RoomAvailabilityResponse(
|
||||
val freeRoomNumbers: List<Int>
|
||||
)
|
||||
|
||||
data class RoomAvailabilityRangeResponse(
|
||||
val roomTypeName: String,
|
||||
val freeRoomNumbers: List<Int>,
|
||||
val freeCount: Int
|
||||
)
|
||||
|
||||
data class RoomImageResponse(
|
||||
val id: UUID,
|
||||
val propertyId: UUID,
|
||||
val roomId: UUID,
|
||||
val url: String,
|
||||
val thumbnailUrl: String,
|
||||
val contentType: String,
|
||||
val sizeBytes: Long,
|
||||
val createdAt: String
|
||||
)
|
||||
|
||||
enum class RoomBoardStatus {
|
||||
FREE,
|
||||
OCCUPIED,
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.android.trisolarisserver.models.room
|
||||
|
||||
import com.android.trisolarisserver.models.property.Property
|
||||
import jakarta.persistence.*
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
|
||||
@Entity
|
||||
@Table(name = "room_image")
|
||||
class RoomImage(
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(columnDefinition = "uuid")
|
||||
val id: UUID? = null,
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "property_id", nullable = false)
|
||||
var property: Property,
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "room_id", nullable = false)
|
||||
var room: Room,
|
||||
|
||||
@Column(name = "original_path", nullable = false)
|
||||
var originalPath: String,
|
||||
|
||||
@Column(name = "thumbnail_path", nullable = false)
|
||||
var thumbnailPath: String,
|
||||
|
||||
@Column(name = "content_type", nullable = false)
|
||||
var contentType: String,
|
||||
|
||||
@Column(name = "size_bytes", nullable = false)
|
||||
var sizeBytes: Long,
|
||||
|
||||
@Column(name = "created_at", nullable = false, columnDefinition = "timestamptz")
|
||||
val createdAt: OffsetDateTime = OffsetDateTime.now()
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.android.trisolarisserver.repo
|
||||
|
||||
import com.android.trisolarisserver.models.room.RoomImage
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import java.util.UUID
|
||||
|
||||
interface RoomImageRepo : JpaRepository<RoomImage, UUID> {
|
||||
fun findByRoomIdOrderByCreatedAtDesc(roomId: UUID): List<RoomImage>
|
||||
fun findByIdAndRoomIdAndPropertyId(id: UUID, roomId: UUID, propertyId: UUID): RoomImage?
|
||||
}
|
||||
@@ -14,4 +14,17 @@ interface RoomStayRepo : JpaRepository<RoomStay, UUID> {
|
||||
and rs.toAt is null
|
||||
""")
|
||||
fun findOccupiedRoomIds(@Param("propertyId") propertyId: UUID): List<UUID>
|
||||
|
||||
@Query("""
|
||||
select distinct rs.room.id
|
||||
from RoomStay rs
|
||||
where rs.property.id = :propertyId
|
||||
and rs.fromAt < :toAt
|
||||
and (rs.toAt is null or rs.toAt > :fromAt)
|
||||
""")
|
||||
fun findOccupiedRoomIdsBetween(
|
||||
@Param("propertyId") propertyId: UUID,
|
||||
@Param("fromAt") fromAt: java.time.OffsetDateTime,
|
||||
@Param("toAt") toAt: java.time.OffsetDateTime
|
||||
): List<UUID>
|
||||
}
|
||||
|
||||
@@ -115,57 +115,7 @@ class EmailIngestionService(
|
||||
return
|
||||
}
|
||||
|
||||
val extracted = extractBookingDetails(body)
|
||||
inbound.extractedData = objectMapper.writeValueAsString(extracted)
|
||||
|
||||
val otaBookingId = extracted["otaBookingId"]?.takeIf { !it.contains("NONE", true) }
|
||||
if (!otaBookingId.isNullOrBlank() &&
|
||||
inboundEmailRepo.existsByPropertyIdAndOtaBookingId(property.id!!, otaBookingId)
|
||||
) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
inbound.otaBookingId = otaBookingId
|
||||
inboundEmailRepo.save(inbound)
|
||||
|
||||
val isCancel = extracted["isCancel"]?.contains("YES", ignoreCase = true) == true
|
||||
if (isCancel) {
|
||||
if (otaBookingId.isNullOrBlank()) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
val booking = bookingRepo.findByPropertyIdAndSourceBookingId(property.id!!, otaBookingId)
|
||||
if (booking != null) {
|
||||
booking.status = BookingStatus.CANCELLED
|
||||
bookingRepo.save(booking)
|
||||
inbound.booking = booking
|
||||
}
|
||||
inbound.status = InboundEmailStatus.CANCELLED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
val sourceBookingId = otaBookingId ?: "email:$messageId"
|
||||
if (bookingRepo.existsByPropertyIdAndSourceBookingId(property.id!!, sourceBookingId)) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
val guest = resolveGuest(property, extracted)
|
||||
val emailUrl = "${publicBaseUrl}/properties/${property.id}/inbound-emails/${inbound.id}/file"
|
||||
val booking = createBooking(property, guest, extracted, sourceBookingId, emailUrl)
|
||||
inbound.booking = booking
|
||||
inbound.status = InboundEmailStatus.CREATED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
handleExtracted(property, inbound, body, "email:$messageId")
|
||||
}
|
||||
|
||||
private fun extractBookingDetails(body: String): Map<String, String> {
|
||||
@@ -241,6 +191,65 @@ class EmailIngestionService(
|
||||
return bookingRepo.save(booking)
|
||||
}
|
||||
|
||||
fun ingestManualPdf(property: Property, inbound: InboundEmail, body: String) {
|
||||
inboundEmailRepo.save(inbound)
|
||||
handleExtracted(property, inbound, body, "manual:${inbound.id}")
|
||||
}
|
||||
|
||||
private fun handleExtracted(property: Property, inbound: InboundEmail, body: String, fallbackKey: String) {
|
||||
val extracted = extractBookingDetails(body)
|
||||
inbound.extractedData = objectMapper.writeValueAsString(extracted)
|
||||
|
||||
val otaBookingId = extracted["otaBookingId"]?.takeIf { !it.contains("NONE", true) }
|
||||
if (!otaBookingId.isNullOrBlank() &&
|
||||
inboundEmailRepo.existsByPropertyIdAndOtaBookingId(property.id!!, otaBookingId)
|
||||
) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
inbound.otaBookingId = otaBookingId
|
||||
inboundEmailRepo.save(inbound)
|
||||
|
||||
val isCancel = extracted["isCancel"]?.contains("YES", ignoreCase = true) == true
|
||||
if (isCancel) {
|
||||
if (otaBookingId.isNullOrBlank()) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
val booking = bookingRepo.findByPropertyIdAndSourceBookingId(property.id!!, otaBookingId)
|
||||
if (booking != null) {
|
||||
booking.status = BookingStatus.CANCELLED
|
||||
bookingRepo.save(booking)
|
||||
inbound.booking = booking
|
||||
}
|
||||
inbound.status = InboundEmailStatus.CANCELLED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
val sourceBookingId = otaBookingId ?: fallbackKey
|
||||
if (bookingRepo.existsByPropertyIdAndSourceBookingId(property.id!!, sourceBookingId)) {
|
||||
inbound.status = InboundEmailStatus.SKIPPED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
return
|
||||
}
|
||||
|
||||
val guest = resolveGuest(property, extracted)
|
||||
val emailUrl = "${publicBaseUrl}/properties/${property.id}/inbound-emails/${inbound.id}/file"
|
||||
val booking = createBooking(property, guest, extracted, sourceBookingId, emailUrl)
|
||||
inbound.booking = booking
|
||||
inbound.status = InboundEmailStatus.CREATED
|
||||
inbound.processedAt = OffsetDateTime.now()
|
||||
inboundEmailRepo.save(inbound)
|
||||
}
|
||||
|
||||
private fun buildMessageId(message: Message): String? {
|
||||
val header = message.getHeader("Message-ID")?.firstOrNull()
|
||||
if (!header.isNullOrBlank()) return header
|
||||
|
||||
@@ -11,6 +11,8 @@ storage.documents.tokenSecret=change-me
|
||||
storage.documents.tokenTtlSeconds=300
|
||||
storage.emails.root=/home/androidlover5842/docs/emails
|
||||
storage.emails.publicBaseUrl=https://api.hoteltrisolaris.in
|
||||
storage.rooms.root=/home/androidlover5842/docs/rooms
|
||||
storage.rooms.publicBaseUrl=https://api.hoteltrisolaris.in
|
||||
mail.imap.host=localhost
|
||||
mail.imap.port=993
|
||||
mail.imap.protocol=imaps
|
||||
|
||||
Reference in New Issue
Block a user