forked from AkkomaGang/akkoma
fixed delete ScheduledActivity
This commit is contained in:
parent
5fc2fa8908
commit
8589632d09
2 changed files with 22 additions and 31 deletions
|
@ -121,13 +121,7 @@ def create(%User{} = user, attrs) do
|
||||||
|> Multi.insert(:scheduled_activity, new(user, attrs))
|
|> Multi.insert(:scheduled_activity, new(user, attrs))
|
||||||
|> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
|
|> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> transaction_response
|
||||||
{:ok, %{scheduled_activity: scheduled_activity}} ->
|
|
||||||
{:ok, scheduled_activity}
|
|
||||||
|
|
||||||
{:error, _, changeset, _} ->
|
|
||||||
{:error, changeset}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_add_jobs(multi, true) do
|
defp maybe_add_jobs(multi, true) do
|
||||||
|
@ -159,7 +153,27 @@ def update(%ScheduledActivity{id: id} = scheduled_activity, attrs) do
|
||||||
set: [scheduled_at: get_field(changeset, :scheduled_at)]
|
set: [scheduled_at: get_field(changeset, :scheduled_at)]
|
||||||
)
|
)
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> transaction_response
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "Deletes a ScheduledActivity and linked jobs."
|
||||||
|
@spec delete(ScheduledActivity.t() | binary() | integer) ::
|
||||||
|
{:ok, ScheduledActivity.t()} | {:error, Ecto.Changeset.t()}
|
||||||
|
def delete(%ScheduledActivity{id: id} = scheduled_activity) do
|
||||||
|
Multi.new()
|
||||||
|
|> Multi.delete(:scheduled_activity, scheduled_activity, stale_error_field: :id)
|
||||||
|
|> Multi.delete_all(:jobs, job_query(id))
|
||||||
|
|> Repo.transaction()
|
||||||
|
|> transaction_response
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete(id) when is_binary(id) or is_integer(id) do
|
||||||
|
delete(%__MODULE__{id: id})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp transaction_response(result) do
|
||||||
|
case result do
|
||||||
{:ok, %{scheduled_activity: scheduled_activity}} ->
|
{:ok, %{scheduled_activity: scheduled_activity}} ->
|
||||||
{:ok, scheduled_activity}
|
{:ok, scheduled_activity}
|
||||||
|
|
||||||
|
@ -167,28 +181,6 @@ def update(%ScheduledActivity{id: id} = scheduled_activity, attrs) do
|
||||||
{:error, changeset}
|
{:error, changeset}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def delete_job(%ScheduledActivity{id: id} = _scheduled_activity) do
|
|
||||||
id
|
|
||||||
|> job_query
|
|
||||||
|> Repo.delete_all()
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete(%ScheduledActivity{} = scheduled_activity) do
|
|
||||||
Repo.delete(scheduled_activity)
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete(id) when is_binary(id) or is_integer(id) do
|
|
||||||
ScheduledActivity
|
|
||||||
|> where(id: ^id)
|
|
||||||
|> select([sa], sa)
|
|
||||||
|> Repo.delete_all()
|
|
||||||
|> case do
|
|
||||||
{1, [scheduled_activity]} -> {:ok, scheduled_activity}
|
|
||||||
_ -> :error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def for_user_query(%User{} = user) do
|
def for_user_query(%User{} = user) do
|
||||||
ScheduledActivity
|
ScheduledActivity
|
||||||
|
|
|
@ -45,8 +45,7 @@ def update(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, params)
|
||||||
|
|
||||||
@doc "DELETE /api/v1/scheduled_statuses/:id"
|
@doc "DELETE /api/v1/scheduled_statuses/:id"
|
||||||
def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
|
def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
|
||||||
with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity),
|
with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
|
||||||
_ <- ScheduledActivity.delete_job(scheduled_activity) do
|
|
||||||
render(conn, "show.json", scheduled_activity: scheduled_activity)
|
render(conn, "show.json", scheduled_activity: scheduled_activity)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue