diff --git a/src/apis/back.ts b/src/apis/back.ts index 9604a7c56c91a7ad0e563ce506922e4de8da2720..8fbca4530ec5ef6365058e511d8e4b208b5f0d6d 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 d8f0e0a290d1ed7ae41a8519159ecd619c8d33a0..fae3ef252a0c92a60e12da99882ccad8a0b48723 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 7ceecda1dbbeeea02548b656f269145f55194427..8a48163b782f2be9486c228db221d2424de0c0ff 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 17bbb867b7b97c05c0b05f48da89a2bcc7549912..c154c65393facc6cf9435554274e0fff00becf12 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 4395f49e11977205d4cc49051220fc9d682ca428..8e01cf562df08444efc9315a0bf32681bddb4178 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;