| 1 |
package segment |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/mitch000001/go-hbci/domain" |
| 9 |
"github.com/mitch000001/go-hbci/element" |
| 10 |
) |
| 11 |
|
| 12 |
func StatusProtocolRequestBuilder(versions []int) (func(from, to time.Time, maxEntries int, continuationReference string) StatusProtocolRequest, error) { |
| 13 |
sort.Sort(sort.Reverse(sort.IntSlice(versions))) |
| 14 |
for _, version := range versions { |
| 15 |
switch version { |
| 16 |
case 4: |
| 17 |
return NewStatusProtocolRequestV3, nil |
| 18 |
case 3: |
| 19 |
return NewStatusProtocolRequestV4, nil |
| 20 |
default: |
| 21 |
continue |
| 22 |
} |
| 23 |
} |
| 24 |
return nil, fmt.Errorf("unsupported versions %v", versions) |
| 25 |
} |
| 26 |
|
| 27 |
type StatusProtocolRequest interface { |
| 28 |
ClientSegment |
| 29 |
} |
| 30 |
|
| 31 |
func NewStatusProtocolRequestV3(from, to time.Time, maxEntries int, continuationReference string) StatusProtocolRequest { |
| 32 |
s := &StatusProtocolRequestSegmentV3{ |
| 33 |
From: element.NewDate(from), |
| 34 |
To: element.NewDate(to), |
| 35 |
MaxEntries: element.NewNumber(maxEntries, 4), |
| 36 |
} |
| 37 |
if continuationReference != "" { |
| 38 |
s.ContinuationReference = element.NewAlphaNumeric(continuationReference, 35) |
| 39 |
} |
| 40 |
s.ClientSegment = NewBasicSegment(1, s) |
| 41 |
return s |
| 42 |
} |
| 43 |
|
| 44 |
type StatusProtocolRequestSegmentV3 struct { |
| 45 |
ClientSegment |
| 46 |
From *element.DateDataElement |
| 47 |
To *element.DateDataElement |
| 48 |
MaxEntries *element.NumberDataElement |
| 49 |
ContinuationReference *element.AlphaNumericDataElement |
| 50 |
} |
| 51 |
|
| 52 |
func (s *StatusProtocolRequestSegmentV3) ID() string { return "HKPRO" } |
| 53 |
func (s *StatusProtocolRequestSegmentV3) Version() int { return 3 } |
| 54 |
func (s *StatusProtocolRequestSegmentV3) referencedId() string { return "" } |
| 55 |
func (s *StatusProtocolRequestSegmentV3) sender() string { return senderUser } |
| 56 |
|
| 57 |
func (s *StatusProtocolRequestSegmentV3) elements() []element.DataElement { |
| 58 |
return []element.DataElement{ |
| 59 |
s.From, |
| 60 |
s.To, |
| 61 |
s.MaxEntries, |
| 62 |
s.ContinuationReference, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func NewStatusProtocolRequestV4(from, to time.Time, maxEntries int, continuationReference string) StatusProtocolRequest { |
| 67 |
s := &StatusProtocolRequestSegmentV4{ |
| 68 |
From: element.NewDate(from), |
| 69 |
To: element.NewDate(to), |
| 70 |
MaxEntries: element.NewNumber(maxEntries, 4), |
| 71 |
} |
| 72 |
if continuationReference != "" { |
| 73 |
s.ContinuationReference = element.NewAlphaNumeric(continuationReference, 35) |
| 74 |
} |
| 75 |
s.ClientSegment = NewBasicSegment(1, s) |
| 76 |
return s |
| 77 |
} |
| 78 |
|
| 79 |
type StatusProtocolRequestSegmentV4 struct { |
| 80 |
ClientSegment |
| 81 |
From *element.DateDataElement |
| 82 |
To *element.DateDataElement |
| 83 |
MaxEntries *element.NumberDataElement |
| 84 |
ContinuationReference *element.AlphaNumericDataElement |
| 85 |
} |
| 86 |
|
| 87 |
func (s *StatusProtocolRequestSegmentV4) ID() string { return "HKPRO" } |
| 88 |
func (s *StatusProtocolRequestSegmentV4) Version() int { return 4 } |
| 89 |
func (s *StatusProtocolRequestSegmentV4) referencedId() string { return "" } |
| 90 |
func (s *StatusProtocolRequestSegmentV4) sender() string { return senderUser } |
| 91 |
|
| 92 |
func (s *StatusProtocolRequestSegmentV4) elements() []element.DataElement { |
| 93 |
return []element.DataElement{ |
| 94 |
s.From, |
| 95 |
s.To, |
| 96 |
s.MaxEntries, |
| 97 |
s.ContinuationReference, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
type StatusProtocolResponse interface { |
| 102 |
BankSegment |
| 103 |
Status() domain.StatusAcknowledgement |
| 104 |
} |
| 105 |
|
| 106 |
//go:generate go run ../cmd/unmarshaler/unmarshaler_generator.go -segment StatusProtocolResponseSegment -segment_interface StatusProtocolResponse -segment_versions="StatusProtocolResponseSegmentV3:3:Segment" |
| 107 |
|
| 108 |
type StatusProtocolResponseSegment struct { |
| 109 |
StatusProtocolResponse |
| 110 |
} |
| 111 |
|
| 112 |
type StatusProtocolResponseSegmentV3 struct { |
| 113 |
Segment |
| 114 |
ReferencingMessage *element.ReferencingMessageDataElement |
| 115 |
ReferencingSegment *element.NumberDataElement |
| 116 |
Date *element.DateDataElement |
| 117 |
Time *element.TimeDataElement |
| 118 |
Acknowledgement *element.AcknowledgementDataElement |
| 119 |
} |
| 120 |
|
| 121 |
func (s *StatusProtocolResponseSegmentV3) ID() string { return "HIPRO" } |
| 122 |
func (s *StatusProtocolResponseSegmentV3) Version() int { return 3 } |
| 123 |
func (s *StatusProtocolResponseSegmentV3) referencedId() string { return "HKPRO" } |
| 124 |
func (s *StatusProtocolResponseSegmentV3) sender() string { return senderBank } |
| 125 |
|
| 126 |
func (s *StatusProtocolResponseSegmentV3) elements() []element.DataElement { |
| 127 |
return []element.DataElement{ |
| 128 |
s.ReferencingMessage, |
| 129 |
s.ReferencingSegment, |
| 130 |
s.Date, |
| 131 |
s.Time, |
| 132 |
s.Acknowledgement, |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func (s *StatusProtocolResponseSegmentV3) Status() domain.StatusAcknowledgement { |
| 137 |
ack := s.Acknowledgement.Val() |
| 138 |
if s.ReferencingMessage != nil { |
| 139 |
ack.ReferencingMessage = s.ReferencingMessage.Val() |
| 140 |
ack.ReferencingSegmentNumber = s.ReferencingSegment.Val() |
| 141 |
ack.Type = domain.SegmentAcknowledgement |
| 142 |
} else { |
| 143 |
ack.Type = domain.MessageAcknowledgement |
| 144 |
} |
| 145 |
status := domain.StatusAcknowledgement{ |
| 146 |
Acknowledgement: ack, |
| 147 |
TransmittedAt: s.Date.Val().Add(time.Since(s.Time.Val())), |
| 148 |
} |
| 149 |
return status |
| 150 |
} |
| 151 |
|
| 152 |
type StatusProtocolResponseSegmentV4 struct { |
| 153 |
Segment |
| 154 |
ReferencingMessage *element.ReferencingMessageDataElement |
| 155 |
ReferencingSegment *element.NumberDataElement |
| 156 |
Date *element.DateDataElement |
| 157 |
Time *element.TimeDataElement |
| 158 |
Acknowledgement *element.AcknowledgementDataElement |
| 159 |
} |
| 160 |
|
| 161 |
func (s *StatusProtocolResponseSegmentV4) ID() string { return "HIPRO" } |
| 162 |
func (s *StatusProtocolResponseSegmentV4) Version() int { return 4 } |
| 163 |
func (s *StatusProtocolResponseSegmentV4) referencedId() string { return "HKPRO" } |
| 164 |
func (s *StatusProtocolResponseSegmentV4) sender() string { return senderBank } |
| 165 |
|
| 166 |
func (s *StatusProtocolResponseSegmentV4) elements() []element.DataElement { |
| 167 |
return []element.DataElement{ |
| 168 |
s.ReferencingMessage, |
| 169 |
s.ReferencingSegment, |
| 170 |
s.Date, |
| 171 |
s.Time, |
| 172 |
s.Acknowledgement, |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func (s *StatusProtocolResponseSegmentV4) Status() domain.StatusAcknowledgement { |
| 177 |
ack := s.Acknowledgement.Val() |
| 178 |
if s.ReferencingMessage != nil { |
| 179 |
ack.ReferencingMessage = s.ReferencingMessage.Val() |
| 180 |
ack.ReferencingSegmentNumber = s.ReferencingSegment.Val() |
| 181 |
ack.Type = domain.SegmentAcknowledgement |
| 182 |
} else { |
| 183 |
ack.Type = domain.MessageAcknowledgement |
| 184 |
} |
| 185 |
status := domain.StatusAcknowledgement{ |
| 186 |
Acknowledgement: ack, |
| 187 |
TransmittedAt: s.Date.Val().Add(time.Since(s.Time.Val())), |
| 188 |
} |
| 189 |
return status |
| 190 |
} |