Skip to content

Commit

Permalink
#993 [Storefront] Reformat order flow to use api client service (#1040)
Browse files Browse the repository at this point in the history
* Reformat storefront service to use apiClientService


---------

Co-authored-by: Tuan Nguyen Trong Anh <tuan.nguyentronganh@nashtechglobal.com>
  • Loading branch information
HnKnA and Tuan Nguyen Trong Anh authored Sep 18, 2024
1 parent 02e6257 commit ec3a226
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 58 deletions.
50 changes: 23 additions & 27 deletions storefront/modules/cart/services/CartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,53 @@ import { ProductThumbnail } from '../../catalog/models/ProductThumbnail';
import { AddToCartModel } from '../models/AddToCartModel';
import { Cart } from '../models/Cart';
import { UpdateCartModel } from '../models/UpdateCartModel';
import apiClientService from '@/common/services/ApiClientService';

const baseUrl = '/api/cart';

export async function addToCart(addToCart: AddToCartModel[]): Promise<Cart> {
const response = await fetch(`/api/cart/storefront/carts`, {
method: 'POST',
body: JSON.stringify(addToCart),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.post(
`${baseUrl}/storefront/carts`,
JSON.stringify(addToCart)
);
if (response.status >= 200 && response.status < 300) return await response.json();
return Promise.reject(response);
throw response;
}

export async function getCart(): Promise<Cart> {
const response = await fetch('/api/cart/storefront/carts', {
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.get(`${baseUrl}/storefront/carts`);
if (response.status >= 200 && response.status < 300) return await response.json();
return Promise.reject(response);
throw response;
}

export async function getCartProductThumbnail(ids: number[]): Promise<ProductThumbnail[]> {
const response = await fetch(`api/product/storefront/products/list-featured?productId=` + ids, {
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.get(
`/api/product/storefront/products/list-featured?productId=${ids}`
);
return await response.json();
}

export async function removeProductInCart(productId: number) {
const response = await fetch('/api/cart/storefront/cart-item?productId=' + productId, {
method: 'DELETE',
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.delete(
`${baseUrl}/storefront/cart-item?productId=${productId}`
);
if (response.status === 204) return response;
else return await response.json();
}

export async function updateCart(updateCartBody: AddToCartModel): Promise<UpdateCartModel> {
const response = await fetch('/api/cart/cart-item', {
method: 'PUT',
body: JSON.stringify(updateCartBody),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.put(
`${baseUrl}/cart-item`,
JSON.stringify(updateCartBody)
);

if (response.status >= 200 && response.status < 300) return await response.json();

return Promise.reject(response);
throw response;
}

export async function getNumberCartItems(): Promise<number> {
const response = await fetch('/api/cart/storefront/count-cart-items', {
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.get(`${baseUrl}/storefront/count-cart-items`);
if (response.status >= 200 && response.status < 300) return await response.json();
return Promise.reject(response);
throw response;
}
34 changes: 12 additions & 22 deletions storefront/modules/order/services/OrderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,39 @@ import { Checkout } from '../models/Checkout';
import { EOrderStatus } from '../models/EOrderStatus';
import { Order } from '../models/Order';
import { OrderGetVm } from '../models/OrderGetVm';
import apiClientService from '@/common/services/ApiClientService';

const baseUrl = '/api/order/storefront';

export async function createOrder(order: Order): Promise<Order | null> {
const response = await fetch('/api/order/storefront/orders', {
method: 'POST',
body: JSON.stringify(order),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.post(`${baseUrl}/orders`, JSON.stringify(order));
if (response.status >= 200 && response.status < 300) {
return await response.json();
}
return Promise.reject(response.status);
throw new Error(response.statusText);
}

export async function getMyOrders(
productName: string,
orderStatus: EOrderStatus | null
): Promise<OrderGetVm[]> {
const res = await fetch(
`/api/order/storefront/orders/my-orders?productName=${productName}&orderStatus=${
orderStatus ?? ''
}`
const res = await apiClientService.get(
`${baseUrl}/orders/my-orders?productName=${productName}&orderStatus=${orderStatus ?? ''}`
);
if (res.status >= 200 && res.status < 300) return res.json();
return Promise.reject(res);
throw res;
}

export async function createCheckout(checkout: Checkout): Promise<Checkout | null> {
const response = await fetch('/api/order/storefront/checkouts', {
method: 'POST',
body: JSON.stringify(checkout),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.post(`${baseUrl}/checkouts`, JSON.stringify(checkout));
if (response.status >= 200 && response.status < 300) {
return await response.json();
}
return Promise.reject(response.status);
throw new Error(response.statusText);
}

export async function getCheckoutById(id: string) {
const response = await fetch('/api/order/storefront/checkouts/' + id, {
method: 'GET',
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const response = await apiClientService.get(`${baseUrl}/checkouts/${id}`);
if (response.status >= 200 && response.status < 300) return response.json();
return Promise.reject(response.status);
throw new Error(response.statusText);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PaymentProvider } from '../models/PaymentProvider';
import apiClientService from '@/common/services/ApiClientService';

export const getEnabledPaymentProviders = async (): Promise<PaymentProvider[]> => {
const response = await fetch('/api/payment/storefront/payment-providers');
const response = await apiClientService.get('/api/payment/storefront/payment-providers');
return response.json();
};
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { InitPaymentPaypalRequest } from '@/modules/paymentPaypal/models/InitPaymentPaypalRequest';
import { InitPaymentPaypalResponse } from '@/modules/paymentPaypal/models/InitPaymentPaypalResponse';
import { CapturePaymentPaypalResponse } from '@/modules/paymentPaypal/models/CapturePaymentPaypalResponse';
import apiClientService from '@/common/services/ApiClientService';

const baseUrl = '/api/payment-paypal';

export async function initPaymentPaypal(
paymentPaypalRequest: InitPaymentPaypalRequest
): Promise<InitPaymentPaypalResponse> {
const res = await fetch('/api/payment-paypal/init', {
method: 'POST',
body: JSON.stringify(paymentPaypalRequest),
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});
const res = await apiClientService.post(`${baseUrl}/init`, JSON.stringify(paymentPaypalRequest));
if (res.ok) {
return res.json();
}
return Promise.reject(res.status);
throw new Error(res.statusText);
}

export async function capturePaymentPaypal(token?: string): Promise<CapturePaymentPaypalResponse> {
const res = await fetch(`/api/payment-paypal/capture?token=${token}`);
const res = await apiClientService.get(`${baseUrl}/capture?token=${token}`);
if (res.ok) {
return res.json();
}
return Promise.reject(res.status);
throw new Error(res.statusText);
}

0 comments on commit ec3a226

Please sign in to comment.