I’m trying to use beforeSave hook when creating an order for my database. This hook is supposed to set the location of the order however the hook is never fired so the order never has a location. This is my hook that I have created:
{
hooks: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
beforeSave: async (order: any) => {
console.log('Test')
// ------------- Set location -------------
const storeId = order.store;
const storeInstance = await Store.findByPk(storeId);
if (!storeInstance) {
throw new Error('Store not found');
}
const locationId = storeInstance.location;
order.location = locationId;
console.log(order.location)
// ------------- Create customer if not exist -------------
const customer = {
shipping_firstname: order.shipping_firstname,
shipping_lastname: order.shipping_lastname,
shipping_country: order.shipping_country,
shipping_region: order.shipping_region,
shipping_city: order.shipping_city,
shipping_postcode: order.shipping_postcode,
shipping_address1: order.shipping_address1,
shipping_address2: order.shipping_address2 || null,
shipping_phone: order.shipping_phone || null,
shipping_email: order.shipping_email || null,
billing_firstname: order.billing_firstname,
billing_lastname: order.billing_lastname,
billing_country: order.billing_country,
billing_region: order.billing_region,
billing_city: order.billing_city,
billing_postcode: order.billing_postcode,
billing_address1: order.billing_address1,
billing_address2: order.billing_address2 || null,
billing_phone: order.billing_phone || null,
billing_email: order.billing_email || null
}
const fcustomer = await Customer.findOne({
where: {
billing_firstname: Seq.where(Seq.fn('lower', Seq.col('billing_firstname')), Seq.fn('lower', customer.billing_firstname)),
billing_lastname: Seq.where(Seq.fn('lower', Seq.col('billing_lastname')), Seq.fn('lower', customer.billing_lastname)),
billing_country: Seq.where(Seq.fn('lower', Seq.col('billing_country')), Seq.fn('lower', customer.billing_country)),
billing_region: Seq.where(Seq.fn('lower', Seq.col('billing_region')), Seq.fn('lower', customer.billing_region)),
billing_postcode: Seq.where(Seq.fn('lower', Seq.col('billing_postcode')), Seq.fn('lower', customer.billing_postcode)),
billing_address1: Seq.where(Seq.fn('lower', Seq.col('billing_address1')), Seq.fn('lower', customer.billing_address1)),
}
})
if (fcustomer) {
fcustomer.update(customer);
} else {
Customer.create(customer)
.catch((err: Error) => {
console.error(err)
});
}
/* const products = order.products;
for (let i = 0; i < products.length; i++) {
const product = products[i];
const foundProduct = await Inventory.findByPk(product.id);
foundProduct.stockOnHold = foundProduct.stockOnHold + product.qty;
foundProduct.save();
} */
},
},
sequelize,
modelName: 'Order',
});
And then this is my fetch request for creating an order:
fetch('/orders/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}).then((res) => res.json()).then((data) => {
if (data.code === 200) {
ordersCreateKit.close();
orders.fetchOrders();
orders.fetchOrderStats();
}
}).catch((err) => {
console.log(err);
})
This is supposed to be the code that adds the location to the order. It used to fire but when I restarted the server it stopped and nothing was changed.






