From f716aa5f5e1d8644d34cfddfe92b9dfd898378a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Mon, 8 Apr 2024 17:11:20 +0100 Subject: [PATCH] fix: CD --- lib/atomic/activities.ex | 15 +++++++++++---- lib/atomic/organizations.ex | 20 ++++++++++++++++---- test/atomic/activities_test.exs | 2 +- test/atomic/organizations_test.exs | 4 +--- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/atomic/activities.ex b/lib/atomic/activities.ex index 3c6affa5b..0098e2d4e 100644 --- a/lib/atomic/activities.ex +++ b/lib/atomic/activities.ex @@ -9,6 +9,7 @@ defmodule Atomic.Activities do alias Atomic.Activities.ActivityEnrollment alias Atomic.Activities.Speaker alias Atomic.Feed.Post + alias Atomic.Organizations alias Atomic.RateLimiter @doc """ @@ -195,8 +196,8 @@ defmodule Atomic.Activities do """ def create_activity_with_post(attrs \\ %{}, after_save \\ &{:ok, &1}) do - case not is_nil(attrs["organization_id"]) and - RateLimiter.limit_activities(attrs["organization_id"]) do + case Organizations.verify_organization_id?(attrs) and + RateLimiter.limit_activities(Map.get(attrs, :organization_id)) do :ok -> Multi.new() |> Multi.insert(:post, fn _ -> @@ -221,12 +222,15 @@ defmodule Atomic.Activities do {:error, reason} -> {:error, reason} + + false -> + {:error, "Organization ID is required."} end end def create_activity(attrs \\ %{}) do - case not is_nil(attrs["organization_id"]) and - RateLimiter.limit_activities(attrs["organization_id"]) do + case Organizations.verify_organization_id?(attrs) and + RateLimiter.limit_activities(Map.get(attrs, :organization_id)) do :ok -> %Activity{} |> Activity.changeset(attrs) @@ -234,6 +238,9 @@ defmodule Atomic.Activities do {:error, reason} -> {:error, reason} + + false -> + {:error, "Organization ID is required."} end end diff --git a/lib/atomic/organizations.ex b/lib/atomic/organizations.ex index 4927130ab..2cf4d84da 100644 --- a/lib/atomic/organizations.ex +++ b/lib/atomic/organizations.ex @@ -732,8 +732,8 @@ defmodule Atomic.Organizations do """ def create_announcement_with_post(attrs \\ %{}) do - case not is_nil(attrs["organization_id"]) and - RateLimiter.limit_announcements(attrs["organization_id"]) do + case verify_organization_id?(attrs) and + RateLimiter.limit_announcements(Map.get(attrs, :organization_id)) do :ok -> Multi.new() |> Multi.insert(:post, fn _ -> @@ -758,12 +758,15 @@ defmodule Atomic.Organizations do {:error, reason} -> {:error, reason} + + false -> + {:error, "Organization ID is required"} end end def create_announcement(attrs \\ %{}) do - case not is_nil(attrs["organization_id"]) and - RateLimiter.limit_announcements(attrs["organization_id"]) do + case verify_organization_id?(attrs) and + RateLimiter.limit_announcements(Map.get(attrs, :organization_id)) do :ok -> %Announcement{} |> Announcement.changeset(attrs) @@ -774,6 +777,15 @@ defmodule Atomic.Organizations do end end + def verify_organization_id?(attrs) do + if not Map.has_key?(attrs, :organization_id) or + (Map.has_key?(attrs, :organization_id) and is_nil(Map.get(attrs, :organization_id))) do + false + else + true + end + end + @doc """ Updates an announcement. diff --git a/test/atomic/activities_test.exs b/test/atomic/activities_test.exs index 0dfdb5186..bb80537a9 100644 --- a/test/atomic/activities_test.exs +++ b/test/atomic/activities_test.exs @@ -32,7 +32,7 @@ defmodule Atomic.ActivitiesTest do end test "create_activity_with_post/2 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Activities.create_activity_with_post(@invalid_attrs) + assert {:error, _reason} = Activities.create_activity_with_post(@invalid_attrs) end test "create_activity_with_post/2 with maximum_entries lower than minimum_entries" do diff --git a/test/atomic/organizations_test.exs b/test/atomic/organizations_test.exs index bbe3465d3..8d7e8c73f 100644 --- a/test/atomic/organizations_test.exs +++ b/test/atomic/organizations_test.exs @@ -220,13 +220,11 @@ defmodule Atomic.OrganizationsTest do test "create_announcement_with_post/2 with valid data creates an announcement" do valid_attrs = params_for(:announcement) - assert {:ok, %Announcement{}} = Organizations.create_announcement_with_post(valid_attrs) end test "create_announcement_with_post/2 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = - Organizations.create_announcement_with_post(@invalid_attrs) + assert {:error, _reason} = Organizations.create_announcement_with_post(@invalid_attrs) end test "update_announcement/2 with valid data updates the announcement" do