Skip to content

Commit

Permalink
chore: Add support for message_templates in API inbox (#4835)
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavrajs committed Jun 10, 2022
1 parent 9bac587 commit 3f3ee6c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ export default {
return false;
},
hasWhatsappTemplates() {
return !!this.inbox.message_templates;
return !!this.$store.getters['inboxes/getWhatsAppTemplates'](this.inboxId)
.length;
},
enterToSendEnabled() {
return !!this.uiSettings.enter_to_send_enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@

<script>
import { mapGetters } from 'vuex';
import { required } from 'vuelidate/lib/validators';
import { shouldBeUrl } from 'shared/helpers/Validators';
import configMixin from 'shared/mixins/configMixin';
import alertMixin from 'shared/mixins/alertMixin';
Expand Down Expand Up @@ -562,7 +561,6 @@ export default {
},
validations: {
webhookUrl: {
required,
shouldBeUrl,
},
},
Expand Down
15 changes: 13 additions & 2 deletions app/javascript/dashboard/store/modules/inboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,20 @@ export const getters = {
const [inbox] = $state.records.filter(
record => record.id === Number(inboxId)
);

const {
message_templates: whatsAppMessageTemplates,
additional_attributes: additionalAttributes,
} = inbox;

const { message_templates: apiInboxMessageTemplates } =
additionalAttributes || {};
const messagesTemplates =
whatsAppMessageTemplates || apiInboxMessageTemplates;

// filtering out the whatsapp templates with media
if (inbox.message_templates) {
return inbox.message_templates.filter(template => {
if (messagesTemplates) {
return messagesTemplates.filter(template => {
return !template.components.some(
i => i.format === 'IMAGE' || i.format === 'VIDEO'
);
Expand Down
19 changes: 10 additions & 9 deletions app/models/channel/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#
# Table name: channel_api
#
# id :bigint not null, primary key
# hmac_mandatory :boolean default(FALSE)
# hmac_token :string
# identifier :string
# webhook_url :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# id :bigint not null, primary key
# additional_attributes :jsonb
# hmac_mandatory :boolean default(FALSE)
# hmac_token :string
# identifier :string
# webhook_url :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
Expand All @@ -21,7 +22,7 @@ class Channel::Api < ApplicationRecord
include Channelable

self.table_name = 'channel_api'
EDITABLE_ATTRS = [:webhook_url].freeze
EDITABLE_ATTRS = [:webhook_url, { additional_attributes: {} }].freeze

has_secure_token :identifier
has_secure_token :hmac_token
Expand Down
15 changes: 8 additions & 7 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,19 @@ def merge_sender_attributes(data)

def webhook_data
{
id: id,
account: account.webhook_data,
additional_attributes: additional_attributes,
content_attributes: content_attributes,
content_type: content_type,
content: content,
conversation: conversation.webhook_data,
created_at: created_at,
id: id,
inbox: inbox.webhook_data,
message_type: message_type,
content_type: content_type,
private: private,
content_attributes: content_attributes,
source_id: source_id,
sender: sender.try(:webhook_data),
inbox: inbox.webhook_data,
conversation: conversation.webhook_data,
account: account.webhook_data
source_id: source_id
}
end

Expand Down
1 change: 1 addition & 0 deletions app/views/api/v1/models/_inbox.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ end
if resource.api?
json.webhook_url resource.channel.try(:webhook_url)
json.inbox_identifier resource.channel.try(:identifier)
json.additional_attributes resource.channel.try(:additional_attributes)
end

### WhatsApp Channel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdditionalAttributesToApiChannel < ActiveRecord::Migration[6.1]
def change
add_column :channel_api, :additional_attributes, :jsonb, default: {}
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_05_25_141844) do
ActiveRecord::Schema.define(version: 2022_06_10_091206) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -205,6 +205,7 @@
t.string "identifier"
t.string "hmac_token"
t.boolean "hmac_mandatory", default: false
t.jsonb "additional_attributes", default: {}
t.index ["hmac_token"], name: "index_channel_api_on_hmac_token", unique: true
t.index ["identifier"], name: "index_channel_api_on_identifier", unique: true
end
Expand Down

0 comments on commit 3f3ee6c

Please sign in to comment.