Skip to content

Commit

Permalink
chore: update repo layer
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce committed May 30, 2023
1 parent 248cab2 commit 5847f75
Show file tree
Hide file tree
Showing 27 changed files with 78 additions and 117 deletions.
8 changes: 4 additions & 4 deletions pkg/handler/payroll/payroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/dwarvesf/fortress-api/pkg/service"
"github.com/dwarvesf/fortress-api/pkg/service/currency"
"github.com/dwarvesf/fortress-api/pkg/store"
commissionStore "github.com/dwarvesf/fortress-api/pkg/store/commission"
"github.com/dwarvesf/fortress-api/pkg/store/employee"
commissionStore "github.com/dwarvesf/fortress-api/pkg/store/employeecommission"
"github.com/dwarvesf/fortress-api/pkg/store/payroll"
"github.com/dwarvesf/fortress-api/pkg/utils"
"github.com/dwarvesf/fortress-api/pkg/utils/timeutil"
Expand Down Expand Up @@ -261,7 +261,7 @@ func GetPayrollDetailHandler(h *handler, month, year, batch int, email string) (
FromDate: &batchDate,
ToDate: &toDate,
}
userCommissions, err := h.store.Commission.Get(h.repo.DB(), commissionQuery)
userCommissions, err := h.store.EmployeeCommission.Get(h.repo.DB(), commissionQuery)
if err != nil {
return nil, err
}
Expand All @@ -279,7 +279,7 @@ func GetPayrollDetailHandler(h *handler, month, year, batch int, email string) (
notes = append(notes, fmt.Sprintf("%v (%v)", j, utils.FormatCurrencyAmount(duplicate[j])))
}

bonusExplain := []model.CommissionExplain{}
var bonusExplain []model.CommissionExplain
if len(payrolls[i].ProjectBonusExplain) > 0 {
if err := json.Unmarshal(payrolls[i].ProjectBonusExplain, &bonusExplain); err != nil {
return nil, errs.ErrCannotReadProjectBonusExplain
Expand Down Expand Up @@ -485,7 +485,7 @@ func getCommissionExplains(h *handler, p *model.Payroll, markPaid bool) ([]model
}
if markPaid {
for i := range commissionExplains {
err := h.store.Commission.MarkPaid(h.repo.DB(), commissionExplains[i].ID)
err := h.store.EmployeeCommission.MarkPaid(h.repo.DB(), commissionExplains[i].ID)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/handler/payroll/payroll_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/dwarvesf/fortress-api/pkg/model"
bcModel "github.com/dwarvesf/fortress-api/pkg/service/basecamp/model"
"github.com/dwarvesf/fortress-api/pkg/service/currency"
commissionStore "github.com/dwarvesf/fortress-api/pkg/store/commission"
commissionStore "github.com/dwarvesf/fortress-api/pkg/store/employeecommission"
"github.com/dwarvesf/fortress-api/pkg/utils/timeutil"
)

Expand Down Expand Up @@ -41,12 +41,12 @@ func calculatePayrolls(h *handler, users []*model.Employee, batchDate time.Time)
opsExpenseID = consts.OpsExpenseTodoID
}

opsTodolists, err := h.service.Basecamp.Todo.GetAllInList(opsExpenseID, opsID)
opsTodoLists, err := h.service.Basecamp.Todo.GetAllInList(opsExpenseID, opsID)
if err != nil {
h.logger.Error(err, "can't get ops expense todo")
return nil, err
}
for _, exps := range opsTodolists {
for _, exps := range opsTodoLists {
isApproved := false
cmts, err := h.service.Basecamp.Comment.Gets(opsID, exps.ID)
if err != nil {
Expand Down Expand Up @@ -122,7 +122,7 @@ func calculatePayrolls(h *handler, users []*model.Employee, batchDate time.Time)
// TODO...
// try to calculate if user start/end after/before the payroll
// fallback to default
baseSalary, contract, _ = tryParticalCalculation(batchDate, dueDate, *u.JoinedDate, u.LeftDate, users[i].BaseSalary.PersonalAccountAmount, users[i].BaseSalary.CompanyAccountAmount)
baseSalary, contract, _ = tryPartialCalculation(batchDate, dueDate, *u.JoinedDate, u.LeftDate, users[i].BaseSalary.PersonalAccountAmount, users[i].BaseSalary.CompanyAccountAmount)

var bonus, commission, reimbursementAmount model.VietnamDong
var bonusExplains, commissionExplains []model.CommissionExplain
Expand Down Expand Up @@ -194,7 +194,7 @@ func calculatePayrolls(h *handler, users []*model.Employee, batchDate time.Time)
if dueDate.Sub(*candidates[i].OfferStartDate).Hours()/24 < 15 {
continue
}
baseSalary, contract, _ := tryParticalCalculation(batchDate, dueDate, *candidates[i].OfferStartDate, nil, int64(candidates[i].OfferSalary), 0)
baseSalary, contract, _ := tryPartialCalculation(batchDate, dueDate, *candidates[i].OfferStartDate, nil, int64(candidates[i].OfferSalary), 0)
total := model.NewVietnamDong(baseSalary)

p := model.Payroll{
Expand Down Expand Up @@ -290,7 +290,7 @@ func getBonus(
EmployeeID: u.ID.String(),
IsPaid: false,
}
userCommissions, err := h.store.Commission.Get(h.repo.DB(), commissionQuery)
userCommissions, err := h.store.EmployeeCommission.Get(h.repo.DB(), commissionQuery)
if err != nil {
return
}
Expand All @@ -317,7 +317,7 @@ func getBonus(
return
}

func tryParticalCalculation(
func tryPartialCalculation(
batchDate, dueDate, startDate time.Time,
leftDate *time.Time,
baseSalary, contract int64,
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/googledrive/google_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type googleService struct {
}

// New function return Google service
func New(config *oauth2.Config, appConfig *config.Config) Service {
func New(config *oauth2.Config, appConfig *config.Config) IService {
return &googleService{
config: config,
appConfig: appConfig,
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/googledrive/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
ErrInvoicePDFNotFound = errors.New("invoice pdf not found")
)

type Service interface {
type IService interface {
UploadInvoicePDF(invoice *model.Invoice, dirName string) error
MoveInvoicePDF(invoice *model.Invoice, fromDirName, toDirName string) error
DownloadInvoicePDF(invoice *model.Invoice, dirName string) ([]byte, error)
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/mochi/mochi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ type IService interface {
GetVaultTransaction(req *model.VaultTransactionRequest) (*model.VaultTransactionResponse, error)
}

type mochiClient struct {
type client struct {
cfg *config.Config
l logger.Logger
}

func New(cfg *config.Config, l logger.Logger) IService {
return &mochiClient{
return &client{
cfg: cfg,
l: l,
}
}

func (m *mochiClient) GetVaultTransaction(req *model.VaultTransactionRequest) (*model.VaultTransactionResponse, error) {
func (m *client) GetVaultTransaction(req *model.VaultTransactionRequest) (*model.VaultTransactionResponse, error) {
var client = &http.Client{}
request, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/vault/%s/transaction?start_time=%s&end_time=%s", m.cfg.Mochi.BaseURL, req.VaultId, req.StartTime, req.EndTime), nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/sendgrid/sendgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sendgrid/sendgrid-go/helpers/mail"
)

type Service interface {
type IService interface {
SendEmail(*model.Email) error
}

Expand All @@ -20,7 +20,7 @@ type sendgridClient struct {
l logger.Logger
}

func New(key string, cfg *config.Config, l logger.Logger) Service {
func New(key string, cfg *config.Config, l logger.Logger) IService {
client := sendgrid.NewSendClient(key)
return &sendgridClient{
client: client,
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type Service struct {
Currency currency.IService
Discord discord.IService
Google googleauth.IService
GoogleDrive googledrive.Service
GoogleDrive googledrive.IService
GoogleMail googlemail.IService
ImprovMX improvmx.IService
Mochi mochi.IService
Notion notion.IService
Sendgrid sendgrid.Service
Sendgrid sendgrid.IService
Wise wise.IService
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 0 additions & 26 deletions pkg/store/commission/interface.go

This file was deleted.

49 changes: 0 additions & 49 deletions pkg/store/commission/pg.go

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bonus
package employeebonus

import (
"gorm.io/gorm"
Expand All @@ -8,7 +8,7 @@ import (

type store struct{}

// New initilize new store for bonus
// New initialize new store for bonus
func New() IStore {
return &store{}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package bonus
package employeebonus

import (
"gorm.io/gorm"

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

// Store is an interface that abstract database method for bonus
// IStore is an interface that abstract database method for bonus
type IStore interface {
GetByUserID(db *gorm.DB, id model.UUID) ([]model.EmployeeBonus, error)
}
27 changes: 26 additions & 1 deletion pkg/store/employeecommission/employee_commission.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
package employeecommission

import (
"time"

"gorm.io/gorm"

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

type store struct{}

// New initialize new store for commission
func New() IStore {
return &store{}
}

// Create make new one by id
func (s *store) Create(db *gorm.DB, employeeCommissions []model.EmployeeCommission) ([]model.EmployeeCommission, error) {
return employeeCommissions, db.Create(&employeeCommissions).Error
}

func (s *store) Get(db *gorm.DB, q Query) ([]model.EmployeeCommission, error) {
var res []model.EmployeeCommission
if q.EmployeeID != "" {
db = db.Where("employee_id = ?", q.EmployeeID)
}
if q.FromDate != nil {
db = db.Where("created_at > ?", q.FromDate)
}
if q.ToDate != nil {
db = db.Where("created_at < ?", q.ToDate)
}
return res, db.Preload("Invoice").Where("is_paid = ?", q.IsPaid).Find(&res).Error
}

func (s *store) MarkPaid(db *gorm.DB, id model.UUID) error {
var cms model.EmployeeCommission
err := db.Where("id = ?", id).Find(&cms).Error
if err != nil {
return err
}
return db.Model(&cms).Updates(map[string]interface{}{"is_paid": true, "paid_at": time.Now()}).Error
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package commission
package employeecommission

// func TestCreate(t *testing.T) {
// t.Parallel()
Expand Down
14 changes: 14 additions & 0 deletions pkg/store/employeecommission/interface.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package employeecommission

import (
"time"

"gorm.io/gorm"

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

// IStore is an interface that abstract database method for commission
type IStore interface {
// Create new row for table user_commissions, save the commission
// for an user for specific invoice
Create(db *gorm.DB, employeeCommissions []model.EmployeeCommission) ([]model.EmployeeCommission, error)
Get(db *gorm.DB, q Query) ([]model.EmployeeCommission, error)
MarkPaid(db *gorm.DB, ids model.UUID) error
}

type Query struct {
EmployeeID string
FromDate *time.Time
ToDate *time.Time
IsPaid bool
}
14 changes: 7 additions & 7 deletions pkg/store/expense/pg.go → pkg/store/expense/expense.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import (
"github.com/dwarvesf/fortress-api/pkg/model"
)

type pgService struct {
type store struct {
}

func New() IStore {
return &pgService{}
return &store{}
}

func (s *pgService) Create(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
func (s *store) Create(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
return e, db.Create(&e).Error
}

func (s *pgService) Delete(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
func (s *store) Delete(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
return e, db.Delete(&e).Error
}

func (s *pgService) Update(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
func (s *store) Update(db *gorm.DB, e *model.Expense) (*model.Expense, error) {
return e, db.Model(&model.Expense{}).Updates(&e).Error
}

func (s *pgService) GetValuation(db *gorm.DB, y int) (*model.CurrencyView, error) {
func (s *store) GetValuation(db *gorm.DB, y int) (*model.CurrencyView, error) {
res := &model.CurrencyView{}
return res, db.Raw("select * from vw_expense where year = ?", y).Find(&res).Error
}

func (s *pgService) GetByQuery(db *gorm.DB, q *ExpenseQuery) (*model.Expense, error) {
func (s *store) GetByQuery(db *gorm.DB, q *ExpenseQuery) (*model.Expense, error) {
e := &model.Expense{}
if q.BasecampID != 0 {
db = db.Where("basecamp_id = ?", q.BasecampID)
Expand Down
File renamed without changes.
Loading

0 comments on commit 5847f75

Please sign in to comment.