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

41 lines
932 B
Go
Raw 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) bootstrapDistributorHook() {
r.pb.OnModelAfterCreate("distributor_base").Add(func(e *core.ModelEvent) error {
farmerRecord, err := r.pb.Dao().FindRecordById("distributor_base", 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 distributors's survey completed:\n\nname: %s\nphone: %s",
userRecord.GetString("name"),
userRecord.GetString("phone"),
)
if err := r.worker.QueueSMSTask(phone, worker.DistributorComplete, ""); err != nil {
return err
}
if err := r.worker.QueueTgTask(msg); err != nil {
return err
}
return nil
})
}