Tricky/Complex Date/Booking Check

Viewed 122

I need help figuring out a check.

The user can book a date, but then that date is taken. I need to check to make sure the user or other users cant take up that slot.

But I have a extra variable to take into account. The user who books E.G 01/01/2021 - 01/08/2021 Cannot book for a "Ban" period which works like this. members.length -1 (which in essence is the number of members - 1 E.G if u have 5 people the ban is 4..

I have the following checks to follow:

  • Years
  • Months
  • Days
  • Same user may not book same date for ban period but other users can

Here is the Model:

export class Entrymodel {
    id: string;
    surname: string;
    color: string;
    startDate: Startdate;
    endDate: Enddate;
    info: string;
    status: boolean;
}

export class Startdate{
    day: number;
    month: number;
    year: number;
}

export class Enddate{
    day: number;
    month: number;
    year: number;
}

The Method where this is happening:

createEntry(startDate, endDate,  entryinfo: string){
   
    var currentYear = new Date().getFullYear();

    var startValue = startDate;
    var startDay = startValue.substring(0,1);
    var startMonth = startValue.substring(2,3);
    var startYear = startValue.substring(4,8);

    var endValue = endDate;
    var endDay = endValue.substring(0,1);
    var endMonth = endValue.substring(2,3);
    var endYear = endValue.substring(4,8);

    for (const element of this.entries) {
      
      if(this.persist.memberinfo.surname == element.surname){

        if(startYear >= element.startDate.year + this.persist.ban){

          if(startMonth != element.startDate.month && endMonth != element.endDate.month){
        
             if(startDay <= element.startDate.day && endDay >= element.endDate.day){
              
              
             }
          }
        }
      }
    }
    this.uniqueID = uuidv4();
    const newEntry = {
       id: this.uniqueID,
       surname: this.persist.memberinfo.surname,
        color: this.persist.memberinfo.color,
        startDate: {
          day: startDay,
          month: startMonth,
          year: startYear
        },
        endDate: {
          day: endDay,
          month: endMonth,
          year: endYear
        },
        info: entryinfo,
        status: true
    } as Entrymodel;
    this.entryservice.createEntry(newEntry);
  }

The bit of Code I need help with:

for (const element of this.entries) {
     
        if(startYear == element.startDate.year){

          console.log("Year || Has Entries");

          if(startMonth === element.startDate.month || endMonth == element.endDate.month){
            console.log("Month || Was Taken");

             if(startDay <= element.startDate.day && endDay >= element.endDate.day){
              //Okay  
              console.log("Day || Okay");
              this.logentry(startDay,startMonth,startYear,endDay,endMonth,endYear,entryinfo);
              return;
             }
             else{
              console.log("Something Went wrong");
              return;
             }
          }
          else{
            this.logentry(startDay,startMonth,startYear,endDay,endMonth,endYear,entryinfo);
            console.log("Month || Okay");
            return;
          }
        }else{
          console.log("Year || Okay");
          this.logentry(startDay,startMonth,startYear,endDay,endMonth,endYear,entryinfo);
          return
        }
      }

Some Issues I run into I get a return out of the loop when im on a new month which in that cause it should automatically just create the event because there are none in that month. When the data is saved It cannot tell the difference in the following scenario:

Bob Booked 5/1/2021 - 10/1/2021

Bill books 4/1/2021 - 9/1/2021

Susan books Books 1/1/2021 - 12/1/2021

All those will pass threw as my check cant read values that arent there And the database ends up only with a Start and End date so in between dates seem to be free game

Stackblitz App I just copied my App files over.

https://stackblitz.com/edit/angular-ivy-pzypta?file=src/app/createentry/createentry.component.ts

Incorporated a way to get the in-between dates to save as-well still no-idea how ill use them to check


logentry(sm, sd, sy, em, ed, ey, info){

    console.log("ed"+ ed + "em" + em + "ey" + ey);

    var diff = ed - sd;
    console.log("Diff amount" + diff);
    let difference: Inbetween[] = [];
    var startdate = sd;
   
    for (let i = 0; i < diff; i++) {
      console.log(startdate);
      if(sm == em){
    
        difference.push({
          day: startdate,
          month: sm,
          year: sy,
        });
     
        startdate++;
     
      }
      else{
        break;
      }

    }
    difference.splice(0, 1);

   

    this.uniqueID = uuidv4();
    const newEntry = {
       id: this.uniqueID,
       surname: this.persist.memberinfo.surname,
        color: this.persist.memberinfo.color,
        startDate: {
          day: sd,
          month: sm,
          year: sy
        },
        endDate: {
          day: ed,
          month: em,
          year: ey
        },
        inbet: difference,
        info: info,
        status: true
    } as Entrymodel;
    console.log(newEntry);
    this.entryservice.createEntry(newEntry);  
  }

}


export class Inbetween{
  day: number;
  month: number;
  year: number;
}

Firestore Result

0 Answers
Related