Skip to content

Commit

Permalink
Merge pull request #35 from danial117/server_dev_33
Browse files Browse the repository at this point in the history
commited
  • Loading branch information
danial117 authored Sep 7, 2024
2 parents 23ec3f8 + 0d160fe commit 8aa6cf6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package-lock.json
public/
.env
npm
.gitignore
.gitignore
error.log
22 changes: 12 additions & 10 deletions controllers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ import CustomError from "../utils/ErrorClass.js";
const order = await Order.findOne({ user: userId, orderStatus: 'Pending', orderStatus: {
$ne: 'Completed' // Not equal to 'completed'
},paymentMethod: 'Unknown' })


if(order){
const Tax=order.itemsAmount*taxRate.total_rate;
const totalAmount=order.itemsAmount+Tax;
console.log(Tax,totalAmount)
const Tax=Number(order.itemsAmount)*Number(taxRate.total_rate);
const totalAmount=Number(order.itemsAmount)+Tax;
console.log('t',Tax,totalAmount)
order.totalAmount = totalAmount;
order.tax= Tax;
order.updatedAt = new Date();
Expand Down Expand Up @@ -106,7 +107,7 @@ export const CreateUserOrder = async (req, res,next) => {
try {

const { userId } = req.user;
console.log(userId)

const cart = await Cart.findOne({ user: userId }).select('items');
const address=await Address.findOne({user:userId}).select('stateCode')

Expand All @@ -117,11 +118,11 @@ export const CreateUserOrder = async (req, res,next) => {

if(cart.items.length !== 0){
const itemsWithPrices = await Promise.all(cart.items.map(async (item) => {
console.log(item)

const product = await Product.findById(item.product).select('options');
console.log('a',product)
const selectedOption = product.options.find(option => option.id === item.option);

console.log(selectedOption.price)
return {
product: item.product,
quantity: item.quantity,
Expand All @@ -145,11 +146,12 @@ export const CreateUserOrder = async (req, res,next) => {





const Tax=itemsAmount*taxRate.total_rate;

const totalAmount=itemsAmount+Tax;




Expand Down Expand Up @@ -286,11 +288,11 @@ function generateOrderNumber() {
paymentMethod: 'Unknown',
}).populate({path: 'items.product',
select: 'options productImage name brand'});
console.log(order)

if (!order) {
return res.status(404).json({ error: 'Order not found for user' });
}
console.log(order)

// Extract only price and option for each item
const items = order.items.map((item) => {
// Find the matching option in the product's options array
Expand Down
2 changes: 2 additions & 0 deletions error.log
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@
{"additionalInfo":{},"level":"error","message":"next is not defined","stack":"CustomError: next is not defined\n at CreatePaypalOrder (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:371:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","statusCode":500,"timestamp":"2024-09-07T01:51:00.456Z"}
{"additionalInfo":{},"level":"error","message":"order.totalAmount.toFixed is not a function","stack":"CustomError: order.totalAmount.toFixed is not a function\n at createOrder (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:423:8)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async CreatePaypalOrder (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:367:48)","statusCode":500,"timestamp":"2024-09-07T01:52:33.938Z"}
{"additionalInfo":{},"level":"error","message":"Cannot destructure property 'jsonResponse' of '(intermediate value)' as it is undefined.","stack":"CustomError: Cannot destructure property 'jsonResponse' of '(intermediate value)' as it is undefined.\n at CreatePaypalOrder (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:371:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","statusCode":500,"timestamp":"2024-09-07T01:52:33.946Z"}
{"additionalInfo":{},"level":"error","message":"Tax is not defined","stack":"CustomError: Tax is not defined\n at OrderTaxRate (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:72:10)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","statusCode":500,"timestamp":"2024-09-07T10:43:31.452Z"}
{"additionalInfo":{},"level":"error","message":"Tax is not defined","stack":"CustomError: Tax is not defined\n at OrderTaxRate (file:///D:/WEB%20development/BACKEND/MERN%20PROJECTS/Main%20Project%201/server/controllers/order.js:72:10)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","statusCode":500,"timestamp":"2024-09-07T10:43:37.338Z"}
45 changes: 40 additions & 5 deletions models/TaxModel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
import mongoose from "mongoose";

const stateTaxRateSchema = new mongoose.Schema({
const Schema = mongoose.Schema;
const SchemaTypes = mongoose.Schema.Types;

// Helper function to convert Decimal128 to string
function getCosts(value) {
if (value && typeof value.toString === 'function') {
// Convert Decimal128 to string and return it
console.log(value.toString());
return value.toString();
}
return value; // Return value as-is if it's undefined or null
}

// Define the toFixedTwo function to return Decimal128-compatible values
const toFixedTwo = (num) => {
// Return Decimal128 with two decimal places
return mongoose.Types.Decimal128.fromString((Math.round(num * 100) / 100).toFixed(2));
};

// Define the schema for state tax rates with Decimal128 handling
const stateTaxRateSchema = new Schema({
state: { type: String, required: true, unique: true },
state_rate: { type: Number, required: true },
total_rate: { type: Number, required: true }
state_rate: {
type: SchemaTypes.Decimal128,
required: true,
set: toFixedTwo, // Ensure two decimal places for state_rate
get: getCosts
},
total_rate: {
type: SchemaTypes.Decimal128,
required: true,
set: toFixedTwo, // Ensure two decimal places for total_rate
get: getCosts
}
}, {
toJSON: { getters: true },
toObject: { getters: true }
});

const StateTax = mongoose.model('State_Tax_Rates', stateTaxRateSchema);
export default StateTax
// Model definition
const StateTax = mongoose.models['State_Tax_Rates'] || mongoose.model('State_Tax_Rates', stateTaxRateSchema);

export default StateTax;

0 comments on commit 8aa6cf6

Please sign in to comment.