Skip to content

Commit

Permalink
Merge pull request hiteshchoudhary#114 from Sarthak-ONS/fix/Address-N…
Browse files Browse the repository at this point in the history
…ot-Found-after-deleting-it-in-orders

Bug Fix: address not found issue
  • Loading branch information
wajeshubham authored Apr 2, 2024
2 parents a9d639b + 0821be7 commit 1fe3447
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
42 changes: 21 additions & 21 deletions src/controllers/apps/ecommerce/order.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,21 @@ const generateRazorpayOrder = asyncHandler(async (req, res) => {
);
}

const { addressLine1, addressLine2, city, country, pincode, state } =
address;

// Create an order while we generate razorpay session
// In case payment is done and there is some network issue in the payment verification api
// We will at least have a record of the order
const unpaidOrder = await EcomOrder.create({
address: addressId,
address: {
addressLine1,
addressLine2,
city,
country,
pincode,
state,
},
customer: req.user._id,
items: orderItems,
orderPrice: totalPrice ?? 0,
Expand Down Expand Up @@ -298,11 +308,20 @@ const generatePaypalOrder = asyncHandler(async (req, res) => {
const paypalOrder = await response.json();

if (paypalOrder?.id) {
const { addressLine1, addressLine2, city, country, pincode, state } =
address;
// Create an order while we generate paypal session
// In case payment is done and there is some network issue in the payment verification api
// We will at least have a record of the order
const unpaidOrder = await EcomOrder.create({
address: addressId,
address: {
addressLine1,
addressLine2,
city,
country,
pincode,
state,
},
customer: req.user._id,
items: orderItems,
orderPrice: totalPrice ?? 0,
Expand Down Expand Up @@ -393,15 +412,6 @@ const getOrderById = asyncHandler(async (req, res) => {
_id: new mongoose.Types.ObjectId(orderId),
},
},
// lookup for an address associated with the order
{
$lookup: {
from: "addresses",
localField: "address",
foreignField: "_id",
as: "address",
},
},
// lookup for a customer associated with the order
{
$lookup: {
Expand Down Expand Up @@ -441,7 +451,6 @@ const getOrderById = asyncHandler(async (req, res) => {
{
$addFields: {
customer: { $first: "$customer" },
address: { $first: "$address" },
coupon: { $ifNull: [{ $first: "$coupon" }, null] },
},
},
Expand Down Expand Up @@ -517,14 +526,6 @@ const getOrderListAdmin = asyncHandler(async (req, res) => {
}
: {},
},
{
$lookup: {
from: "addresses",
localField: "address",
foreignField: "_id",
as: "address",
},
},
// lookup for a customer associated with the order
{
$lookup: {
Expand Down Expand Up @@ -562,7 +563,6 @@ const getOrderListAdmin = asyncHandler(async (req, res) => {
{
$addFields: {
customer: { $first: "$customer" },
address: { $first: "$address" },
coupon: { $ifNull: [{ $first: "$coupon" }, null] },
totalOrderItems: { $size: "$items" },
},
Expand Down
9 changes: 0 additions & 9 deletions src/controllers/apps/ecommerce/profile.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ const getMyOrders = asyncHandler(async (req, res) => {
customer: req.user._id,
},
},
{
$lookup: {
from: "addresses",
localField: "address",
foreignField: "_id",
as: "address",
},
},
// lookup for a customer associated with the order
{
$lookup: {
Expand Down Expand Up @@ -90,7 +82,6 @@ const getMyOrders = asyncHandler(async (req, res) => {
{
$addFields: {
customer: { $first: "$customer" },
address: { $first: "$address" },
coupon: { $ifNull: [{ $first: "$coupon" }, null] },
totalOrderItems: { $size: "$items" },
},
Expand Down
25 changes: 23 additions & 2 deletions src/models/apps/ecommerce/order.models.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,29 @@ const orderSchema = new Schema(
default: [],
},
address: {
type: Schema.Types.ObjectId,
ref: "Address",
addressLine1: {
required: true,
type: String,
},
addressLine2: {
type: String,
},
city: {
required: true,
type: String,
},
country: {
required: true,
type: String,
},
pincode: {
required: true,
type: String,
},
state: {
required: true,
type: String,
},
},
status: {
type: String,
Expand Down

0 comments on commit 1fe3447

Please sign in to comment.