Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove hard-coded invoice template IDs #230

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ project/boot/
project/plugins/project/
metals.sbt
.sc
.metals
6 changes: 0 additions & 6 deletions src/main/scala/com/gu/invoicing/common/ZuoraAuth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ object ZuoraAuth extends JsonSupport {
case "PROD" => "https://rest.zuora.com"
}

lazy val GNMAustralia_InvoiceTemplateID: String =
stage match {
case "CODE" => "2c92c0f85ecc47e5015ee7360d602757"
case "PROD" => "2c92a0fd5ecce80c015ee71028643020"
} // GNM Australia Pty Ltd

/** Because list invoices is hit frequently JVM is kept warm and val access token would seem to persist across lambda
* executions which meant the token would expire after one hour and because it was val it would not be requested
* again. Hence now we periodically refreshes the token (making it def would have performance penalty)
Expand Down
13 changes: 1 addition & 12 deletions src/main/scala/com/gu/invoicing/pdf/Impl.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gu.invoicing.pdf

import com.gu.invoicing.common.ZuoraAuth.{accessToken, zuoraApiHost, GNMAustralia_InvoiceTemplateID}
import com.gu.invoicing.common.ZuoraAuth.{accessToken, zuoraApiHost}
import com.gu.invoicing.pdf.Model._
import com.gu.invoicing.common.Http
import scala.util.chaining._
Expand All @@ -23,17 +23,6 @@ object Impl {
.body
.pipe(read[PutResponse](_))

def setGNMAustraliaInvoiceTemplateId(accountId: String): PutResponse = {
Http(s"$zuoraApiHost/v1/object/account/$accountId")
.header("Authorization", s"Bearer $accessToken")
.put(
s"""{"InvoiceTemplateId":"$GNMAustralia_InvoiceTemplateID"}""",
)
.asString
.body
.pipe(read[PutResponse](_))
}

def getAccount(accountId: String): Account =
Http(s"$zuoraApiHost/v1/accounts/$accountId")
.header("Authorization", s"Bearer $accessToken")
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/gu/invoicing/pdf/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Model extends JsonSupport {
AccountId: String,
Body: String, /* Base64 encoded PDF */
)
case class BasicInfo(IdentityId__c: String, invoiceTemplateId: String)
case class BasicInfo(IdentityId__c: String)
case class BillingAndPayment(currency: String)
case class SoldToContact(country: String)
case class Account(
Expand Down
18 changes: 1 addition & 17 deletions src/main/scala/com/gu/invoicing/pdf/Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.gu.invoicing.pdf
import com.gu.invoicing.pdf.Model._
import com.gu.invoicing.pdf.Impl._
import com.gu.invoicing.common.Retry._
import com.gu.invoicing.common.ZuoraAuth.{GNMAustralia_InvoiceTemplateID}

import java.lang.System.getenv

Expand All @@ -18,21 +17,6 @@ object Program {
identityId == account.basicInfo.IdentityId__c,
s"Requested invoice id: $invoiceId appears to belong to different identity: ${account.basicInfo.IdentityId__c}",
)
if (repairRequired(account)) {
setGNMAustraliaInvoiceTemplateId(invoice.AccountId)
regenerateInvoice(invoice.Id)
getInvoice(invoice.Id).Body
} else {
invoice.Body
}
invoice.Body
}

// If the sold to country is Australia, the currency is AUD but the invoice template ID is not the correct one,
// then repair things by setting the correct invoice template ID, regenerating the PDF, and re-getting the PDF body.
private def repairRequired(account: Account): Boolean = {
account.soldToContact.country == "Australia" &&
account.billingAndPayment.currency == "AUD" &&
account.basicInfo.invoiceTemplateId != GNMAustralia_InvoiceTemplateID
}

}
Loading