farmstar-survey-backend/internal/router/distributor.go

102 lines
3.5 KiB
Go
Raw Permalink Normal View History

2024-02-05 14:05:28 +01:00
package router
import (
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/models"
)
func (r *RouterContainer) bootstrapDistributorRoute() {
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.POST("/distributor", func(c echo.Context) error {
requestData := struct {
Phone string `json:"phone"`
Distributor string `json:"distributor_name"`
FertilizerType string `json:"type"`
SyntheticPercentage string `json:"synthetic_percentage"`
OrganicPercentage string `json:"organic_percentage"`
SyntheticFactors []string `json:"synthetic_factors"`
OrganicFactors []string `json:"organic_factors"`
Trends string `json:"trends"`
Obstacles string `json:"obstacles"`
NeedEducation string `json:"need_education"`
FarmerStrategies string `json:"farmer_strategies"`
FarmstarStrategies string `json:"farmstar_strategies"`
}{}
if err := c.Bind(&requestData); err != nil {
return apis.NewBadRequestError("Failed to read request data", err)
}
if err := r.PB.Dao().RunInTransaction(func(txDao *daos.Dao) error {
userRecord, err := r.PB.Dao().FindFirstRecordByData("users", "phone", requestData.Phone)
if err != nil {
return err
}
if userRecord.GetString("participant_type") != "distributor" {
return apis.NewNotFoundError("User is not registered as a distributor", err)
}
distributorExt, err := r.PB.Dao().FindCollectionByNameOrId("distributor_both_fertilizers")
if err != nil {
return err
}
distributorExtRecord := models.NewRecord(distributorExt)
distributorExtForm := forms.NewRecordUpsert(r.PB, distributorExtRecord)
distributorExtForm.SetDao(txDao)
distributorExtForm.LoadData(map[string]any{
"user": userRecord.Id,
"synthetic_percentage": requestData.SyntheticPercentage,
"organic_percentage": requestData.OrganicPercentage,
"synthetic_factors": requestData.SyntheticFactors,
"organic_factors": requestData.OrganicFactors,
"trends": requestData.Trends,
"obstacles": requestData.Obstacles,
"need_education": requestData.NeedEducation,
"farmer_strategies": requestData.FarmerStrategies,
})
if err := distributorExtForm.Submit(); err != nil {
return apis.NewBadRequestError("Failed to submit distributor details", err)
}
distributorBaseCollection, err := r.PB.Dao().FindCollectionByNameOrId("distributor_base")
if err != nil {
return err
}
distributorBaseRecord := models.NewRecord(distributorBaseCollection)
distributorBaseForm := forms.NewRecordUpsert(r.PB, distributorBaseRecord)
distributorBaseForm.SetDao(txDao)
distributorBaseForm.LoadData(map[string]any{
"user": userRecord.Id,
"distributor_name": requestData.Distributor,
"fertilizer_type": requestData.FertilizerType,
})
if err := distributorBaseForm.Submit(); err != nil {
return apis.NewBadRequestError("Failed to submit additional distributor details", err)
}
return nil
}); err != nil {
c.JSON(400, map[string]any{"ok": "false", "error": err.Error()})
return nil
}
c.JSON(200, map[string]any{"ok": "true"})
return nil
})
return nil
})
}