github.com/companieshouse/lfp-pay-api@v0.0.0-20230203133422-0ca455cd79f9/transformers/payable_resource.go (about) 1 package transformers 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/companieshouse/chs.go/log" 8 "github.com/companieshouse/lfp-pay-api-core/constants" 9 "github.com/companieshouse/lfp-pay-api-core/models" 10 "github.com/companieshouse/lfp-pay-api/utils" 11 ) 12 13 // PayableResourceRequestToDB will take the input request from the REST call and transform it to a dao ready for 14 // insertion into the database 15 func PayableResourceRequestToDB(req *models.PayableRequest) *models.PayableResourceDao { 16 transactionsDAO := map[string]models.TransactionDao{} 17 for _, tx := range req.Transactions { 18 transactionsDAO[tx.TransactionID] = models.TransactionDao{ 19 Amount: tx.Amount, 20 MadeUpDate: tx.MadeUpDate, 21 Type: tx.Type, 22 } 23 } 24 25 reference := utils.GenerateReferenceNumber() 26 etag, err := utils.GenerateEtag() 27 if err != nil { 28 log.Error(fmt.Errorf("error generating etag: [%s]", err)) 29 } 30 format := "/company/%s/penalties/late-filing/payable/%s" 31 32 self := fmt.Sprintf(format, req.CompanyNumber, reference) 33 34 paymentLinkFormat := "%s/payment" 35 paymentLink := fmt.Sprintf(paymentLinkFormat, self) 36 37 resumeJourneyLinkFormat := "/late-filing-penalty/company/%s/penalty/%s/view-penalties" 38 resumeJourneyLink := fmt.Sprintf(resumeJourneyLinkFormat, req.CompanyNumber, req.Transactions[0].TransactionID) // Assumes there is only one transaction 39 40 createdAt := time.Now().Truncate(time.Millisecond) 41 dao := &models.PayableResourceDao{ 42 CompanyNumber: req.CompanyNumber, 43 Reference: reference, 44 Data: models.PayableResourceDataDao{ 45 Etag: etag, 46 Transactions: transactionsDAO, 47 Payment: models.PaymentDao{ 48 Status: constants.Pending.String(), 49 }, 50 CreatedAt: &createdAt, 51 CreatedBy: models.CreatedByDao{ 52 Email: req.CreatedBy.Email, 53 ID: req.CreatedBy.ID, 54 Forename: req.CreatedBy.Forename, 55 Surname: req.CreatedBy.Surname, 56 }, 57 Links: models.PayableResourceLinksDao{ 58 Self: self, 59 Payment: paymentLink, 60 ResumeJourney: resumeJourneyLink, 61 }, 62 }, 63 } 64 65 return dao 66 } 67 68 // PayableResourceDaoToCreatedResponse will transform a payable resource dao that has successfully been created into 69 // a http response entity 70 func PayableResourceDaoToCreatedResponse(model *models.PayableResourceDao) *models.CreatedPayableResource { 71 return &models.CreatedPayableResource{ 72 ID: model.Reference, 73 Links: models.CreatedPayableResourceLinks{ 74 Self: model.Data.Links.Self, 75 }, 76 } 77 } 78 79 // PayableResourceDBToRequest will take the Dao version of a payable resource and convert to an request version 80 func PayableResourceDBToRequest(payableDao *models.PayableResourceDao) *models.PayableResource { 81 transactions := []models.TransactionItem{} 82 for key, val := range payableDao.Data.Transactions { 83 tx := models.TransactionItem{ 84 TransactionID: key, 85 Amount: val.Amount, 86 MadeUpDate: val.MadeUpDate, 87 Type: val.Type, 88 } 89 transactions = append(transactions, tx) 90 } 91 92 payable := models.PayableResource{ 93 CompanyNumber: payableDao.CompanyNumber, 94 Reference: payableDao.Reference, 95 Transactions: transactions, 96 Etag: payableDao.Data.Etag, 97 CreatedAt: payableDao.Data.CreatedAt, 98 CreatedBy: models.CreatedBy(payableDao.Data.CreatedBy), 99 Links: models.PayableResourceLinks(payableDao.Data.Links), 100 Payment: models.Payment(payableDao.Data.Payment), 101 } 102 103 return &payable 104 } 105 106 // PayableResourceToPaymentDetails will create a PaymentDetails resource (for integrating into payment service) from an LFP PayableResource 107 func PayableResourceToPaymentDetails(payable *models.PayableResource) *models.PaymentDetails { 108 costs := []models.Cost{} 109 for _, tx := range payable.Transactions { 110 cost := models.Cost{ 111 Amount: fmt.Sprintf("%g", tx.Amount), 112 AvailablePaymentMethods: []string{"credit-card"}, 113 ClassOfPayment: []string{"penalty"}, 114 Description: "Late Filing Penalty", 115 DescriptionIdentifier: "late-filing-penalty", 116 Kind: "cost#cost", 117 ResourceKind: "late-filing-penalty#late-filing-penalty", 118 ProductType: "late-filing-penalty", 119 } 120 costs = append(costs, cost) 121 } 122 123 payment := models.PaymentDetails{ 124 Description: "Late Filing Penalty", 125 Etag: payable.Etag, // use the same Etag as PayableResource its built from - if PayableResource changes PaymentDetails may change too 126 Kind: "payment-details#payment-details", 127 Links: models.PaymentDetailsLinks{ 128 Self: payable.Links.Payment, // this is the payment details resource so should use payment link from PayableResource 129 Resource: payable.Links.Self, // PayableResources Self link is the resource this PaymentDetails is paying for 130 }, 131 PaidAt: payable.Payment.PaidAt, 132 PaymentReference: payable.Payment.Reference, 133 Status: payable.Payment.Status, 134 CompanyNumber: payable.CompanyNumber, 135 Items: costs, 136 } 137 138 return &payment 139 }