Packages

An open source two way communication platform for the social sector

Current section

Files

Jump to
glific lib glific_web schema middleware add_organization.ex
Raw

lib/glific_web/schema/middleware/add_organization.ex

defmodule GlificWeb.Schema.Middleware.AddOrganization do
@moduledoc """
Implementing middleware functions to transform errors from Ecto Changeset into a format
consumable and displayable to the API user. This version is specifically for mutations.
"""
@behaviour Absinthe.Middleware
@doc """
This is the main middleware callback.
It receives an %Absinthe.Resolution{} struct and it needs to return an %Absinthe.Resolution{} struct.
The second argument will be whatever value was passed to the middleware call that setup the middleware.
"""
@spec call(Absinthe.Resolution.t(), term()) :: Absinthe.Resolution.t()
def call(resolution, _) do
case resolution.context do
%{current_user: current_user} ->
%{resolution | arguments: put_organization_id(resolution.arguments, current_user)}
_ ->
resolution
end
end
@spec put_organization_id(map(), Glific.Users.User.t()) :: map()
defp put_organization_id(%{input: _input} = arguments, current_user) do
put_in(arguments, [:input, :organization_id], current_user.organization_id)
end
defp put_organization_id(%{filter: _filter} = arguments, current_user) do
put_in(arguments, [:filter, :organization_id], current_user.organization_id)
end
defp put_organization_id(arguments, current_user) do
Map.put_new(arguments, :organization_id, current_user.organization_id)
end
end