Commit fdecf06b authored by Christopher Cottier's avatar Christopher Cottier

Merge branch 'AFP-127/add-promotions-to-post-order' into 'master'

Afp 127/add promotions to post order

See merge request !15
parents 5e676eed 4b3273ec
......@@ -8,6 +8,9 @@ import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
......@@ -24,7 +27,6 @@ public class OrderService {
User user = orderRequest.getUser();
Address address = orderRequest.getAddress();
CartPostDTO cart = orderRequest.getCart();
//for each item grab product details from products API
List<Mono<Product>> productsToOrder = cart.getCartItems().stream()
.map(cartItem -> cartItem.getProductRef().getSku())
......@@ -50,8 +52,27 @@ public class OrderService {
orderItem.setItemQuantity(quantity);
return orderItem;
}).flatMap(orderItem -> {
Flux<Promotion> promotionFlux = productService
.getPromotionBySkus(Arrays.asList(orderItem.getItemSku()));
Mono<OrderItem> orderItemMono = promotionFlux
.collectList()
.map(promotionList -> {
System.out.println("Give me promo list");
if (promotionList.size() > 0) {
Promotion promotion = promotionList.get(0);
if (orderItem.getItemQuantity().intValue() >= promotion.getMinimumQuantity().intValue()) {
Float originalPrice = orderItem.getItemPrice();
Float newPrice = originalPrice * ((100 - promotion.getDiscountPercentage()) / 100);
orderItem.setItemPrice(newPrice);
}
}
return orderItem;
});
return orderItemMono;
});
//
//falttens the things
Mono<Mono<Order>> map = orderItems.collectList().map(itemList -> {
OrderSubmission orderSubmission = new OrderSubmission();
......
......@@ -8,6 +8,8 @@ import reactor.core.publisher.Mono;
import com.nisum.ecomservice.model.Promotion;
import reactor.core.publisher.Flux;
import java.util.List;
@Service
public class ProductService {
......@@ -40,4 +42,12 @@ public class ProductService {
.bodyToFlux(Promotion.class);
}
public Flux<Promotion> getPromotionBySkus(List<String> listOfSkus){
return WebClient.create(String.format("%s/api/promos/bulkSearch",AppConfig.getPromoManagementUrl()))
.post()
.bodyValue(listOfSkus)
.retrieve()
.bodyToFlux(Promotion.class);
}
}
import OrderHistory from "../components/order-history/order-history";
import { getOrderHistory } from "../util/order_api_util";
export const SET_USER_HISTORY = "SET_USER_HISTORY";
export const dispatchOrderHistory = (userId) => async dispatch =>{
const orderHistory = await getOrderHistory(userId);
return dispatch(orderHistoryAction(orderHistory));
}
const orderHistoryAction = (orderHistory) => (
{
type: SET_USER_HISTORY,
orderHistory
}
)
\ No newline at end of file
......@@ -23,3 +23,9 @@ main {
.order-field {
font-weight: 600;
}
#order-history-header {
font-size: 40px;
text-align: center;
padding: 10px;
}
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux';
import { dispatchOrderHistory } from '../../actions/order_history_actions';
import { getOrderHistory } from '../../util/order_api_util';
import Order from './Order';
......@@ -6,20 +8,26 @@ import './order-history.css'
const OrderHistory = () => {
const [orderHistory, setOrderHistory] = useState([]);
const [callMade, setCallMade] = useState(false);
const dispatch = useDispatch();
const user = useSelector(state => state.user);
const {orderHistory} = useSelector(state => state.orderStatus)
//const [callMade, setCallMade] = useState(false);
useEffect( async () => {
if (callMade) return;
const userHistory = await getOrderHistory('e-com-test-id2'); //insert user id from user details stored in redux
setOrderHistory(userHistory);
setCallMade(true);
});
if (!user) return;
if (!user.currentUser) return;
dispatch(dispatchOrderHistory(user.currentUser.userId));
//setCallMade(true);
}, [user]);
return (
<main>
<h1>Order History</h1>
<h1 id="order-history-header">Order History</h1>
{user === null ? (
<div>Please login to place orders!</div>
) : null}
<div id="orders">
{orderHistory.map(order => {
return <Order order={order}/>;
......
import { SET_USER_HISTORY } from '../actions/order_history_actions';
import { LOGOUT_USER } from '../actions/session_actions';
import {SEND_USER_ORDER} from './../actions/checkout_actions'
const initialState = {
orderResponse: {},
orderHistory: []
}
const orderReducer = (prevState = initialState, action) => {
......@@ -13,6 +16,12 @@ const orderReducer = (prevState = initialState, action) => {
nextState.orderResponse = action.payload
return nextState
case SET_USER_HISTORY:
nextState.orderHistory = action.orderHistory;
return nextState;
case LOGOUT_USER:
nextState.orderHistory = [];
return nextState;
default:
return nextState
}
......
......@@ -2,37 +2,36 @@ import Config from "../config";
export const getOrderHistory = async (userId) => {
//This is what we will return once our oder history API is operational
// const res = await fetch(`${Config.orderHistoryApiUrlMethod(userId)}`);
// const orders = await res.json();
// return orders;
const res = await fetch(`${Config.orderHistoryApiUrlMethod(userId)}`);
const orders = await res.json();
return orders;
return (
[
{
id: "609968a89cc32e1ef8208e8b",
orderTrackingCode: "N/A",
orderStatus: "RECEIVED",
orderCreatedAt: 1620666536727,
orderUpdatedAt: 1620666536727,
customerId: "e-com-test-id2",
customerEmailAddress: "test@test.test",
orderItems: [
{
itemId: "AFP-989",
itemName: "this item",
itemSku: "AFP-989",
itemQuantity: 2,
itemPrice: 14.0
}
],
customerAddress: {
street: "Grand",
city: "chicago",
state: "IL",
zip: "90210"
}
}
]
)
// return (
// [
// {
// id: "609968a89cc32e1ef8208e8b",
// orderTrackingCode: "N/A",
// orderStatus: "RECEIVED",
// orderCreatedAt: 1620666536727,
// orderUpdatedAt: 1620666536727,
// customerId: "e-com-test-id2",
// customerEmailAddress: "test@test.test",
// orderItems: [
// {
// itemId: "AFP-989",
// itemName: "this item",
// itemSku: "AFP-989",
// itemQuantity: 2,
// itemPrice: 14.0
// }
// ],
// customerAddress: {
// street: "Grand",
// city: "chicago",
// state: "IL",
// zip: "90210"
// }
// }
// ]
// )
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment