Airbnb clone, clean bookings

Join the 2022 Full-Stack Web Dev Bootcamp!


This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

Clearing unpaid bookings

Now we have a little issue. Since we book dates before paying, some people might not complete the payment (it will happen!) and this leaves us with booked dates that are not really confirmed.

To solve this, we need to clear out bookings from time to time.

I made a POST endpoint to do this, in api/clean.js:

server.js

import { Booking } from '../../model.js'

export default async (req, res) => {
  if (req.method !== 'POST') {
    res.status(405).end() //Method Not Allowed
    return
  }

  Booking.destroy({
    where: {
      paid: false
    }
  })

  res.writeHead(200, {
    'Content-Type': 'application/json'
  })

  res.end(
    JSON.stringify({
      status: 'success',
      message: 'ok'
    })
  )
}

It calls Booking.destroy to remove all unpaid bookings.

Ideally you’d remove all bookings that have been created, say, more than 1 hour ago, and are still unpaid.

This needs some refinement, but it’s good for us now.

I added an Insomnia POST request to help me clean the database, manually:

See the code on GitHub

Next part: Airbnb clone, managing houses

Want to become a better Web Developer? Join the 2022 Web Development Bootcamp!

⭐️⭐️⭐️ Join the 2022 Web Development Bootcamp ⭐️⭐️⭐️