mirror of
https://github.com/grassrootseconomics/farmstar-survey-backend.git
synced 2025-04-24 21:11:00 +02:00
refactor: update users registration to include blockchain address
* closes #14
This commit is contained in:
parent
c7dd7a326c
commit
0f332decaf
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/labstack/echo/v5"
|
"github.com/labstack/echo/v5"
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
"github.com/pocketbase/pocketbase/forms"
|
"github.com/pocketbase/pocketbase/forms"
|
||||||
"github.com/pocketbase/pocketbase/models"
|
"github.com/pocketbase/pocketbase/models"
|
||||||
)
|
)
|
||||||
@ -11,10 +12,19 @@ import (
|
|||||||
func (r *RouterContainer) bootstrapRegistrationRoute() {
|
func (r *RouterContainer) bootstrapRegistrationRoute() {
|
||||||
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||||
e.Router.POST("/registration", func(c echo.Context) error {
|
e.Router.POST("/registration", func(c echo.Context) error {
|
||||||
data := apis.RequestInfo(c).Data
|
requestData := struct {
|
||||||
phone := data["phone"].(string)
|
Name string `json:"name"`
|
||||||
|
Phone string `json:"phone"`
|
||||||
|
Gender string `json:"gender"`
|
||||||
|
AgeGroup string `json:"age_group"`
|
||||||
|
ParticipantType string `json:"participant_type"`
|
||||||
|
}{}
|
||||||
|
if err := c.Bind(&requestData); err != nil {
|
||||||
|
return apis.NewBadRequestError("Failed to read request data", err)
|
||||||
|
}
|
||||||
|
|
||||||
address, err := r.ussd.GetAddress(c.Request().Context(), phone)
|
if err := r.PB.Dao().RunInTransaction(func(txDao *daos.Dao) error {
|
||||||
|
address, err := r.ussd.GetAddress(c.Request().Context(), requestData.Phone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -23,20 +33,35 @@ func (r *RouterContainer) bootstrapRegistrationRoute() {
|
|||||||
return apis.NewNotFoundError("Phone # not registered on Sarafu Network", nil)
|
return apis.NewNotFoundError("Phone # not registered on Sarafu Network", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
collection, err := r.PB.Dao().FindCollectionByNameOrId("users")
|
usersCollection, err := r.PB.Dao().FindCollectionByNameOrId("users")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
record := models.NewRecord(collection)
|
userRecord := models.NewRecord(usersCollection)
|
||||||
form := forms.NewRecordUpsert(r.PB, record)
|
userForm := forms.NewRecordUpsert(r.PB, userRecord)
|
||||||
|
userForm.SetDao(txDao)
|
||||||
|
|
||||||
if err := form.LoadRequest(c.Request(), ""); err != nil {
|
userForm.LoadData(map[string]any{
|
||||||
return apis.NewBadRequestError("Failed to register", err)
|
"name": requestData.Name,
|
||||||
|
"phone": requestData.Phone,
|
||||||
|
"gender": requestData.Gender,
|
||||||
|
"age_group": requestData.AgeGroup,
|
||||||
|
"participant_type": requestData.ParticipantType,
|
||||||
|
"blockchain_address": address,
|
||||||
|
"activated": true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := userForm.Submit(); err != nil {
|
||||||
|
return apis.NewBadRequestError("Failed to submit user details", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := form.Submit(); err != nil {
|
return nil
|
||||||
return apis.NewBadRequestError("Failed to register", err)
|
|
||||||
|
}); err != nil {
|
||||||
|
c.JSON(400, map[string]any{"ok": "false", "error": err.Error()})
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, map[string]any{"ok": "true"})
|
c.JSON(200, map[string]any{"ok": "true"})
|
||||||
|
53
migrations/1707140128_updated_users.go
Normal file
53
migrations/1707140128_updated_users.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/pocketbase/dbx"
|
||||||
|
"github.com/pocketbase/pocketbase/daos"
|
||||||
|
m "github.com/pocketbase/pocketbase/migrations"
|
||||||
|
"github.com/pocketbase/pocketbase/models/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
m.Register(func(db dbx.Builder) error {
|
||||||
|
dao := daos.New(db);
|
||||||
|
|
||||||
|
collection, err := dao.FindCollectionByNameOrId("no89qd9ku8qo11e")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// add
|
||||||
|
new_blockchain_address := &schema.SchemaField{}
|
||||||
|
json.Unmarshal([]byte(`{
|
||||||
|
"system": false,
|
||||||
|
"id": "hlbyntap",
|
||||||
|
"name": "blockchain_address",
|
||||||
|
"type": "text",
|
||||||
|
"required": true,
|
||||||
|
"presentable": false,
|
||||||
|
"unique": false,
|
||||||
|
"options": {
|
||||||
|
"min": null,
|
||||||
|
"max": null,
|
||||||
|
"pattern": ""
|
||||||
|
}
|
||||||
|
}`), new_blockchain_address)
|
||||||
|
collection.Schema.AddField(new_blockchain_address)
|
||||||
|
|
||||||
|
return dao.SaveCollection(collection)
|
||||||
|
}, func(db dbx.Builder) error {
|
||||||
|
dao := daos.New(db);
|
||||||
|
|
||||||
|
collection, err := dao.FindCollectionByNameOrId("no89qd9ku8qo11e")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove
|
||||||
|
collection.Schema.RemoveField("hlbyntap")
|
||||||
|
|
||||||
|
return dao.SaveCollection(collection)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user