-
Notifications
You must be signed in to change notification settings - Fork 38
/
tac.go
57 lines (48 loc) · 1.79 KB
/
tac.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
package ghcapi
import (
"strings"
"github.com/go-openapi/runtime/middleware"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
tacop "github.com/transcom/mymove/pkg/gen/ghcapi/ghcoperations/tac"
"github.com/transcom/mymove/pkg/gen/ghcmessages"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/models"
)
// TacValidationHandler validates a TAC value
type TacValidationHandler struct {
handlers.HandlerConfig
}
// Handle accepts the TAC value and returns a payload showing if it is valid
func (h TacValidationHandler) Handle(params tacop.TacValidationParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
if appCtx.Session() == nil {
sessionErr := apperror.NewSessionError(
"user is not authorized",
)
appCtx.Logger().Error(sessionErr.Error())
return tacop.NewTacValidationUnauthorized(), sessionErr
}
if !appCtx.Session().IsOfficeApp() || !appCtx.Session().IsOfficeUser() {
sessionOfficeErr := apperror.NewSessionError(
"user is not authenticated with TOO office role",
)
appCtx.Logger().Error(sessionOfficeErr.Error())
return tacop.NewTacValidationForbidden(), sessionOfficeErr
}
db := appCtx.DB()
isValid, err := db.Where("tac = $1", strings.ToUpper(params.Tac)).
Exists(&models.TransportationAccountingCode{})
if err != nil {
appCtx.Logger().
Error("Error looking for transportation accounting code", zap.Error(err))
return tacop.NewTacValidationInternalServerError(), err
}
tacValidationPayload := &ghcmessages.TacValid{
IsValid: &isValid,
}
return tacop.NewTacValidationOK().WithPayload(tacValidationPayload), nil
})
}