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

commited #35

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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;