Skip to content

Commit

Permalink
[REFACTOR][FEAT]: better logs, delete mailgun (#127)
Browse files Browse the repository at this point in the history
Co-authored-by: ignavan39 <ivanignatenko28@gmail.com>
  • Loading branch information
enmex and ignavan39 authored Aug 6, 2022
1 parent c4bf707 commit ef912c3
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 113 deletions.
28 changes: 14 additions & 14 deletions app/card/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"net/http"
"strconv"

"github.com/go-chi/chi"
"ucrm/app/card"
repository "ucrm/app/card"

"ucrm/app/models"
"ucrm/pkg/httpext"
"github.com/go-chi/chi"

blogger "github.com/sirupsen/logrus"
dashboardSettings "ucrm/app/dashboard-settings"
"ucrm/app/models"
"ucrm/pkg/httpext"
"ucrm/pkg/logger"
)

type Controller struct {
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *Controller) CreateOne(w http.ResponseWriter, r *http.Request) {
return
}

blogger.Errorf("[card/createOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/createOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[CreateOne]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand All @@ -75,7 +75,7 @@ func (c *Controller) CreateOne(w http.ResponseWriter, r *http.Request) {

webhook, err := c.cardWebhookRepo.GetCardWebhookByPipelineId(payload.PipelineId)
if err != nil {
blogger.Errorf("[card/createOne] CTX: [%v], ERROR:[%s]", err.Error())
logger.Logger.Errorf("[card/createOne] CTX: [%v], ERROR:[%s]", err.Error())
httpext.JSON(w, httpext.CommonError{
Error: "[CreateOne] failed to get webhook",
Code: http.StatusBadRequest,
Expand Down Expand Up @@ -124,7 +124,7 @@ func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {

err = c.repo.Delete(id)
if err != nil {
blogger.Errorf("[card/delete] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/delete] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[Delete]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand All @@ -134,7 +134,7 @@ func (c *Controller) Delete(w http.ResponseWriter, r *http.Request) {

webhook, err := c.cardWebhookRepo.GetCardWebhookByPipelineId(res.PipelineId)
if err != nil {
blogger.Errorf("[card/delete] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/delete] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: "[Delete] failed to get webhook",
Code: http.StatusBadRequest,
Expand Down Expand Up @@ -196,7 +196,7 @@ func (c *Controller) Update(w http.ResponseWriter, r *http.Request) {

oldCard, err := c.repo.GetOne(id)
if err != nil {
blogger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[Update]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand All @@ -214,7 +214,7 @@ func (c *Controller) Update(w http.ResponseWriter, r *http.Request) {

updatedCard, err := c.repo.Update(id, payload.Name, payload.Fields)
if err != nil {
blogger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[Update]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand All @@ -232,7 +232,7 @@ func (c *Controller) Update(w http.ResponseWriter, r *http.Request) {

webhook, err := c.cardWebhookRepo.GetCardWebhookByPipelineId(oldCard.PipelineId)
if err != nil {
blogger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/update] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: "[Update] failed to get pipeline",
Code: http.StatusBadRequest,
Expand Down Expand Up @@ -274,7 +274,7 @@ func (c *Controller) GetOne(w http.ResponseWriter, r *http.Request) {

card, err := c.repo.GetOne(id)
if err != nil {
blogger.Errorf("[card/getOne] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/getOne] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[GetOne]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down Expand Up @@ -338,7 +338,7 @@ func (cr *Controller) UpdateOrder(w http.ResponseWriter, r *http.Request) {

cards, err := cr.repo.GetAllByPipelineId(cardId)
if err != nil {
blogger.Errorf("[card/updateOrder] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/updateOrder] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: err.Error(),
Code: http.StatusInternalServerError,
Expand Down Expand Up @@ -409,7 +409,7 @@ func (cr *Controller) UpdateOrder(w http.ResponseWriter, r *http.Request) {

err = cr.repo.UpdateOrders(cardIdToNewOrder)
if err != nil {
blogger.Errorf("[card/updateOrder] ctx: %v, error: %s", ctx, err.Error())
logger.Logger.Errorf("[card/updateOrder] ctx: %v, error: %s", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[UpdateOrder]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down
13 changes: 6 additions & 7 deletions app/card/card_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"net/http"
"net/url"
"time"

blogger "github.com/sirupsen/logrus"
"ucrm/pkg/logger"
)

func SendCardUpdatesToSubscriber(hostUrl string, newState interface{}, oldState interface{}) {
Expand All @@ -17,18 +16,18 @@ func SendCardUpdatesToSubscriber(hostUrl string, newState interface{}, oldState

stringifyArgs, err := json.Marshal(argMap)
if err != nil {
blogger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
logger.Logger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
}

body := bytes.NewReader(stringifyArgs)
reqURL, err := url.Parse(hostUrl)
if err != nil {
blogger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
logger.Logger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
}

req, err := http.NewRequest(http.MethodPost, reqURL.String(), body)
if err != nil {
blogger.Infof("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
logger.Logger.Infof("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -37,8 +36,8 @@ func SendCardUpdatesToSubscriber(hostUrl string, newState interface{}, oldState

resp, err := client.Do(req)
if err != nil {
blogger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
logger.Logger.Errorf("[Trigger] url webhook: %s, [Error] :%s", hostUrl, err.Error())
}

blogger.Infof("[Trigger] url webhook: %s, [Result] :%s", hostUrl, resp.Status)
logger.Logger.Infof("[Trigger] url webhook: %s, [Result] :%s", hostUrl, resp.Status)
}
22 changes: 14 additions & 8 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"strconv"
"time"

blogger "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"ucrm/pkg/logger"
"ucrm/pkg/pg"

"gopkg.in/yaml.v2"
)

const (
Expand All @@ -26,8 +27,9 @@ type MailLetter struct {
}

type MailConfig struct {
Sender string `yaml:"sender"`
Letters map[string]MailLetter `yaml:"letters"`
GmailUser string
GmailPassword string
Letters map[string]MailLetter `yaml:"letters"`
}

type CoreConfig struct {
Expand Down Expand Up @@ -69,7 +71,7 @@ func validateEnvironment(env string) bool {
}

func confFromFile(fileName string) (*CoreConfig, error) {
blogger.Infoln(fmt.Sprintf("reading from %s", fileName))
logger.Logger.Infoln(fmt.Sprintf("reading from %s", fileName))

file, err := os.Open(fileName)
if err != nil {
Expand Down Expand Up @@ -143,9 +145,13 @@ func Init() error {
SigningKey: os.Getenv("JWT_SIGNING_KEY"),
ExpireDuration: expireDuration,
},
Redis: redis,
Cors: coreConf.Cors,
Mail: coreConf.Mail,
Redis: redis,
Cors: coreConf.Cors,
Mail: MailConfig{
Letters: coreConf.Mail.Letters,
GmailUser: os.Getenv("GMAIL_USER"),
GmailPassword: os.Getenv("GMAIL_PASS"),
},
Environment: environment,
}
return nil
Expand Down
13 changes: 7 additions & 6 deletions app/contact/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"net/http"

"github.com/go-chi/chi"
blogger "github.com/sirupsen/logrus"
"ucrm/app/card"
"ucrm/app/contact"
"ucrm/pkg/httpext"
"ucrm/pkg/logger"

"github.com/go-chi/chi"
)

type Controller struct {
Expand Down Expand Up @@ -49,7 +50,7 @@ func (c *Controller) GetOne(w http.ResponseWriter, r *http.Request) {

contact, err := c.contactRepo.GetOne(ctx, contactId)
if err != nil {
blogger.Errorf("[contact/GetOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/GetOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[GetOne]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down Expand Up @@ -95,7 +96,7 @@ func (c *Controller) CreateOne(w http.ResponseWriter, r *http.Request) {
return
}

blogger.Errorf("[contact/createOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/createOne] CTX: [%v], ERROR:[%s]", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[CreateOne]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c *Controller) Rename(w http.ResponseWriter, r *http.Request) {

err := c.contactRepo.Rename(ctx, contactId, newName)
if err != nil {
blogger.Errorf("[contact/Rename] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/Rename] CTX: [%v], ERROR:[%s]", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[Rename]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down Expand Up @@ -196,7 +197,7 @@ func (c *Controller) Update(w http.ResponseWriter, r *http.Request) {

err = c.contactRepo.Update(ctx, contactId, payload.Name, payload.Phone, payload.City, payload.Fields)
if err != nil {
blogger.Errorf("[contact/Update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/Update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
httpext.JSON(w, httpext.CommonError{
Error: fmt.Sprintf("[Update]:%s", err.Error()),
Code: http.StatusInternalServerError,
Expand Down
23 changes: 12 additions & 11 deletions app/contact/repository/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"database/sql"
"errors"

sq "github.com/Masterminds/squirrel"
"ucrm/app/dashboard/api"
"ucrm/app/models"

blogger "github.com/sirupsen/logrus"
sq "github.com/Masterminds/squirrel"

repository "ucrm/app/contact"
"ucrm/pkg/logger"
"ucrm/pkg/pg"
)

Expand Down Expand Up @@ -40,7 +41,7 @@ func (r *Repository) GetOne(ctx context.Context, contactId string) (*models.Cont
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
blogger.Errorf("[card/GetOneCard] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/GetOneCard] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return nil, err
}

Expand All @@ -54,7 +55,7 @@ func (r *Repository) GetOne(ctx context.Context, contactId string) (*models.Cont
if err := rows.Scan(&contact.Id, &contact.DashboardId, &contact.CardId, &contact.Name, &contact.Phone, &contact.City,
&name, &id, &contactId, &fieldId, &value,
); err != nil {
blogger.Errorf("[card/GetOneCard] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/GetOneCard] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return nil, err
}

Expand Down Expand Up @@ -100,7 +101,7 @@ func (r *Repository) Create(ctx context.Context, dashboardId string, cardId *str
row := qb.PlaceholderFormat(sq.Dollar).
QueryRow()
if err := row.Scan(&contact.Id, &contact.DashboardId, &contact.CardId, &contact.Name, &contact.Phone, &contact.City); err != nil {
blogger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return nil, err
}

Expand Down Expand Up @@ -189,7 +190,7 @@ func (r *Repository) Update(ctx context.Context, contactId string, name *string,
var err error
tx, err = r.pool.Write().Begin()
if err != nil {
blogger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}
}
Expand All @@ -211,7 +212,7 @@ func (r *Repository) Update(ctx context.Context, contactId string, name *string,
if err != nil {
if isUpdateWithTransaction {
if err := tx.Rollback(); err != nil {
blogger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}
}
Expand Down Expand Up @@ -251,7 +252,7 @@ func (r *Repository) Update(ctx context.Context, contactId string, name *string,
if err != nil {
if isUpdateWithTransaction {
if err := tx.Rollback(); err != nil {
blogger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}
}
Expand All @@ -264,7 +265,7 @@ func (r *Repository) Update(ctx context.Context, contactId string, name *string,

if isUpdateWithTransaction {
if err = tx.Commit(); err != nil {
blogger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[card/update] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}
}
Expand All @@ -281,7 +282,7 @@ func (r *Repository) Rename(ctx context.Context, contactId string, newName strin
PlaceholderFormat(sq.Dollar).
Exec()
if err != nil {
blogger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}

Expand All @@ -295,7 +296,7 @@ func (r *Repository) Delete(ctx context.Context, contactId string) error {
PlaceholderFormat(sq.Dollar).
Exec()
if err != nil {
blogger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
logger.Logger.Errorf("[contact/Delete] CTX: [%v], ERROR:[%s]", ctx, err.Error())
return err
}

Expand Down
Loading

0 comments on commit ef912c3

Please sign in to comment.