It was more a general question whether the Backend HTTP Function is available in WIX Studio.
The code in the frontend has been deleted because I needed to go back to the original code I had but here is the Backend code -
import wixData from ‘wix-data’;
export async function get_downloadBookingsCsv(request) {
**const** email = request.query\["email"\];
console.log("Email received:", email);
**if** (!email) {
console.log("No email provided");
**return** {
status: 400,
body: "Missing email parameter"
};
}
**const** ownerResults = **await** wixData.query("OwnerInformation")
.eq("ownerEmail", email)
.find();
console.log("Owner results:", ownerResults.items);
**if** (ownerResults.items.length === 0) {
console.log("Owner not found");
**return** {
status: 404,
body: "Owner not found"
};
}
**const** ownerId = ownerResults.items\[0\].\_id;
**const** villaResults = **await** wixData.query("Villas")
.eq("ownerEmailRef", ownerId)
.find();
console.log("Villa results:", villaResults.items);
**if** (villaResults.items.length === 0) {
console.log("No villas found");
**return** {
status: 404,
body: "No villas found"
};
}
**const** villaIds = villaResults.items.map(v => v.\_id);
**const** villaMap = {};
villaResults.items.forEach(v => {
villaMap\[v.\_id\] = {
villaName: v.villaName,
community: v.community
};
});
**const** bookingResults = **await** wixData.query("Bookings")
.hasSome("villasReference", villaIds)
.find();
console.log("Booking results:", bookingResults.items);
**if** (bookingResults.items.length === 0) {
console.log("No bookings found");
**return** {
status: 404,
body: "No bookings found"
};
}
// Build CSV
**let** csv = "Villa Name,Community,Guest Name,Check-In,Check-Out,Nights,Guests,Adults,Children,Pool Heating,Additional Requirements\\n";
bookingResults.items.forEach(item => {
**let** villaInfo = villaMap\[item.villasReference\] || {};
**let** nights = "";
**if** (item.checkInDate && item.checkOutDate) {
**const** checkIn = **new** Date(item.checkInDate);
**const** checkOut = **new** Date(item.checkOutDate);
**const** diffTime = checkOut - checkIn;
nights = Math.round(diffTime / (1000 \* 60 \* 60 \* 24));
}
csv += \[
\`"${villaInfo.villaName || ""}"\`,
\`"${villaInfo.community || ""}"\`,
\`"${item.guestName || ""}"\`,
\`"${item.checkInDate ? **new** Date(item.checkInDate).toLocaleDateString() : ""}"\`,
\`"${item.checkOutDate ? **new** Date(item.checkOutDate).toLocaleDateString() : ""}"\`,
\`"${nights}"\`,
\`"${item.numberOfGuests || ""}"\`,
\`"${item.numberOfAdults || ""}"\`,
\`"${item.numberOfChildren !== **null** && item.numberOfChildren !== **undefined** ? item.numberOfChildren : ""}"\`,
\`"${item.poolHeating === **true** ? "Yes" : "No"}"\`,
\`"${item.additionalRequirements || ""}"\`
\].join(",") + "\\n";
});
console.log("CSV generated, length:", csv.length);
**return** {
status: 200,
headers: {
"Content-Type": "text/csv",
"Content-Disposition": "attachment; filename=bookings.csv"
},
body: csv
};
}
Also a Test code -
export function get_test(request) {
**return** {
status: 200,
body: "Test function is working"
};
}