refactor: update users registration to include blockchain address

* closes #14
This commit is contained in:
Mohamed Sohail 2024-02-05 16:44:52 +03:00
parent c7dd7a326c
commit 0f332decaf
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 98 additions and 20 deletions

View File

@ -4,6 +4,7 @@ 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"
)
@ -11,32 +12,56 @@ import (
func (r *RouterContainer) bootstrapRegistrationRoute() {
r.PB.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.POST("/registration", func(c echo.Context) error {
data := apis.RequestInfo(c).Data
phone := data["phone"].(string)
address, err := r.ussd.GetAddress(c.Request().Context(), phone)
if err != nil {
return err
requestData := struct {
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)
}
if address == "" {
return apis.NewNotFoundError("Phone # not registered on Sarafu Network", nil)
}
if err := r.PB.Dao().RunInTransaction(func(txDao *daos.Dao) error {
address, err := r.ussd.GetAddress(c.Request().Context(), requestData.Phone)
if err != nil {
return err
}
collection, err := r.PB.Dao().FindCollectionByNameOrId("users")
if err != nil {
return err
}
if address == "" {
return apis.NewNotFoundError("Phone # not registered on Sarafu Network", nil)
}
record := models.NewRecord(collection)
form := forms.NewRecordUpsert(r.PB, record)
usersCollection, err := r.PB.Dao().FindCollectionByNameOrId("users")
if err != nil {
return err
}
if err := form.LoadRequest(c.Request(), ""); err != nil {
return apis.NewBadRequestError("Failed to register", err)
}
userRecord := models.NewRecord(usersCollection)
userForm := forms.NewRecordUpsert(r.PB, userRecord)
userForm.SetDao(txDao)
if err := form.Submit(); err != nil {
return apis.NewBadRequestError("Failed to register", err)
userForm.LoadData(map[string]any{
"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)
}
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"})

View 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)
})
}