-
Notifications
You must be signed in to change notification settings - Fork 5
/
list_returns_cmd.go
217 lines (195 loc) · 5.62 KB
/
list_returns_cmd.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/alufers/inpost-cli/swagger"
"github.com/mdp/qrterminal/v3"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"rsc.io/qr"
)
/*
Possible status values:
NEW,
ACCEPTED,
USED,
REJECTED,
EXPIRED,
DELIVERED,
CANCELED,
OTHER;
TODO: add to swagger
*/
func PrintReturnsListTable(parcels []swagger.ReturnTicketNetwork, showDates bool) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Org name", "Created date", "Expry date", "Status", "Return code"})
for _, v := range parcels {
expiryTime := ""
if !v.ExpiryDate.IsZero() {
if showDates {
expiryTime = v.ExpiryDate.Format("2006-01-02 15:04:05")
} else {
expiryTime = formatDuration(time.Until(v.ExpiryDate))
}
}
var colors []tablewriter.Colors
if v.Status == "ACCEPTED" {
var expiryColor = tablewriter.Colors{tablewriter.FgHiMagentaColor}
// if expiry is near, highlight it in red
if !v.ExpiryDate.IsZero() && time.Until(v.ExpiryDate) < 12*time.Hour {
expiryColor = tablewriter.Colors{tablewriter.Bold, tablewriter.BgWhiteColor, tablewriter.BgRedColor}
}
colors = []tablewriter.Colors{
{tablewriter.Bold, tablewriter.FgHiMagentaColor},
{tablewriter.FgHiMagentaColor},
expiryColor,
{tablewriter.FgHiMagentaColor},
{tablewriter.Bold, tablewriter.FgHiMagentaColor},
}
colors = append(colors)
}
table.Rich([]string{
v.OrganizationName,
v.CreatedDate.Format("2006-01-02 15:04:05"),
expiryTime,
v.Status,
v.ReturnCode,
}, colors)
}
table.Render()
}
func PrintReturnsList(parcels []swagger.ReturnTicketNetwork, showDates bool, qrLevel *qr.Level) {
for _, v := range parcels {
fmt.Printf("-----------\n")
expiryTime := ""
if !v.ExpiryDate.IsZero() {
if showDates {
expiryTime = v.ExpiryDate.Format("2006-01-02 15:04:05")
} else {
expiryTime = formatDuration(time.Until(v.ExpiryDate))
}
}
fmt.Printf("Org name: %s\n", v.OrganizationName)
fmt.Printf("Created date: %s\n", v.CreatedDate.Format("2006-01-02 15:04:05"))
fmt.Printf("Expiry date: %s\n", expiryTime)
fmt.Printf("Status: %s\n", v.Status)
fmt.Printf("Return code: %s\n", v.ReturnCode)
if strings.TrimSpace(v.QrCode) != "" {
qrBuf := &bytes.Buffer{}
qrterminal.GenerateWithConfig(v.QrCode, qrterminal.Config{
Level: *qrLevel,
Writer: qrBuf,
BlackChar: qrterminal.BLACK,
WhiteChar: qrterminal.WHITE,
QuietZone: 2,
}) // inpost uses HIGH error correction
_, err := qr.Encode(v.QrCode, *qrLevel)
if err != nil {
fmt.Printf("Error generating QR code: %s", err)
}
fmt.Printf("QR code: \n%s\n", qrBuf.String())
}
}
}
var ListReturnsCmd = &cli.Command{
Name: "list-returns",
Aliases: []string{"r", "returns", "r-ls"},
Usage: "[--format list|table|json] [--date|-d] [--status STATUS]... -- List pending returns",
ArgsUsage: "fffff",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "status",
Aliases: []string{"s"},
Usage: "ACCEPTED etc.",
},
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
DefaultText: "list",
Value: "list",
Usage: "The output format: list (with QR codes), json or table",
},
&cli.StringFlag{
Name: "qr",
Aliases: []string{"q"},
DefaultText: "H",
Value: "H",
Usage: "QR code quality settings: H, M, L, none (no QR code)",
},
&cli.BoolFlag{
Name: "date",
Aliases: []string{"d"},
Usage: "Show dates instead of time ago",
},
},
Action: func(c *cli.Context) error {
qrLevel, err := stringToQRLevel(c.String("qr"))
if err != nil {
return err
}
if err := RefreshAllTokens(c); err != nil {
return fmt.Errorf("failed to refresh token: %v", err)
}
limitedAccounts := GetLimitedAccounts(c)
// uab, _ := time.Unix(0, 0).AddDate(0, -1, 0).MarshalJSON()
// ua := "1970-01-01T00:00:00.001Z"
returnsByPhoneNumber := map[string][]swagger.ReturnTicketNetwork{}
returnsForJson := []struct {
swagger.ReturnTicketNetwork
RecipientPhoneNumber string `json:"recipient_phone_number"`
}{}
for _, acc := range limitedAccounts {
apiClient := acc.ToClient()
data, _, err := apiClient.DefaultApi.V1ReturnsTicketsGet(c.Context)
if err != nil {
return fmt.Errorf("failed to request returns: %w", err)
}
filteredParcels := []swagger.ReturnTicketNetwork{}
for _, parcel := range data.Tickets {
if len(c.StringSlice("status")) == 0 {
filteredParcels = append(filteredParcels, parcel)
}
for _, status := range c.StringSlice("status") {
if parcel.Status == status {
filteredParcels = append(filteredParcels, parcel)
break
}
}
}
returnsByPhoneNumber[acc.PhoneNumber] = filteredParcels
for _, parcel := range filteredParcels {
returnsForJson = append(returnsForJson, struct {
swagger.ReturnTicketNetwork
RecipientPhoneNumber string `json:"recipient_phone_number"`
}{
ReturnTicketNetwork: parcel,
RecipientPhoneNumber: acc.PhoneNumber,
})
}
}
switch c.String("format") {
case "list":
for phoneNumber, parcels := range returnsByPhoneNumber {
if len(returnsByPhoneNumber) > 1 {
fmt.Printf("%v:\n", phoneNumber)
}
PrintReturnsList(parcels, c.Bool("date"), qrLevel)
}
case "table":
for phoneNumber, parcels := range returnsByPhoneNumber {
if len(returnsByPhoneNumber) > 1 {
fmt.Printf("%v:\n", phoneNumber)
}
PrintReturnsListTable(parcels, c.Bool("date"))
}
case "json":
s, _ := json.MarshalIndent(returnsForJson, "", " ")
fmt.Print(string(s))
}
return nil
},
}