export function searchForDuplicates ( value , info ) {
// info contains the hook context
// use the collectionName property to use function for multiple collections return wixData . query ( “Employees” )
. eq ( “startDate” , value . startDate )
. find ()
. then (( results ) => { return results . items . length ;
})
. catch (( err ) => {
let errorMsg = err ;
})
}
export function Employees_beforeUpdate ( item , context ) {
// Calls routine to check if value already exists in the collection.
// If value not found, then save record.
// Else, if value is found, then reject this insert to prevent duplicate values.
// Note: concurrent inserts may result in duplicates.
// Pass context to searchForDuplicates to use for multiple collections. return searchForDuplicates ( item , context ). then (( res ) => {
if ( res > 0 ) { return Promise . reject ( ‘This item already exists’ );
} return item ;
});
}
The database is named “Employees” and ideally the code will search for both start and end date of the PTO request. The dates are entered into two date pickers: “Start Date” and “End Date”.
Hi, first of all your code disallows form permission for the same data even if it’s different member. If you want to make it per member, change your query. Second , you used beforeUpdate instead of beforeInsert. Third (optional), another thing you can do to make sure there’re no duplicates is to create the _id value based on concatenation of the member._id and the current date (after converting it to string). This way, every new submission attempt with same _id (i.e. same member._id and date) will be automatically rejected by the system.
P.S as matter of fact, if you wish to disallow submitting in the same date based on the client time zone , you should create the _id on the front end (and for the first option without the concat, you should have clientDateString field in the collection, and create the clientDateString on the client side).