diff --git a/box/apis/v2/management/ebics_users.rb b/box/apis/v2/management/ebics_users.rb index 39b3e446..da9b8925 100644 --- a/box/apis/v2/management/ebics_users.rb +++ b/box/apis/v2/management/ebics_users.rb @@ -63,6 +63,18 @@ class EbicsUsers < Grape::API end end + params do + requires :id, type: Integer, desc: 'ID of the ebics_user' + end + post ':id/refresh_bank_keys' do + ebics_user = @account.ebics_users_dataset.first!(Sequel.qualify(:ebics_users, :id) => params[:id]) + if ebics_user.refresh_bank_keys! + present ebics_user, with: Entities::EbicsUser + else + error!({ message: 'Bank keys could not be refreshed!' }, 500) + end + end + ### ### GET /management/accounts/DExx/ebics_users ### diff --git a/box/models/ebics_user.rb b/box/models/ebics_user.rb index 3f158c65..41484c11 100644 --- a/box/models/ebics_user.rb +++ b/box/models/ebics_user.rb @@ -83,6 +83,19 @@ def activate! false end + def refresh_bank_keys! + Box.logger.info("Refresh bank keys for account #{id}") + client.HPB + + self.encryption_keys = client.send(:dump_keys) + save + true + rescue StandardError => e + # TODO: show the error to the user + Box.logger.error("Failed to refresh bank keys for account #{id}: #{e}") + false + end + def state if active? 'active'