-
Notifications
You must be signed in to change notification settings - Fork 38
/
ppm_document_fetcher.go
81 lines (66 loc) · 3.01 KB
/
ppm_document_fetcher.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
package ppmshipment
import (
"github.com/gofrs/uuid"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/db/utilities"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
// ppmDocumentFetcher is the concrete implementation of the services.PPMDocumentFetcher interface
type ppmDocumentFetcher struct{}
// NewPPMDocumentFetcher creates a new struct
func NewPPMDocumentFetcher() services.PPMDocumentFetcher {
return &ppmDocumentFetcher{}
}
// GetPPMDocuments returns all documents associated with a PPM shipment.
func (f *ppmDocumentFetcher) GetPPMDocuments(appCtx appcontext.AppContext, mtoShipmentID uuid.UUID) (*models.PPMDocuments, error) {
var documents models.PPMDocuments
err := appCtx.DB().
Scope(utilities.ExcludeDeletedScope(models.WeightTicket{})).
EagerPreload(
"EmptyDocument.UserUploads.Upload",
"FullDocument.UserUploads.Upload",
"ProofOfTrailerOwnershipDocument.UserUploads.Upload",
).
InnerJoin("ppm_shipments ppm", "ppm.id = weight_tickets.ppm_shipment_id").
Where("ppm.shipment_id = ? AND ppm.deleted_at IS NULL", mtoShipmentID).
All(&documents.WeightTickets)
if err != nil {
return nil, apperror.NewQueryError("WeightTicket", err, "unable to search for WeightTickets")
}
for i := range documents.WeightTickets {
documents.WeightTickets[i].EmptyDocument.UserUploads = documents.WeightTickets[i].EmptyDocument.UserUploads.FilterDeleted()
documents.WeightTickets[i].FullDocument.UserUploads = documents.WeightTickets[i].FullDocument.UserUploads.FilterDeleted()
documents.WeightTickets[i].ProofOfTrailerOwnershipDocument.UserUploads = documents.WeightTickets[i].ProofOfTrailerOwnershipDocument.UserUploads.FilterDeleted()
}
err = appCtx.DB().
Scope(utilities.ExcludeDeletedScope(models.ProgearWeightTicket{})).
EagerPreload(
"Document.UserUploads.Upload",
).
InnerJoin("ppm_shipments ppm", "ppm.id = progear_weight_tickets.ppm_shipment_id").
Where("ppm.shipment_id = ? AND ppm.deleted_at IS NULL", mtoShipmentID).
All(&documents.ProgearWeightTickets)
if err != nil {
return nil, apperror.NewQueryError("ProgearWeightTicket", err, "unable to search for ProgearWeightTickets")
}
for i := range documents.ProgearWeightTickets {
documents.ProgearWeightTickets[i].Document.UserUploads = documents.ProgearWeightTickets[i].Document.UserUploads.FilterDeleted()
}
err = appCtx.DB().
Scope(utilities.ExcludeDeletedScope(models.MovingExpense{})).
EagerPreload(
"Document.UserUploads.Upload",
).
InnerJoin("ppm_shipments ppm", "ppm.id = moving_expenses.ppm_shipment_id").
Where("ppm.shipment_id = ? AND ppm.deleted_at IS NULL", mtoShipmentID).
All(&documents.MovingExpenses)
if err != nil {
return nil, apperror.NewQueryError("MovingExpense", err, "unable to search for MovingExpenses")
}
for i := range documents.MovingExpenses {
documents.MovingExpenses[i].Document.UserUploads = documents.MovingExpenses[i].Document.UserUploads.FilterDeleted()
}
return &documents, nil
}