Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(BEDS-425) implement endpoint for mobile app bundles #842

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/cmd/typescript_converter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// Files that should not be converted to TypeScript
var ignoredFiles = []string{"data_access", "search_types"}
var ignoredFiles = []string{"data_access", "search_types", "archiver"}

var typeMappings = map[string]string{
"decimal.Decimal": "string /* decimal.Decimal */",
Expand Down
14 changes: 14 additions & 0 deletions backend/pkg/api/data_access/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dataaccess

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/gobitfly/beaconchain/pkg/api/enums"
t "github.com/gobitfly/beaconchain/pkg/api/types"
"github.com/gobitfly/beaconchain/pkg/commons/utils"
"github.com/gobitfly/beaconchain/pkg/userservice"
Expand All @@ -19,6 +21,8 @@ type AppRepository interface {
AddMobileNotificationToken(userID uint64, deviceID, notifyToken string) error
GetAppSubscriptionCount(userID uint64) (uint64, error)
AddMobilePurchase(tx *sql.Tx, userID uint64, paymentDetails t.MobileSubscription, verifyResponse *userservice.VerifyResponse, extSubscriptionId string) error
GetLatestBundleForNativeVersion(ctx context.Context, nativeVersion uint64, environment enums.Environment) (*t.MobileAppBundleStats, error)
IncrementBundleDeliveryCount(ctx context.Context, bundleVerison uint64) error
}

// GetUserIdByRefreshToken basically used to confirm the claimed user id with the refresh token. Returns the userId if successful
Expand Down Expand Up @@ -105,3 +109,13 @@ func (d *DataAccessService) AddMobilePurchase(tx *sql.Tx, userID uint64, payment

return err
}

func (d *DataAccessService) GetLatestBundleForNativeVersion(ctx context.Context, nativeVersion uint64, environment enums.Environment) (*t.MobileAppBundleStats, error) {
// @TODO data access
return d.dummy.GetLatestBundleForNativeVersion(ctx, nativeVersion, environment)
}

func (d *DataAccessService) IncrementBundleDeliveryCount(ctx context.Context, bundleVerison uint64) error {
// @TODO data access
return d.dummy.IncrementBundleDeliveryCount(ctx, bundleVerison)
}
8 changes: 8 additions & 0 deletions backend/pkg/api/data_access/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,11 @@ func (d *DummyService) GetHealthz(ctx context.Context, showAll bool) types.Healt
r, _ := getDummyData[types.HealthzData]()
return r
}

func (d *DummyService) GetLatestBundleForNativeVersion(ctx context.Context, nativeVersion uint64, environment enums.Environment) (*t.MobileAppBundleStats, error) {
return getDummyStruct[t.MobileAppBundleStats]()
}

func (d *DummyService) IncrementBundleDeliveryCount(ctx context.Context, bundleVerison uint64) error {
return nil
}
35 changes: 35 additions & 0 deletions backend/pkg/api/enums/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,38 @@ func (c ChartAggregation) Duration(secondsPerEpoch uint64) time.Duration {
return 0
}
}

// ----------------
// Environment

type Environment int

var _ EnumFactory[Environment] = Environment(0)

const (
EnvironmentProduction Environment = iota
EnvironmentStaging
)

func (e Environment) Int() int {
return int(e)
}

func (Environment) NewFromString(s string) Environment {
switch s {
case "production", "prod", "":
return EnvironmentProduction
case "staging":
return EnvironmentStaging
default:
return Environment(-1)
}
}

var Environments = struct {
Production Environment
Staging Environment
}{
EnvironmentProduction,
EnvironmentStaging,
}
LuccaBitfly marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions backend/pkg/api/handlers/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,50 @@ func (h *HandlerService) InternalGetValidatorDashboardRocketPoolMinipools(w http
h.PublicGetValidatorDashboardRocketPoolMinipools(w, r)
}

// --------------------------------------
// Mobile

func (h *HandlerService) InternalGetMobileLatestBundle(w http.ResponseWriter, r *http.Request) {
var v validationError
q := r.URL.Query()
force := v.checkBool(q.Get("force"), "force")
bundleVersion := v.checkUint(q.Get("bundle_version"), "bundle_version")
nativeVersion := v.checkUint(q.Get("native_version"), "native_version")
environment := checkEnum[enums.Environment](&v, q.Get("environment"), "environment")
if v.hasErrors() {
handleErr(w, r, v)
return
}
stats, err := h.dai.GetLatestBundleForNativeVersion(r.Context(), nativeVersion, environment)
if err != nil {
handleErr(w, r, err)
return
}
var data types.MobileBundleData
data.HasNativeUpdateAvailable = stats.MaxNativeVersion > nativeVersion
// if given bundle version is smaller than the latest and delivery count is less than target count, return the latest bundle
if force || (bundleVersion < stats.LatestBundleVersion && (stats.TargetCount == 0 || stats.DeliveryCount < stats.TargetCount)) {
data.BundleUrl = stats.BundleUrl
}
returnOk(w, r, data)
LuccaBitfly marked this conversation as resolved.
Show resolved Hide resolved
}

func (h *HandlerService) InternalPostMobileBundleDeliveries(w http.ResponseWriter, r *http.Request) {
var v validationError
vars := mux.Vars(r)
bundleVersion := v.checkUint(vars["bundle_version"], "bundle_version")
if v.hasErrors() {
handleErr(w, r, v)
return
}
err := h.dai.IncrementBundleDeliveryCount(r.Context(), bundleVersion)
if err != nil {
handleErr(w, r, err)
return
}
returnNoContent(w, r)
}

// --------------------------------------
// Notifications

Expand Down
2 changes: 2 additions & 0 deletions backend/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func addRoutes(hs *handlers.HandlerService, publicRouter, internalRouter *mux.Ro
{http.MethodGet, "/mobile/authorize", nil, hs.InternalPostMobileAuthorize},
{http.MethodPost, "/mobile/equivalent-exchange", nil, hs.InternalPostMobileEquivalentExchange},
{http.MethodPost, "/mobile/purchase", nil, hs.InternalHandleMobilePurchase},
{http.MethodGet, "/mobile/latest-bundle", nil, hs.InternalGetMobileLatestBundle},
{http.MethodPost, "/mobile/bundles/{bundle_version}/deliveries", nil, hs.InternalPostMobileBundleDeliveries},

{http.MethodPost, "/logout", nil, hs.InternalPostLogout},

Expand Down
11 changes: 11 additions & 0 deletions backend/pkg/api/types/data_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,14 @@ type HealthzData struct {
DeploymentType string `json:"deployment_type"`
Reports map[string][]HealthzResult `json:"status_reports"`
}

// -------------------------
// Mobile structs

type MobileAppBundleStats struct {
LatestBundleVersion uint64
BundleUrl string
TargetCount uint64 // coalesce to 0 if column is null
DeliveryCount uint64
MaxNativeVersion uint64 // the max native version of the whole table for the given environment
}
8 changes: 8 additions & 0 deletions backend/pkg/api/types/mobile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

type MobileBundleData struct {
BundleUrl string `json:"bundle_url,omitempty"`
HasNativeUpdateAvailable bool `json:"has_native_update_available"`
}

type GetMobileLatestBundleResponse ApiDataResponse[MobileBundleData]
12 changes: 12 additions & 0 deletions frontend/types/api/mobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Code generated by tygo. DO NOT EDIT.
/* eslint-disable */
import type { ApiDataResponse } from './common'

//////////
// source: mobile.go

export interface MobileBundleData {
bundle_url?: string;
has_native_update_available: boolean;
}
export type GetMobileLatestBundleResponse = ApiDataResponse<MobileBundleData>;
Loading