Some checks failed
dev test / test (push) Failing after 9s
dev test / vulnCheck (push) Has been skipped
dev test / Ci-Lint (push) Has been skipped
${{ github.actor }} executed Build Push Prod / deploy (push) Has been cancelled
${{ github.actor }} executed Build Push Prod / build (push) Has been cancelled
143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package dolar
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"time"
|
|
|
|
"git.maximotejeda.com/maximo/dolar/proto/golang/dolar"
|
|
"git.maximotejeda.com/maximo/us-dop-api/internal/domain"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type Adapter struct {
|
|
dolar dolar.DollarClient
|
|
conn *grpc.ClientConn
|
|
log *slog.Logger
|
|
}
|
|
|
|
func NewAdapter(conn *grpc.ClientConn) (*Adapter, error) {
|
|
client := dolar.NewDollarClient(conn)
|
|
log := slog.New(slog.NewJSONHandler(os.Stderr))
|
|
log.With("dapter", "dolar-grpc")
|
|
return &Adapter{dolar: client, conn: conn, log: log}, nil
|
|
}
|
|
func (a *Adapter) GetLatest(name string) (*domain.History, error) {
|
|
hr, err := a.dolar.GetLatest(context.Background(), &dolar.GetLatestRequest{Name: name})
|
|
if err != nil {
|
|
a.log.Error("Get Lastest", "error", err)
|
|
return nil, err
|
|
}
|
|
date := time.Unix(hr.Actual.Parsed, 0)
|
|
loc, err := time.LoadLocation("America/Santo_Domingo")
|
|
if err != nil {
|
|
a.log.Error("Get Lastest", "error", err)
|
|
}
|
|
history := &domain.History{
|
|
ID: hr.Actual.Id,
|
|
Institution: domain.Institution{
|
|
Name: hr.Actual.Name,
|
|
},
|
|
Compra: float64(hr.Actual.Compra),
|
|
Venta: float64(hr.Actual.Venta),
|
|
Parser: hr.Actual.Parser,
|
|
Parsed: date.In(loc).Format(time.DateTime),
|
|
}
|
|
return history, nil
|
|
}
|
|
|
|
func (a Adapter) GetSince(name string, duration int64) (list []*domain.History, err error) {
|
|
hrl, err := a.dolar.GetSince(context.Background(), &dolar.GetSinceRequest{
|
|
Name: name,
|
|
Duration: duration,
|
|
})
|
|
if err != nil {
|
|
a.log.Error("get since", "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
list = []*domain.History{}
|
|
|
|
for _, hr := range hrl.Histories {
|
|
date := time.Unix(hr.Parsed, 0)
|
|
loc, err := time.LoadLocation("America/Santo_Domingo")
|
|
if err != nil {
|
|
a.log.Error("get since", "error", err)
|
|
}
|
|
hist := &domain.History{
|
|
ID: hr.Id,
|
|
Institution: domain.Institution{
|
|
Name: hr.Name,
|
|
},
|
|
Compra: float64(hr.Compra),
|
|
Venta: float64(hr.Venta),
|
|
Parser: hr.Parser,
|
|
Parsed: date.In(loc).Format(time.DateTime),
|
|
}
|
|
list = append(list, hist)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (a Adapter) GetInstByType(name string) (list []string, err error) {
|
|
hrl, err := a.dolar.GetInstByType(context.Background(), &dolar.GetInstByTypeRequest{
|
|
Name: name,
|
|
})
|
|
if err != nil {
|
|
a.log.Error("get inst by type", "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
list = []string{}
|
|
|
|
list = append(list, hrl.InstList...)
|
|
return list, nil
|
|
}
|
|
|
|
func (a Adapter) Subscribe(tgbid int64, instName string) (bool, error) {
|
|
ctx := context.Background()
|
|
_, err := a.dolar.TGBSubscribe(ctx, &dolar.TGBSubscribeRequest{TgbId: tgbid, InstName: instName})
|
|
if err != nil {
|
|
a.log.Error("subscribe", "error", err)
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (a Adapter) Unsubscribe(tgbid int64, instName string) (bool, error) {
|
|
ctx := context.Background()
|
|
_, err := a.dolar.TGBUnsubscribe(ctx, &dolar.TGBUnsubscribeRequest{TgbId: tgbid, InstName: instName})
|
|
if err != nil {
|
|
a.log.Error("unsubscribe", "error", err)
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
|
|
}
|
|
func (a Adapter) GetSubscribedUsers(instName string) ([]int64, error) {
|
|
ctx := context.Background()
|
|
users, err := a.dolar.TGBGetSubscribedUsers(ctx, &dolar.TGBGetSubscribedUsersRequest{InstName: instName})
|
|
if err != nil {
|
|
a.log.Error("get subscribed users", "error", err)
|
|
return nil, err
|
|
}
|
|
list := []int64{}
|
|
list = append(list, users.TgbIds...)
|
|
|
|
return list, nil
|
|
|
|
}
|
|
func (a Adapter) GetSubscribedInsts(tgbid int64) ([]string, error) {
|
|
ctx := context.Background()
|
|
insts, err := a.dolar.TGBGetSubscribedInsts(ctx, &dolar.TGBGetSubscribedInstRequest{TgbId: tgbid})
|
|
if err != nil {
|
|
a.log.Error("get subscribed inst", "error", err)
|
|
return nil, err
|
|
}
|
|
list := []string{}
|
|
|
|
list = append(list, insts.InstName...)
|
|
return list, nil
|
|
}
|