package jointexpense import ( "nos-comptes/internal/jointaccount" "nos-comptes/internal/storage/dao" "nos-comptes/internal/storage/model" "nos-comptes/internal/utils" "strconv" "strings" "time" ) 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} }