/
idMatch.go
82 lines (65 loc) · 2.38 KB
/
idMatch.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
package core
import (
"encoding/json"
"log"
"regexp"
"time"
"github.com/naveenchander/GoKube/dal"
"github.com/naveenchander/GoKube/models"
"github.com/naveenchander/GoKube/outbound"
)
// ProcessIDMatch .. ProcessIDMatch
func ProcessIDMatch(incomingRequest string, idDal dal.IIDMatch, expOutbound outbound.IIDMatch) (models.ApplicationErrorCodes, string) {
var patron models.IDMatchPatron
json.Unmarshal([]byte(incomingRequest), &patron)
log.Println("------ Inside Core -----------")
data, err := json.Marshal(patron)
if err != nil {
log.Println("ProcessIDMatch: Error while Marshalling :", err)
return models.HTTPBadRequest, err.Error()
}
if ret, errREG := regexp.Match(`^\d`, []byte(patron.Tin)); errREG != nil || ret == false {
log.Println("Invalid TIN number :" + patron.Tin)
return models.HTTPBadRequest, errREG.Error()
}
if _, tErr := time.Parse("2006/01/02", patron.Dob); tErr != nil {
if _, tErr = time.Parse("01/02/2006", patron.Dob); tErr != nil {
log.Println("Invalid Date format Must be in [yyyy/mm/dd] or [mm/dd/yyyy] :" + patron.Dob)
return models.HTTPBadRequest, tErr.Error()
}
}
log.Println("Incoming Request -> " + string(data[:]))
flakeID := GetNextFlakeID()
returnValue, errValue := idDal.CreateIDMatchRequest(flakeID, incomingRequest)
if returnValue != models.DBOK {
return models.HTTPInternalServerError, errValue
}
cacheValue, errCache := getCacheInBytes("IDMatch:" + patron.Tin)
if errCache != nil {
log.Println("Data not found in Cache. Proceeding to request IDMatch Cache.")
xmlForIDMatch, errXML := buildIDMatchRequest(patron)
if errXML != nil {
log.Println("Invalid Data while creating XML :" + errXML.Error())
return models.HTTPBadRequest, errXML.Error()
}
ret, err := expOutbound.Dial(xmlForIDMatch)
if err != nil {
return models.HTTPInternalServerError, err.Error()
}
// TODO: Check Returu Value and remove TODO
cacheValue = ret
_ = setCacheInBytes("IDMatch:"+patron.Tin, cacheValue, 60)
} else {
log.Println("Found in Cache Responding from Cache")
}
log.Println(string(cacheValue[:]))
response := cacheValue
returnValue, errValue = idDal.UpdateIDMatchResponse(flakeID, string(response[:]))
if returnValue != models.DBOK {
return models.HTTPInternalServerError, errValue
}
return models.HTTPOK, string(data[:])
}
func buildIDMatchRequest(patron models.IDMatchPatron) ([]byte, error) {
return nil, nil
}