From 8954e0b819f5ed44aca17ea5b1a280d988b4ade2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAFOR=C3=8AT=20Nicolas?= <nlaforet@etu.unistra.fr> Date: Fri, 17 Dec 2021 13:57:19 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20cart=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/back.ts | 18 ++++++++------- src/apis/money.ts | 26 ++++++++++++++++++++-- src/apis/student.ts | 23 +++++++++---------- src/cart/CartTypes.ts | 1 + src/components/sell/cart.tsx | 43 ++++++++++++++++++++++-------------- 5 files changed, 73 insertions(+), 38 deletions(-) diff --git a/src/apis/back.ts b/src/apis/back.ts index 9604a7c..8fbca45 100644 --- a/src/apis/back.ts +++ b/src/apis/back.ts @@ -22,6 +22,7 @@ export async function getItems(): Promise<Product[]> { // Format products data const products = stocks.map(stock => new Product({ + id: stock.item.id, name: stock.item.name, category: stock.item.category.name, price: stock.item.price, @@ -29,24 +30,25 @@ export async function getItems(): Promise<Product[]> { stock: stock.quantity })); + // console.table(products); + return products; } -export async function setStock(name: number, stock: number): Promise<void> { +export async function updateStock(itemId: number, stock: number): Promise<boolean> { const response = await axios.put(`${BASE_URL}/stock`, { - item_id: name, + item_id: itemId, quantity: stock }, { headers: { 'Content-Type': 'application/json', 'apikey': APIKEY, } - }) - .then(({ data }) => data) - .catch(error => { - console.error(error); - return "Error"; - }); + }).then(({ status }) => status === 200) + .catch(error => { + console.error(error); + return false; + }); return response; }; diff --git a/src/apis/money.ts b/src/apis/money.ts index d8f0e0a..fae3ef2 100644 --- a/src/apis/money.ts +++ b/src/apis/money.ts @@ -5,7 +5,7 @@ const BASE_URL = "https://" + process.env.REACT_APP_MONEY_URL; // Fetch transactions from API export async function getTransactions(studentNumber: string): Promise<any> { // Fetch transactions data - const transactionsData: any[] = await axios.get(`${BASE_URL}/api/etudiants`, { + const transactionsData: any[] = await axios.get(`${BASE_URL}/transactions/get/${studentNumber}`, { headers: { 'Content-Type': 'application/json', } @@ -19,4 +19,26 @@ export async function getTransactions(studentNumber: string): Promise<any> { // TODO: Format transaction data return transactionsData; -} \ No newline at end of file +} + +export async function sendTransaction(studentNumber: string, userName: string, price: number): Promise<boolean> { + return await axios.post(`${BASE_URL}/payments/add/mastercard`, { + student_number: parseInt(studentNumber), + user_name: userName, + payment_method: "TODO", + payment_name: "mastercard", + identifier: "123454678", + amount: price, + }, { + headers: { + 'Content-Type': 'application/json', + } + }).then((res) => { + console.log(res); + return res.status === 200; + }) + .catch(error => { + console.error(error); + return false; + }); +} diff --git a/src/apis/student.ts b/src/apis/student.ts index 7ceecda..8a48163 100644 --- a/src/apis/student.ts +++ b/src/apis/student.ts @@ -48,22 +48,21 @@ export async function getUsers(): Promise<User[]> { return users; }; -export async function setAdhesion() : Promise<number> { - // Fetch users data - await axios.post(`${BASE_URL}/api/pay_adhesion`, { +export async function setAdhesion(studentNumber: string) : Promise<boolean> { + // Set adhesion for given user + const success = await axios.post(`${BASE_URL}/api/pay_adhesion`, { + 'student_number': studentNumber ?? '12345678' + }, { headers: { 'Content-Type': 'application/json', - }, - body: { - 'student_number': '12345678', } - }).then(({ data }) => data) - .catch(error => { - console.error(error); - return 0; - }); + }).then(({ status }) => status === 200) + .catch(error => { + console.error(error); + return false; + }); - return 1; + return success; } function validateSubscriptionDate(dateStr: string): boolean { diff --git a/src/cart/CartTypes.ts b/src/cart/CartTypes.ts index 17bbb86..c154c65 100644 --- a/src/cart/CartTypes.ts +++ b/src/cart/CartTypes.ts @@ -1,6 +1,7 @@ import { Dispatch } from "react"; export class Product { + public id: number; public name: string; public category: string; public price: number; diff --git a/src/components/sell/cart.tsx b/src/components/sell/cart.tsx index 4395f49..8e01cf5 100644 --- a/src/components/sell/cart.tsx +++ b/src/components/sell/cart.tsx @@ -6,7 +6,9 @@ import { XIcon } from '@heroicons/react/outline' import { cartContext } from '../../cart/CartStore'; import { updateProductQuantity } from '../../cart/CartActions'; -import {setAdhesion} from '../../apis/student'; +import { setAdhesion } from '../../apis/student'; +import { sendTransaction } from '../../apis/money'; +import { updateStock } from '../../apis/back'; const Cart = () => { const { cartState, dispatch } = useContext(cartContext); @@ -16,27 +18,36 @@ const Cart = () => { return `${price.toFixed(2).replace(".", ",")} €`; } - function doValidate(): number { - const existingProduct = cartState.cart.find(p => p.name === "Adhésion"); - if (existingProduct){ - setAdhesion().then((users) => { - console.log("Envoi adhésion à STUDENT: 12345678"); - }).catch((_) => { - console.log("error adhesion"); - }); + async function doValidate() { + const studentNumber: string = "12345678"; + const userName: string = "UserName"; + + // Validate transaction with Money + const successTransaction = await sendTransaction(studentNumber, userName, cartState.totalPrice); + if (!successTransaction) { + console.error("Error during transaction with Money."); + return; } - cartState.totalPrice = 0; - cartState.cart.forEach((product) => { - if (product.quantity) { - cartState.totalPrice += product.price * product.quantity; + + // Set adhesion in Student + const adhesionInCart = cartState.cart.find(p => p.name === "Adhésion"); + if (adhesionInCart !== undefined){ + const adhesionSuccess = await setAdhesion(studentNumber); + if (!adhesionSuccess) { + console.error("Error during adhesion with Student."); } + } + + // Update stock in Back + cartState.cart.forEach(product => { + updateStock(product.id, -(product?.quantity ?? 1)); }); - console.log("Envoi transaction à MONEY: ", cartState); - return 1; + + // Reset cart + dispatch({ type: 'SET_CART', payload: [] }); } let [totalPrice, setTotalPrice] = useState(0); - useEffect(() => { // Update cart totalPrice let totalPrice = 0; -- GitLab