I want to create order items using two related models, "Product" and "Order". Both order and product have many order items. An order item belongs to both product and order (one-to-many relation).
Right now, I am using the mixin from the order model to create an order item, giving it the "product_id". I can also use the mixin from the product model to create an order item, giving it the "order_id" like this:
product.createOrderItem({
order_id: order.id,
quantity,
amount: product?.price * quantity,
currency: 'SAR'
});
But can I use both of the relations(mixins from both models) to create order items? Here is what I have done so far:
const order = await user?.createOrder({
status: 'Processing',
source: 'manual'
});
const orderItemsPromises = products?.map(async ({ productId, quantity }) => {
const product = await Product.findByPk(productId);
if (product) {
return order.createOrderItem({
local_product_id: product.id,
quantity,
amount: product?.price * quantity,
currency: 'SAR'
});
}
return null;
});