defmodule DaProductAppWeb.AaniController do use DaProductAppWeb, :controller require Logger alias DaProductApp.MerchantRegistration.AaniProvider @doc """ Aani-specific merchant details update endpoint This endpoint allows updating specific merchant details for merchants that have already been onboarded with Aani. Expected headers: - RequestId: Unique request identifier - BankUserId: Enrolled BankUserId to modify - RequestUserId: Requestor name/ID - MerchantTag: Merchant tag received from Aani after onboarding - Authorization: Bearer token Expected payload format: { "name": "Gold", "surname": "Cafe", "denomination": "Iyer-Fyer Co", "vatNumber": "HDTBAOY", "logo": "data:image/png;base64,..." } """ def update_merchant_details(conn, params) do Logger.info("Received Aani merchant details update request: #{inspect(params)}") # Extract required headers case extract_required_headers(conn) do {:ok, headers} -> # Call AaniProvider to handle the update case AaniProvider.update_merchant_details(params, headers) do {:ok, response} -> Logger.info("Aani merchant details update successful: #{inspect(response)}") json(conn, %{ status: "success", provider: "aani", data: response }) {:error, reason} -> Logger.error("Aani merchant details update failed: #{inspect(reason)}") conn |> put_status(:bad_request) |> json(%{ status: "error", provider: "aani", reason: reason }) end {:error, missing_headers} -> Logger.error("Missing required headers: #{inspect(missing_headers)}") conn |> put_status(:bad_request) |> json(%{ status: "error", code: "MISSING_HEADERS", errors: missing_headers }) end end @doc """ Generic Aani merchant update endpoint This endpoint accepts different types of updates based on the "type" parameter: - "updateprofile": For updating merchant profile details - "mobile": For updating merchant mobile number - "mcc": For updating merchant category code - Other types can be added as needed Required params: - type: The type of update to perform ("updateprofile", "mobile", "mcc", etc.) - Additional params based on the update type """ def update_merchant(conn, params) do Logger.info("Received Aani generic update request: #{inspect(params)}") # Extract the update type update_type = Map.get(params, "type") if is_nil(update_type) do Logger.error("Missing required 'type' parameter for Aani update") conn |> put_status(:bad_request) |> json(%{ status: "error", code: "MISSING_TYPE", message: "Missing required 'type' parameter. Supported types: updateprofile, mobile, mcc" }) else # Extract required headers case extract_required_headers(conn) do {:ok, headers} -> # Call AaniProvider to handle the update case AaniProvider.update_merchant(params, headers) do {:ok, response} -> Logger.info("Aani merchant update successful for type #{update_type}: #{inspect(response)}") json(conn, %{ status: "success", provider: "aani", type: update_type, data: response }) {:error, reason} -> Logger.error("Aani merchant update failed for type #{update_type}: #{inspect(reason)}") conn |> put_status(:bad_request) |> json(%{ status: "error", provider: "aani", type: update_type, reason: reason }) end {:error, missing_headers} -> Logger.error("Missing required headers: #{inspect(missing_headers)}") conn |> put_status(:bad_request) |> json(%{ status: "error", code: "MISSING_HEADERS", errors: missing_headers }) end end end @doc """ Aani-specific merchant MCC (Merchant Category Code) update endpoint This endpoint allows updating the merchant category code for merchants that have already been onboarded with Aani. Expected headers: - RequestId: Unique request identifier - BankUserId: Enrolled BankUserId to modify - RequestUserId: Requestor name/ID - MerchantTag: Merchant tag received from Aani after onboarding - Authorization: Bearer token Expected payload format: { "mcc": "9994" } """ def update_merchant_mcc(conn, params) do Logger.info("Received Aani merchant MCC update request: #{inspect(params)}") # Extract required headers case extract_required_headers(conn) do {:ok, headers} -> # Call AaniProvider to handle the MCC update case AaniProvider.update_merchant_mcc(params, headers) do {:ok, response} -> Logger.info("Aani merchant MCC update successful: #{inspect(response)}") json(conn, %{ status: "success", provider: "aani", data: response }) {:error, reason} -> Logger.error("Aani merchant MCC update failed: #{inspect(reason)}") conn |> put_status(:bad_request) |> json(%{ status: "error", provider: "aani", reason: reason }) end {:error, missing_headers} -> Logger.error("Missing required headers: #{inspect(missing_headers)}") conn |> put_status(:bad_request) |> json(%{ status: "error", code: "MISSING_HEADERS", errors: missing_headers }) end end # Extract and validate required headers defp extract_required_headers(conn) do required_headers = ["requestid", "bankuserid", "requestuserid", "merchanttag", "authorization"] headers = Enum.reduce(required_headers, %{}, fn header_name, acc -> case get_req_header(conn, header_name) do [value | _] -> Map.put(acc, header_name, value) [] -> acc end end) missing = Enum.filter(required_headers, fn header -> not Map.has_key?(headers, header) end) if Enum.empty?(missing) do {:ok, headers} else {:error, Enum.map(missing, &"#{&1} header is required")} end end end