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 1 commit
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
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'
});