Airbnb clone, clean bookings

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


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

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


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-12-30T05:00:00+00:00) Airbnb clone, clean bookings. Retrieved from https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/

MLA
" » Airbnb clone, clean bookings." flaviocopes.com | Sciencx - Thursday December 30, 2021, https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/
HARVARD
flaviocopes.com | Sciencx Thursday December 30, 2021 » Airbnb clone, clean bookings., viewed ,<https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/>
VANCOUVER
flaviocopes.com | Sciencx - » Airbnb clone, clean bookings. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/
CHICAGO
" » Airbnb clone, clean bookings." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/
IEEE
" » Airbnb clone, clean bookings." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/. [Accessed: ]
rf:citation
» Airbnb clone, clean bookings | flaviocopes.com | Sciencx | https://www.scien.cx/2021/12/30/airbnb-clone-clean-bookings/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.