enquiryAction.js 5.32 KB
import axios from "axios";
import { getSession } from "next-auth/react";
import {
  CLEAR_ERRORS,
  ENQUIRY_SUBMIT_FAIL,
  ENQUIRY_SUBMIT_REQUEST,
  ENQUIRY_SUBMIT_SUCCESS,
  GET_ENQUIRIES_BY_ADMIN_FAIL,
  GET_ENQUIRIES_BY_ADMIN_REQUEST,
  GET_ENQUIRIES_BY_ADMIN_SUCCESS,
  GET_ENQUIRIES_BY_ENDUSER_FAIL,
  GET_ENQUIRIES_BY_ENDUSER_REQUEST,
  GET_ENQUIRIES_BY_ENDUSER_SUCCESS,
  GET_ENQUIRIES_BY_VENDOR_FAIL,
  GET_ENQUIRIES_BY_VENDOR_REQUEST,
  GET_ENQUIRIES_BY_VENDOR_SUCCESS
} from "../constants/enquiryConstants";
import qs from "qs";

export const sendEnquiry = async ({ activityId, userId, date, vendorId, quantity }) => {
  const data = {
    data: {
      experience: activityId,
      end_user: userId,
      vendor: vendorId,
      date,
      status: "pending",
      quantity
    }
  };
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };
  console.log("data", data);
  const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/enquires`, data, config);

  return response.data;
};

// Clear errors
export const clearErrors = () => async dispatch => {
  dispatch({
    type: CLEAR_ERRORS
  });
};

export const getEnquiriesByVendor = () => async dispatch => {
  try {
    const session = await getSession();
    if (!session) {
      return;
    }
    const vendorConfig = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };
    const vendorQuery = {
      filters: {
        user: {
          id: { $eq: session.id }
        }
      }
    };

    const vendorQueryString = qs.stringify(vendorQuery, {
      encodeValuesOnly: true
    });

    const vendorResponse = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${vendorQueryString}`, vendorConfig);
    console.log("session", vendorResponse.data.data[0].id);
    dispatch({
      type: GET_ENQUIRIES_BY_VENDOR_REQUEST
    });

    const config = {
      headers: {
        "Content-type": "application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };

    const query = {
      filters: {
        vendor: {
          id: { $eq: vendorResponse.data.data[0].id }
        }
      },
      populate: ["vendor", "experience", "end_user"]
    };

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });
    // console.log("querystring", query);
    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/enquires/?${queryString}`, config);
    console.log("response", response);
    dispatch({
      type: GET_ENQUIRIES_BY_VENDOR_SUCCESS,
      payload: response.data.data
    });
  } catch (error) {
    dispatch({
      type: GET_ENQUIRIES_BY_VENDOR_FAIL,
      payload: error.response.data
    });
  }
};

export const getEnquiriesByAdmin = () => async dispatch => {
  try {
    dispatch({
      type: GET_ENQUIRIES_BY_ADMIN_REQUEST,
      loading: true
    });
    const session = await getSession();
    if (!session) {
      return;
    }

    const config = {
      headers: {
        "Content-type": "application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };

    const query = {
      filters: {},
      populate: ["vendor", "experience", "end_user"]
    };

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/enquires/?${queryString}`, config);

    dispatch({
      type: GET_ENQUIRIES_BY_ADMIN_SUCCESS,
      payload: response.data,
      loading: false
    });
  } catch (error) {
    dispatch({
      type: GET_ENQUIRIES_BY_ADMIN_FAIL,
      payload: error.response.data
    });
  }
};

export const getEnquiriesByEndUser =
  ({ endUserId }) =>
  async dispatch => {
    try {
      dispatch({
        type: GET_ENQUIRIES_BY_ENDUSER_REQUEST,
        loading: true
      });
      const session = await getSession();
      if (!session) {
        return;
      }
      const config = {
        headers: {
          "Content-type": "application/json",
          Authorization: `Bearer ${session.jwt}`
        }
      };

      console.log("res enq");
      const query = {
        filters: {
          end_user: {
            id: {
              $eq: endUserId
            }
          }
        },
        populate: ["vendor", "experience", "end_user", "experience.image", "experience.reviews"],
        sort: ["createdAt:desc"]
      };

      const queryString = qs.stringify(query, {
        encodeValuesOnly: true
      });

      const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/enquires/?${queryString}`, config);
      dispatch({
        type: GET_ENQUIRIES_BY_ENDUSER_SUCCESS,
        payload: response.data.data,
        loading: false
      });
    } catch (error) {
      dispatch({
        type: GET_ENQUIRIES_BY_ENDUSER_FAIL,
        payload: error.response.data
      });
    }
  };

export const changeEnquiryStatus = async ({ status, id }) => {
  try {
    const session = await getSession();
    if (!session) {
      return;
    }
    const config = {
      headers: {
        "Content-type": "application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };

    const response = await axios.put(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/enquires/${id}`, { data: { status } }, config);
    return response.data;
  } catch (error) {}
};