youge
3
export async function isBookConflit(user, chalet, dtBegin, dtEnd, comment) {
xDtBegin = await Number(await dateToString(dtBegin));
xDtEnd = await Number(await dateToString(dtEnd));
//await console.log("\n 1111111 xdtbegin to xdtend = \n" + xDtBegin + " , " + xDtEnd + " \n");
let isBook = new Boolean();
isConflit = true;
let NotBook = "wix:image://v1/a6f6fa_6272bb1da4f24069981e82808359feef~mv2.png/Favicon-blue-My-Home-Product.png#originWidth=512&originHeight=512";
// let xchalet = null;
let xBook = null;
let resCount = 1;
const Booked = null;
let recIDtoUpd = null;
let member = await getUser(user.id);
// await console.log("user= " + JSON.stringify(user));
// await console.log("member= " + JSON.stringify(member));
let userId = await member.id;
switch (chalet) {
case c1:
xBook = "book1";
xchalet = "b1";
break;
case c2:
xBook = "book2";
xchalet = "b2";
break;
case c3:
xBook = "book3";
xchalet = "b3";
break;
case c4:
xBook = "book4";
xchalet = "b4";
break;
case c5:
xBook = "book5";
xchalet = "b5";
break;
case c7:
xBook = "book7";
xchalet = "b7";
break;
case c9:
xBook = "book9";
xchalet = "b9";
break;
case c10:
xBook = "book10";
xchalet = "b10";
break;
case c14:
xBook = "book14";
xchalet = "b14";
break;
case c17:
xBook = "book17";
xchalet = "b17";
break;
case c18:
xBook = "book18";
xchalet = "b18";
break;
case c20:
xBook = "book20";
xchalet = "b20";
break;
case c22:
xBook = "book22";
xchalet = "b22";
break;
case c23:
xBook = "book23";
xchalet = "b23";
break;
case c24:
xBook = "book24";
xchalet = "b24";
break;
case c25:
xBook = "book25";
xchalet = "b25";
break;
case c26:
xBook = "book26";
xchalet = "b26";
break;
case c27:
xBook = "book27";
xchalet = "b27";
break;
case c28:
xBook = "book28";
xchalet = "b28";
break;
case c29:
xBook = "book29";
xchalet = "b29";
break;
case c30:
xBook = "book30";
xchalet = "b30";
break;
case c31:
xBook = "book31";
xchalet = "b31";
break;
case c32:
xBook = "book32";
xchalet = "b32";
break;
case c33:
xBook = "book33";
xchalet = "b33";
break;
default:
xBook = undefined;
xchalet = undefined;
}
// console.log("\n4444442xchalet, xdtbegin to xdtend = \n" + xchalet + " , " + xDtBegin + " , " + xDtEnd + " \n");
// await setTimeout( () => {
// console.log("\n222222222xchalet, xdtbegin to xdtend = \n" + xchalet + " , " + xDtBegin + " , " + xDtEnd + " \n");
let dateString = chalet + xDtBegin + xDtEnd + dateIn.getMilliseconds();
// console.log("catch first call 88888 " + xchalet + xDtBegin + xDtEnd);
//console.log("\n# return is isConflit " + isConflit);
let bookSel = await wixData.query("bookingY")
.eq('"' + xchalet + '"', true)
.between("bookDate", xDtBegin, xDtEnd)
.find()
.then(async (res) => {
let resCount = await res.totalCount;
let recUpd = null;
let items = res.items;
// console.log("##333333333##Data bookingY is for chalet " + resCount + " " + chalet + " ######date from to " + dtBegin + " " + dtEnd);
if (resCount === 0) {
isConflit = false;
// console.log("catch first call 777777 " + xDtBegin + xDtEnd);
// console.log("\n# return is isConflit " + isConflit);
let bookupd = await wixData.query("bookingY")
.between("bookDate", xDtBegin, xDtEnd)
.find()
.then(async (res2) => {
let x = 0
let resCount2 = await res2.totalCount;
// await console.log("Data bookingY to update for chalet resCount22222222= " + resCount2 + " ###### \n" + chalet);
// console.log("recUPD.bookingY= " + JSON.stringify(res2.items));
if (resCount2 > 0) {
isConflit = false;
// console.log("\n# return is isConflit " + isConflit);
let logEml = await member.loginEmail;
let mId = await member.id;
let toInsert = {
"title": dateString,
"member": member,
"loginEmail": logEml,
"memberId": mId,
"chalet": chalet,
"from": dtBegin,
"to": dtEnd,
"commentaire": comment
}
await wixData.insert("Book", toInsert)
.then((results) => {
let item = results;
isConflit = false;
})
.catch((err) => {
let errorMsg = err;
isConflit = true;
console.log("!!!!!!!!!!!!!!!!!####Data ERROR4 Book TO INSERT is " + err + " ############ ");
console.log("return is " + isConflit);
});
// console.log("INSERT Book Done =" + isConflit);
switch (chalet) {
case c1:
xBook = "book1";
xchalet = "b1";
crecUpd = "book1";
recUpd.book1 = null;
recUpd.b1 = true;
break;
case c2:
xBook = "book2";
xchalet = "b2";
crecUpd = "book2";
recUpd.book2 = null;
recUpd.b2 = true;
break;
case c3:
xBook = "book3";
xchalet = "b3";
crecUpd = "book3";
recUpd.book3 = null;
recUpd.b3 = true;
break;
case c4:
xBook = "book4";
xchalet = "b4";
crecUpd = "book4";
recUpd.book4 = null;
recUpd.b4 = true;
break;
case c5:
xBook = "book5";
xchalet = "b5";
crecUpd = "book5";
recUpd.book5 = null;
recUpd.b5 = true;
break;
case c7:
xBook = "book7";
xchalet = "b7";
crecUpd = "book7";
recUpd.book7 = null;
recUpd.b7 = true;
break;
case c9:
xBook = "book9";
xchalet = "b9";
crecUpd = "book9";
recUpd.book9 = null;
recUpd.b9 = true;
break;
case c10:
xBook = "book10";
xchalet = "b10";
crecUpd = "book10";
recUpd.book10 = null;
recUpd.b10 = true;
break;
case c14:
xBook = "book14";
xchalet = "b14";
recUpd.book14 = null;
crecUpd = "book14";
recUpd.b14 = true;
break;
case c17:
xBook = "book17";
xchalet = "b17";
recUpd.book17 = null;
recUpd.b17 = true;
crecUpd = "book17";
break;
case c18:
xBook = "book18";
xchalet = "b18";
recUpd.book18 = null;
recUpd.b18 = true;
crecUpd = "book18";
break;
case c20:
xBook = "book20";
xchalet = "b20";
recUpd.book20 = null;
recUpd.b20 = true;
crecUpd = "book20";
break;
case c22:
xBook = "book22";
xchalet = "b22";
recUpd.book22 = null;
crecUpd = "book22";
recUpd.b22 = true;
break;
case c23:
xBook = "book23";
xchalet = "b23";
crecUpd = "book23";
recUpd.book23 = null;
recUpd.b23 = true;
break;
case c24:
xBook = "book24";
xchalet = "b24";
recUpd.book24 = null;
crecUpd = "book24";
recUpd.b24 = true;
break;
case c25:
xBook = "book25";
xchalet = "b25";
recUpd.book25 = null;
crecUpd = "book25";
recUpd.b25 = true;
break;
case c26:
xBook = "book26";
xchalet = "b26";
recUpd.book26 = null;
crecUpd = "book26";
recUpd.b26 = true;
break;
case c27:
xBook = "book27";
xchalet = "b27";
recUpd.book27 = null;
crecUpd = "book27";
recUpd.b27 = true;
break;
case c28:
xBook = "book28";
xchalet = "b28";
crecUpd = "book28";
recUpd.book28 = null;
recUpd.b28 = true;
break;
case c29:
xBook = "book29";
xchalet = "b29";
crecUpd = "book29";
recUpd.book29 = null;
recUpd.b29 = true;
break;
case c30:
xBook = "book30";
xchalet = "b30";
crecUpd = "book30";
recUpd.book30 = null;
recUpd.b30 = true;
break;
case c31:
xBook = "book31";
xchalet = "b31";
crecUpd = "book31";
recUpd.book31 = null;
recUpd.b31 = true;
break;
case c32:
xBook = "book32";
xchalet = "b32";
break;
crecUpd = "book32";
recUpd.book32 = null;
recUpd.b32 = true;
break;
case c33:
xBook = "book33";
xchalet = "b33";
crecUpd = "NONE";
recUpd.book33 = null;
recUpd.b33 = true;
break;
default:
xBook = undefined;
xchalet = undefined;
}
for (let x = 0; x < resCount2; x++) {
// console.log("UPDATE bookingY x =" + x);
recUpd = await res2.items[x];
recIDtoUpd = await res2.items[x]._id;
// console.log("rec bookingY Before= ");
// await console.log("rec bookingY Before= \n######### " + chalet);
// console.log("rec bookingY 30 Before= " + recUpd.book30 + " ||||| isBook30 " + recUpd.b30);
// console.log("rec bookingY AFTER= ");
// console.log("rec bookingY AFTER= ");
// console.log("rec bookingY 30 AFTER= " + recUpd.book30 + " ||||| isBook30 " + recUpd.b30);
try {
let sqlStmt = 'UPDATE bookingY SET ' + xBook + ' = null Where _id = "' + recIDtoUpd + '"';
let sqlRun1 = await sql(sqlStmt);
console.log("Update bookingY 1 SQL = \n" + sqlStmt + "\n" + JSON.stringify(sqlRun1));
console.info("record mis à jour = " + sqlRun1.payload.updated + " id=" + recIDtoUpd)
} catch (error) {
console.log("ERROR Update 1 bookingY error = \n" + error);
// console.error(error);
isConflit = true;
}
/*
await wixData.update("bookingY", recUpd)
.then((results) => {
let item = results;
console.log("maj bookingY =\n" + item.dateCalendar + " case Book = " + crecUpd);
})
.catch((err) => {
let errorMsg = err;
isConflit = true;
console.log("!!!!!!!!!!!!!!!!!####Data ERROR5 bookingY TO UPDATE is " + errorMsg + " ############ ");
console.log("return is 33333 " + isConflit);
console.error();
});
*/
// await console.log("\ndateString , chalet= \n#" + dateString + chalet);
// console.log("\n# return is isConflit " + isConflit);
console.log("\n# fin de la boucle return is isConflit " + isConflit);
} // boucle for (
// isConflit = false;
} else {
isConflit = true;
console.log("!#### NO REC TO UPDATE . Logic problem ############ " + isConflit);
}
})
.catch((error) => {
isConflit = true;
console.log("!!!!!!!!!!!!!!!!!####Data ERROR3 bookingY TO UPDATEis " + error + " ############ ");
console.log("return is 4444 " + isConflit);
});
let bookupd2 = await wixData.query("bookingX")
.between("bookDate", xDtBegin, xDtEnd)
.find()
.then(async (res5) => {
let x = 0
let resCount5 = await res5.totalCount;
// await console.log("Data bookingX to update for chalet resCount22222222= " + resCount2 + " ###### \n" + chalet);
// console.log("recUPD.bookingX= " + JSON.stringify(res2.items));
if (resCount5 > 0) {
isConflit = false;
// console.log("\n# return is isConflit " + isConflit);
let logEml = await member.loginEmail;
let mId = await member.id;
// console.log("INSERT Book Done =" + isConflit);
for (let x = 0; x < resCount5; x++) {
// console.log("UPDATE bookingX x =" + x);
recUpd = await res5.items[x];
recIDtoUpd = await res5.items[x]._id;
// console.log("rec bookingX Before= ");
// await console.log("rec bookingX Before= \n######### " + chalet);
// console.log("rec bookingX 30 Before= " + recUpd.book30 + " ||||| isBook30 " + recUpd.b30);
// console.log("rec bookingX AFTER= ");
// console.log("rec bookingX AFTER= ");
// console.log("rec bookingX 30 AFTER= " + recUpd.book30 + " ||||| isBook30 " + recUpd.b30);
try {
let sqlStmt = 'UPDATE bookingX SET ' + xchalet + ' = true Where _id = "' + recIDtoUpd + '"';
// let sqlStmt = 'SELECT * from bookingX Where _id = "' + recIDtoUpd + '"';
let sqlRun2 = await sql(sqlStmt);
console.log("UPDATE BookingX SQL = \n" + sqlStmt + "\n" + JSON.stringify(sqlRun2.payload.updated));
console.log(sqlRun2.payload.updated + " id=" + recIDtoUpd)
console.info(sqlRun2.payload.updated + " id=" + recIDtoUpd)
} catch (error) {
console.log("ERROR Update 6 BookingX error = \n" + error);
// console.error(error);
isConflit = true;
}
// await console.log("\ndateString , chalet= \n#" + dateString + chalet);
// console.log("\n# return is isConflit " + isConflit);
console.log("\n# fin de la boucle 2 return is isConflit " + isConflit);
} // boucle for (
// isConflit = false;
} else {
isConflit = true;
console.log("!#### NO REC TO UPDATE 2 . Logic problem ############ " + isConflit);
}
})
.catch((error) => {
isConflit = true;
console.log("!!!!!!!!!!!!!!!!!####Data ERROR5 Booking TO UPDATEis " + error + " ############ ");
console.log("return is 55555 " + isConflit);
});
}
})
.catch((error) => {
isConflit = true;
console.log("!!!!!!!!!!!!!!!!!####Data ERROR4 BookingX is " + error + " ############ ")
console.log("return is 66666 " + isConflit);
});
// },300);
// console.log("\n# return is isConflit " + isConflit);
// console.log("\n# return is isConflit " + isConflit);
// console.log("\n# return is isConflit " + isConflit);
//if (isConflit) {return "true"} else {return "false"}
return (await isConflit);
}
function greeting() {
console.log("Hello World");
}