Skip to content

Commit

Permalink
Add notif. when accepting priv. channel join reqs
Browse files Browse the repository at this point in the history
[Re #1625]

Now we notify (via email) the user when it is accepted after he has
requested to join a Forum's private channel.

In order to simplify the process, we expect the `channel_name` to be
provided as part of the endpoint used for the point above, since this
information is already available in the front-end, so we avoid to get
it in an additional request to Rocket.chat.
  • Loading branch information
joseAyudarte91 committed Oct 5, 2023
1 parent 34dcb20 commit 0319f74
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 77 deletions.
18 changes: 15 additions & 3 deletions backend/src/gpml/handler/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[gpml.handler.resource.permission :as h.r.permission]
[gpml.handler.responses :as r]
[gpml.service.chat :as srv.chat]
[gpml.util.email :as email]
[integrant.core :as ig]))

(def ^:private channel-types
Expand Down Expand Up @@ -38,6 +39,12 @@
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]
[:channel_name
{:optional false
:swagger {:description "The channel name"
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]
[:user_id
{:optional false
:swagger {:description "The user's identifier in GPML"
Expand Down Expand Up @@ -170,15 +177,20 @@
(r/server-error (dissoc result :success?)))))

(defn- add-user-to-private-channel
[{:keys [db] :as config} parameters]
(let [{:keys [channel_id user_id]} (:body parameters)
[{:keys [db mailjet-config] :as config} parameters]
(let [{:keys [channel_id channel_name user_id]} (:body parameters)
target-user (db.stakeholder/get-stakeholder-by-id (:spec db) {:id user_id})]
(if (seq target-user)
(let [result (srv.chat/add-user-to-private-channel config
(:chat_account_id target-user)
channel_id)]
(if (:success? result)
(r/ok {})
(do
(email/notify-user-about-chat-private-channel-invitation-request-accepted
mailjet-config
target-user
channel_name)
(r/ok {}))
(r/server-error (dissoc result :success?))))
(r/server-error {:reason :user-not-found}))))

Expand Down
183 changes: 109 additions & 74 deletions backend/src/gpml/util/email.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns gpml.util.email
(:require [clj-http.client :as client]
[clojure.string :as str]
[gpml.db.stakeholder :as db.stakeholder]
[gpml.handler.util :as util]
[jsonista.core :as j]))
[clojure.string :as str]
[gpml.db.stakeholder :as db.stakeholder]
[gpml.handler.util :as util]
[jsonista.core :as j]))

(defn make-message [sender receiver subject text html]
{:From sender :To [receiver] :Subject subject :TextPart text :HTMLPart html})
Expand All @@ -16,10 +16,10 @@
(defn send-email [{:keys [api-key secret-key]} sender subject receivers texts htmls]
(let [messages (map make-message (repeat sender) receivers (repeat subject) texts htmls)]
(client/post "https://api.mailjet.com/v3.1/send"
{:basic-auth [api-key secret-key]
:content-type :json
:throw-exceptions false
:body (j/write-value-as-string {:Messages messages})})))
{:basic-auth [api-key secret-key]
:content-type :json
:throw-exceptions false
:body (j/write-value-as-string {:Messages messages})})))

;; FIXME: this shouldn't be hardcoded here. We'll be moving to
;; mailchimp soon so we'll refactor everything here.
Expand Down Expand Up @@ -51,7 +51,7 @@ A new subscription request has arrived from %s.

(defn notify-expert-invitation-text [first-name last-name invitation-id app-domain]
(let [platform-link (str app-domain "/login?invite=" invitation-id)
user-full-name (get-user-full-name {:first_name first-name :last_name last-name})]
user-full-name (get-user-full-name {:first_name first-name :last_name last-name})]
(format "Dear %s,
You have been invited to join the UNEP GPML Digital Platform as an expert.
Expand Down Expand Up @@ -86,9 +86,9 @@ Your submission has been published to %s/%s/%s.
- UNEP GPML Digital Platform
"
(:app-domain mailjet-config)
(util/get-api-topic-type topic-type topic-item)
(:id topic-item)))
(:app-domain mailjet-config)
(util/get-api-topic-type topic-type topic-item)
(:id topic-item)))

(defn notify-user-review-rejected-text [mailjet-config topic-type topic-item]
(format "Dear user,
Expand All @@ -100,33 +100,49 @@ again, please visit this URL: %s/edit-%s/%s
- UNEP GPML Digital Platform
"
(util/get-title topic-type topic-item)
(:app-domain mailjet-config)
(-> (util/get-api-topic-type topic-type topic-item)
(str/replace "_" "-"))
(:id topic-item)))
(util/get-title topic-type topic-item)
(:app-domain mailjet-config)
(-> (util/get-api-topic-type topic-type topic-item)
(str/replace "_" "-"))
(:id topic-item)))

(defn notify-user-review-subject [mailjet-config review-status topic-type topic-item]
(format "[%s] %s %s"
(:app-name mailjet-config)
(util/get-display-topic-type topic-type topic-item)
(str/lower-case review-status)))
(:app-name mailjet-config)
(util/get-display-topic-type topic-type topic-item)
(str/lower-case review-status)))

(defn notify-private-channel-invitation-request-subject
[app-name channel-name]
(format "[%s] Invitation request for private channel %s" app-name channel-name))

(defn notify-user-about-chat-private-channel-invitation-request-accepted-subject
[app-name channel-name]
(format "[%s] You've joined %s" app-name channel-name))

(defn notify-private-channel-invitation-request-text
[admin-name user-name user-email channel-name]
(format "Dear %s
%s user with email %s, is requesting access to the private channel %s.
- UNEP GPML Digital Platform"
admin-name
user-name
user-email
channel-name))
admin-name
user-name
user-email
channel-name))

(defn notify-user-about-chat-private-channel-invitation-request-accepted-text
[channel-name base-url]
(format "Your request to join %s channel on the GPML platform was approved.
View the forums in your GPML workspace:
%s/forum
- UNEP GPML Digital Platform"
channel-name
base-url))

(defn notify-user-invitation-text [inviter-name app-domain entity-name]
(format "Dear user,
Expand All @@ -144,18 +160,18 @@ again, please visit this URL: %s/edit-%s/%s

(defn notify-admins-pending-approval [db mailjet-config new-item]
(let [admins (db.stakeholder/get-admins db)
item-type (:type new-item)
item-title (if (= item-type "stakeholder")
(get-user-full-name new-item)
(or (:title new-item) (:name new-item) (:tag new-item)))
subject (format "[%s] New %s needs approval" (:app-name mailjet-config) item-type)
sender unep-sender
names (map get-user-full-name admins)
receivers (map #(assoc {} :Name %1 :Email (:email %2)) names admins)
texts (->> names (map #(format notify-admins-pending-approval-text
%1 item-type item-title
(:app-domain mailjet-config))))
htmls (repeat nil)]
item-type (:type new-item)
item-title (if (= item-type "stakeholder")
(get-user-full-name new-item)
(or (:title new-item) (:name new-item) (:tag new-item)))
subject (format "[%s] New %s needs approval" (:app-name mailjet-config) item-type)
sender unep-sender
names (map get-user-full-name admins)
receivers (map #(assoc {} :Name %1 :Email (:email %2)) names admins)
texts (->> names (map #(format notify-admins-pending-approval-text
%1 item-type item-title
(:app-domain mailjet-config))))
htmls (repeat nil)]
(when (> (count receivers) 0)
(send-email mailjet-config sender subject receivers texts htmls))))

Expand All @@ -166,11 +182,11 @@ again, please visit this URL: %s/edit-%s/%s
`texts` a collection of a single element, since in this case we are sending a single message."
[mailjet-config dest-email req-email]
(let [subject (format "[%s] New subscription request" (:app-name mailjet-config))
sender unep-sender
receivers [{:Name "GPML Secretariat"
:Email dest-email}]
texts [(format notify-secretariat-new-subscription-text req-email)]
htmls (repeat nil)]
sender unep-sender
receivers [{:Name "GPML Secretariat"
:Email dest-email}]
texts [(format notify-secretariat-new-subscription-text req-email)]
htmls (repeat nil)]
(send-email mailjet-config sender subject receivers texts htmls)))

(defn notify-about-new-contact
Expand All @@ -179,41 +195,60 @@ again, please visit this URL: %s/edit-%s/%s
to be sent. That is why we provide an infinite sequence for non-used `htmls` option. Besides, we make `sender` and
`texts` a collection of a single element, since in this case we are sending a single message."
[mailjet-config {dest-email :dest-email
req-email :email
name :name
organization :organization
msg :message
subject :subject}]
req-email :email
name :name
organization :organization
msg :message
subject :subject}]
(let [msg-body (format "Name: %s\nEmail: %s\nOrganization: %s\nMessage: \n%s"
name
req-email
organization
msg)
sender unep-sender
receivers [{:Name "Contact Management"
:Email dest-email}]
texts [msg-body]
htmls (repeat nil)]
name
req-email
organization
msg)
sender unep-sender
receivers [{:Name "Contact Management"
:Email dest-email}]
texts [msg-body]
htmls (repeat nil)]
(send-email mailjet-config sender subject receivers texts htmls)))

(defn notify-admins-new-chat-private-channel-invitation-request
[mailjet-config admins user channel-name]
(let [sender unep-sender
subject (notify-private-channel-invitation-request-subject
(:app-name mailjet-config)
channel-name)
receivers (map
(fn [admin] {:Name (get-user-full-name admin)
:Email (:email admin)})
admins)
texts (map (fn [receiver]
(notify-private-channel-invitation-request-text (:Name receiver)
(get-user-full-name user)
(:email user)
channel-name))
receivers)
htmls (repeat nil)
{:keys [status body]} (send-email mailjet-config sender subject receivers texts htmls)]
subject (notify-private-channel-invitation-request-subject
(:app-name mailjet-config)
channel-name)
receivers (map
(fn [admin] {:Name (get-user-full-name admin)
:Email (:email admin)})
admins)
texts (map (fn [receiver]
(notify-private-channel-invitation-request-text (:Name receiver)
(get-user-full-name user)
(:email user)
channel-name))
receivers)
htmls (repeat nil)
{:keys [status body]} (send-email mailjet-config sender subject receivers texts htmls)]
(if (<= 200 status 299)
{:success? true}
{:success? false
:reason :failed-to-send-email
:error-details body})))

(defn notify-user-about-chat-private-channel-invitation-request-accepted
[mailjet-config user channel-name]
(let [sender unep-sender
subject (notify-user-about-chat-private-channel-invitation-request-accepted-subject
(:app-name mailjet-config)
channel-name)
receivers [{:Name (get-user-full-name user)
:Email (:email user)}]
texts [(notify-user-about-chat-private-channel-invitation-request-accepted-text
channel-name
(:app-domain mailjet-config))]
htmls (repeat nil)
{:keys [status body]} (send-email mailjet-config sender subject receivers texts htmls)]
(if (<= 200 status 299)
{:success? true}
{:success? false
Expand All @@ -223,8 +258,8 @@ again, please visit this URL: %s/edit-%s/%s
(comment
(require 'dev)
(let [db (dev/db-conn)
config {:api-key (System/getenv "MAILJET_API_KEY")
:secret-key (System/getenv "MAILJET_SECRET_KEY")
:app-name (System/getenv "APP_NAME")
:app-domain (System/getenv "APP_DOMAIN")}]
config {:api-key (System/getenv "MAILJET_API_KEY")
:secret-key (System/getenv "MAILJET_SECRET_KEY")
:app-name (System/getenv "APP_NAME")
:app-domain (System/getenv "APP_DOMAIN")}]
(notify-admins-pending-approval db config {:type "stakeholder" :title "Mr" :first_name "Puneeth" :last_name "Chaganti"})))

0 comments on commit 0319f74

Please sign in to comment.