Skip to content

Commit

Permalink
Page de confirmation de la commande ko #16
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Jun 27, 2024
1 parent 31a0364 commit a6e2a4f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
14 changes: 0 additions & 14 deletions TODO

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import eu.viandeendirect.domains.payment.StripePaymentRepository;
import eu.viandeendirect.domains.payment.StripeService;
import eu.viandeendirect.domains.user.CustomerRepository;
import eu.viandeendirect.domains.user.CustomerService;
import eu.viandeendirect.model.*;
import eu.viandeendirect.domains.production.PackageLotRepository;
import eu.viandeendirect.security.AuthenticationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -43,14 +45,34 @@ public class OrderService implements OrdersApiDelegate {
@Autowired
private StripePaymentRepository stripePaymentRepository;

@Autowired
private AuthenticationService authenticationService;

@Override
public ResponseEntity<Order> getOrder(Integer orderId) {
Order order = orderRepository.findById(orderId).get();
checkOrderAccess(order);
List<OrderItem> items = orderItemRepository.findByOrder(order);
order.setItems(items);
return new ResponseEntity<>(order, HttpStatus.OK);
}

private void checkOrderAccess(Order order) {
Customer customer = authenticationService.getAuthenticatedCustomer();
if (customer != null) {
if (!order.getCustomer().equals(customer)) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
return;
}
Producer producer = authenticationService.getAuthenticatedProducer();
if (producer!= null) {
if (order.getItems().stream().noneMatch(item -> item.getPackageLot().getProduction().getProducer().equals(producer))) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
}
}

@Override
public ResponseEntity<Order> createOrder(Order order) {
loadCustomer(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class SaleService implements SalesApiDelegate {

@Autowired
private OrderRepository orderRepository;

@Autowired
private ProductionRepository productionRepository;

Expand Down
22 changes: 19 additions & 3 deletions frontend/app/src/layouts/customer/CustomerLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { AppBar, Box, CssBaseline, Toolbar, Typography } from '@mui/material'
import { AppBar, Box, CssBaseline, IconButton, Toolbar, Typography } from '@mui/material'
import { useEffect, useState } from 'react'

import { useCookies } from 'react-cookie'
Expand All @@ -15,6 +15,7 @@ import { AuthenticationService } from '../../authentication/AuthenticationServic
import CustomerOrderForm from '../../domains/sale/views/CustomerOrderForm.tsx'
import CustomerCreationForm from '../../domains/customer/views/CustomerCreationForm.tsx'
import Welcome from '../../domains/welcome/Welcome.tsx'
import { Login, Logout } from '@mui/icons-material'


export default function CustomerLayout() {
Expand Down Expand Up @@ -53,7 +54,7 @@ export default function CustomerLayout() {
}
}

function renderMainContent() {
function displayMainContent() {
if(authenticationService.isAuthenticated() && customer && !customer.id) {
return <CustomerCreationForm customer={customer} returnCallback={newCustomer => setCustomer(newCustomer)}></CustomerCreationForm>
} else if(cookies.pendingOrder ) {
Expand All @@ -71,6 +72,20 @@ export default function CustomerLayout() {
setMainContent(ORDER_CREATION)
}

function displayAuthenticationButton(): React.ReactNode {
if (authenticationService.isAuthenticated()) {
return <>
<Typography>{authenticationService.getCurrentUserFirstName() } {authenticationService.getCurrentUserLastName()}</Typography>
<IconButton onClick={keycloak.logout} color="inherit">
<Logout/>
</IconButton>
</>
}
return <IconButton onClick={keycloak.login} color="inherit">
<Login/>
</IconButton>
}

return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
Expand All @@ -84,9 +99,10 @@ export default function CustomerLayout() {
<Typography variant="h5" component="div" sx={{ flexGrow: 1 }}>
Viande en direct
</Typography>
{displayAuthenticationButton()}
</Toolbar>
</AppBar>
{renderMainContent()}
{displayMainContent()}
</Box>
)
}
45 changes: 41 additions & 4 deletions frontend/app/src/layouts/customer/PaymentLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ import { AppBar, Box, Button, CircularProgress, CssBaseline, Toolbar, Typography

import OrderSummary from '../../domains/sale/components/OrderSummary.tsx'
import Order from 'viandeendirect_eu/dist/model/Order.js';
import { AuthenticationService } from '../../authentication/AuthenticationService.ts';


export default function PaymentLayout() {
const { keycloak, initialized } = useKeycloak()
const apiBuilder = new ApiBuilder()
const authenticationService = new AuthenticationService(keycloak)
let { orderId } = useParams()

const [order, setOrder] = useState<Order>()
const [orderAccessError, setOrderAccessError] = useState<Boolean>(false)
const [forbiddenAccess, setForbiddenAccess] = useState<Boolean>(false)
let intervalId = 0

useEffect(() => {
intervalId = setInterval(loadOrder, 1000)
if (initialized) {
intervalId = setInterval(loadOrder, 1000)
}
}, [initialized])

function loadOrder() {
Expand All @@ -29,6 +35,11 @@ export default function PaymentLayout() {
api.getOrder(orderId, (error, orderLoaded, response) => {
if (error) {
console.error(error)
if (response.statusCode === 403) {
setForbiddenAccess(true)
setOrderAccessError(true)
clearInterval(intervalId)
}
} else {
console.log('api.getOrder called successfully. Returned data: ' + orderLoaded)
if (orderLoaded.status !== 'PAYMENT_PENDING') {
Expand Down Expand Up @@ -58,14 +69,40 @@ export default function PaymentLayout() {
</AppBar>
<Box component="main" sx={{ flexGrow: 1, p: 3 }}>
<Toolbar />
{getContent()}
</Box>
</Box>
)

function getContent() {
if (orderAccessError) {
if (forbiddenAccess) {
return <>
<Typography variant='h5'>Désolé, vous n'êtes pas autorisé à consulter cette commande</Typography>
<Button variant='contained' onClick={() => window.open('/', '_self')}>Retour à l'accueil</Button>
</>
} else {
return <>
<Typography variant='h5'>Oups... une erreur s'est produite lors de l'accès à la commande {orderId}</Typography>
<Button variant='contained' onClick={() => window.open('/', '_self')}>Retour à l'accueil</Button>
</>
}
}
if (authenticationService.isAuthenticated()) {
return <>
<div>
{getPaymentState()}
{getOrderSummary()}
</div>
<Button variant='contained' onClick={() => window.open('/', '_self')}>Retour à l'accueil</Button>
</Box>
</Box>
)
</>
} else {
return <>
<Typography variant='h5'>Vous devez vous identifier pour voir l'état de votre commande</Typography>
<Button variant='contained' onClick={() => keycloak.login()}>S'identifier</Button>
</>
}
}

function getPaymentState(){
if (order) {
Expand Down

0 comments on commit a6e2a4f

Please sign in to comment.