diff --git a/components/ProductLinkCard.js b/components/ProductLinkCard.js index ee3fa5e..910a47d 100644 --- a/components/ProductLinkCard.js +++ b/components/ProductLinkCard.js @@ -1,101 +1,101 @@ import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; +import Avatar from '@material-ui/core/Avatar'; import Card from '@material-ui/core/Card'; import CardActionArea from '@material-ui/core/CardActionArea'; import CardContent from '@material-ui/core/CardContent'; -import CardMedia from '@material-ui/core/CardMedia'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import Button from '@material-ui/core/Button'; -import FileCopyIcon from '@material-ui/icons/FileCopy'; -import Grid from "@material-ui/core/Grid"; -import Typography from '@material-ui/core/Typography'; +import EmailOutlinedIcon from '@material-ui/icons/EmailOutlined'; +import FileCopyOutlinedIcon from '@material-ui/icons/FileCopyOutlined'; import Snackbar from '@material-ui/core/Snackbar'; import IconButton from '@material-ui/core/IconButton'; import CloseIcon from '@material-ui/icons/Close'; import config from "../config"; +import Table from '@material-ui/core/Table'; +import TableBody from '@material-ui/core/TableBody'; +import TableCell from '@material-ui/core/TableCell'; +import TableContainer from '@material-ui/core/TableContainer'; +import Paper from '@material-ui/core/Paper'; +import TableHead from '@material-ui/core/TableHead'; +import TableRow from '@material-ui/core/TableRow'; +import Typography from '@material-ui/core/Typography'; -const useStyles = makeStyles({ - root: { - maxWidth: 400, - }, - media: { - objectFit: 'cover', - }, -}); - -const ProductLinkCard = ({ product, user, useCredits }) => { +const ProductLinkCard = ({ link, product, user, onSendEmailClick, calculatedAmountDetails }) => { const [open, setOpen] = React.useState(false); - const generatedLink = useCredits - ? `${config.domain}buy?productId=${product.id}&oneauthId=${user.oneauth_id}&userCredits=true` - : `${config.domain}buy?productId=${product.id}&oneauthId=${user.oneauth_id}` - const handleClick = () => { - setOpen(true); - }; + setOpen(true); + } const handleClose = (event, reason) => { - if (reason === 'clickaway') { - return; - } + if (reason === 'clickaway') { + return; + } setOpen(false); - }; - - const classes = useStyles(); + } return ( -
- - + - - - - Product -
    -
  • Name: {product.name}
  • -
  • Description: {product.description}
  • -
  • Type: {product.type}
  • -
  • Mrp: {product.mrp / 100}
  • - -
-
- - User -
    -
  • UserName: {user.username}
  • -
  • OneauthId: {user.oneauth_id}
  • -
  • Email: {user.email}
  • -
  • Wallet Amount: {user.wallet_amount / 100}
  • -
-
-
-
- - - - {generatedLink} - - - - - - - - + + + + {product.description} + + + + + + Product Name + Product Mrp + Discount + Taxes + Credits + Amount To Pay + Type + + + + + + {product.name} + + {product.mrp / 100} + {calculatedAmountDetails.discount / 100} + {calculatedAmountDetails.tax /100} + {calculatedAmountDetails.creditsApplied /100 } + {calculatedAmountDetails.amount /100} + {product.type} + + +
+
+ + +
+
+ Product Buy Link: +
+ +
+ {link} + + + +
+ + +
+ +
+ { } /> -
+ +
-
); } diff --git a/controllers/products.js b/controllers/products.js index 2609b57..d20c5ed 100644 --- a/controllers/products.js +++ b/controllers/products.js @@ -1,6 +1,8 @@ import {axios} from "../DukaanAPI"; import ErrorHandler from "../helpers/ErrorHandler"; import organizationController from './organizations'; +import userController from './users'; + const querystring = require('querystring'); @@ -131,6 +133,26 @@ const fetchCenters = (organizationId) => { return organizationController.getOrganizationCenters(organizationId) } +const sendBuyLinkEmail = (data) => { + return axios.post("/api/v2/admin/products/productBuyLinkEmail", data) +} + +const getUserCartDetailsUrls = (data) => { + return userController.getUserCartDetailsUrls(data) +} + +const getProductBuyLinkData = (data) => { + return Promise.all ([ + getUserCartDetailsUrls({id: data.userId}), + handleCalculatePrice({ + oneauthId: data.oneauthId, + productId: data.productId, + quantity: data.quantity, + useCredits: data.useCredits + }) + ]) +} + module.exports = { handleGetProducts, handleAddProduct, @@ -138,5 +160,8 @@ module.exports = { handleCalculatePrice, searchProducts, fetchGenerateLinkData, - fetchCenters + fetchCenters, + sendBuyLinkEmail, + getUserCartDetailsUrls, + getProductBuyLinkData } diff --git a/controllers/users.js b/controllers/users.js index 3b4348f..924c505 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -60,11 +60,27 @@ const updateUserDetails = (values) => { }; +const getUsersActiveCartDetailsUrl = (data) => { + return axios.get(`api/v2/admin/users/activeCartDetails`, {params: data}) +} + +const getUsersPurchasedProductsDetialsUrl = (data) => { + return axios.get(`api/v2/admin/users/purchasedProductsDetails`, {params: data}) +} + +const getUserCartDetailsUrls = (data) => { + return Promise.all([ + getUsersActiveCartDetailsUrl(data), + getUsersPurchasedProductsDetialsUrl(data) + ]) +} + module.exports = { handleGetUserByEmailOrPhone, handleAddUser, handleGetUserById, getUsernameAvailability, getUserByFromOneAuthByOneAuthId, - updateUserDetails + updateUserDetails, + getUserCartDetailsUrls }; diff --git a/forms/ProductLink.js b/forms/ProductLink.js index 1188a1a..2a336d6 100644 --- a/forms/ProductLink.js +++ b/forms/ProductLink.js @@ -60,8 +60,17 @@ class ProductLinkForm extends React.Component { super(props) } - generateLink = () => { - this.props.ongenerateLinkClick() + generateLink = (fields) => { + + const productId = fields.product.id + const oneauthId = fields.user.oneauth_id + + let useCreditsQueryParams = '' + if (fields.applyCredits) + useCreditsQueryParams = '/useCredits=true' + + const link = `${config.domain}buy?productId=${productId}&oneauthId=${oneauthId}${useCreditsQueryParams}` + this.props.ongenerateLink(link) } render() { @@ -69,11 +78,11 @@ class ProductLinkForm extends React.Component { const { classes } = this.props; return ( -
+

- Generate + Generate Buy Link

@@ -96,7 +105,7 @@ class ProductLinkForm extends React.Component { onClose={() => { this.props.onAutoCompleteClose('user') }} loading={this.props.selectUserOpen && !this.props.userSearchResults.length} value={values.user} - onChange={(e, value) => { + onChange={ (e, value) => { this.props.handleUserChange(e, value) setFieldValue("user", value) } } diff --git a/pages/admin/products/generateLink.js b/pages/admin/products/generateLink.js index 134cf54..69a7d94 100644 --- a/pages/admin/products/generateLink.js +++ b/pages/admin/products/generateLink.js @@ -1,5 +1,7 @@ import React from 'react' import Head from '../../../components/head'; +import Backdrop from '@material-ui/core/Backdrop'; +import CircularProgress from '@material-ui/core/CircularProgress'; import Layout from "../../../components/layout"; import ProductLinkForm from "../../../forms/ProductLink"; import CheckLogin from "../../../components/CheckLogin"; @@ -8,8 +10,27 @@ import * as userController from '../../../controllers/users' import ProductLinkCard from "../../../components/ProductLinkCard" import ErrorHandler from "../../../helpers/ErrorHandler"; import Swal from 'sweetalert2'; +import { withStyles } from '@material-ui/core'; +import Table from '@material-ui/core/Table'; +import TableBody from '@material-ui/core/TableBody'; +import TableCell from '@material-ui/core/TableCell'; +import TableContainer from '@material-ui/core/TableContainer'; +import Paper from '@material-ui/core/Paper'; +import TableHead from '@material-ui/core/TableHead'; +import TableRow from '@material-ui/core/TableRow'; +import Typography from '@material-ui/core/Typography'; +const useStyles = theme => ({ + backdrop: { + zIndex: theme.zIndex.drawer + 1, + color: '#fff', + }, + title: { + // minWidth: 650, + }, +}); + class GenerateLink extends React.Component { constructor(props) { @@ -28,12 +49,19 @@ class GenerateLink extends React.Component { product:'', user:'', useCredits:false, - generateLinkClicked:false + generatedLink: '', + generateLinkClicked:false, + activeCartIframeUrl: '', + purchasedProductIframeurl: '', + calculatedAmountDetails: '', + loading: false, } } componentDidMount() { - controller.fetchGenerateLinkData().then((organizations) => { + controller.fetchGenerateLinkData({ + user_id: this.state.user_id + }).then((organizations) => { this.setState({ organizations: organizations.data, }) @@ -126,8 +154,10 @@ class GenerateLink extends React.Component { handleProductChange = async (event, value) => { this.setState({ - product: value - }); + product: value, + }) + + this.unsetGeneratedLink() } @@ -157,37 +187,90 @@ class GenerateLink extends React.Component { } } - handleUserChange = (event, value) => { + handleUserChange = (event, value) => { + this.setState({ user: value, - }); + }) + this.unsetGeneratedLink() } onApplyCreditsChange = (event) => { this.setState({ useCredits: !JSON.parse(event.target.value) }) + this.unsetGeneratedLink() + } + + ongenerateLink = (link) => { + + controller.getProductBuyLinkData({ + userId: this.state.user.id, + oneauthId: this.state.user.oneauth_id, + productId: this.state.product.id, + quantity: 1, + useCredits: this.state.useCredits + }).then(([ [activeCartDetails, purchasedProductDetails], calculatedAmountDetails]) => { + this.setState({ + activeCartIframeUrl: activeCartDetails.data.iframeUrl, + purchasedProductIframeurl: purchasedProductDetails.data.iframeUrl, + generatedLink: link, + generateLinkClicked: true, + calculatedAmountDetails: calculatedAmountDetails.data, + loading: true + }) + }) } - ongenerateLinkClick = () => { + unsetGeneratedLink = () => { this.setState({ - generateLinkClicked: true + generatedLink: '', + generateLinkClicked: false }) } + onSendEmailClick = () => { + controller.sendBuyLinkEmail({ + user_id: this.state.user.id, + link: this.state.generatedLink + }).then((response) => { + Swal.fire({ + title: "Success", + text: "Email sent successfully!", + icon: "success", + }); + }).catch((err) => { + Swal.fire({ + type: "error", + title: "Error", + text: "Error sending email!" + }); + }) + } + + hideSpinner = () => { + this.setState({ + loading: false + }); + }; + render() { + const { classes } = this.props; + return (
+ + -
-
-

Create Buy Link

-
- -
+ + + + +
+
-
+ { this.state.generateLinkClicked && +
- { this.state.generateLinkClicked && this.state.product && this.state.user && - - } -
+ {!this.state.loading && + +
+ + + User Details + + + + + + Name + Username + Wallet Amount + Oneauth Id + Email + Mobile Number + Address + + + + + + {this.state.user.firstname} {this.state.user.lastname} + + {this.state.user.username} + {this.state.user.wallet_amount /100} + {this.state.user.oneauth_id} + {this.state.user.email} + {this.state.user.mobile_number} + {this.state.user.permanent_address} + + +
+
+
+ } + +
+ +
+ +
+ +
+
+ + {!this.state.loading && + + } +
+ +
+ }
+
) } } -export default GenerateLink +export default withStyles(useStyles)(GenerateLink)