Files
ubicloud/model/usage_alert.rb
Jeremy Evans 3ecfa2c726 Add ubid types for 4 models, do not leak TYPE_ETC ubid values
This adds a check in the route specs that TYPE_ETC ubid values are not
present in responses.  It then changes four models to use model-specific
ubid types instead of TYPE_ETC.

The original impetus for this change is to eventually remove
authorization code that explicitly checks for TYPE_ETC and assumes
ApiKey, which will cause problems in the future if we ever have
another model using TYPE_ETC that the authorization system needs
to deal with.
2025-02-26 09:09:54 -08:00

42 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
class UsageAlert < Sequel::Model
many_to_one :project
many_to_one :user, class: :Account, key: :user_id
include ResourceMethods
def trigger
send_email
update(last_triggered_at: Time.now)
end
def send_email
Util.send_email(user.email, "Usage alert is triggered for project #{project.name}",
greeting: "Hello #{user.name},",
body: ["The usage alert, #{name}, you set for project #{project.name} (id: #{project.ubid}) has been triggered.",
"Current cost: $#{project.current_invoice.content["cost"].to_f.round(2)}",
"Please note that this alert is only for informational purposes and no action is taken automatically."],
button_title: "See usage",
button_link: "#{Config.base_url}#{project.path}/billing")
end
end
# Table: usage_alert
# Columns:
# id | uuid | PRIMARY KEY
# project_id | uuid | NOT NULL
# name | text | NOT NULL
# limit | integer | NOT NULL
# user_id | uuid | NOT NULL
# last_triggered_at | timestamp with time zone | NOT NULL DEFAULT (now() - '42 days'::interval)
# Indexes:
# usage_alert_pkey | PRIMARY KEY btree (id)
# usage_alert_project_id_user_id_name_uidx | UNIQUE btree (project_id, user_id, name)
# usage_alert_last_triggered_at_index | btree (last_triggered_at)
# Foreign key constraints:
# usage_alert_project_id_fkey | (project_id) REFERENCES project(id)
# usage_alert_user_id_fkey | (user_id) REFERENCES accounts(id)