admin.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ente-io/cli/internal/api/models"
  6. "time"
  7. )
  8. func (c *Client) GetUserIdFromEmail(ctx context.Context, email string) (*models.UserDetails, error) {
  9. var res models.UserDetails
  10. r, err := c.restClient.R().
  11. SetContext(ctx).
  12. SetResult(&res).
  13. SetQueryParam("email", email).
  14. Get("/admin/user/")
  15. if err != nil {
  16. return nil, err
  17. }
  18. if r.IsError() {
  19. return nil, &ApiError{
  20. StatusCode: r.StatusCode(),
  21. Message: r.String(),
  22. }
  23. }
  24. return &res, nil
  25. }
  26. func (c *Client) UpdateFreePlanSub(ctx context.Context, userDetails *models.UserDetails, storageInBytes int64, expiryTimeInMicro int64) error {
  27. var res interface{}
  28. if userDetails.Subscription.ProductID != "free" {
  29. return fmt.Errorf("user is not on free plan")
  30. }
  31. payload := map[string]interface{}{
  32. "userID": userDetails.User.ID,
  33. "expiryTime": expiryTimeInMicro,
  34. "transactionID": fmt.Sprintf("cli-on-%d", time.Now().Unix()),
  35. "productID": "free",
  36. "paymentProvider": "",
  37. "storage": storageInBytes,
  38. }
  39. r, err := c.restClient.R().
  40. SetContext(ctx).
  41. SetResult(&res).
  42. SetBody(payload).
  43. Put("/admin/user/subscription")
  44. if err != nil {
  45. return err
  46. }
  47. if r.IsError() {
  48. return &ApiError{
  49. StatusCode: r.StatusCode(),
  50. Message: r.String(),
  51. }
  52. }
  53. return nil
  54. }