-
Notifications
You must be signed in to change notification settings - Fork 0
/
use_case.go
187 lines (150 loc) · 4.01 KB
/
use_case.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package update_invoice
import (
"context"
"errors"
"github.com/devdammit/shekel/cmd/unit/internal/entities"
port "github.com/devdammit/shekel/cmd/unit/internal/ports/use-cases"
"github.com/devdammit/shekel/pkg/log"
)
type PeriodsRepository interface {
GetLast(ctx context.Context) (*entities.Period, error)
}
type InvoicesRepository interface {
GetByID(ctx context.Context, id uint64) (*entities.Invoice, error)
GetByTemplateID(ctx context.Context, id uint64) ([]entities.Invoice, error)
}
type ContactsRepository interface {
GetByID(ctx context.Context, id uint64) (*entities.Contact, error)
}
type InvoicesService interface {
GetScheduledInvoices(ctx context.Context, template entities.InvoiceTemplate) ([]entities.Invoice, error)
}
type CalendarService interface {
Sync(ctx context.Context) error
}
type Logger interface {
Warn(msg string, fields ...log.Field)
}
type UnitOfWork interface {
CreateInvoices(invoices []entities.Invoice, template entities.InvoiceTemplate)
DeleteInvoice(id uint64)
DeleteTemplate(id uint64)
UpdateInvoice(invoice entities.Invoice)
Commit(ctx context.Context) error
}
type UseCase struct {
periods PeriodsRepository
invoices InvoicesRepository
contacts ContactsRepository
service InvoicesService
calendar CalendarService
logger Logger
uow UnitOfWork
}
func NewUseCase(
periods PeriodsRepository,
invoices InvoicesRepository,
contacts ContactsRepository,
service InvoicesService,
calendar CalendarService,
logger Logger,
uow UnitOfWork,
) *UseCase {
return &UseCase{
periods: periods,
invoices: invoices,
contacts: contacts,
service: service,
calendar: calendar,
logger: logger,
uow: uow,
}
}
func (u *UseCase) Execute(ctx context.Context, req port.UpdateInvoiceRequest) error {
period, err := u.periods.GetLast(ctx)
if err != nil {
return err
}
if period.ClosedAt != nil {
return errors.New("cannot update invoice at closed period")
}
if req.Date.Before(period.CreatedAt.Time) {
return errors.New("cannot update invoice at previous period")
}
invoice, err := u.invoices.GetByID(ctx, req.InvoiceID)
if err != nil {
return err
}
if invoice.Date.Before(period.CreatedAt.Time) {
return errors.New("cannot update invoice at previous period")
}
if invoice.Status == entities.InvoiceStatusPaid {
return errors.New("cannot update paid invoice")
}
contact, err := u.contacts.GetByID(ctx, req.ContactID)
if err != nil {
return err
}
invoice.Name = req.Name
invoice.Desc = req.Description
invoice.Type = req.Type
invoice.Amount = req.Amount
invoice.Contact = contact
invoice.Date = req.Date
if req.Plan == nil {
invoice.Template = nil
u.uow.UpdateInvoice(*invoice)
err = u.uow.Commit(ctx)
if err != nil {
return err
}
err = u.calendar.Sync(ctx)
if err != nil {
u.logger.Warn("cannot sync calendar", log.Err(err))
}
return nil
}
invoices, err := u.invoices.GetByTemplateID(ctx, invoice.Template.ID)
if err != nil {
return err
}
newTemplate := entities.InvoiceTemplate{
Name: invoice.Name,
Desc: invoice.Desc,
Type: invoice.Type,
Amount: invoice.Amount,
ContactID: invoice.Contact.ID,
Date: invoice.Date,
RepeatPlanner: &entities.RepeatPlanner{
Interval: req.Plan.Interval,
IntervalCount: req.Plan.IntervalCount,
DaysOfWeek: req.Plan.DaysOfWeek,
EndDate: req.Plan.EndDate,
EndCount: req.Plan.EndCount,
},
}
u.uow.DeleteTemplate(invoice.Template.ID)
deletingInvoices := make([]entities.Invoice, 0, len(invoices))
for _, inv := range invoices {
if req.Date.After(inv.Date.Time) {
deletingInvoices = append(deletingInvoices, inv)
}
}
for _, inv := range deletingInvoices {
u.uow.DeleteInvoice(inv.ID)
}
scheduledInvoices, err := u.service.GetScheduledInvoices(ctx, newTemplate)
if err != nil {
return err
}
u.uow.CreateInvoices(scheduledInvoices, newTemplate)
err = u.uow.Commit(ctx)
if err != nil {
return err
}
err = u.calendar.Sync(ctx)
if err != nil {
u.logger.Warn("cannot sync calendar", log.Err(err))
}
return nil
}