Adding visible to user and enterprises report, and sorting by confirmation date

This commit is contained in:
Rob Harrington
2015-01-29 10:46:44 +11:00
parent 9163b0c1ad
commit cbae7dcc8e
2 changed files with 34 additions and 11 deletions

View File

@@ -16,6 +16,7 @@ module OpenFoodNetwork
"Enterprise",
"Producer?",
"Sells",
"Visible",
"Confirmation Date"
]
end
@@ -27,25 +28,26 @@ module OpenFoodNetwork
uae["name"],
to_bool(uae["is_primary_producer"]),
uae["sells"],
uae["visible"],
to_local_datetime(uae["confirmed_at"])
]
end
end
def owners_and_enterprises
query = "SELECT enterprises.name, enterprises.sells, enterprises.is_primary_producer, enterprises.confirmed_at,
query = "SELECT enterprises.name, enterprises.sells, enterprises.visible, enterprises.is_primary_producer, enterprises.confirmed_at,
'owns' AS relationship_type, owners.email as user_email FROM enterprises
LEFT JOIN spree_users AS owners ON owners.id=enterprises.owner_id
WHERE enterprises.id IS NOT NULL
#{ params[:enterprise_id_in].present? ? "AND enterprises.id IN (#{ params[:enterprise_id_in] })" : "" }
#{ params[:user_id_in].present? ? "AND owners.id IN (#{ params[:user_id_in] })" : "" }
ORDER BY enterprises.name DESC"
ORDER BY confirmed_at DESC"
ActiveRecord::Base.connection.execute(query).to_a
end
def managers_and_enterprises
query = "SELECT enterprises.name, enterprises.sells, enterprises.is_primary_producer, enterprises.confirmed_at,
query = "SELECT enterprises.name, enterprises.sells, enterprises.visible, enterprises.is_primary_producer, enterprises.confirmed_at,
'manages' AS relationship_type, managers.email as user_email FROM enterprises
LEFT JOIN enterprise_roles ON enterprises.id=enterprise_roles.enterprise_id
LEFT JOIN spree_users AS managers ON enterprise_roles.user_id=managers.id
@@ -53,7 +55,7 @@ module OpenFoodNetwork
#{ params[:enterprise_id_in].present? ? "AND enterprise_id IN (#{ params[:enterprise_id_in] })" : "" }
AND user_id IS NOT NULL
#{ params[:user_id_in].present? ? "AND user_id IN (#{ params[:user_id_in] })" : "" }
ORDER BY enterprises.name DESC, user_email DESC"
ORDER BY confirmed_at DESC"
ActiveRecord::Base.connection.execute(query).to_a
end
@@ -64,8 +66,13 @@ module OpenFoodNetwork
def sort(results)
results.sort do |a,b|
[ a["name"], b["relationship_type"], a["user_email"] ] <=>
[ b["name"], a["relationship_type"], b["user_email"] ]
if a["confirmed_at"].nil? || b["confirmed_at"].nil?
[ (a["confirmed_at"].nil? ? 0 : 1), a["name"], b["relationship_type"], a["user_email"] ] <=>
[ (b["confirmed_at"].nil? ? 0 : 1), b["name"], a["relationship_type"], b["user_email"] ]
else
[ DateTime.parse(b["confirmed_at"]), a["name"], b["relationship_type"], a["user_email"] ] <=>
[ DateTime.parse(a["confirmed_at"]), b["name"], a["relationship_type"], b["user_email"] ]
end
end
end
@@ -78,4 +85,4 @@ module OpenFoodNetwork
string.to_datetime.in_time_zone.strftime "%Y-%m-%d %H:%M"
end
end
end
end

View File

@@ -26,7 +26,23 @@ module OpenFoodNetwork
describe "sorting results" do
let!(:subject) { OpenFoodNetwork::UsersAndEnterprisesReport.new {} }
it "sorts by name first" do
it "sorts unconfirmed enterprises to the top" do
uae_mock = [
{ "confirmed_at" => "2015-01-01", "name" => "aaa" },
{ "confirmed_at" => nil, "name" => "bbb" }
]
expect(subject.sort uae_mock).to eq [ uae_mock[1], uae_mock[0] ]
end
it "then sorts by confirmation date" do
uae_mock = [
{ "confirmed_at" => "2015-01-01", "name" => "bbb" },
{ "confirmed_at" => "2015-01-02", "name" => "aaa" }
]
expect(subject.sort uae_mock).to eq [ uae_mock[1], uae_mock[0] ]
end
it "then sorts by name" do
uae_mock = [
{ "name" => "aaa", "relationship_type" => "bbb", "user_email" => "bbb" },
{ "name" => "bbb", "relationship_type" => "aaa", "user_email" => "aaa" }
@@ -34,7 +50,7 @@ module OpenFoodNetwork
expect(subject.sort uae_mock).to eq [ uae_mock[0], uae_mock[1] ]
end
it "sorts by relationship type (reveresed) second" do
it "then sorts by relationship type (reveresed)" do
uae_mock = [
{ "name" => "aaa", "relationship_type" => "bbb", "user_email" => "bbb" },
{ "name" => "aaa", "relationship_type" => "aaa", "user_email" => "aaa" },
@@ -43,7 +59,7 @@ module OpenFoodNetwork
expect(subject.sort uae_mock).to eq [ uae_mock[2], uae_mock[0], uae_mock[1] ]
end
it "sorts by user_email third" do
it "then sorts by user_email" do
uae_mock = [
{ "name" => "aaa", "relationship_type" => "bbb", "user_email" => "aaa" },
{ "name" => "aaa", "relationship_type" => "aaa", "user_email" => "aaa" },
@@ -113,4 +129,4 @@ module OpenFoodNetwork
end
end
end
end
end