Skip to content

Commit 505ce0a

Browse files
CayoPOliveiragabrieljablonski
authored andcommitted
feat: add name to webhook (#4)
* feat(migration): add name column to webhook table * feat(webhooks): add name parameter to webhook params * feat(webhooks): add example webhook name constant and input field to form * fix(webhooks): add webhook name label and placeholder to multiple locales in the form * feat(webhooks): display webhook name in the UI and include it in the API response * Revert 'fix(webhooks): add webhook name label and placeholder to multiple locales in the form' This reverts commit e547778. * test(webhooks): add tests for creating and updating webhooks with name attribute * chore(webhooks): add name property to webhook definitions in Swagger documentation * chore(webhooks): remove unnecessary input touch event for webhook name field * chore(webhooks): apply review changes requested * chore(webhooks): revert auto lint changes in commit 18ec4ca
1 parent 33f80b4 commit 505ce0a

File tree

13 files changed

+67
-5
lines changed

13 files changed

+67
-5
lines changed

app/controllers/api/v1/accounts/webhooks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def destroy
2323
private
2424

2525
def webhook_params
26-
params.require(:webhook).permit(:inbox_id, :url, subscriptions: [])
26+
params.require(:webhook).permit(:inbox_id, :name, :url, subscriptions: [])
2727
end
2828

2929
def fetch_webhook

app/javascript/dashboard/i18n/locale/en/integrations.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"CONTACT_UPDATED": "Contact updated"
3030
}
3131
},
32+
"NAME": {
33+
"LABEL": "Webhook Name",
34+
"PLACEHOLDER": "Enter the name of the webhook"
35+
},
3236
"END_POINT": {
3337
"LABEL": "Webhook URL",
3438
"PLACEHOLDER": "Example: {webhookExampleURL}",

app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookForm.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default {
4949
data() {
5050
return {
5151
url: this.value.url || '',
52+
name: this.value.name || '',
5253
subscriptions: this.value.subscriptions || [],
5354
supportedWebhookEvents: SUPPORTED_WEBHOOK_EVENTS,
5455
};
@@ -62,11 +63,15 @@ export default {
6263
}
6364
);
6465
},
66+
webhookNameInputPlaceholder() {
67+
return this.$t('INTEGRATION_SETTINGS.WEBHOOK.FORM.NAME.PLACEHOLDER');
68+
},
6569
},
6670
methods: {
6771
onSubmit() {
6872
this.$emit('submit', {
6973
url: this.url,
74+
name: this.name,
7075
subscriptions: this.subscriptions,
7176
});
7277
},
@@ -91,6 +96,15 @@ export default {
9196
{{ $t('INTEGRATION_SETTINGS.WEBHOOK.FORM.END_POINT.ERROR') }}
9297
</span>
9398
</label>
99+
<label>
100+
{{ $t('INTEGRATION_SETTINGS.WEBHOOK.FORM.NAME.LABEL') }}
101+
<input
102+
v-model="name"
103+
type="text"
104+
name="name"
105+
:placeholder="webhookNameInputPlaceholder"
106+
/>
107+
</label>
94108
<label :class="{ error: v$.url.$error }" class="mb-2">
95109
{{ $t('INTEGRATION_SETTINGS.WEBHOOK.FORM.SUBSCRIPTIONS.LABEL') }}
96110
</label>

app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookRow.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ const subscribedEvents = computed(() => {
3535
<template>
3636
<tr>
3737
<td class="py-4 ltr:pr-4 rtl:pl-4">
38-
<div class="font-medium break-words text-slate-700 dark:text-slate-100">
39-
{{ webhook.url }}
38+
<div
39+
class="flex gap-2 font-medium break-words text-slate-700 dark:text-slate-100"
40+
>
41+
<template v-if="webhook.name">
42+
{{ webhook.name }}
43+
<span class="text-slate-500 dark:text-slate-400">
44+
{{ webhook.url }}
45+
</span>
46+
</template>
47+
<template v-else>
48+
{{ webhook.url }}
49+
</template>
4050
</div>
4151
<div class="block mt-1 text-sm text-slate-500 dark:text-slate-400">
4252
<span class="font-medium">

app/models/webhook.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Table name: webhooks
44
#
55
# id :bigint not null, primary key
6+
# name :string
67
# subscriptions :jsonb
78
# url :string
89
# webhook_type :integer default("account_type")

app/views/api/v1/accounts/webhooks/_webhook.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
json.id webhook.id
2+
json.name webhook.name
23
json.url webhook.url
34
json.account_id webhook.account_id
45
json.subscriptions webhook.subscriptions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddNameToWebhooks < ActiveRecord::Migration[7.0]
2+
def change
3+
add_column :webhooks, :name, :string, null: true
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@
10501050
t.datetime "updated_at", null: false
10511051
t.integer "webhook_type", default: 0
10521052
t.jsonb "subscriptions", default: ["conversation_status_changed", "conversation_updated", "conversation_created", "contact_created", "contact_updated", "message_created", "message_updated", "webwidget_triggered"]
1053+
t.string "name"
10531054
t.index ["account_id", "url"], name: "index_webhooks_on_account_id_and_url", unique: true
10541055
end
10551056

spec/controllers/api/v1/accounts/webhook_controller_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe 'Webhooks API', type: :request do
44
let(:account) { create(:account) }
55
let(:inbox) { create(:inbox, account: account) }
6-
let(:webhook) { create(:webhook, account: account, inbox: inbox, url: 'https://hello.com') }
6+
let(:webhook) { create(:webhook, account: account, inbox: inbox, url: 'https://hello.com', name: 'My Webhook') }
77
let(:administrator) { create(:user, account: account, role: :administrator) }
88
let(:agent) { create(:user, account: account, role: :agent) }
99

@@ -49,6 +49,16 @@
4949
expect(response.parsed_body['payload']['webhook']['url']).to eql 'https://hello.com'
5050
end
5151

52+
it 'creates webhook with name' do
53+
post "/api/v1/accounts/#{account.id}/webhooks",
54+
params: { account_id: account.id, inbox_id: inbox.id, url: 'https://hello.com', name: 'My Webhook' },
55+
headers: administrator.create_new_auth_token,
56+
as: :json
57+
expect(response).to have_http_status(:success)
58+
59+
expect(response.parsed_body['payload']['webhook']['name']).to eql 'My Webhook'
60+
end
61+
5262
it 'throws error when invalid url provided' do
5363
post "/api/v1/accounts/#{account.id}/webhooks",
5464
params: { account_id: account.id, inbox_id: inbox.id, url: 'javascript:alert(1)' },
@@ -103,11 +113,12 @@
103113
context 'when it is an authenticated admin user' do
104114
it 'updates webhook' do
105115
put "/api/v1/accounts/#{account.id}/webhooks/#{webhook.id}",
106-
params: { url: 'https://hello.com' },
116+
params: { url: 'https://hello.com', name: 'Another Webhook' },
107117
headers: administrator.create_new_auth_token,
108118
as: :json
109119
expect(response).to have_http_status(:success)
110120
expect(response.parsed_body['payload']['webhook']['url']).to eql 'https://hello.com'
121+
expect(response.parsed_body['payload']['webhook']['name']).to eql 'Another Webhook'
111122
end
112123
end
113124
end

spec/factories/webhooks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
account_id { 1 }
44
inbox_id { 1 }
55
url { 'https://api.chatwoot.com' }
6+
name { 'My Webhook' }
67
subscriptions do
78
%w[
89
conversation_status_changed

0 commit comments

Comments
 (0)