mirror of
https://github.com/grassrootseconomics/farmstar-survey-backend.git
synced 2025-01-05 21:47:32 +01:00
parent
639d0cd67d
commit
c7dd7a326c
@ -3,6 +3,7 @@ package router
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/pocketbase/pocketbase/apis"
|
||||
@ -11,6 +12,8 @@ import (
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
)
|
||||
|
||||
const timezone = "Europe/Moscow"
|
||||
|
||||
func (r *RouterContainer) bootstrapTransactionRoute() {
|
||||
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
e.Router.POST("/transaction", func(c echo.Context) error {
|
||||
@ -69,6 +72,14 @@ func (r *RouterContainer) bootstrapTransactionRoute() {
|
||||
return apis.NewNotFoundError(fmt.Sprintf("Counterparty is not registered to %s", requestData.DistributorName), err)
|
||||
}
|
||||
|
||||
incorrectDate, err := isFutureDate(requestData.TxBuyDate)
|
||||
if err != nil {
|
||||
return apis.NewBadRequestError("Incorrect date format", err)
|
||||
}
|
||||
if incorrectDate {
|
||||
return apis.NewBadRequestError("Date cannot be in the future", err)
|
||||
}
|
||||
|
||||
txData["evergrow_quantity"] = requestData.EvergrowQuantityPurchase
|
||||
txData["distributor_name"] = requestData.DistributorName
|
||||
txData["counterparty"] = counterpartyRecord.Id
|
||||
@ -85,6 +96,14 @@ func (r *RouterContainer) bootstrapTransactionRoute() {
|
||||
return apis.NewNotFoundError("Counterparty is not registered as a farmer", err)
|
||||
}
|
||||
|
||||
incorrectDate, err := isFutureDate(requestData.TxSellDate)
|
||||
if err != nil {
|
||||
return apis.NewBadRequestError("Incorrect date format", err)
|
||||
}
|
||||
if incorrectDate {
|
||||
return apis.NewBadRequestError("Date cannot be in the future", err)
|
||||
}
|
||||
|
||||
txData["evergrow_quantity"] = requestData.EvergrowQuantitySale
|
||||
txData["counterparty"] = counterpartyRecord.Id
|
||||
txData["tx_date"] = requestData.TxSellDate
|
||||
@ -106,3 +125,22 @@ func (r *RouterContainer) bootstrapTransactionRoute() {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func isFutureDate(inputDate string) (bool, error) {
|
||||
parsedDate, err := time.Parse("2006-01-02", inputDate)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
parsedDate = parsedDate.In(location)
|
||||
|
||||
currentDate := time.Now().In(location)
|
||||
fmt.Println(currentDate)
|
||||
|
||||
return parsedDate.After(currentDate), nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user