mirror of
https://github.com/grassrootseconomics/farmstar-survey-backend.git
synced 2025-01-07 14:37:33 +01:00
parent
639d0cd67d
commit
c7dd7a326c
@ -3,6 +3,7 @@ package router
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/v5"
|
"github.com/labstack/echo/v5"
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
@ -11,6 +12,8 @@ import (
|
|||||||
"github.com/pocketbase/pocketbase/models"
|
"github.com/pocketbase/pocketbase/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const timezone = "Europe/Moscow"
|
||||||
|
|
||||||
func (r *RouterContainer) bootstrapTransactionRoute() {
|
func (r *RouterContainer) bootstrapTransactionRoute() {
|
||||||
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
e.Router.POST("/transaction", func(c echo.Context) 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)
|
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["evergrow_quantity"] = requestData.EvergrowQuantityPurchase
|
||||||
txData["distributor_name"] = requestData.DistributorName
|
txData["distributor_name"] = requestData.DistributorName
|
||||||
txData["counterparty"] = counterpartyRecord.Id
|
txData["counterparty"] = counterpartyRecord.Id
|
||||||
@ -85,6 +96,14 @@ func (r *RouterContainer) bootstrapTransactionRoute() {
|
|||||||
return apis.NewNotFoundError("Counterparty is not registered as a farmer", err)
|
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["evergrow_quantity"] = requestData.EvergrowQuantitySale
|
||||||
txData["counterparty"] = counterpartyRecord.Id
|
txData["counterparty"] = counterpartyRecord.Id
|
||||||
txData["tx_date"] = requestData.TxSellDate
|
txData["tx_date"] = requestData.TxSellDate
|
||||||
@ -106,3 +125,22 @@ func (r *RouterContainer) bootstrapTransactionRoute() {
|
|||||||
return nil
|
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