Skip to content

Commit

Permalink
Create a re usable history page GUI template (#870)
Browse files Browse the repository at this point in the history
* feat(2): Refactored History page to a package

* chore(2): Replaced file with actual package

* chore(2): Changed Description

* chore(2): Synced ended chats sql with backoffice

* chore(2): Bumped Header Version

* chore(2): Remove Unused components

* chore(2): Removed Used component from index

* chore(2): Bumped Package Version
  • Loading branch information
1AhmedYasser authored Feb 19, 2025
1 parent 302019d commit a1f9cdc
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 1,195 deletions.
327 changes: 180 additions & 147 deletions DSL/Resql/users/get-cs-all-ended-chats.sql
Original file line number Diff line number Diff line change
@@ -1,71 +1,84 @@
WITH MaxChatHistoryComments AS (
SELECT MAX(id) AS maxId
FROM chat_history_comments
GROUP BY chat_id
SELECT MAX(id) AS maxId
FROM chat_history_comments
GROUP BY chat_id
),
ChatHistoryComments AS (
SELECT comment, chat_id
FROM chat_history_comments
JOIN MaxChatHistoryComments ON id = maxId
),
MessageWithContent AS (
SELECT
MAX(id) AS maxId,
MIN(id) AS minId
FROM message
WHERE content <> ''
AND content <> 'message-read'
GROUP BY chat_base_id
),
FirstContentMessage AS (
SELECT created, chat_base_id
FROM message
JOIN MessageWithContent ON message.id = MessageWithContent.minId
),
LastContentMessage AS (
SELECT content, chat_base_id, author_id
FROM message
JOIN MessageWithContent ON message.id = MessageWithContent.maxId
),
TitleVisibility AS (
SELECT value
FROM configuration
WHERE key = 'is_csa_title_visible'
AND NOT deleted
ORDER BY id DESC
LIMIT 1
),
FulfilledMessages AS (
SELECT MAX(id) AS maxId
FROM message
WHERE event = 'contact-information-fulfilled'
GROUP BY chat_base_id
),
ContactsMessage AS (
SELECT chat_base_id, content
FROM message
JOIN FulfilledMessages ON id = maxId
),
MaxMessages AS (
SELECT MAX(id) AS maxId
FROM message
GROUP BY chat_base_id
),
Messages AS (
SELECT event, updated, chat_base_id
FROM message
JOIN MaxMessages ON id = maxID
),
MaxChats AS (
SELECT MAX(id) AS maxId
FROM chat
WHERE ended IS NOT NULL
ChatUser AS (
SELECT DISTINCT ON (id_code)
id_code,
display_name,
first_name,
last_name
FROM "user"
ORDER BY id_code, id DESC
),
ChatHistoryComments AS (
SELECT
comment,
chat_id,
created,
author_display_name
FROM chat_history_comments
JOIN MaxChatHistoryComments ON id = maxId
),
MessageWithContent AS (
SELECT
MAX(id) AS maxId,
MIN(id) AS minId
FROM message
WHERE content <> ''
AND content <> 'message-read'
GROUP BY chat_base_id
),
FirstContentMessage AS (
SELECT created, chat_base_id
FROM message
JOIN MessageWithContent ON message.id = MessageWithContent.minId
),
LastContentMessage AS (
SELECT content, chat_base_id
FROM message
JOIN MessageWithContent ON message.id = MessageWithContent.maxId
),
TitleVisibility AS (
SELECT value
FROM configuration
WHERE key = 'is_csa_title_visible'
AND NOT deleted
ORDER BY id DESC
LIMIT 1
),
FulfilledMessages AS (
SELECT MAX(id) AS maxId
FROM message
WHERE event = 'contact-information-fulfilled'
GROUP BY chat_base_id
),
ContactsMessage AS (
SELECT chat_base_id, content
FROM message
JOIN FulfilledMessages ON id = maxId
),
MaxMessages AS (
SELECT MAX(id) AS maxId
FROM message
GROUP BY chat_base_id
),
Messages AS (
SELECT event, updated, chat_base_id, author_id
FROM message
JOIN MaxMessages ON id = maxID
),
MaxChats AS (
SELECT MAX(id) AS maxId
FROM chat
WHERE ended IS NOT NULL
AND status <> 'IDLE'
AND ended::date BETWEEN :start::date AND :end::date
GROUP BY base_id
),
EndedChatMessages AS (
SELECT
GROUP BY base_id
),
EndedChatMessages AS (
SELECT
base_id,
customer_support_id,
customer_support_display_name,
Expand All @@ -83,33 +96,33 @@ EndedChatMessages AS (
forwarded_to_name,
received_from,
labels,
feedback_rating,
created,
feedback_text,
created
FROM chat
RIGHT JOIN MaxChats ON id = maxId
),
RatedChats AS (
SELECT MAX(feedback_rating) AS rating
FROM chat
WHERE feedback_rating IS NOT NULL
GROUP BY base_id
),
RatedChatsCount AS (
SELECT COUNT(rating) AS total FROM RatedChats
),
Promoters AS (
SELECT COUNT(rating) AS p FROM RatedChats WHERE rating >= 9
),
Detractors AS (
SELECT COUNT(rating) AS d FROM RatedChats WHERE rating <= 6
),
NPS AS (
SELECT ROUND(((p / (GREATEST(total, 1) * 1.0)) - (d / (GREATEST(total, 1) * 1.0))) * 100.0, 2) AS nps
FROM RatedChatsCount
CROSS JOIN Promoters
CROSS JOIN Detractors
)
feedback_rating
FROM chat
RIGHT JOIN MaxChats ON id = maxId
),
RatedChats AS (
SELECT MAX(feedback_rating) AS rating
FROM chat
WHERE feedback_rating IS NOT NULL
GROUP BY base_id
),
RatedChatsCount AS (
SELECT COUNT(rating) AS total FROM RatedChats
),
Promoters AS (
SELECT COUNT(rating) AS p FROM RatedChats WHERE rating >= 9
),
Detractors AS (
SELECT COUNT(rating) AS d FROM RatedChats WHERE rating <= 6
),
NPS AS (
SELECT ROUND(((p / (GREATEST(total, 1) * 1.0)) - (d / (GREATEST(total, 1) * 1.0))) * 100.0, 2) AS nps
FROM RatedChatsCount
CROSS JOIN Promoters
CROSS JOIN Detractors
)
SELECT c.base_id AS id,
c.customer_support_id,
c.customer_support_display_name,
Expand All @@ -129,68 +142,88 @@ SELECT c.base_id AS id,
c.received_from,
c.labels,
s.comment,
s.created as comment_added_date,
s.author_display_name as comment_author,
mu.display_name AS user_display_name,
cu.first_name AS customer_support_first_name,
cu.last_name AS customer_support_last_name,
LastContentMessage.content AS last_message,
(CASE WHEN m.event = '' THEN NULL ELSE LOWER(m.event) END) as last_message_event,
(CASE WHEN m.event = '' THEN NULL ELSE LOWER(m.event) END) AS last_message_event,
ContactsMessage.content AS contacts_message,
m.updated AS last_message_timestamp,
c.feedback_text as feedback,
c.feedback_rating as rating,
c.feedback_text,
c.feedback_rating,
nps,
CEIL(COUNT(*) OVER() / :page_size::DECIMAL) AS total_pages
FROM EndedChatMessages AS c
JOIN Messages AS m ON c.base_id = m.chat_base_id
LEFT JOIN ChatHistoryComments AS s ON s.chat_id = m.chat_base_id
JOIN LastContentMessage ON c.base_id = LastContentMessage.chat_base_id
JOIN FirstContentMessage ON c.base_id = FirstContentMessage.chat_base_id
LEFT JOIN ContactsMessage ON ContactsMessage.chat_base_id = c.base_id
CROSS JOIN TitleVisibility
CROSS JOIN NPS
JOIN Messages AS m ON c.base_id = m.chat_base_id
LEFT JOIN ChatHistoryComments AS s ON s.chat_id = m.chat_base_id
LEFT JOIN ChatUser AS mu ON mu.id_code = m.author_id
LEFT JOIN ChatUser AS cu ON cu.id_code = c.customer_support_id
JOIN LastContentMessage ON c.base_id = LastContentMessage.chat_base_id
JOIN FirstContentMessage ON c.base_id = FirstContentMessage.chat_base_id
LEFT JOIN ContactsMessage ON ContactsMessage.chat_base_id = c.base_id
CROSS JOIN TitleVisibility
CROSS JOIN NPS
WHERE (
(
LENGTH(:customerSupportIds) = 0 OR
('-' = ANY(string_to_array(:customerSupportIds, ','))
AND (LastContentMessage.author_id IS NULL OR LastContentMessage.author_id = ''))
OR ('chatbot' = ANY(string_to_array(:customerSupportIds, ',')) AND LastContentMessage.author_id = 'chatbot')
OR c.customer_support_id = ANY(string_to_array(:customerSupportIds, ','))
) AND (
:search IS NULL OR
:search = '' OR
LOWER(c.customer_support_display_name) LIKE LOWER('%' || :search || '%') OR
LOWER(c.end_user_first_name) LIKE LOWER('%' || :search || '%') OR
LOWER(ContactsMessage.content) LIKE LOWER('%' || :search || '%') OR
LOWER(s.comment) LIKE LOWER('%' || :search || '%') OR
LOWER(c.status) LIKE LOWER('%' || :search || '%') OR
LOWER(m.event) LIKE LOWER('%' || :search || '%') OR
LOWER(c.base_id) LIKE LOWER('%' || :search || '%') OR
TO_CHAR(FirstContentMessage.created, 'DD.MM.YYYY HH24:MI:SS') LIKE '%' || :search || '%' OR
TO_CHAR(c.ended, 'DD.MM.YYYY HH24:MI:SS') LIKE '%' || :search || '%' OR
EXISTS (
SELECT 1
FROM message AS msg
WHERE msg.chat_base_id = c.base_id
AND LOWER(msg.content) LIKE LOWER('%' || :search || '%')
)
)
(
LENGTH(:customerSupportIds) = 0 OR
c.customer_support_id = ANY(string_to_array(:customerSupportIds, ','))
) AND (
:search IS NULL OR
:search = '' OR
LOWER(c.customer_support_display_name) LIKE LOWER('%' || :search || '%') OR
LOWER(c.end_user_first_name) LIKE LOWER('%' || :search || '%') OR
LOWER(ContactsMessage.content) LIKE LOWER('%' || :search || '%') OR
LOWER(s.comment) LIKE LOWER('%' || :search || '%') OR
LOWER(c.status) LIKE LOWER('%' || :search || '%') OR
LOWER(m.event) LIKE LOWER('%' || :search || '%') OR
LOWER(c.base_id) LIKE LOWER('%' || :search || '%') OR
TO_CHAR(FirstContentMessage.created, 'DD.MM.YYYY HH24:MI:SS') LIKE '%' || :search || '%' OR
TO_CHAR(c.ended, 'DD.MM.YYYY HH24:MI:SS') LIKE '%' || :search || '%' OR
EXISTS (
SELECT 1
FROM message AS msg
WHERE msg.chat_base_id = c.base_id
AND LOWER(msg.content) LIKE LOWER('%' || :search || '%')
)
)
ORDER BY
CASE WHEN :sorting = 'created asc' THEN FirstContentMessage.created END ASC,
CASE WHEN :sorting = 'created desc' THEN FirstContentMessage.created END DESC,
CASE WHEN :sorting = 'ended asc' THEN c.ended END ASC,
CASE WHEN :sorting = 'ended desc' THEN c.ended END DESC,
CASE WHEN :sorting = 'customerSupportDisplayName asc' THEN c.customer_support_display_name END ASC,
CASE WHEN :sorting = 'customerSupportDisplayName desc' THEN c.customer_support_display_name END DESC,
CASE WHEN :sorting = 'endUserName asc' THEN c.end_user_first_name END ASC,
CASE WHEN :sorting = 'endUserName desc' THEN c.end_user_first_name END DESC,
CASE WHEN :sorting = 'endUserId asc' THEN c.end_user_id END ASC,
CASE WHEN :sorting = 'endUserId desc' THEN c.end_user_id END desc,
CASE WHEN :sorting = 'contactsMessage asc' THEN ContactsMessage.content END ASC,
CASE WHEN :sorting = 'contactsMessage desc' THEN ContactsMessage.content END DESC,
CASE WHEN :sorting = 'comment asc' THEN s.comment END ASC,
CASE WHEN :sorting = 'comment desc' THEN s.comment END DESC,
CASE WHEN :sorting = 'labels asc' THEN c.labels END ASC,
CASE WHEN :sorting = 'labels desc' THEN c.labels END DESC,
CASE WHEN :sorting = 'status asc' THEN c.status END ASC,
CASE WHEN :sorting = 'status desc' THEN c.status END DESC,
CASE WHEN :sorting = 'id asc' THEN c.base_id END ASC,
CASE WHEN :sorting = 'id desc' THEN c.base_id END DESC
OFFSET ((GREATEST(:page, 1) - 1) * :page_size) LIMIT :page_size;
)
ORDER BY
CASE WHEN :sorting = 'created asc' THEN FirstContentMessage.created END ASC,
CASE WHEN :sorting = 'created desc' THEN FirstContentMessage.created END DESC,
CASE WHEN :sorting = 'ended asc' THEN c.ended END ASC,
CASE WHEN :sorting = 'ended desc' THEN c.ended END DESC,
CASE WHEN :sorting = 'customerSupportDisplayName asc' THEN c.customer_support_display_name END ASC,
CASE WHEN :sorting = 'customerSupportDisplayName desc' THEN c.customer_support_display_name END DESC,
CASE WHEN :sorting = 'endUserName asc' THEN c.end_user_first_name END ASC,
CASE WHEN :sorting = 'endUserName desc' THEN c.end_user_first_name END DESC,
CASE WHEN :sorting = 'endUserId asc' THEN c.end_user_id END ASC,
CASE WHEN :sorting = 'endUserId desc' THEN c.end_user_id END desc,
CASE WHEN :sorting = 'contactsMessage asc' THEN ContactsMessage.content END ASC,
CASE WHEN :sorting = 'contactsMessage desc' THEN ContactsMessage.content END DESC,
CASE WHEN :sorting = 'comment asc' THEN s.comment END ASC,
CASE WHEN :sorting = 'comment desc' THEN s.comment END DESC,
CASE WHEN :sorting = 'labels asc' THEN c.labels END ASC,
CASE WHEN :sorting = 'labels desc' THEN c.labels END DESC,
CASE
WHEN :sorting = 'status asc' THEN
CASE
WHEN m.event IS NULL OR m.event = '' THEN NULL
ELSE m.event
END
END ASC NULLS LAST,
CASE
WHEN :sorting = 'status desc' THEN
CASE
WHEN m.event IS NULL OR m.event = '' THEN NULL
ELSE m.event
END
END DESC NULLS LAST,
CASE WHEN :sorting = 'feedbackRating desc' THEN c.feedback_rating END DESC NULLS LAST,
CASE WHEN :sorting = 'feedbackRating asc' THEN c.feedback_rating END ASC,
CASE WHEN :sorting = 'customerSupportFullName desc' THEN (cu.first_name || ' ' || cu.last_name) END DESC NULLS LAST,
CASE WHEN :sorting = 'customerSupportFullName asc' THEN (cu.first_name || ' ' || cu.last_name) END ASC NULLS LAST,
CASE WHEN :sorting = 'id asc' THEN c.base_id END ASC,
CASE WHEN :sorting = 'id desc' THEN c.base_id END DESC
OFFSET ((GREATEST(:page, 1) - 1) * :page_size) LIMIT :page_size;
29 changes: 29 additions & 0 deletions DSL/Ruuter.private/POST/chats/get.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
declaration:
call: declare
version: 0.1
description: "Decription placeholder for 'POST'"
method: post
accepts: json
returns: json
namespace: backoffice
allowlist:
body:
- field: chatId
type: string
description: "Body field 'chatId'"

extractRequestData:
assign:
chatId: ${incoming.body.chatId}

getChatById:
call: http.post
args:
url: "[#TRAINING_USERS_RESQL]/get-chat-by-id"
body:
id: ${chatId}
result: res

return_result:
return: ${res.response.body[0]}
next: end
Loading

0 comments on commit a1f9cdc

Please sign in to comment.