diff --git a/go.mod b/go.mod index 15371bf..30c29f8 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect github.com/Microsoft/hcsshim v0.11.5 // indirect + github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect github.com/bytedance/sonic v1.12.0 // indirect github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect @@ -78,6 +79,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/pquerna/otp v1.4.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/shirou/gopsutil/v3 v3.23.12 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect diff --git a/go.sum b/go.sum index d300d02..e64dc40 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/Microsoft/hcsshim v0.11.5 h1:haEcLNpj9Ka1gd3B3tAEs9CpE0c+1IhoL59w/exYU38= github.com/Microsoft/hcsshim v0.11.5/go.mod h1:MV8xMfmECjl5HdO7U/3/hFVnkmSBjAjmA09d4bExKcU= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= @@ -159,6 +161,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg= +github.com/pquerna/otp v1.4.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= diff --git a/pkg/abstract/delivery/delivery.go b/pkg/abstract/delivery/delivery.go index 7b9b61e..9ee26fb 100644 --- a/pkg/abstract/delivery/delivery.go +++ b/pkg/abstract/delivery/delivery.go @@ -6,5 +6,6 @@ import ( ) type DeliveryPerson interface { - AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error) + AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (bool, error) + GenerateTOTP(_ context.Context, phone string) (string, string, error) } diff --git a/pkg/database/models/delivery/delivery.go b/pkg/database/models/delivery/delivery.go index df320be..1907b12 100644 --- a/pkg/database/models/delivery/delivery.go +++ b/pkg/database/models/delivery/delivery.go @@ -11,9 +11,12 @@ type DeliveryPerson struct { bun.BaseModel `bun:"table:delivery_person"` DeliveryPersonID int64 `bun:",pk,autoincrement" json:"delivery_person_id"` Name string `bun:"name,notnull" json:"name"` - Phone string `bun:"phone,notnull" json:"phone"` + Phone string `bun:"phone,unique,notnull" json:"phone"` VehicleDetails string `bun:"vehicle_details,notnull" json:"vehicle_details"` Status string `bun:"status,notnull" json:"status"` + AuthKey string `bun:"auth_key,notnull" json:"auth_key"` + AuthKeyURL string `bun:"auth_key_url,notnull" json:"auth_key_url"` + IsAuthSet bool `bun:"is_auth_set,notnull" json:"is_auth_set"` utils.Timestamp } @@ -27,3 +30,9 @@ type Deliveries struct { Order *order.Order `bun:"rel:belongs-to,join:order_id=order_id" json:"-"` utils.Timestamp } + +type DeliveryPersonParams struct { + Name string `json:"name"` + Phone string `json:"phone"` + VehicleDetails string `json:"vehicle_details"` +} diff --git a/pkg/handler/delivery/delivery.go b/pkg/handler/delivery/delivery.go index 3186405..70048fb 100644 --- a/pkg/handler/delivery/delivery.go +++ b/pkg/handler/delivery/delivery.go @@ -12,14 +12,29 @@ func (s *DeliveryHandler) addDeliveryPerson(c *gin.Context) { ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) defer cancel() - var deliverPerson delivery.DeliveryPerson + var deliverPerson delivery.DeliveryPersonParams + var deliverPersonModel delivery.DeliveryPerson if err := c.BindJSON(&deliverPerson); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) return } - _, err := s.service.AddDeliveryPerson(ctx, &deliverPerson) + deliverPersonModel.Name = deliverPerson.Name + deliverPersonModel.Phone = deliverPerson.Phone + deliverPersonModel.VehicleDetails = deliverPerson.VehicleDetails + deliverPersonModel.Status = "AVAILABLE" + + authKey, authKeyURL, err := s.service.GenerateTOTP(ctx, deliverPerson.Phone) + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + deliverPersonModel.AuthKey = authKey + deliverPersonModel.AuthKeyURL = authKeyURL + deliverPersonModel.IsAuthSet = false + + _, err = s.service.AddDeliveryPerson(ctx, &deliverPersonModel) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/pkg/service/delivery/add_delivery_person.go b/pkg/service/delivery/add_delivery_person.go index 8e484ae..a40e9ac 100644 --- a/pkg/service/delivery/add_delivery_person.go +++ b/pkg/service/delivery/add_delivery_person.go @@ -5,10 +5,10 @@ import ( "context" ) -func (deliverSrv *DeliveryService) AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error) { +func (deliverSrv *DeliveryService) AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (bool, error) { _, err := deliverSrv.db.Insert(ctx, deliveryPerson) if err != nil { - return nil, err + return false, err } - return deliveryPerson, nil + return true, nil } diff --git a/pkg/service/delivery/generate_2fa.go b/pkg/service/delivery/generate_2fa.go new file mode 100644 index 0000000..253f6ba --- /dev/null +++ b/pkg/service/delivery/generate_2fa.go @@ -0,0 +1,18 @@ +package delivery + +import ( + "context" + "errors" + "github.com/pquerna/otp/totp" +) + +func (deliverSrv *DeliveryService) GenerateTOTP(_ context.Context, phone string) (string, string, error) { + key, err := totp.Generate(totp.GenerateOpts{ + Issuer: "Food Delivery", + AccountName: phone, + }) + if err != nil { + return "", "", errors.New("error generating key") + } + return key.Secret(), key.URL(), nil +}