Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG FIX] Ensure deleted resources in major publications are removed from section resources [MER-2987] #4621

Merged
merged 2 commits into from
Feb 2, 2024
Merged
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
93 changes: 59 additions & 34 deletions lib/oli/delivery/sections.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2439,42 +2439,49 @@ defmodule Oli.Delivery.Sections do
new_published_resource = new_published_resources_map[resource_id]
new_children = new_published_resource.children

case current_children do
nil ->
# this section resource was just created so it can assume the newly published value
%SectionResource{
section_resource
| children: Enum.map(new_children, &resource_id_to_sr_id[&1])
}

current_children ->
# ensure we are comparing resource_ids to resource_ids (and not section_resource_ids)
# by translating the current section_resource children ids to resource_ids
current_children_resource_ids =
Enum.map(current_children, &sr_id_to_resource_id[&1])

# check if the children resource_ids have diverged from the new value
if current_children_resource_ids != new_children do
# There is a merge conflict between the current section resource and the new published resource.
# Use the AIRRO three way merge algorithm to resolve
base = prev_published_resource.children
source = new_published_resource.children
target = current_children_resource_ids

case Oli.Publishing.Updating.Merge.merge(base, source, target) do
{:ok, merged} ->
%SectionResource{
updated_section_resource =
case current_children do
nil ->
# this section resource was just created so it can assume the newly published value
%SectionResource{
section_resource
| children: Enum.map(new_children, &resource_id_to_sr_id[&1])
}

current_children ->
# ensure we are comparing resource_ids to resource_ids (and not section_resource_ids)
# by translating the current section_resource children ids to resource_ids
current_children_resource_ids =
Enum.map(current_children, &sr_id_to_resource_id[&1])

# check if the children resource_ids have diverged from the new value
if current_children_resource_ids != new_children do
# There is a merge conflict between the current section resource and the new published resource.
# Use the AIRRO three way merge algorithm to resolve
base = prev_published_resource.children
source = new_published_resource.children
target = current_children_resource_ids

case Oli.Publishing.Updating.Merge.merge(base, source, target) do
{:ok, merged} ->
%SectionResource{
section_resource
| children: Enum.map(merged, &resource_id_to_sr_id[&1])
}

{:no_change} ->
section_resource
| children: Enum.map(merged, &resource_id_to_sr_id[&1])
}

{:no_change} ->
section_resource
end
else
section_resource
end
else
section_resource
end
end
end

clean_children(
updated_section_resource,
sr_id_to_resource_id,
new_published_resources_map
)
else
section_resource
end
Expand Down Expand Up @@ -2544,6 +2551,24 @@ defmodule Oli.Delivery.Sections do
result
end

# For a given section resource, clean the children attribute to ensure that:
# 1. Any nil records are removed
# 2. All non-nil sr id references map to a non-deleted revision in the new pub
defp clean_children(section_resource, sr_id_to_resource_id, new_published_resources_map) do
updated_children =
section_resource.children
|> Enum.filter(fn child_id -> !is_nil(child_id) end)
|> Enum.filter(fn child_id ->
case Map.get(new_published_resources_map, sr_id_to_resource_id[child_id]) do
nil -> false
%{deleted: true} -> false
_ -> true
end
end)

%{section_resource | children: updated_children}
end

@doc """
Returns a map of resource_id to published resource
"""
Expand Down
9 changes: 6 additions & 3 deletions lib/oli/delivery/sections/minimal_hierarchy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ defmodule Oli.Delivery.Sections.MinimalHierarchy do
collab_space_config: r.collab_space_config,
max_attempts: r.max_attempts,
retake_mode: r.retake_mode,
project_id: p.project_id
project_id: p.project_id,
deleted: r.deleted
})
|> Repo.all()
|> Enum.reduce(%{}, fn r, m -> Map.put(m, r.resource_id, r) end)
Expand All @@ -59,7 +60,8 @@ defmodule Oli.Delivery.Sections.MinimalHierarchy do
scoring_strategy_id: r.scoring_strategy_id,
collab_space_config: r.collab_space_config,
max_attempts: r.max_attempts,
retake_mode: r.retake_mode
retake_mode: r.retake_mode,
deleted: r.deleted
})
|> Repo.all()
|> Enum.map(fn pr -> Map.put(pr, :project_id, project_id) end)
Expand All @@ -84,7 +86,8 @@ defmodule Oli.Delivery.Sections.MinimalHierarchy do
Map.put(
node,
:children,
Enum.map(children_ids, fn sr_id ->
Enum.filter(children_ids, fn sr_id -> !is_nil(Map.get(nodes_by_sr_id, sr_id)) end)
|> Enum.map(fn sr_id ->
Map.get(nodes_by_sr_id, sr_id)
|> hierarchy_node_with_children(nodes_by_sr_id)
end)
Expand Down
Loading