Files
budget-backend/internal/jointexpense/service.go
kratisto a9bca767a9
Some checks failed
golangci-lint / lint (push) Failing after 1m20s
Test / test (push) Failing after 2m15s
chore: migrate to gitea
2026-01-27 00:40:46 +01:00

122 lines
3.2 KiB
Go

package jointexpense
import (
"strconv"
"strings"
"time"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/jointaccount"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/storage/dao"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/storage/model"
"gitea.frenchtouch.duckdns.org/kratisto/budget-backend/internal/utils"
)
type Service struct {
db *Database
}
func (s Service) GetJointexpensesOfAnJointaccountBetween(jointaccountId, from, to string) ([]*Jointexpense, error) {
jointexpenses, err := s.db.GetJointexpensesOfAnJointaccountBetween(jointaccountId, from, to)
utils.GetLogger().Info(err)
if e, ok := err.(*dao.Error); ok {
switch {
case e.Type == dao.ErrTypeNotFound:
return nil, &model.ErrNotFound
default:
return nil, &model.ErrInternalServer
}
} else if err != nil {
return nil, &model.ErrInternalServer
}
if jointexpenses == nil {
return nil, &model.ErrNotFound
}
return jointexpenses, nil
}
func (s Service) GetAllJointexpensesOfAnJointaccount(jointaccountId string) ([]*Jointexpense, error) {
jointexpenses, err := s.db.GetAllJointexpensesOfAnJointaccount(jointaccountId)
utils.GetLogger().Info(err)
if e, ok := err.(*dao.Error); ok {
switch {
case e.Type == dao.ErrTypeNotFound:
return nil, &model.ErrNotFound
default:
return nil, &model.ErrInternalServer
}
} else if err != nil {
return nil, &model.ErrInternalServer
}
if jointexpenses == nil {
return nil, &model.ErrNotFound
}
return jointexpenses, nil
}
func (s Service) CreateJointexpense(jointexpense *Jointexpense) error {
return s.db.CreateJointexpense(jointexpense)
}
func (s Service) ProcessCSVFile(filedata [][]string, jointaccount *jointaccount.Jointaccount) error {
switch jointaccount.Provider {
case "caisse-epargne":
return s.processCaisseEpargne(filedata, jointaccount)
case "boursorama":
return s.processBoursorama(filedata, jointaccount)
case "bnp":
return s.processBnp(filedata, jointaccount)
default:
return nil
}
}
func (s Service) processCaisseEpargne(filedata [][]string, jointaccount *jointaccount.Jointaccount) error {
for _, val := range filedata[4:] {
jointexpenseDate, err := time.Parse("02/01/06", val[0])
if err != nil {
utils.GetLogger().Info(err)
continue
}
amount := val[3]
typeJointexpense := "D"
if amount == "" {
amount = val[4]
typeJointexpense = "C"
}
amountParsed, err := strconv.ParseFloat(strings.Trim(strings.ReplaceAll(amount, ",", "."), "+"), 32)
if err != nil {
utils.GetLogger().Info(err)
continue
}
jointexpense := &Jointexpense{
JointexpenseEditable: JointexpenseEditable{
Value: float32(amountParsed),
Libelle: val[2],
TypeJointexpense: typeJointexpense,
JointexpenseDate: jointexpenseDate,
},
JointaccountId: jointaccount.ID,
}
s.CreateJointexpense(jointexpense)
utils.GetLogger().Info(val)
}
return nil
}
func (s Service) processBoursorama(filedata [][]string, jointaccount *jointaccount.Jointaccount) error {
return nil
}
func (s Service) processBnp(filedata [][]string, jointaccount *jointaccount.Jointaccount) error {
return nil
}
func NewService(database *Database) *Service {
return &Service{db: database}
}