-
Notifications
You must be signed in to change notification settings - Fork 62
/
get_tp_user_product.go
159 lines (148 loc) · 4.2 KB
/
get_tp_user_product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ecommerce
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/dotenx/dotenx/ao-api/pkg/utils"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func (ec *EcommerceController) GetTpUserProduct() gin.HandlerFunc {
return func(c *gin.Context) {
accountId, _ := utils.GetAccountId(c)
projectTag := c.Param("project_tag")
productId := c.Param("product_id")
productVersionId := c.Param("product_version")
project, err := ec.ProjectService.GetProjectByTag(projectTag)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
if project.Type != "ecommerce" {
c.JSON(http.StatusBadRequest, gin.H{
"message": "this project isn't an 'ecommerce' project",
})
return
}
tpAccountId, _ := utils.GetThirdPartyAccountId(c)
user, err := ec.UserManagementService.GetUserInfoById(tpAccountId, projectTag)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
listProductsQuery := fmt.Sprintf(`
select __products as id, version from user_products
where email='%s' and valid_until > '%s';`, user.Email, time.Now().Format(time.RFC3339))
listProductsRes, err := ec.DatabaseService.RunDatabaseQuery(projectTag, listProductsQuery)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
listProductIds, ok := listProductsRes["rows"].([]map[string]interface{})
if !ok {
c.JSON(http.StatusBadRequest, gin.H{
"message": "you haven't buy any product",
})
return
}
boughtThis := false
for _, pid := range listProductIds {
if fmt.Sprint(pid["id"]) == productId && fmt.Sprint(pid["version"]) == productVersionId {
boughtThis = true
}
}
if !boughtThis {
c.JSON(http.StatusBadRequest, gin.H{
"message": "you haven't buy this product or selected version",
})
return
}
getProductQuery := fmt.Sprintf(`
select * from products
where id=%s;`, productId)
productRes, err := ec.DatabaseService.RunDatabaseQuery(projectTag, getProductQuery)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
productRows, ok := productRes["rows"].([]map[string]interface{})
if !ok || len(productRows) != 1 {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "products table isn't in correct state",
})
return
}
productMap := productRows[0]
tableProductBytes, err := json.Marshal(productMap)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
var tableProduct productDTO
err = json.Unmarshal(tableProductBytes, &tableProduct)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
}
var productFileNames []string
if productVersionId == "-1" {
productFileNames = tableProduct.FileNames
} else {
for _, v := range tableProduct.Versions.Versions {
if fmt.Sprint(v.Id) == productVersionId {
productFileNames = v.FileNames
productMap["versions"] = productVersions{
Versions: []productVersion{
v,
},
}
break
}
}
}
// create pre-sign url for all product files
files := make([]map[string]interface{}, 0)
for _, fileName := range productFileNames {
// all links will expires in 24h or 24*60*60 seconds
expiresIn := fmt.Sprintf("%d", 24*60*60)
linkInfo, err := ec.ObjectstoreService.GetPresignUrl(accountId, projectTag, fileName, expiresIn)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
fileInfo, err := ec.ObjectstoreService.GetObject(accountId, projectTag, fileName)
if err != nil {
logrus.Error(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
return
}
linkInfo["display_name"] = fileInfo.DisplayName
linkInfo["file_name"] = fileName
files = append(files, linkInfo)
}
productMap["files"] = files
c.JSON(http.StatusOK, productMap)
}
}