Skip to content

Commit

Permalink
Include rider name in updated slack message
Browse files Browse the repository at this point in the history
  • Loading branch information
KjellBerlin committed Sep 20, 2024
1 parent 0d2841e commit f9d101d
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 11 deletions.
182 changes: 175 additions & 7 deletions src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.carbonara.core.slack
import com.carbonara.core.order.OrderStatus
import com.slack.api.Slack
import com.slack.api.methods.kotlin_extension.request.chat.blocks
import com.slack.api.methods.response.chat.ChatUpdateResponse
import mu.KotlinLogging
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -72,13 +73,32 @@ class SlackMessageService {
}
}

// TODO: Update depending on actual status
fun updateOrderMessageToAccepted(
fun updateOrderMessage(
params: SlackMessageParams
) {
val slackResponse = when(params.orderStatus) {
OrderStatus.RIDER_ASSIGNED -> updateOrderMessageToAccepted(params)
OrderStatus.DELIVERY_IN_PROGRESS -> updateOrderMessageToDeliveryInProgress(params)
OrderStatus.DELIVERED -> updateOrderMessageToDelivered(params)
OrderStatus.CANCELLED -> updateOrderMessageToCancelled(params)
else -> throw IllegalArgumentException("Cannot update slack message based on order status ${params.orderStatus}")
}

if (slackResponse == null) {
log.error("Slack API error: Slack response null")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: Slack response null")
} else if (!slackResponse.isOk) {
log.error("Slack API error: ${slackResponse.error}")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: ${slackResponse.error}")
}
}

private fun updateOrderMessageToAccepted(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
val response = slack.methods(slackToken).chatUpdate { req -> req
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
Expand All @@ -94,6 +114,11 @@ class SlackMessageService {
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
section {
fields {
markdownText("*Rider:*\n<@${params.userName}>")
}
}
actions {
button {
text("ACCEPT", emoji = true)
Expand All @@ -120,13 +145,155 @@ class SlackMessageService {
divider()
}
}
}

if (!response.isOk) {
log.error("Slack API error: ${response.error}")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: ${response.error}")
private fun updateOrderMessageToDeliveryInProgress(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
style("primary")
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
value(params.orderId)
actionId("cancelled")
}
}
divider()
}
}
}

private fun updateOrderMessageToDelivered(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
style("primary")
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
value(params.orderId)
actionId("cancelled")
}
}
divider()
}
}
}

private fun updateOrderMessageToCancelled(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
style("primary")
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
style("danger")
value(params.orderId)
actionId("cancelled")
}
}
divider()
}
}
}

companion object {
private val log = KotlinLogging.logger {}
Expand All @@ -140,5 +307,6 @@ data class SlackMessageParams(
val googleMapsLink: String,
val productNames: List<String>,
val timeStamp: String? = null,
val orderStatus: OrderStatus? = null
val orderStatus: OrderStatus? = null,
val userName: String? = null
)
2 changes: 1 addition & 1 deletion src/main/kotlin/com/carbonara/core/slack/SlackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SlackService(
orderId = orderId,
orderStatus = orderStatus
)
slackMessageService.updateOrderMessageToAccepted(
slackMessageService.updateOrderMessage(
SlackMessageParams(
customerName = order.userName,
orderId = orderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ data class SlackWebhookRequestBody(
@JsonIgnoreProperties(ignoreUnknown = true)
data class SlackPayload(
val actions: List<SlackAction>,
val message: SlackMessage
val message: SlackMessage,
val user: SlackUser
)

@JsonIgnoreProperties(ignoreUnknown = true)
Expand All @@ -51,3 +52,8 @@ data class SlackAction(
data class SlackMessage(
val ts: String
)

@JsonIgnoreProperties(ignoreUnknown = true)
data class SlackUser(
val name: String
)
4 changes: 2 additions & 2 deletions src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class SlackServiceTests {
)

coEvery { orderService.updateOrderStatus(any(), any()) } returns orderDao
coEvery { slackMessageService.updateOrderMessageToAccepted(any()) } returns Unit
coEvery { slackMessageService.updateOrderMessage(any()) } returns Unit

runBlocking {
slackService.handleOrderStatusUpdate(orderDao.orderId.toString(), scenario.orderType, "1726842841")
}

coVerify { orderService.updateOrderStatus(orderDao.orderId.toString(), scenario.expectedOrderStatus) }
coVerify { slackMessageService.updateOrderMessageToAccepted(slackMessageParams) }
coVerify { slackMessageService.updateOrderMessage(slackMessageParams) }
}
}
}
Expand Down

0 comments on commit f9d101d

Please sign in to comment.