2016-03-23 18:39:41 +01:00
namespace :fablab do
2016-09-05 15:15:31 +02:00
# desc "Get all stripe plans and create in fablab app"
# task stripe_plan: :environment do
# Stripe::Plan.all.data.each do |plan|
2016-11-23 16:30:19 +01:00
# unless Plan.find_by(stp_plan_id: plan.id)
2016-09-05 15:15:31 +02:00
# group = Group.friendly.find(plan.id.split('-').first)
# if group
# Plan.create(stp_plan_id: plan.id, name: plan.name, amount: plan.amount, interval: plan.interval, group_id: group.id, skip_create_stripe_plan: true)
# else
# puts plan.name + " n'a pas été créé. [error]"
# end
# end
# end
#
# if Plan.column_names.include? "training_credit_nb"
# Plan.all.each do |p|
# p.update_columns(training_credit_nb: (p.interval == 'month' ? 1 : 5))
# end
# end
# end
desc 'Regenerate the invoices'
2016-03-23 18:39:41 +01:00
task :regenerate_invoices , [ :year , :month ] = > :environment do | task , args |
year = args . year || Time . now . year
month = args . month || Time . now . month
start_date = Time . new ( year . to_i , month . to_i , 1 )
end_date = start_date . next_month
puts " -> Start regenerate the invoices between #{ I18n . l start_date , format : :long } in #{ I18n . l end_date - 1 . minute , format : :long } "
2016-09-05 15:15:31 +02:00
invoices = Invoice . only_invoice . where ( 'created_at >= :start_date AND created_at < :end_date' , { start_date : start_date , end_date : end_date } ) . order ( created_at : :asc )
2016-03-23 18:39:41 +01:00
invoices . each ( & :regenerate_invoice_pdf )
2016-09-05 15:15:31 +02:00
puts '-> Done'
2016-03-23 18:39:41 +01:00
end
2016-09-05 15:15:31 +02:00
desc 'Cancel stripe subscriptions'
2016-03-23 18:39:41 +01:00
task cancel_subscriptions : :environment do
2016-09-05 15:15:31 +02:00
Subscription . where ( 'expired_at >= ?' , Time . now . at_beginning_of_day ) . each do | s |
2016-03-23 18:39:41 +01:00
puts " -> Start cancel subscription of #{ s . user . email } "
s . cancel
2016-09-05 15:15:31 +02:00
puts '-> Done'
2016-03-23 18:39:41 +01:00
end
end
desc '(re)Build ElasticSearch fablab base for stats'
task es_build_stats : :environment do
2018-11-29 16:53:58 +01:00
puts 'DELETE stats'
2016-03-23 18:39:41 +01:00
` curl -XDELETE http:// #{ ENV [ " ELASTICSEARCH_HOST " ] } :9200/stats `
2018-11-29 16:53:58 +01:00
puts 'PUT index stats'
2018-04-04 14:05:48 +02:00
` curl -XPUT http:// #{ ENV [ " ELASTICSEARCH_HOST " ] } :9200/stats -d'
{
" settings " : {
" index " : {
" number_of_replicas " : 0
}
}
}
' `
2016-03-23 18:39:41 +01:00
2017-02-28 17:49:19 +01:00
%w[ account event machine project subscription training user space ] . each do | stat |
2016-03-23 18:39:41 +01:00
puts " PUT Mapping stats/ #{ stat } "
2018-11-29 16:53:58 +01:00
` curl -XPUT http:// #{ ENV [ " ELASTICSEARCH_HOST " ] } :9200/stats/ #{ stat } /_mapping -d '
2016-03-23 18:39:41 +01:00
{
" properties " : {
" type " : {
" type " : " string " ,
" index " : " not_analyzed "
} ,
" subType " : {
" type " : " string " ,
" index " : " not_analyzed "
} ,
" date " : {
" type " : " date "
} ,
" name " : {
" type " : " string " ,
" index " : " not_analyzed "
}
}
} ' ; `
end
2016-06-30 14:14:46 +02:00
es_add_event_filters
end
desc 'add event filters to statistics'
task es_add_event_filters : :environment do
es_add_event_filters
end
def es_add_event_filters
` curl -XPUT http:// #{ ENV [ " ELASTICSEARCH_HOST " ] } :9200/stats/event/_mapping -d '
{
" properties " : {
" ageRange " : {
" type " : " string " ,
" index " : " not_analyzed "
} ,
" eventTheme " : {
" type " : " string " ,
" index " : " not_analyzed "
}
}
} ' ; `
2016-03-23 18:39:41 +01:00
end
2017-02-28 17:49:19 +01:00
desc 'add spaces reservations to statistics'
task es_add_spaces : :environment do
` curl -XPUT http:// #{ ENV [ " ELASTICSEARCH_HOST " ] } :9200/stats/space/_mapping -d '
{
" properties " : {
" type " : {
" type " : " string " ,
" index " : " not_analyzed "
} ,
" subType " : {
" type " : " string " ,
" index " : " not_analyzed "
} ,
" date " : {
" type " : " date "
} ,
" name " : {
" type " : " string " ,
" index " : " not_analyzed "
}
}
} ' ; `
end
2016-09-05 15:15:31 +02:00
desc 'sync all/one project in ElasticSearch index'
2016-03-23 18:39:41 +01:00
task :es_build_projects_index , [ :id ] = > :environment do | task , args |
2016-09-05 15:15:31 +02:00
client = Project . __elasticsearch__ . client
# create index if not exists
unless client . indices . exists? index : Project . index_name
2018-04-04 14:05:48 +02:00
Project . __elasticsearch__ . create_index! force : true
2016-03-23 18:39:41 +01:00
end
2016-09-05 15:15:31 +02:00
# index requested documents
2016-03-23 18:39:41 +01:00
if args . id
2016-09-05 15:15:31 +02:00
ProjectIndexerWorker . perform_async ( :index , id )
2016-03-23 18:39:41 +01:00
else
Project . pluck ( :id ) . each do | project_id |
2016-09-05 15:15:31 +02:00
ProjectIndexerWorker . perform_async ( :index , project_id )
end
end
end
desc 'sync all/one availabilities in ElasticSearch index'
task :es_build_availabilities_index , [ :id ] = > :environment do | task , args |
client = Availability . __elasticsearch__ . client
# create index if not exists
unless client . indices . exists? index : Availability . index_name
2018-04-04 14:05:48 +02:00
Availability . __elasticsearch__ . create_index! force : true
2016-09-05 15:15:31 +02:00
end
# delete doctype if exists
if client . indices . exists_type? index : Availability . index_name , type : Availability . document_type
client . indices . delete_mapping index : Availability . index_name , type : Availability . document_type
end
# create doctype
2016-09-05 15:30:46 +02:00
client . indices . put_mapping index : Availability . index_name , type : Availability . document_type , body : Availability . mappings . to_hash
2016-09-05 15:15:31 +02:00
2016-10-05 12:28:13 +02:00
# verify doctype creation was successful
if client . indices . exists_type? index : Availability . index_name , type : Availability . document_type
puts " [ElasticSearch] #{ Availability . index_name } / #{ Availability . document_type } successfully created with its mapping. "
# index requested documents
if args . id
AvailabilityIndexerWorker . perform_async ( :index , id )
else
Availability . pluck ( :id ) . each do | availability_id |
AvailabilityIndexerWorker . perform_async ( :index , availability_id )
end
2016-03-23 18:39:41 +01:00
end
2016-10-05 12:28:13 +02:00
else
puts " [ElasticSearch] An error occurred while creating #{ Availability . index_name } / #{ Availability . document_type } . Please check your ElasticSearch configuration. "
puts " \n Cancelling... "
2016-03-23 18:39:41 +01:00
end
end
desc 'recreate every versions of images'
task build_images_versions : :environment do
Project . find_each do | project |
project . project_image . attachment . recreate_versions! if project . project_image . present? and project . project_image . attachment . present?
end
ProjectStepImage . find_each do | project_step_image |
project_step_image . attachment . recreate_versions! if project_step_image . present? and project_step_image . attachment . present?
end
Machine . find_each do | machine |
machine . machine_image . attachment . recreate_versions! if machine . machine_image . present?
end
Event . find_each do | event |
event . event_image . attachment . recreate_versions! if event . event_image . present?
end
end
desc 'switch the active authentication provider'
task :switch_auth_provider , [ :provider ] = > :environment do | task , args |
unless args . provider
fail 'FATAL ERROR: You must pass a provider name to activate'
end
unless AuthProvider . find_by ( name : args . provider ) != nil
providers = AuthProvider . all . inject ( '' ) do | str , item |
str += item [ :name ] + ', '
end
fail " FATAL ERROR: the provider ' #{ args . provider } ' does not exists. Available providers are: #{ providers [ 0 .. - 3 ] } "
end
if AuthProvider . active . name == args . provider
fail " FATAL ERROR: the provider ' #{ args . provider } ' is already enabled "
end
# disable previous provider
prev_prev = AuthProvider . find_by ( status : 'previous' )
unless prev_prev . nil?
prev_prev . update_attribute ( :status , 'pending' )
end
AuthProvider . active . update_attribute ( :status , 'previous' )
# enable given provider
AuthProvider . find_by ( name : args . provider ) . update_attribute ( :status , 'active' )
# migrate the current users.
if AuthProvider . active . providable_type != DatabaseProvider . name
User . all . each do | user |
# Concerns any providers except local database
user . generate_auth_migration_token
end
else
User . all . each do | user |
# Concerns local database provider
user . update_attribute ( :auth_token , nil )
end
end
# ask the user to restart the application
puts " \n Activation successful "
puts " \n /! \\ WARNING: Please consider the following, otherwise the authentication will be bogus: "
puts " \t 1) CLEAN the cache with `rake tmp:clear` "
2016-09-28 12:14:21 +02:00
puts " \t 2) REBUILD the assets with `rake assets:precompile` "
puts " \t 3) RESTART the application "
puts " \t 4) NOTIFY the current users with `rake fablab:notify_auth_changed` \n \n "
2016-03-23 18:39:41 +01:00
end
desc 'notify users that the auth provider has changed'
task notify_auth_changed : :environment do
2016-09-13 10:48:07 +02:00
I18n . locale = I18n . default_locale
2016-03-23 18:39:41 +01:00
# notify every users if the provider is not local database provider
if AuthProvider . active . providable_type != DatabaseProvider . name
User . all . each do | user |
NotificationCenter . call type : 'notify_user_auth_migration' ,
receiver : user ,
attached_object : user
end
end
puts " \n Users successfully notified \n \n "
end
2016-04-05 11:02:49 +02:00
2016-09-05 15:15:31 +02:00
desc 'generate fixtures from db'
2016-04-05 11:02:49 +02:00
task generate_fixtures : :environment do
Rails . application . eager_load!
2016-04-05 12:05:40 +02:00
ActiveRecord :: Base . descendants . reject { | c | c == ActiveRecord :: SchemaMigration or c == PartnerPlan } . each do | ar_base |
2016-04-05 11:02:49 +02:00
p " ========== #{ ar_base } ============== "
ar_base . dump_fixtures
end
end
2016-04-12 16:43:10 +02:00
desc 'clean stripe secrets from VCR cassettes'
task clean_cassettes_secrets : :environment do
Dir [ 'test/vcr_cassettes/*.yml' ] . each do | cassette_file |
cassette = File . read ( cassette_file )
cassette . gsub! ( Rails . application . secrets . stripe_api_key , 'sk_test_testfaketestfaketestfake' )
cassette . gsub! ( Rails . application . secrets . stripe_publishable_key , 'pk_test_faketestfaketestfaketest' )
puts cassette
File . write ( cassette_file , cassette )
end
end
2016-07-27 11:28:54 +02:00
desc '(re)generate statistics in elasticsearch for the past period'
task :generate_stats , [ :period ] = > :environment do | task , args |
unless args . period
fail 'FATAL ERROR: You must pass a number of days (=> past period) to generate statistics on'
end
days = args . period . to_i
days . times . each do | i |
StatisticService . new . generate_statistic ( { start_date : i . day . ago . beginning_of_day , end_date : i . day . ago . end_of_day } )
end
end
2017-01-09 10:54:30 +01:00
desc 'set slugs to plans'
task set_plans_slugs : :environment do
# this will maintain compatibility with existing statistics
Plan . all . each do | p |
p . slug = p . stp_plan_id
p . save
end
end
add task Id: 3713, reference: 1706002/VL, stripe id: in_1ASRQy2sOmf47Nz9Xpxtw46A, invoice total: 30.0, stripe invoice total: 80.0, date: 2017-06-08 16:16:26 +0200
Id: 3716, reference: 1706005/VL, stripe id: in_1ASRye2sOmf47Nz9utkjPDve, invoice total: 30.0, stripe invoice total: 40.0, date: 2017-06-08 16:51:15 +0200
Id: 3717, reference: 1706006/VL, stripe id: in_1ASS1X2sOmf47Nz93Xn2UxVh, invoice total: 30.0, stripe invoice total: 40.0, date: 2017-06-08 16:54:14 +0200
Id: 3718, reference: 1706007/VL, stripe id: in_1ASSBI2sOmf47Nz9Ol0gEEfC, invoice total: 30.0, stripe invoice total: 40.0, date: 2017-06-08 17:04:19 +0200 allow find the invoices incoherent
2017-06-09 11:08:08 +02:00
desc 'get incoherent invoice'
task :get_incoherent_invoice , [ :start_date ] = > :environment do | task , args |
date = Date . parse ( '2017-05-01' )
if args . start_date
begin
date = Date . parse ( args . start_date )
rescue = > e
fail e
end
end
Invoice . where ( 'created_at > ? AND stp_invoice_id IS NOT NULL' , date ) . each do | invoice |
stp_invoice = Stripe :: Invoice . retrieve ( invoice . stp_invoice_id )
if invoice . amount_paid != stp_invoice . total
puts " Id: #{ invoice . id } , reference: #{ invoice . reference } , stripe id: #{ stp_invoice . id } , invoice total: #{ invoice . amount_paid / 100 . 0 } , stripe invoice total: #{ stp_invoice . total / 100 . 0 } , date: #{ invoice . created_at } "
end
end
end
2016-03-23 18:39:41 +01:00
end