From f9d101d101c8dce4893413d9da35db7af9c383d7 Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Fri, 20 Sep 2024 18:09:26 +0200 Subject: [PATCH 01/10] Include rider name in updated slack message --- .../core/slack/SlackMessageService.kt | 182 +++++++++++++++++- .../com/carbonara/core/slack/SlackService.kt | 2 +- .../core/slack/SlackWebhookController.kt | 8 +- .../carbonara/core/slack/SlackServiceTests.kt | 4 +- 4 files changed, 185 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt index 68932be..5f60d8b 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt @@ -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 @@ -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 { @@ -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) @@ -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 {} @@ -140,5 +307,6 @@ data class SlackMessageParams( val googleMapsLink: String, val productNames: List, val timeStamp: String? = null, - val orderStatus: OrderStatus? = null + val orderStatus: OrderStatus? = null, + val userName: String? = null ) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt index 5b1661f..f06dba2 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt @@ -20,7 +20,7 @@ class SlackService( orderId = orderId, orderStatus = orderStatus ) - slackMessageService.updateOrderMessageToAccepted( + slackMessageService.updateOrderMessage( SlackMessageParams( customerName = order.userName, orderId = orderId, diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 15ab0ed..6722cee 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -38,7 +38,8 @@ data class SlackWebhookRequestBody( @JsonIgnoreProperties(ignoreUnknown = true) data class SlackPayload( val actions: List, - val message: SlackMessage + val message: SlackMessage, + val user: SlackUser ) @JsonIgnoreProperties(ignoreUnknown = true) @@ -51,3 +52,8 @@ data class SlackAction( data class SlackMessage( val ts: String ) + +@JsonIgnoreProperties(ignoreUnknown = true) +data class SlackUser( + val name: String +) diff --git a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt index a79c8aa..b0560ce 100644 --- a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt +++ b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt @@ -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) } } } } From 149d3c226bd40b4cc04e703bcb46068b4b77ae4b Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Fri, 20 Sep 2024 18:18:37 +0200 Subject: [PATCH 02/10] Info log for user --- .../kotlin/com/carbonara/core/slack/SlackMessageService.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt index 5f60d8b..2198533 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt @@ -76,6 +76,9 @@ class SlackMessageService { fun updateOrderMessage( params: SlackMessageParams ) { + + log.info("Username: ${params.userName}") + val slackResponse = when(params.orderStatus) { OrderStatus.RIDER_ASSIGNED -> updateOrderMessageToAccepted(params) OrderStatus.DELIVERY_IN_PROGRESS -> updateOrderMessageToDeliveryInProgress(params) From 642b03b2191138f57405605da96132d6886e3dbe Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Fri, 20 Sep 2024 18:26:30 +0200 Subject: [PATCH 03/10] username instead of name --- .../kotlin/com/carbonara/core/slack/SlackWebhookController.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 6722cee..6f6cdcb 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -55,5 +55,5 @@ data class SlackMessage( @JsonIgnoreProperties(ignoreUnknown = true) data class SlackUser( - val name: String + val username: String ) From fb2aced9699e28527d680124b8f061c662baad20 Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 10:59:32 +0200 Subject: [PATCH 04/10] Info log slack payload --- .../com/carbonara/core/slack/SlackWebhookController.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 6f6cdcb..deb8186 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -2,6 +2,7 @@ package com.carbonara.core.slack import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import mu.KotlinLogging import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RestController @@ -15,6 +16,9 @@ class SlackDeliveryWebhookController( suspend fun handleSlackWebhook(requestBody: SlackWebhookRequestBody): ResponseEntity { val slackPayload = objectMapper.readValue(requestBody.payload, SlackPayload::class.java) + + log.info("Slack payload: $slackPayload") + slackPayload.actions.forEach { action -> slackService.handleOrderStatusUpdate( orderId = action.value, @@ -28,6 +32,7 @@ class SlackDeliveryWebhookController( companion object { private val objectMapper = jacksonObjectMapper() + private val log = KotlinLogging.logger {} } } @@ -39,12 +44,12 @@ data class SlackWebhookRequestBody( data class SlackPayload( val actions: List, val message: SlackMessage, - val user: SlackUser + val user: SlackUser // Not sure if this is here ) @JsonIgnoreProperties(ignoreUnknown = true) data class SlackAction( - val action_id: String, + val action_id: String, // Storing orderId in here val value: String, ) From 389addef4bcbfd8a26e37d37a2f7b2c87dc8a775 Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 11:12:19 +0200 Subject: [PATCH 05/10] Provide user name to slack message service --- src/main/kotlin/com/carbonara/core/slack/SlackService.kt | 6 ++++-- .../com/carbonara/core/slack/SlackWebhookController.kt | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt index f06dba2..249e348 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt @@ -13,7 +13,8 @@ class SlackService( suspend fun handleOrderStatusUpdate( orderId: String, slackOrderStatus: String, - messageTimestamp: String + messageTimestamp: String, + userName: String ) { val orderStatus = mapSlackOrderStatusToOrderStatus(slackOrderStatus) val order = orderService.updateOrderStatus( @@ -28,7 +29,8 @@ class SlackService( googleMapsLink = order.deliveryAddress.createGoogleMapsLink(), productNames = order.products.map { it.productName }, timeStamp = messageTimestamp, - orderStatus = orderStatus + orderStatus = orderStatus, + userName = userName ) ) } diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index deb8186..711940b 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -23,7 +23,8 @@ class SlackDeliveryWebhookController( slackService.handleOrderStatusUpdate( orderId = action.value, slackOrderStatus = action.action_id, - messageTimestamp = slackPayload.message.ts + messageTimestamp = slackPayload.message.ts, + userName = slackPayload.user.username ) } From 039a69f4503b60f4cb2afbf6c3070031a07afa3c Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 11:16:10 +0200 Subject: [PATCH 06/10] Fix tests --- .../com/carbonara/core/slack/SlackServiceTests.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt index b0560ce..52d12e0 100644 --- a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt +++ b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt @@ -31,7 +31,7 @@ class SlackServiceTests { OrderStatusUpdateScenario("delivered", OrderStatus.DELIVERED), OrderStatusUpdateScenario("cancelled", OrderStatus.CANCELLED) ).map { scenario -> - DynamicTest.dynamicTest("Happy case for order status update with status ${scenario.orderType}") { + DynamicTest.dynamicTest("Happy case for order status update with status ${scenario.orderStatus}") { val orderDao = createOrderDao(orderStatus = scenario.expectedOrderStatus) val slackMessageParams = SlackMessageParams( customerName = orderDao.userName, @@ -40,14 +40,20 @@ class SlackServiceTests { googleMapsLink = orderDao.deliveryAddress.createGoogleMapsLink(), productNames = orderDao.products.map { it.productName }, orderStatus = scenario.expectedOrderStatus, - timeStamp = "1726842841" + timeStamp = "1726842841", + userName = "sherlock.holmes" ) coEvery { orderService.updateOrderStatus(any(), any()) } returns orderDao coEvery { slackMessageService.updateOrderMessage(any()) } returns Unit runBlocking { - slackService.handleOrderStatusUpdate(orderDao.orderId.toString(), scenario.orderType, "1726842841") + slackService.handleOrderStatusUpdate( + orderId = orderDao.orderId.toString(), + slackOrderStatus = scenario.orderStatus, + messageTimestamp = "1726842841", + userName = "sherlock.holmes" + ) } coVerify { orderService.updateOrderStatus(orderDao.orderId.toString(), scenario.expectedOrderStatus) } @@ -57,6 +63,6 @@ class SlackServiceTests { } data class OrderStatusUpdateScenario( - val orderType: String, + val orderStatus: String, val expectedOrderStatus: OrderStatus ) From 9a5a9c993fc1c1a394bfbcfc9ae4a221e547b0bc Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 11:27:37 +0200 Subject: [PATCH 07/10] Tag user with id instead of name --- .../kotlin/com/carbonara/core/slack/SlackWebhookController.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 711940b..80c9f86 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -24,7 +24,7 @@ class SlackDeliveryWebhookController( orderId = action.value, slackOrderStatus = action.action_id, messageTimestamp = slackPayload.message.ts, - userName = slackPayload.user.username + userName = slackPayload.user.id ) } @@ -61,5 +61,5 @@ data class SlackMessage( @JsonIgnoreProperties(ignoreUnknown = true) data class SlackUser( - val username: String + val id: String ) From 274dc9b32969848b6db0c7ec7cc33d6a189c0c2b Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 11:39:04 +0200 Subject: [PATCH 08/10] Info log and update all slack messages --- .../core/slack/SlackMessageService.kt | 23 ++++++++++++++----- .../com/carbonara/core/slack/SlackService.kt | 4 ++-- .../core/slack/SlackWebhookController.kt | 9 ++++---- .../carbonara/core/slack/SlackServiceTests.kt | 4 ++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt index 2198533..6213719 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt @@ -76,9 +76,6 @@ class SlackMessageService { fun updateOrderMessage( params: SlackMessageParams ) { - - log.info("Username: ${params.userName}") - val slackResponse = when(params.orderStatus) { OrderStatus.RIDER_ASSIGNED -> updateOrderMessageToAccepted(params) OrderStatus.DELIVERY_IN_PROGRESS -> updateOrderMessageToDeliveryInProgress(params) @@ -119,7 +116,7 @@ class SlackMessageService { } section { fields { - markdownText("*Rider:*\n<@${params.userName}>") + markdownText("*Rider:*\n<@${params.slackUserId}>") } } actions { @@ -171,6 +168,11 @@ class SlackMessageService { markdownText("*Products:*\n${params.productNames.joinToString(", ")}") } } + section { + fields { + markdownText("*Rider:*\n<@${params.slackUserId}>") + } + } actions { button { text("ACCEPT", emoji = true) @@ -220,6 +222,11 @@ class SlackMessageService { markdownText("*Products:*\n${params.productNames.joinToString(", ")}") } } + section { + fields { + markdownText("*Rider:*\n<@${params.slackUserId}>") + } + } actions { button { text("ACCEPT", emoji = true) @@ -269,6 +276,11 @@ class SlackMessageService { markdownText("*Products:*\n${params.productNames.joinToString(", ")}") } } + section { + fields { + markdownText("*Rider:*\n<@${params.slackUserId}>") + } + } actions { button { text("ACCEPT", emoji = true) @@ -282,7 +294,6 @@ class SlackMessageService { } button { text("DELIVERED", emoji = true) - style("primary") value(params.orderId) actionId("delivered") } @@ -311,5 +322,5 @@ data class SlackMessageParams( val productNames: List, val timeStamp: String? = null, val orderStatus: OrderStatus? = null, - val userName: String? = null + val slackUserId: String? = null ) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt index 249e348..c882038 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt @@ -14,7 +14,7 @@ class SlackService( orderId: String, slackOrderStatus: String, messageTimestamp: String, - userName: String + slackUserId: String ) { val orderStatus = mapSlackOrderStatusToOrderStatus(slackOrderStatus) val order = orderService.updateOrderStatus( @@ -30,7 +30,7 @@ class SlackService( productNames = order.products.map { it.productName }, timeStamp = messageTimestamp, orderStatus = orderStatus, - userName = userName + slackUserId = slackUserId ) ) } diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 80c9f86..1364c4a 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -16,15 +16,15 @@ class SlackDeliveryWebhookController( suspend fun handleSlackWebhook(requestBody: SlackWebhookRequestBody): ResponseEntity { val slackPayload = objectMapper.readValue(requestBody.payload, SlackPayload::class.java) - - log.info("Slack payload: $slackPayload") + log.info("Received slack webhook for orderId=${slackPayload.actions.first().value} " + + "and userName=${slackPayload.user.userName}") slackPayload.actions.forEach { action -> slackService.handleOrderStatusUpdate( orderId = action.value, slackOrderStatus = action.action_id, messageTimestamp = slackPayload.message.ts, - userName = slackPayload.user.id + slackUserId = slackPayload.user.id ) } @@ -61,5 +61,6 @@ data class SlackMessage( @JsonIgnoreProperties(ignoreUnknown = true) data class SlackUser( - val id: String + val id: String, + val userName: String ) diff --git a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt index 52d12e0..41cf1c7 100644 --- a/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt +++ b/src/test/kotlin/com/carbonara/core/slack/SlackServiceTests.kt @@ -41,7 +41,7 @@ class SlackServiceTests { productNames = orderDao.products.map { it.productName }, orderStatus = scenario.expectedOrderStatus, timeStamp = "1726842841", - userName = "sherlock.holmes" + slackUserId = "sherlock.holmes" ) coEvery { orderService.updateOrderStatus(any(), any()) } returns orderDao @@ -52,7 +52,7 @@ class SlackServiceTests { orderId = orderDao.orderId.toString(), slackOrderStatus = scenario.orderStatus, messageTimestamp = "1726842841", - userName = "sherlock.holmes" + slackUserId = "sherlock.holmes" ) } From 3d8df175608a2a96e9e6f781787a4928135c105f Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 11:46:45 +0200 Subject: [PATCH 09/10] Fix slack request body webhook --- .../kotlin/com/carbonara/core/slack/SlackWebhookController.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt index 1364c4a..6ff71b9 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackWebhookController.kt @@ -17,7 +17,7 @@ class SlackDeliveryWebhookController( val slackPayload = objectMapper.readValue(requestBody.payload, SlackPayload::class.java) log.info("Received slack webhook for orderId=${slackPayload.actions.first().value} " + - "and userName=${slackPayload.user.userName}") + "and userName=${slackPayload.user.username}") slackPayload.actions.forEach { action -> slackService.handleOrderStatusUpdate( @@ -62,5 +62,5 @@ data class SlackMessage( @JsonIgnoreProperties(ignoreUnknown = true) data class SlackUser( val id: String, - val userName: String + val username: String ) From aa34325acf1a928ec83369694ebcccb985247020 Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 23 Sep 2024 12:13:48 +0200 Subject: [PATCH 10/10] Add unassign button --- .../core/slack/SlackMessageService.kt | 73 +++++++++++++++++++ .../com/carbonara/core/slack/SlackService.kt | 1 + 2 files changed, 74 insertions(+) diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt index 6213719..075a22c 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt @@ -81,6 +81,7 @@ class SlackMessageService { OrderStatus.DELIVERY_IN_PROGRESS -> updateOrderMessageToDeliveryInProgress(params) OrderStatus.DELIVERED -> updateOrderMessageToDelivered(params) OrderStatus.CANCELLED -> updateOrderMessageToCancelled(params) + OrderStatus.FINDING_AVAILABLE_RIDER -> updateOrderMessageToUnassigned(params) else -> throw IllegalArgumentException("Cannot update slack message based on order status ${params.orderStatus}") } @@ -141,6 +142,11 @@ class SlackMessageService { value(params.orderId) actionId("cancelled") } + button { + text("UNASSIGN", emoji = true) + value(params.orderId) + actionId("unassign") + } } divider() } @@ -195,6 +201,11 @@ class SlackMessageService { value(params.orderId) actionId("cancelled") } + button { + text("UNASSIGN", emoji = true) + value(params.orderId) + actionId("unassign") + } } divider() } @@ -249,6 +260,11 @@ class SlackMessageService { value(params.orderId) actionId("cancelled") } + button { + text("UNASSIGN", emoji = true) + value(params.orderId) + actionId("unassign") + } } divider() } @@ -303,6 +319,63 @@ class SlackMessageService { value(params.orderId) actionId("cancelled") } + button { + text("UNASSIGN", emoji = true) + value(params.orderId) + actionId("unassign") + } + } + divider() + } + } + } + + fun updateOrderMessageToUnassigned( + 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) + style("primary") + 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) + style("primary") + value(params.orderId) + actionId("delivered") + } + button { + text("CANCELLED", emoji = true) + style("danger") + value(params.orderId) + actionId("cancelled") + } } divider() } diff --git a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt index c882038..ae1459f 100644 --- a/src/main/kotlin/com/carbonara/core/slack/SlackService.kt +++ b/src/main/kotlin/com/carbonara/core/slack/SlackService.kt @@ -43,6 +43,7 @@ class SlackService( "delivery_in_progress" -> OrderStatus.DELIVERY_IN_PROGRESS "delivered" -> OrderStatus.DELIVERED "cancelled" -> OrderStatus.CANCELLED + "unassign" -> OrderStatus.FINDING_AVAILABLE_RIDER else -> throw IllegalArgumentException("Invalid slack order status: $slackOrderStatus") } }