mirror of
https://github.com/grassrootseconomics/farmstar-survey-backend.git
synced 2024-11-19 00:16:45 +01:00
101 lines
3.1 KiB
Go
101 lines
3.1 KiB
Go
package router
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"github.com/pocketbase/pocketbase/forms"
|
|
"github.com/pocketbase/pocketbase/models"
|
|
)
|
|
|
|
func (r *RouterContainer) bootstrapTransactionRoute() {
|
|
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
e.Router.POST("/transaction", func(c echo.Context) error {
|
|
requestData := struct {
|
|
Phone string `json:"phone"`
|
|
Tx string `json:"tx" `
|
|
TxBuyType string `json:"buy"`
|
|
TxSellType string `json:"sell"`
|
|
TxBuyDate string `json:"buy_date"`
|
|
TxSellDate string `json:"sell_date"`
|
|
Feedback string `json:"feedback"`
|
|
EvergrowQuantityPurchase string `json:"evergrow_quantity_purchase"`
|
|
EvergrowQuantitySale string `json:"evergrow_quantity_sale"`
|
|
DistributorName string `json:"distributor_name"`
|
|
}{}
|
|
if err := c.Bind(&requestData); err != nil {
|
|
return apis.NewBadRequestError("Failed to read request data", err)
|
|
}
|
|
|
|
initiatorRecord, err := r.PB.Dao().FindFirstRecordByData("users", "phone", requestData.Phone)
|
|
if err != nil {
|
|
return apis.NewNotFoundError("Initiator not registered", err)
|
|
}
|
|
|
|
txCollection, err := r.PB.Dao().FindCollectionByNameOrId("transactions")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
txRecord := models.NewRecord(txCollection)
|
|
txForm := forms.NewRecordUpsert(r.PB, txRecord)
|
|
|
|
txData := map[string]any{
|
|
"initiator": initiatorRecord.Id,
|
|
"tx_type": requestData.Tx,
|
|
"feedback": requestData.Feedback,
|
|
"completed": false,
|
|
}
|
|
|
|
if requestData.Tx == "buy" {
|
|
log.Println("buy")
|
|
counterpartyRecord, err := r.PB.Dao().FindFirstRecordByData("users", "phone", requestData.TxBuyType)
|
|
if err != nil {
|
|
return apis.NewNotFoundError("Counterparty not registered", err)
|
|
}
|
|
|
|
if counterpartyRecord.GetString("participant_type") != "distributor" {
|
|
return apis.NewNotFoundError("Counterparty is not registered as a distributor", err)
|
|
}
|
|
|
|
txData["evergrow_quantity"] = requestData.EvergrowQuantityPurchase
|
|
txData["distributor_name"] = requestData.DistributorName
|
|
txData["counterparty"] = counterpartyRecord.Id
|
|
txData["tx_date"] = requestData.TxBuyDate
|
|
}
|
|
|
|
if requestData.Tx == "sell" {
|
|
log.Println("sell")
|
|
counterpartyRecord, err := r.PB.Dao().FindFirstRecordByData("users", "phone", requestData.TxSellType)
|
|
if err != nil {
|
|
return apis.NewNotFoundError("", err)
|
|
}
|
|
|
|
if counterpartyRecord.GetString("participant_type") != "farmer" {
|
|
return apis.NewNotFoundError("Counterparty is not registered as a farmer", err)
|
|
}
|
|
|
|
txData["evergrow_quantity"] = requestData.EvergrowQuantitySale
|
|
txData["counterparty"] = counterpartyRecord.Id
|
|
txData["tx_date"] = requestData.TxSellDate
|
|
}
|
|
|
|
txForm.LoadData(txData)
|
|
|
|
log.Println("pre_submit")
|
|
if err := txForm.Submit(); err != nil {
|
|
log.Println(err)
|
|
return apis.NewBadRequestError("Failed to submit tx details", err)
|
|
}
|
|
|
|
c.JSON(200, map[string]any{"ok": "true"})
|
|
|
|
return nil
|
|
})
|
|
|
|
return nil
|
|
})
|
|
}
|