---
# System prepended metadata

title: Scripts - Rate Import for Updated DLT ML

---

# Scripts - Rate Import for Updated DLT ML

``` ruby
names = %w[
  # from DLT ML Google Sheets
];

admin_names = Courier.belongs_to_easyship.where(admin_name: names).pluck(:admin_name);

# check if couriers are valid
not_es_couriers = names - admin_names
not_country_rate_couriers = Courier.belongs_to_easyship.where(admin_name: admin_names).where.not(rate_api_engine: "CountryRate").distinct.pluck(:admin_name)

country = Country.find_by(alpha2: "{{COUNTRY_ALPHA2}}");

# find all couriers with new/updated DLT ML and re-generate rates
targe_couriers = Courier.belongs_to_easyship.where(origin_country_id: country.id, rate_api_engine: "CountryRate", admin_name: admin_names);

targe_couriers.find_each do |courier| 
  RatesJob.new.perform(country: country, couriers: [courier], type: :rates)
end

# find all couriers with outdated DLT ML and re-generate rates
out_dated_couriers = Courier.distinct.joins(:delivery_time_mls).belongs_to_easyship.where(origin_country_id: country.id, rate_api_engine: "CountryRate").where.not(admin_name: admin_names);

out_dated_couriers.find_each do |courier| 
  courier.delivery_time_mls.delete_all
  RatesJob.new.perform(country: country, couriers: [courier], type: :rates)
end
