put front-view mockup extras first in image gallery

When the enricher adds extra angle images, sort them so front views
come first (position 0) and shift existing images down. This ensures
the product gallery leads with a proper front shot rather than a
handle-side or angled preview.

Also adds Products.update_product_image/2.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey 2026-02-15 16:58:54 +00:00
parent 1aceaf9444
commit 29d8839ac2
2 changed files with 39 additions and 4 deletions

View File

@ -443,6 +443,15 @@ defmodule SimpleshopTheme.Products do
|> Repo.all()
end
@doc """
Updates a product image with the given attributes.
"""
def update_product_image(%ProductImage{} = product_image, attrs) do
product_image
|> ProductImage.changeset(attrs)
|> Repo.update()
end
@doc """
Links a product image to a Media.Image by setting its image_id.
"""

View File

@ -203,13 +203,32 @@ defmodule SimpleshopTheme.Sync.MockupEnricher do
end
defp append_images_to_product(product, extra_images) do
# Find the current max position so we append after existing images
existing_images = Products.list_product_images(product.id)
max_position = existing_images |> Enum.map(& &1.position) |> Enum.max(fn -> -1 end)
existing_count = length(existing_images)
# Sort extras: front views first, then the rest
sorted_extras =
Enum.sort_by(extra_images, fn img ->
title = String.downcase(img.alt || "")
if String.contains?(title, "front"), do: 0, else: 1
end)
has_front_extra =
Enum.any?(sorted_extras, fn img ->
String.contains?(String.downcase(img.alt || ""), "front")
end)
# If we have a front extra, shift existing images down to make room
if has_front_extra do
shift_images_down(existing_images, length(sorted_extras))
end
# Insert extras: front extras at position 0+, others after existing
start_position = if has_front_extra, do: 0, else: existing_count
results =
extra_images
|> Enum.with_index(max_position + 1)
sorted_extras
|> Enum.with_index(start_position)
|> Enum.map(fn {img, position} ->
attrs = %{
product_id: product.id,
@ -233,6 +252,13 @@ defmodule SimpleshopTheme.Sync.MockupEnricher do
{:ok, count}
end
# Bump all existing image positions up by `offset` to make room at the front
defp shift_images_down(existing_images, offset) do
Enum.each(existing_images, fn img ->
Products.update_product_image(img, %{position: img.position + offset})
end)
end
# Check if this product already has extra angle images from a prior enrichment
defp already_enriched?(product) do
images = Products.list_product_images(product.id)