Hi everyone,
I have created a website www.letsgetyousmiling.com
Part of what I did includes webhooks that will send an email to a client and a notification to the website owner when the status of one of the boxes in my "Boxes" collection changes to a certain value. You can take a look at the code at the end of the message to see the webhooks.
Everything work exactly as intended when I go to preview. Unfortunately, it only works sometimes on the published version of the website.
Any ideas?
Thanks in advance.
here is the code:
import { notifyOwnerOnDashboard } from 'backend/notifications';
import { boxStatus } from 'public/statics';
import { statusTime } from 'public/statics';
import wixUsers from 'wix-users-backend';
let user = wixUsers.currentUser;
// Store the old status here
let oldStatus
// This hook will be executed before any update to save the initial status of the operation (before updating)
export function Boxes_beforeUpdate(item, context) {
console.info('using beforeUpdate hook')
console.log(item);
console.log(context);
oldStatus = context.currentItem.status;
return item;
}
// This hook will check if the status changed. If it did, then it will check to what and send an email and a notification
export async function Boxes_afterUpdate(item, context) {
console.info('using afterUpdate hook')
console.log(item);
console.log(context);
let userEmail = await user.getEmail();
// Notify the owner of the website that there has been a change in one of the boxes.
notifyOwnerOnDashboard('User ' + userEmail + ' made changes to box #' + item.boxCode + ' in the Boxes collection.')
console.log('oldStatus is: ' + oldStatus)
console.log('newStatus is: ' + item.status)
// Check if status changed
if (oldStatus !== item.status) {
console.log('the status changed!')
// Check if the status is either Submitted, Designing, Waiting or fabricating.
if ((item.status === boxStatus.submitted) || (item.status === boxStatus.designing) || (item.status === boxStatus.designWait) || (item.status === boxStatus.fabricating)) {
//Email client
wixUsers.emailUser('RX4Aqh7', item.userId, {
variables: {
subscriberName: item.patientName,
newStatus: item.status,
stepTime: statusTime(item.status)
}
});
console.log('Generic status change email sent to ' + item.patientName + ' ' + item.patientLastName);
}
//Check if the status is Approved
if (item.status === boxStatus.approved) {
wixUsers.emailUser('RXiWrNp', item.userId, {
variables: {
subscriberName: item.patientName,
newStatus: item.status
}
});
console.log('Approved status change email sent to ' + item.patientName + ' ' + item.patientLastName);
}
}
return item;
}
Any updates? Same problem happened to me