farmstar-survey-backend/internal/hooks/redeem.go
2024-02-05 16:05:28 +03:00

72 lines
1.9 KiB
Go

package hooks
import (
"fmt"
"github.com/pocketbase/pocketbase/core"
)
var (
distributors = map[string]string{
"maraba_investments": "Maraba Investments",
"farmers_center": "Farmers Center",
"farmers_world": "Farmers World",
"farmers_desk": "Farmers Desk",
"mazao_na_afya": "Mazao na Afya",
}
rewardsSchedule = map[int]string{
60: "25% discount on a 50kg bag of EverGrow",
120: "50% discount on a 50kg bag of EverGrow",
180: "75% discount on a 50kg bag of EverGrow",
240: "1 free 50kg bag of EverGrow",
}
)
func (r *HooksContainer) bootstrapRedeemHook() {
r.pb.OnModelAfterCreate("redemption").Add(func(e *core.ModelEvent) error {
redemptionRecord, err := r.pb.Dao().FindRecordById("redemption", e.Model.GetId())
if err != nil {
return err
}
initiatorRecord, err := r.pb.Dao().FindRecordById("users", redemptionRecord.GetString("distributor_initiator"))
if err != nil {
return err
}
counterpartyRecord, err := r.pb.Dao().FindRecordById("users", redemptionRecord.GetString("farmer"))
if err != nil {
return err
}
tgMsg := fmt.Sprintf(
"New redemption request:\n\ndistributor name: %s\ndistributor entity: %s\nfarmer name: %s\nredemption request: %d FSP",
initiatorRecord.GetString("name"),
redemptionRecord.GetString("distributor"),
counterpartyRecord.GetString("name"),
redemptionRecord.GetInt("fsp_redemption_request"),
)
if err := r.worker.QueueTgTask(tgMsg); err != nil {
return err
}
if err := r.worker.QueueSMSTask(
counterpartyRecord.GetString("phone"),
"",
fmt.Sprintf(
"FSP redemption request received at %s for %s. Send %d FSP to %s to complete redemption.",
distributors[redemptionRecord.GetString("distributor")],
rewardsSchedule[redemptionRecord.GetInt("fsp_redemption_request")],
redemptionRecord.GetInt("fsp_redemption_request"),
r.redemptionVault,
),
); err != nil {
return err
}
return nil
})
}