Skip to content

Instantly share code, notes, and snippets.

@sambhav2612
Created August 23, 2020 08:24
Show Gist options
  • Save sambhav2612/eb8f2d8efd997868008b1cf09e520971 to your computer and use it in GitHub Desktop.
Save sambhav2612/eb8f2d8efd997868008b1cf09e520971 to your computer and use it in GitHub Desktop.
/**
*
* @typedef {{Cart: Object, AccessToken: String}} CartAndAccessToken
*
* @module addProductToCart - API to add product to active cart and create and add if not present.
* @param {String} product - the ID of product to add to cart (required)
* @param {Number} quantity - the quantity of product to add to cart (required)
*
* @returns {CartAndAccessToken}
*/
exports.addProductToCart = {
schema: "addProductToCart(product: ID!, quantity: Int!): CartAndAccessToken",
resolver: async (obj, {product, quantity}, context, info, {query}) => {
let cartType = {};
let user, cart, coupon = null;
console.log("addProductToCart ---------------------", product, quantity);
const decodedUser = context.req.user;
console.log("decodedUser ---------------------", decodedUser);
async function createNewCart(user) {
const {data: {createCart}} = await CartService.createCart({user: user.id}, query);
return createCart;
}
if (!decodedUser) {
const {data: {createUser}} = await UserService.generateGuestUser(query);
user = createUser;
cartType.accessToken = jwtEncoder({
id: user.id,
exp: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 30)
}, "accessToken");
cart = await createNewCart(user);
} else {
const {data: {User}} = await UserService.getUserDetails({id: decodedUser.id}, query);
user = User;
const {data: {allCarts}} = await CartService.findCartIfAlreadyPresent({user: user.id}, query);
if (allCarts && allCarts.length > 0) { // cart found for user
cart = allCarts[0];
if (cart.coupon && cart.coupon.code) {
coupon = {data: {allCoupons}} = await CouponService.findCoupon({coupon: cart.coupon.code}, query);
}
} else {
cart = await createNewCart(user);
const {data: {allAddresses}} = await AddressService.getUserAddresses({userId: decodedUser.id}, query);
if (allAddresses && allAddresses.length > 0) {
const defaultAddress = allAddresses.find(address => address.isDefault);
if (defaultAddress) {
await CartService.addAddressToCart({
id: cart.id,
billingAddress: defaultAddress.id,
shippingAddress: defaultAddress.id
}, query);
} else {
await CartService.addAddressToCart({
id: cart.id,
billingAddress: allAddresses[0].id,
shippingAddress: allAddresses[0].id
}, query);
}
}
}
cartType.accessToken = null;
}
if (user && cart && cart.id) {
const {data: {Product}} = await ProductService.findProduct({id: product}, query);
if (!Product) {
throw new Error("Invalid Product ID");
}
if (Product.quantity < quantity) {
throw new Error("Requested amount not available!");
}
if (Product.allowOnePerAccount) {
quantity = 1;
}
let discount = 0.0;
if (coupon && coupon["length"] > 0 && coupon[0].products.findIndex(product => product.id === Product.id)) {
discount = coupon[0].discount;
}
const cartItem = await CartService.createCartItem({
cart: cart.id,
user: user.id,
product,
quantity,
discount,
tax: Product.tax.taxRate
}, query);
const newProductQuantity = Product.quantity - quantity;
if (cartItem) {
const price = Number(Product.price) * quantity;
const discount = Number(cartItem.discount) * quantity;
const tax = Number(cartItem.tax) * quantity;
const cartTotal = cart.cartTotal ? price + Number(cart.cartTotal) : price;
const discountTotal = cart.discountTotal ? discount + Number(cart.discountTotal) : discount;
const orderTotal = cartTotal - discountTotal;
const taxTotal = cart.taxTotal ? tax + Number(cart.taxTotal) : tax;
let shippingTotal = cart.shippingTotal ? Number(cart.shippingTotal) : 0.0;
if (Product.shippingMethods && Product.shippingMethods.length > 0) {
for (let shippingMethod of Product.shippingMethods) {
shippingTotal += Number(shippingMethod.minimumRate || 0.0);
}
}
const grandTotal = orderTotal + taxTotal + shippingTotal;
if (Product.shippingMethods && Product.shippingMethods.length > 0) {
for (let shippingMethod of Product.shippingMethods) {
await CartService.addShippingToCart({
id: cart.id,
shipping: shippingMethod.id,
shippingTotal,
grandTotal
}, query);
}
}
const response = await CartService.addProductToCart({
id: cart.id,
cartItem: cartItem.id,
cartTotal,
discountTotal,
orderTotal,
taxTotal,
shippingTotal,
grandTotal,
quantity: newProductQuantity,
product: Product.id,
}, query);
cartType.cartDetails = response.data["updateCart"];
} else {
await CartService.updateProductQuantity({product: Product.id, quantity: newProductQuantity}, query);
const {data: {allCarts}} = await CartService.findCartIfAlreadyPresent({user: user.id}, query);
cartType.cartDetails = allCarts[0];
}
}
return cartType;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment