Created
January 5, 2023 05:40
-
-
Save vitroz/0e8e1c71d64ed8f416c791b69cc95d63 to your computer and use it in GitHub Desktop.
required data to run smile-webhook consumers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'config/environment' | |
require_relative 'config/application' | |
def auth_context | |
AuthenticatedContext.new(partner_user: third_party_dev) | |
end | |
def system_auth_context | |
AuthenticatedContext.new(system:true) | |
end | |
def developer_account_seed | |
DeveloperAccountSeed.run(auth_context: auth_context) | |
end | |
def create_webhook(topic) | |
webhooks = Webhook.where(webhook_params) | |
if webhooks.size > 0 | |
webhooks.each do |webhook| | |
if webhook.topic == topic | |
Rails.logger.info("\nThere is already a webhook for this app #{webhook_params[:app_id]} and this topic: #{topic}") | |
return webhook | |
end | |
end | |
end | |
webhook = Webhook.new(webhook_params) | |
new_webhook = Webhooks::Create.run(webhook: webhook, auth_context: auth_context) | |
new_webhook | |
end | |
def create_integration_to_third_party_app | |
new_integration = NewIntegration.find_by(app_id: third_party_app.id, account_id: dev_account.id) | |
return if new_integration.present? | |
sql = "INSERT INTO new_integrations | |
(app_id, account_id, authentication_token, created_at, updated_at) | |
VALUES (#{third_party_app.id}, #{dev_account.id}, 'auth_sample_token', 'Jan 01, 2023, 12:20 PM', 'Jan 01, 2023, 12:20 PM') | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nNew integration to third party app added: #{result}\n") | |
end | |
def create_webhook_event_definitions_webhooks(webhook, event_definition) | |
params = { | |
:webhook_id => webhook.id, | |
:webhook_event_definition_id => event_definition.id | |
} | |
sql = "SELECT * FROM webhook_event_definitions_webhooks | |
WHERE webhook_id = #{params[:webhook_id]} AND | |
webhook_event_definition_id = #{params[:webhook_event_definition_id]} | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nCurrent webhook_event_definitions_webhooks: #{result}\n") | |
return if result.present? | |
sql = "INSERT INTO webhook_event_definitions_webhooks | |
(webhook_id, webhook_event_definition_id, created_at, updated_at) | |
VALUES (#{params[:webhook_id]}, #{params[:webhook_event_definition_id]}, 'Jan 01, 2023, 12:20 PM', 'Jan 01, 2023, 12:20 PM') | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nNew webhook_event_definitions_webhooks: #{result}\n") | |
end | |
def create_oauth2_access_token(new_integration_id) | |
sql = "SELECT * FROM oauth2_access_tokens | |
WHERE new_integration_id = #{new_integration_id} AND | |
account_id = #{dev_account.id} AND | |
oauth2_application_id = #{oauth_application.id} | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nCurrent oauth2_access_token: #{result}\n") | |
return if result.present? | |
sql = "INSERT INTO oauth2_access_tokens | |
(oauth2_application_id, new_integration_id, account_id, refresh_token, token, token_expires_at, created_at, updated_at) | |
VALUES (#{oauth_application.id}, #{new_integration_id}, #{dev_account.id}, 'refresh token', 'token', | |
'Jan 01, 2023, 12:20 PM', 'Jan 01, 2023, 12:20 PM', 'Jan 01, 2023, 12:20 PM') | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nNew oauth2_access_tokens: #{result}\n") | |
end | |
def create_oauth2_access_token_permissions | |
sql = "SELECT * FROM oauth2_access_tokens_permissions | |
WHERE oauth2_access_token_id = #{oauth_access_token.id} AND | |
oauth2_permission_id = #{oauth_access_permission.id} | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nCurrent oauth2_access_token_permission: #{result}\n") | |
return if result.present? | |
sql = "INSERT INTO oauth2_access_tokens_permissions | |
(oauth2_access_token_id, oauth2_permission_id, created_at, updated_at) | |
VALUES (#{oauth_access_token.id}, #{oauth_access_permission.id},'Jan 01, 2023, 12:20 PM', 'Jan 01, 2023, 12:20 PM') | |
" | |
result = ActiveRecord::Base.connection.execute(sql).to_a | |
Rails.logger.info("\nNew oauth2_access_tokens: #{result}\n") | |
end | |
def third_party_dev | |
@third_party_dev ||= PartnerUser.find_by(email: "[email protected]") | |
end | |
def dev_account | |
@dev_account ||= Account.find_by(billing_email: '[email protected]') | |
end | |
def oauth_application | |
@oauth_application ||= Oauth2Application.find_by(app_id: third_party_app.id) | |
end | |
def oauth_access_token | |
@oauth_access_token ||= Oauth2AccessToken.find_by(account_id: dev_account.id, oauth2_application_id: oauth_application.id) | |
end | |
def oauth_access_permission | |
@oauth_access_token ||= Oauth2Permission.find_by(name: Oauth2Permission::CUSTOMER_READ_PERMISSION) | |
end | |
def webhook_type | |
"Webhook::PostToUrl" | |
end | |
def webhook_params | |
params = { | |
:app_id => third_party_app.id, | |
:type => webhook_type, | |
:url_template => "https://listener-smile.requestcatcher.com/" | |
} | |
Rails.logger.info("\nCurrent webhook_params: #{params}\n") | |
params | |
end | |
def third_party_app | |
@third_party_app ||= App.find_by(name: 'Third Party Developer App') | |
end | |
def third_party_app_new_integration | |
@new_integration_third_party ||= NewIntegration.find_by(app_id: third_party_app.id, account_id: dev_account.id) | |
end | |
# the webhook event definitions are created when we run the db:seed rake task | |
def customer_updated_webhook_event_definition | |
w = WebhookEventDefinition.find_by(topic: WebhookEventDefinition::CUSTOMER_UPDATED) | |
Rails.logger.info("\nCurrent webhook_event_definition: #{w.to_hash}\n") | |
w | |
end | |
def new_integration_deleted_webhook_event_definition | |
w = WebhookEventDefinition.find_by(topic: WebhookEventDefinition::APP_UNINSTALLED) | |
Rails.logger.info("\nCurrent webhook_event_definition: #{w.to_hash}\n") | |
w | |
end | |
def run | |
Rails.logger.info("\nStarting...\n") | |
Rails.logger.info("\nDeveloper Account Seed...\n") | |
developer_account_seed | |
Rails.logger.info("\nCreating customer updated webhook...\n") | |
customer_updated_webhook = create_webhook('customer/updated') | |
Rails.logger.info("\nCreating new integration deleted webhook...\n") | |
new_integration_deleted_webhook = create_webhook('app/uninstalled') | |
Rails.logger.info("\nCreating customer updated webhook_event_definitions_webhooks...\n") | |
customer_updated_event = customer_updated_webhook_event_definition | |
new_integration_deleted_event = new_integration_deleted_webhook_event_definition | |
create_webhook_event_definitions_webhooks(customer_updated_webhook, customer_updated_event) | |
Rails.logger.info("\nCreating new integration deleted webhook_event_definitions_webhooks...\n") | |
create_webhook_event_definitions_webhooks(new_integration_deleted_webhook, new_integration_deleted_event) | |
Rails.logger.info("\nCreating new integration to third party app...\n") | |
create_integration_to_third_party_app | |
Rails.logger.info("\nCreating oauth2_access_token...\n") | |
create_oauth2_access_token(third_party_app_new_integration.id) | |
Rails.logger.info("\nCreating oauth2_access_token_permissions...\n") | |
create_oauth2_access_token_permissions | |
Rails.logger.info("\nIt ends here.\n") | |
end | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment