Skip to content

Commit

Permalink
feat: support get delivery metric by current month
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce committed Mar 1, 2024
1 parent a616dc9 commit 5fb8fbf
Show file tree
Hide file tree
Showing 29 changed files with 672 additions and 35 deletions.
9 changes: 6 additions & 3 deletions pkg/adapter/fortress/delivery_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package fortress
import (
"encoding/json"
"fmt"
"net/http"

"github.com/dwarvesf/fortress-discord/pkg/model"
"net/http"
"strconv"
)

func (f *Fortress) GetDeliveryMetricsWeeklyReportDiscordMsg() (msg *model.AdapterDeliveryMetricsReportMsg, err error) {
Expand All @@ -29,12 +29,15 @@ func (f *Fortress) GetDeliveryMetricsWeeklyReportDiscordMsg() (msg *model.Adapte
return msg, nil
}

func (f *Fortress) GetDeliveryMetricsMonthlyReportDiscordMsg() (msg *model.AdapterDeliveryMetricsReportMsg, err error) {
func (f *Fortress) GetDeliveryMetricsMonthlyReportDiscordMsg(now bool) (msg *model.AdapterDeliveryMetricsReportMsg, err error) {
req, err := f.makeReq("/api/v1/delivery-metrics/report/monthly/discord-msg", http.MethodGet, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("to-now", strconv.FormatBool(now))

req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand Down
30 changes: 30 additions & 0 deletions pkg/adapter/fortress/fortress.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,33 @@ func (f *Fortress) Get30daysTotalReward() (*model.AdapterICYTotalEarned, error)

return &total, nil
}

func (f *Fortress) GetBanks(id, bin, swiftCode string) (banks *model.AdapterBank, err error) {
req, err := f.makeReq("/api/v1/metadata/banks", http.MethodGet, nil)
if err != nil {
return nil, err
}

q := req.URL.Query()
q.Add("id", id)
q.Add("bin", bin)
q.Add("swiftCode", swiftCode)
req.URL.RawQuery = q.Encode()

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
}

if err := json.NewDecoder(resp.Body).Decode(&banks); err != nil {
return nil, fmt.Errorf("invalid decoded, error %v", err.Error())
}

return banks, nil
}
5 changes: 4 additions & 1 deletion pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type FortressAdapter interface {
GetBraineryReport(view string, date string) (*model.BraineryMetric, error)

GetDeliveryMetricsWeeklyReportDiscordMsg() (msg *model.AdapterDeliveryMetricsReportMsg, err error)
GetDeliveryMetricsMonthlyReportDiscordMsg() (msg *model.AdapterDeliveryMetricsReportMsg, err error)
GetDeliveryMetricsMonthlyReportDiscordMsg(now bool) (msg *model.AdapterDeliveryMetricsReportMsg, err error)
SyncDeliveryMetricsData() (err error)

GetEmployees(in EmployeeSearch) (rs []model.Employee, err error)
Expand All @@ -52,4 +52,7 @@ type FortressAdapter interface {
ListICYEarnedTransactions(discordID string, page, size int) (*model.AdapterICYEarnedTransactions, error)
GetICYTotalEarned(discordID string) (*model.AdapterICYTotalEarned, error)
Get30daysTotalReward() (*model.AdapterICYTotalEarned, error)

CheckWithdrawCondition(discordID string) (rs *model.AdapterCheckWithdrawCondition, err error)
GetBanks(id, bin, swiftCode string) (banks *model.AdapterBank, err error)
}
33 changes: 33 additions & 0 deletions pkg/adapter/fortress/withdrawal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fortress

import (
"encoding/json"
"fmt"
"net/http"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (f *Fortress) CheckWithdrawCondition(discordID string) (rs *model.AdapterCheckWithdrawCondition, err error) {
req, err := f.makeReq(fmt.Sprintf("/api/v1/discords/withdraw/check?discordID=%v", discordID), http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid call, code %v", resp.StatusCode)
}

if err := json.NewDecoder(resp.Body).Decode(&rs); err != nil {
return nil, fmt.Errorf("invalid decoded, error %v", err.Error())
}

return rs, nil
}
17 changes: 10 additions & 7 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
RegexPatternUrl = `((?:https?://)[^\s]+)`
RegexPatternGithub = `gh:(\w+)`
RegexPatternDescription = `d:"(.*?)"`
RegexPatternTime = `t:(\w+)`
)

const (
Expand All @@ -45,13 +46,7 @@ var mapEmoji = map[string]string{
"STAR_ANIMATED": "<a:star_animated:1131862886592024586>",
"INCREASING_ANIMATED": "<a:increasing_animated:1131862879319097394>",
"CLOCK_NEW": "<:clock_new:1131863089185292428>",
}

func GetEmoji(emoji string) string {
if os.Getenv("ENV") != "prod" {
return mapEmojiDev[emoji]
}
return mapEmoji[emoji]
"ICY": "<:ICY:1049620715374133288>",
}

var mapEmojiDev = map[string]string{
Expand All @@ -65,6 +60,14 @@ var mapEmojiDev = map[string]string{
"STAR_ANIMATED": "<a:star_animated:1133460443550195832>",
"INCREASING_ANIMATED": "<a:increasing_animated:1133460451091550289>",
"CLOCK_NEW": "<:clock:1133460445257281658>",
"ICY": ":ice_cube:",
}

func GetEmoji(emoji string) string {
if os.Getenv("ENV") != "prod" {
return mapEmojiDev[emoji]
}
return mapEmoji[emoji]
}

var DwarvesRole = map[string]int{
Expand Down
2 changes: 1 addition & 1 deletion pkg/discord/command/brainery/brainery_post.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package brainery

import (
"github.com/dwarvesf/fortress-discord/pkg/utils/stringutils"
"strings"
"time"

"github.com/dwarvesf/fortress-discord/pkg/constant"
"github.com/dwarvesf/fortress-discord/pkg/discord/service/brainery"
"github.com/dwarvesf/fortress-discord/pkg/model"
"github.com/dwarvesf/fortress-discord/pkg/utils/stringutils"
)

func (e *Brainery) Post(message *model.DiscordMessage) error {
Expand Down
2 changes: 2 additions & 0 deletions pkg/discord/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/dwarvesf/fortress-discord/pkg/discord/command/trend"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/trial"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/updates"
"github.com/dwarvesf/fortress-discord/pkg/discord/command/withdraw"
"github.com/dwarvesf/fortress-discord/pkg/discord/history"
"github.com/dwarvesf/fortress-discord/pkg/discord/service"
"github.com/dwarvesf/fortress-discord/pkg/discord/view"
Expand Down Expand Up @@ -81,6 +82,7 @@ func New(cfg *config.Config, l logger.Logger, svc service.Servicer, view view.Vi
mma.New(l, svc, view, cfg),
trend.New(l, svc, view),
salary.New(l, svc, view),
withdraw.New(l, svc, view),
})

return cmd
Expand Down
34 changes: 22 additions & 12 deletions pkg/discord/command/deliverymetrics/deliverymetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,55 @@ package deliverymetrics

import (
"github.com/bwmarrin/discordgo"
"github.com/dwarvesf/fortress-discord/pkg/constant"
"github.com/dwarvesf/fortress-discord/pkg/model"
"github.com/dwarvesf/fortress-discord/pkg/utils/stringutils"
)

func (c *DeliveryMetricsCmd) WeeklyReport(message *model.DiscordMessage) error {
func (e *DeliveryMetricsCmd) WeeklyReport(message *model.DiscordMessage) error {
// 1. get data from service
r, err := c.svc.DeliveryMetrics().GetWeeklyReportDiscordMsg()
r, err := e.svc.DeliveryMetrics().GetWeeklyReportDiscordMsg()
if err != nil {
c.L.Error(err, "can't get WeeklyReport")
e.L.Error(err, "can't get WeeklyReport")
return err
}

// 2. render
return c.view.DeliveryMetrics().Send(message, r)
return e.view.DeliveryMetrics().Send(message, r)
}

func (c *DeliveryMetricsCmd) MonthlyReport(message *model.DiscordMessage) error {
func (e *DeliveryMetricsCmd) MonthlyReport(message *model.DiscordMessage) error {
rawFormattedContent := stringutils.FormatString(message.RawContent)

// 1. get data from service
r, err := c.svc.DeliveryMetrics().GetMonthlyReportDiscordMsg()
times := stringutils.ExtractPattern(rawFormattedContent, constant.RegexPatternTime)
now := false
if len(times) > 0 {
now = times[0] == "now"
}

r, err := e.svc.DeliveryMetrics().GetMonthlyReportDiscordMsg(now)
if err != nil {
c.L.Error(err, "can't get MonthlyReport")
e.L.Error(err, "can't get MonthlyReport")
return err
}

// 2. render
return c.view.DeliveryMetrics().Send(message, r)
return e.view.DeliveryMetrics().Send(message, r)
}

func (c *DeliveryMetricsCmd) SyncRawData(message *model.DiscordMessage) error {
func (e *DeliveryMetricsCmd) SyncRawData(message *model.DiscordMessage) error {
msg := &discordgo.MessageEmbed{
Title: "**Delivery Sync**",
Description: "Sync data successfully \n",
}

err := c.svc.DeliveryMetrics().SyncData()
err := e.svc.DeliveryMetrics().SyncData()
if err != nil {
c.L.Error(err, "can't get SyncRawData response")
e.L.Error(err, "can't get SyncRawData response")
msg.Description = "Sync data failed " + err.Error() + "\n"
}

// 2. render
return c.view.DeliveryMetrics().Send(message, msg)
return e.view.DeliveryMetrics().Send(message, msg)
}
2 changes: 1 addition & 1 deletion pkg/discord/command/index/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (i *Index) Execute(message *model.DiscordMessage) error {
}

func (i *Index) Name() string {
return "Index Command"
return "Home Command"
}

func (i *Index) Help(message *model.DiscordMessage) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/discord/command/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (t *Index) Search(message *model.DiscordMessage) error {
// 1. get data from service
data, err := t.svc.TechRadar().GetList(constant.TechRadarAll, &message.ContentArgs[1])
if err != nil {
t.L.Error(err, "can't get list of Index techradar")
t.L.Error(err, "can't get list of Home techradar")
return err
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/discord/command/withdraw/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package withdraw

import (
"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (e *Withdraw) Prefix() []string {
return []string{"withdraw"}
}

// Execute is where we handle logic for each command
func (e *Withdraw) Execute(message *model.DiscordMessage) error {
if len(message.ContentArgs) == 1 {
return e.DefaultCommand(message)
}

//switch message.ContentArgs[1] {
//case "with":
// return e.Home(message)
//}

return nil
}

func (e *Withdraw) Name() string {
return "Home Command"
}

func (e *Withdraw) Help(message *model.DiscordMessage) error {
return nil
}

func (e *Withdraw) DefaultCommand(message *model.DiscordMessage) error {
return e.Withdraw(message)
}

func (e *Withdraw) PermissionCheck(message *model.DiscordMessage) (bool, []string) {
return true, []string{}
}
12 changes: 12 additions & 0 deletions pkg/discord/command/withdraw/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package withdraw

import (
"github.com/dwarvesf/fortress-discord/pkg/discord/base"
"github.com/dwarvesf/fortress-discord/pkg/model"
)

type Commander interface {
base.TextCommander

Withdraw(message *model.DiscordMessage) error
}
43 changes: 43 additions & 0 deletions pkg/discord/command/withdraw/withdraw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package withdraw

import (
"github.com/dwarvesf/fortress-discord/pkg/discord/service"
"github.com/dwarvesf/fortress-discord/pkg/discord/view"
"github.com/dwarvesf/fortress-discord/pkg/logger"
"github.com/dwarvesf/fortress-discord/pkg/model"
)

type Withdraw struct {
L logger.Logger
svc service.Servicer
view view.Viewer
}

func New(l logger.Logger, svc service.Servicer, view view.Viewer) Commander {
return &Withdraw{
L: l,
svc: svc,
view: view,
}
}

func (e *Withdraw) Withdraw(message *model.DiscordMessage) error {
cond, err := e.svc.Withdrawal().CheckWithdrawCondition(message.Author.ID)
if err != nil {
e.L.Error(err, "failed to check withdraw condition")
return e.view.Withdraw().ErrorWithdraw(message, err)
}

banks, err := e.svc.Withdrawal().GetBanks("", "", "")
if err != nil {
e.L.Error(err, "failed to get banks")
return e.view.Withdraw().ErrorWithdraw(message, err)
}

in := &model.WithdrawInput{
ICYAmount: cond.ICYAmount,
VNDAmount: cond.VNDAmount,
ICYVNDRate: cond.ICYVNDRate,
}
return e.view.Withdraw().Home(message, in, banks)
}
Loading

0 comments on commit 5fb8fbf

Please sign in to comment.