Untitled

 avatar
unknown
plain_text
2 years ago
3.4 kB
5
Indexable
const createOrder = handleAsync(async (req, res) => {
  const { products, price, qty } = req.body;

  var fetchedProducts = [];
  try {
    fetchedProducts = await Promise.all(
      products.map(
        (order) =>
          new Promise(async function (resolve, reject) {
            const productExists = await Product.findByPk(order.id);
            if (!productExists) reject("Invalid Id");
            resolve(productExists);
          })
      )
    );
  } catch (error) {
    return new ErrorResponse({ res, error: "Invalid Product Id" });
  }

  const finalAmount = fetchedProducts.reduce(
    (acc, cv) => acc + cv.qty * cv.sellingPrice,
    0
  );
  const finalQty = products.reduce((acc, cv) => acc + cv.qty, 0);

  const transaction = await sequelize.transaction();
  try {
    const savedOrder = await Order.create(
      {
        finalPrice: finalAmount,
        quantity: finalQty,
        products: fetchedProducts,
      },
      { transaction }
    );

    const test = fetchedProducts.map((product) => {
      return {
        orderId: savedOrder.orderId,
        productId: product.productId,
      };
    });

    await OrderProduct.bulkCreate(test, {
      transaction,
    });

    await transaction.commit();

    return new SuccessResponse({ res });
    // if (savedOrderProducts) {
    // }
  } catch (error) {
    console.log(error);
    transaction.rollback();
    return new ErrorResponse({ res });
  }
});





Models

Order Model - 
const Order = sequelize.define(
  "Order",
  {
    orderId: {
      type: Sequelize.UUID,
      allowNull: false,
      defaultValue: Sequelize.UUIDV4,
      primaryKey: true,
    },
    quantity: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
    finalPrice: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
  },
  {
    timestamps: true,
  }
);


Product Model - 
const Product = sequelize.define(
  "Product",
  {
    productId: {
      type: Sequelize.UUID,
      allowNull: false,
      defaultValue: Sequelize.UUIDV4,
      primaryKey: true,
    },
    title: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    description: {
      type: Sequelize.STRING,
      allowNull: true,
    },
    mrpPrice: {
      type: Sequelize.INTEGER,
      allowNull: true,
    },
    costPrice: {
      type: Sequelize.INTEGER,
      allowNull: true,
    },
    sellingPrice: {
      type: Sequelize.INTEGER,
      allowNull: true,
    },
    stock: {
      type: Sequelize.INTEGER,
      allowNull: true,
    },
    itemCode: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      allowNull: false,
    },
    isDisabled: {
      type: Sequelize.BOOLEAN,
      allowNull: false,

      defaultValue: false,
    },
  },
  {
    timestamps: true,
    initialAutoIncrement: 25000,
  }
);

Order Product - 
const OrderProduct = sequelize.define(
  "OrderProduct",
  {
    orderProductId: {
      type: Sequelize.UUID,
      allowNull: false,
      defaultValue: Sequelize.UUIDV4,
      primaryKey: true,
    },
  },
  {
    timestamps: true,
  }
);



Associations - 
Order.belongsToMany(Product, {
  foreignKey: "productId",
  through: OrderProduct,
});

Product.belongsToMany(Order, {
  through: OrderProduct,
  foreignKey: "orderId",
});

Editor is loading...