text: const httpStatus = require('http-status');
  const { Order } = require('../models');
  const ApiError = require('../utils/ApiError');
  
  /**
   * Create a order
   * @param {Object} orderBody
   * @returns {Promise<Order>}
   */
  const createOrder = async (orderBody) => {
    const order = await Order.create(orderBody);
    return order;
  };
  
  /**
   * Get order by id
   * @param {ObjectId} id
   * @returns {Promise<Order>}
   */
  const getOrderById = async (id) => {
    return Order.findById(id).populate('items');
  };
  
  /**
   * Query for orders
   * @param {Object} filter - Mongo filter
   * @param {Object} options - Query options
   * @param {string} [options.sortBy] - Sort option in the format: sortField:(desc|asc)
   * @param {number} [options.limit] - Maximum number of results per page (default = 10)
   * @param {number} [options.page] - Current page (default = 1)
   * @returns {Promise<QueryResult>}
   */
  const queryOrders = async (filter, options) => {
    const orders = await Order.paginate(filter, options);
    return orders;
  };
  
  /**
   * Update order by id
   * @param {ObjectId} orderId
   * @param {Object} updateBody
   * @returns {Promise<Order>}
   */
  const updateOrderById = async (orderId, updateBody) => {
    const order = await Order.findByIdAndUpdate(orderId, updateBody);
    if (!order) {
      throw new ApiError(httpStatus.NOT_FOUND, 'Order not found');
    }
    Object.assign(order, updateBody);
    await order.save();
    return order;
  };
  
  /**
   * Delete order by id
   * @param {ObjectId} orderId
   * @returns {Promise<Order>}
   */
  const deleteOrderById = async (orderId) => {
    const order = await Order.findByIdAndDelete(orderId);
    if (!order) {
      throw new ApiError(httpStatus.NOT_FOUND, 'Order not found');
    }
    await order.remove();
    return order;
  };
  
  module.exports = {
    createOrder,
    queryOrders,
    getOrderById,
    updateOrderById,
    deleteOrderById,
  };
  