Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/create cart #19

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/sylius/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { REST_METHODS, SYLIUS_API_ENDPOINT } from 'lib/constants';
import { normalizeCart } from './normalizer/cart-normalizer';
import { normalizeCollection } from './normalizer/collection-normalizer';
import { normalizeProduct } from './normalizer/product-normalizer';
import { SyliusProduct, SyliusTaxon } from './sylius-types/product-types';
import { AddToCartPayload, Collection, GetCollectionProductsPayload, GetProductsPayload } from './types';
import {
AddToCartPayload,
Cart,
Collection,
GetCollectionProductsPayload,
GetProductsPayload
} from './types';

const DOMAIN = `${process.env.SYLIUS_STORE_DOMAIN}`;
const ENDPOINT = `${DOMAIN}${SYLIUS_API_ENDPOINT}`;
Expand Down Expand Up @@ -146,9 +153,11 @@ export const getCollectionProducts = async (payload: GetCollectionProductsPayloa
};

// Cart
export const createCart = async () => {
const cart = await syliusRequest(REST_METHODS.POST, '/orders', { localeCode: 'fr_FR' });
return cart;
export const createCart = async (): Promise<Cart> => {
const data = await syliusRequest(REST_METHODS.POST, '/orders', { localeCode: 'fr_FR' });
const syliusCart = data.body;

return normalizeCart(syliusCart);
};
export const getCart = (cartId: string) => {
syliusRequest(REST_METHODS.GET, `/orders/${cartId}`);
Expand All @@ -162,13 +171,13 @@ export const removeFromCart = () => {};
export const updateCart = () => {};

// Site
export const getMenu = async () => {
export const getMenu = async () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

faudrait pas juste qu'on configure le prettier ? surtout si j'arrive à faire marcher mon front et que je commence à coder haha

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si 👍

const collections = await getCollections();
return [
{
title: 'All',
path: '/search'
},
...collections.slice(0,2).map(({ title, path }) => ({ title, path }))
...collections.slice(0, 2).map(({ title, path }) => ({ title, path }))
];
};
50 changes: 50 additions & 0 deletions lib/sylius/normalizer/cart-normalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { SyliusCart, SyliusCartItem } from '../sylius-types/cart-types';
import { SyliusProductOption, SyliusProductOptionValue } from '../sylius-types/product-types';
import { Cart, CartItem } from '../types';
import { normalizeProduct } from './product-normalizer';
import { normalizePrice } from './utils-normalizer';

export const normalizeCart = (syliusCart: SyliusCart): Cart => {
return {
id: syliusCart.tokenValue,
checkoutUrl: '',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pourquoi on la laisse vide là ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parce que le checkout est pas géré pour le moment et j'ai aucune idée de ce qu'on va faire :D

cost: {
subtotalAmount: normalizePrice(syliusCart.itemsTotal),
totalAmount: normalizePrice(syliusCart.total),
totalTaxAmount: normalizePrice(syliusCart.taxTotal)
},
lines: syliusCart.items.map((item) => normalizeCartItem(item)),
totalQuantity: syliusCart.items.reduce((acc, item) => acc + item.quantity, 0)
};
};

const normalizeCartItem = (syliusCartItem: SyliusCartItem): CartItem => {
return {
id: syliusCartItem.id.toString(),
quantity: syliusCartItem.quantity,
cost: {
totalAmount: normalizePrice(syliusCartItem.total)
},
merchandise: {
id: syliusCartItem.variant.id.toString(),
title: syliusCartItem.variant.name,
selectedOptions: syliusCartItem.variant.optionValues.map((optionValue) =>
normalizeOrderItemOptionValue(optionValue, syliusCartItem.product.options)
),
product: normalizeProduct(syliusCartItem.product)
}
};
};

const normalizeOrderItemOptionValue = (
optionValue: SyliusProductOptionValue,
options: SyliusProductOption[]
): { name: string; value: string } => {
const rightOption = options.filter((option) =>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je pense qu'en terme de naming option ça passe, et tu peux utiliser option.value pour le return
parce que là il faut relire / se souvenir que optionValue.value === option.value

option.values.some((value) => value.code === optionValue.code)
)[0];
return {
name: rightOption?.name ?? '',
value: optionValue.value
};
};
8 changes: 2 additions & 6 deletions lib/sylius/normalizer/product-normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
SyliusProductOption,
SyliusProductVariant
} from '../sylius-types/product-types';
import { Image, Money, Product, ProductOption, ProductVariant } from '../types';
import { Image, Product, ProductOption, ProductVariant } from '../types';
import { normalizePrice } from './utils-normalizer';

export const normalizeProduct = (product: SyliusProduct): Product => ({
seo: {
Expand Down Expand Up @@ -51,11 +52,6 @@ export const normalizeProductImage = (image: SyliusProductImage): Image => ({
height: 400
});

const normalizePrice = (amount: number): Money => ({
amount: (amount / 100).toString(),
currencyCode: 'EUR'
});

const normalizePriceRange = (product: SyliusProduct) => {
let minVariantPrice = 0;
let maxVariantPrice = 0;
Expand Down
6 changes: 6 additions & 0 deletions lib/sylius/normalizer/utils-normalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Money } from '../types';

export const normalizePrice = (amount: number): Money => ({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on pourrait mettre la currency en param optionnel avec EUR en défaut ?
et peut etre centsPriceToUnitsPrice ? je sais pas trop dans quel contexte on va vouloir appeler cette fonction mais vu que c'est dans utils autant la rendre un peu générique

amount: (amount / 100).toString(),
currencyCode: 'EUR'
});
35 changes: 35 additions & 0 deletions lib/sylius/sylius-types/cart-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SyliusProduct, SyliusProductOptionValue, SyliusProductVariant } from './product-types';

export interface SyliusCart {
id: number;
tokenValue: string;
currencyCode: string;
taxTotal: number;
itemsTotal: number;
total: number;
taxExcludedTotal: number;
taxIncludedTotal: number;
shippingTotal: number;
orderPromotionTotal: number;
items: SyliusCartItem[];
}

export interface SyliusCartItem {
id: number;
productName: string;
quantity: number;
unitPrice: number;
discountedUnitPrice: number;
variant: SyliusProductVariant;
optionValues: SyliusProductOptionValue[];
product: SyliusProduct;
adjustments: SyliusAdjustment[];
total: number;
}

export interface SyliusAdjustment {
id: number;
type: string;
label: string;
amount: number;
}