Preventing Duplicate Form Submissions

Hi!
I am creating a PTO tracking website for our employees. I have written code to prevent duplicate requests for the same dates off using this example: https://www.wix.com/code/home/forum/wix-tips-and-updates/example-no-database-duplicates
but it does not work on my page. This is my written code:

import wixData from ‘wix-data’ ;

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.

Hi!
Thanks for your help.
I’m new to coding and this is the first time I have used a query.

What would I need to change the query to so that it is per member?
How would I go about concatenating the member._id?

I don’t know your collection schema, but I’ll assume you have a memberId filed that contains the memberId.

Then you can either do without concat:

//backend/data.js
import wixData from 'wix-data';
function lookUpForDuplicates(item, collection){
    let today = new Date();
    today.setHours(0,0,0,0);
    return wixData.query(collection)
        .ge('_createdDate',  today)
        .eq('memberId', item.memberId)
        .count();
}
export function Employees_beforeInsert(item, context) {
return lookUpForDuplicates(item, context.collectionName)
.then(r => {
    if(r){return Promise.reject('already submitted');}
        return item;
    })
}

Or with concat (shorter and works even if the same user submits 2 forms from 2 browser tabs at the same time):

export function Employees_beforeInsert(item, context) {
     let today = new Date();
      today.setHours(0,0,0,0);
    item._id = item.memberId + "--" + today.getTime();
     return item;
}

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).

That worked! Thank you so much for your help!