Skip to content

Commit

Permalink
Merge pull request #4 from zylcom/order-service
Browse files Browse the repository at this point in the history
refactor(order-service): improve function create
  • Loading branch information
zylcom authored Jul 31, 2023
2 parents 5ad21f5 + 7c5394f commit 31e3fc4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __test__/order.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import supertest from "supertest";
import { createTestUser, name, phonenumberForm, removeManyCartItems, removeTestReview, removeTestUser, token, username } from "./test-util";
import { createTestUser, removeManyCartItems, removeTestUser, token, username } from "./test-util";
import { web } from "../src/app/web";
import { logger } from "../src/app/logging";
import { calculateTotalPrice } from "../src/utils";
Expand Down
2 changes: 1 addition & 1 deletion prisma/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const saltRounds = 10;
const salt = genSaltSync(saltRounds);
const password_hash = hashSync("rahasia123", salt);

const createUserCount = 100;
const createUserCount = 20;

const categories = [
{ id: 1, name: "Food", slug: "food" },
Expand Down
28 changes: 18 additions & 10 deletions src/services/order-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ import { checkoutValidation, createOrderValidation } from "../validation/order-v
import { prismaClient } from "../app/database.js";
import { ResponseError } from "../errors/response-error.js";
import { stripe } from "../plugin/stripe.js";
import { calculateTotalPrice } from "../utils/index.js";

const create = async (username) => {
username = validate(createOrderValidation, username);

const cart = await prismaClient.cart.findUnique({ where: { username }, include: { cartItems: { include: { product: true } } } });
const items = await prismaClient.cartItem.findMany({ where: { cart: { username } }, include: { product: true } });

if (cart.cartItems.length < 1) {
if (items.length < 1) {
throw new ResponseError(400, "At least you must have one item in cart to create an order!");
}

const orderItems = items.map((item) => ({
quantity: item.quantity,
price: item.product.price,
productName: item.product.name,
product: { connect: { slug: item.productSlug } },
}));

const totalPrice = calculateTotalPrice(items);

return prismaClient.order.create({
data: {
subTotal: cart.totalPrice,
total: cart.totalPrice,
subTotal: totalPrice,
total: totalPrice,
items: {
create: cart.cartItems.map((item) => ({
quantity: item.quantity,
price: item.product.price,
productName: item.product.name,
product: { connect: { slug: item.productSlug } },
})),
create: orderItems,
},
user: { connect: { username } },
},
include: {
items: { include: { product: true } },
},
});
};

Expand Down

0 comments on commit 31e3fc4

Please sign in to comment.