vendorActions.js 5.89 KB
import axios from "axios";
import { getSession } from "next-auth/react";
import qs from "qs";
import {
  GET_ALL_VENDORS_FAIL,
  GET_ALL_VENDORS_REQUEST,
  GET_ALL_VENDORS_SUCCESS,
  GET_LOGGED_IN_VENDOR_FAIL,
  GET_LOGGED_IN_VENDOR_REQUEST,
  GET_LOGGED_IN_VENDOR_SUCCESS,
  GET_VENDOR_DETAILS_FAIL,
  GET_VENDOR_DETAILS_REQUEST,
  GET_VENDOR_DETAILS_SUCCESS,
  UPDATE_VENDOR_DETAILS_FAIL,
  UPDATE_VENDOR_DETAILS_REQUEST,
  UPDATE_VENDOR_DETAILS_SUCCESS
} from "../constants/vendorConstants";

export const finishVendorOtpVerification = async verificationData => {
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  return await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendor/finish-otp-verification`, verificationData, config);
};

export const pincodeSearchByFilter = async code => {
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  const query = {
    filters: {
      name: {
        $contains: code
      }
    },
    populate: ["masterCity"]
  };
  const queryString = qs.stringify(query, {
    encodeValuesOnly: true
  });

  return await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/master-pincodes?${queryString}`, config);
};

export const updateVendorBusinessDetails =
  ({ businessDetails, vendorId }) =>
  async dispatch => {
    const session = await getSession();

    if (!session) {
      throw new Error("You are not authenticated. Please log in.");
    }

    try {
      dispatch({
        type: UPDATE_VENDOR_DETAILS_REQUEST
      });

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

      const response = await axios.put(
        `${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/${vendorId}`,
        {
          data: businessDetails
        },
        config
      );
      dispatch({
        type: UPDATE_VENDOR_DETAILS_SUCCESS
      });
      return response.data;
    } catch (error) {
      dispatch({
        type: UPDATE_VENDOR_DETAILS_FAIL
      });
    }
  };

export const getLoggedInVendor = () => async dispatch => {
  const session = await getSession();
  console.log("session lfkjskfjsdh", session);

  if (!session) {
    throw new Error("You are not authenticated. Please log in.");
  }

  try {
    dispatch({
      type: GET_LOGGED_IN_VENDOR_REQUEST
    });

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

    const query = {
      filters: {
        user: {
          id: { $eq: session.id }
        }
      },
      populate: ["brandLogo"]
    };

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

    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
    console.log("response", response.data.data[0]);
    dispatch({
      type: GET_LOGGED_IN_VENDOR_SUCCESS,
      payload: response.data.data[0]
    });
    return response.data.data[0];
  } catch (error) {
    dispatch({
      type: GET_LOGGED_IN_VENDOR_FAIL
    });
  }
};

export const getVendorDetails = () => async dispatch => {
  const session = await getSession();
  if (!session) {
    return;
  }
  try {
    dispatch({
      type: GET_VENDOR_DETAILS_REQUEST
    });

    console.log("here 1", session);
    const config = {
      headers: {
        "Content-Type": "Application/json",
        Authorization: `Bearer ${session.jwt}`
      }
    };
    console.log("here 2");
    if (!session) {
      return;
    }
    const query = {
      filters: {
        user: {
          id: {
            $eq: session.id
          }
        }
      },
      populate: ["user", "brandLogo"]
    };
    console.log("here 3", query);

    const queryString = qs.stringify(query, {
      encodeValuesOnly: true
    });
    console.log("session", session, session.id);
    const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
    console.log("response", response);
    dispatch({
      type: GET_VENDOR_DETAILS_SUCCESS,
      payload: response.data.data[0]
    });

    return response.data.data[0];
  } catch (error) {
    dispatch({
      type: GET_VENDOR_DETAILS_FAIL,
      payload: error.response.data
    });
  }
};

export const getAllVendors =
  ({ pageNumber = 1, pageSize = 10, str }) =>
  async dispatch => {
    const session = await getSession();
    try {
      dispatch({
        type: GET_ALL_VENDORS_REQUEST
      });

      // console.log("here 1", session);
      const config = {
        headers: {
          "Content-Type": "Application/json",
          Authorization: `Bearer ${session.jwt}`
        }
      };
      if (!session) {
        return;
      }
      const query = {
        filters: {
          // user: {
          //   id: {
          //     $eq: session.id
          //   }
          // }
        },
        populate: ["user", "experiences", "brandLogo"],
        sort: ["createdAt:desc"],
        pagination: {
          pageSize: pageSize,
          page: pageNumber
        }
      };

      if (str) {
        query.filters["uniqueVendorId"] = { $contains: str };
      }
      // console.log("here 3", query);

      const queryString = qs.stringify(query, {
        encodeValuesOnly: true
      });
      // console.log("session", session, session.id);
      const response = await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/?${queryString}`, config);
      console.log("response", response);
      dispatch({
        type: GET_ALL_VENDORS_SUCCESS,
        payload: response.data.data,
        totalCount: response.data.meta.pagination.total,
        resultsPerPage: response.data.meta.pagination.pageSize
      });

      return response.data.data[0];
    } catch (error) {
      dispatch({
        type: GET_ALL_VENDORS_FAIL,
        payload: error.response.data
      });
    }
  };