stockpile-co / api

The API for Stockpile, an app that manages stuff for organizations.
0 stars 1 forks source link

Add date validation on rentals #127

Closed AdamVig closed 7 years ago

AdamVig commented 7 years ago

Provides a second line of defense for the same validation in the frontend.

AdamVig commented 7 years ago

To check that a date is not in the past: startDate >= current_date() Source

To check that a date is at least a day after another date: datediff(endDate, startDate) >= 1 Source

To check that a date span does not conflict with other date spans:

select * from rental
  where returnDate = null
  and not (testStartDate between startDate and endDate)
  and not (testEndDate between startDate and endDate)

Source

AdamVig commented 7 years ago

Currently blocked on this because Knex.js does not pass through the message from the trigger as its own property on the error object:

code: "ER_SIGNAL_EXCEPTION",
errno: 1644,
index: 0,
sqlState: "45000",
message: "insert into `rental` (`endDate`, `itemID`, `organizationID`, `startDate`, `userID`) values ('2017-05-10', '5', 37, '2017-05-02', 60) - ER_SIGNAL_EXCEPTION: Rental start date must not be in the past"

The message that I want is at the end of the actual message property.

AdamVig commented 7 years ago

Waiting on mysqljs/1714 to be merged. This will add a sqlMessage property containing the message associated with the sqlState, allowing direct pass-through of messages from triggers or procedures to end users.