farmstar-survey-backend/internal/hooks/farmer.go

45 lines
973 B
Go
Raw Permalink Normal View History

2024-02-05 14:05:28 +01:00
package hooks
import (
"fmt"
"github.com/grassrootseconomics/farmstar-survey-backend/internal/worker"
"github.com/pocketbase/pocketbase/core"
)
func (r *HooksContainer) bootstrapFarmerHook() {
r.pb.OnModelAfterCreate("farmer_farm").Add(func(e *core.ModelEvent) error {
farmerRecord, err := r.pb.Dao().FindRecordById("farmer_farm", e.Model.GetId())
if err != nil {
return err
}
userRecord, err := r.pb.Dao().FindRecordById("users", farmerRecord.GetString("user"))
if err != nil {
return err
}
phone := userRecord.GetString("phone")
msg := fmt.Sprintf(
"New farmer's survey completed:\n\nname: %s\nphone: %s",
userRecord.GetString("name"),
phone,
)
if err := r.worker.QueueSMSTask(phone, worker.FarmerComplete, ""); err != nil {
return err
}
if err := r.worker.QueueTgTask(msg); err != nil {
return err
}
if err := r.worker.QueueRewardsTask(phone, 130_000_000); err != nil {
return err
}
return nil
})
}