Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/bike_brigade/campaign_summary_poster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,23 @@ defmodule BikeBrigade.CampaignSummaryPoster do
"""
def post_summary_for_campaign(campaign) do
campaign = Repo.preload(campaign, :program)
do_post_summary(campaign, campaign.program.slack_channel_id)

do_post_summary(
campaign,
campaign.program.slack_channel_id,
campaign.program.send_delivery_summaries
)
end

defp do_post_summary(campaign, _, enabled) when enabled != true do
Logger.info("Skipping campaign #{campaign.id}: delivery summaries not enabled for program")
end

defp do_post_summary(campaign, nil) do
Logger.warning("Skipping campaign #{campaign.id}: no Slack channel configured for program")
Slack.Operations.notify_campaign_error(campaign, "No Slack channel configured")
defp do_post_summary(campaign, nil, _) do
Logger.info("Skipping campaign #{campaign.id}: no Slack channel configured for program")
end

defp do_post_summary(campaign, channel_id) do
defp do_post_summary(campaign, channel_id, true) do
summary = CampaignDeliverySummary.create_for(campaign)

with {:ok, record} <- find_or_create_record(campaign.id, channel_id, summary),
Expand Down
4 changes: 3 additions & 1 deletion lib/bike_brigade/delivery/program.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule BikeBrigade.Delivery.Program do
field :public, :boolean, default: false
field :hide_pickup_address, :boolean, default: false
field :slack_channel_id, :string
field :send_delivery_summaries, :boolean, default: false

# TODO: this is me trying out virtual fields again
field :campaign_count, :integer, virtual: true
Expand Down Expand Up @@ -80,7 +81,8 @@ defmodule BikeBrigade.Delivery.Program do
:spreadsheet_layout,
:start_date,
:photo_description,
:photos
:photos,
:send_delivery_summaries
])
|> validate_required([:name, :start_date])
|> foreign_key_constraint(:lead_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@
/>

<.input type="checkbox" field={f[:active]} label="Active" />
<.input
:if={Phoenix.HTML.Form.input_value(f, :slack_channel_id) not in [nil, ""]}
type="checkbox"
field={f[:send_delivery_summaries]}
label="Send Campaign Delivery Summary to Slack"
/>
</div>
</.inputs_for>
</.simple_form>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BikeBrigade.Repo.Migrations.AddSendDeliverySummariesToPrograms do
use Ecto.Migration

def change do
alter table(:programs) do
add :send_delivery_summaries, :boolean, default: false
Comment thread
neal-bpm marked this conversation as resolved.
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule BikeBrigade.Repo.Migrations.EnableSendDeliverySummariesForProgramsWithSlackChannel do
use Ecto.Migration
import Ecto.Query

alias BikeBrigade.Repo

def up do
from(prog in BikeBrigade.Delivery.Program,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Using application schema modules (BikeBrigade.Delivery.Program) in migrations can break replay on new environments if the schema later changes. Use raw SQL via execute/1 instead to keep the migration self-contained.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At priv/repo/migrations/20260519000001_enable_send_delivery_summaries_for_programs_with_slack_channel.exs, line 8:

<comment>Using application schema modules (`BikeBrigade.Delivery.Program`) in migrations can break replay on new environments if the schema later changes. Use raw SQL via `execute/1` instead to keep the migration self-contained.</comment>

<file context>
@@ -0,0 +1,16 @@
+  alias BikeBrigade.Repo
+
+  def up do
+    from(prog in BikeBrigade.Delivery.Program,
+      update: [set: [send_delivery_summaries: true]],
+      where: not is_nil(prog.slack_channel_id)
</file context>

update: [set: [send_delivery_summaries: true]],
where: not is_nil(prog.slack_channel_id)
)
|> Repo.update_all([])
Comment thread
neal-bpm marked this conversation as resolved.
end

def down, do: :ok
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BikeBrigade.Repo.Migrations.AddNotNullConstraintToSendDeliverySummaries do
use Ecto.Migration

def change do
alter table(:programs) do
modify :send_delivery_summaries, :boolean, null: false
end
end
end
Loading