-
Notifications
You must be signed in to change notification settings - Fork 5
/
PreReqCheck.scala
77 lines (69 loc) · 3.62 KB
/
PreReqCheck.scala
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
package com.gu.identityBackfill
import com.gu.identity.GetByEmail
import com.gu.identity.GetByEmail.{NotFound, NotValidated, OtherError}
import com.gu.identityBackfill.Types._
import com.gu.identityBackfill.zuora.GetZuoraSubTypeForAccount
import com.gu.identityBackfill.zuora.GetZuoraSubTypeForAccount.ReaderType.ReaderTypeValue
import com.gu.util.apigateway.ApiGatewayResponse
import com.gu.util.reader.Types.ApiGatewayOp._
import com.gu.util.reader.Types._
import com.gu.util.resthttp.Types.ClientFailableOp
import scalaz.\/
import TypeConvert._
import com.gu.identityBackfill.salesforce.UpdateSalesforceIdentityId.IdentityId
import com.gu.salesforce.TypesForSFEffectsData.SFContactId
object PreReqCheck {
case class PreReqResult(zuoraAccountId: Types.AccountId, sFContactId: SFContactId, requiredIdentityId: IdentityId)
def apply(
getByEmail: EmailAddress => \/[GetByEmail.ApiError, IdentityId],
getSingleZuoraAccountForEmail: EmailAddress => ApiGatewayOp[ZuoraAccountIdentitySFContact],
noZuoraAccountsForIdentityId: IdentityId => ApiGatewayOp[Unit],
zuoraSubType: AccountId => ApiGatewayOp[Unit],
syncableSFToIdentity: SFContactId => ApiGatewayOp[Unit]
)(emailAddress: EmailAddress): ApiGatewayOp[PreReqResult] = {
for {
identityId <- getByEmail(emailAddress).leftMap({
case NotFound => ApiGatewayResponse.notFound("user doesn't have identity")
case NotValidated => ApiGatewayResponse.notFound("identity email not validated")
case OtherError(unknownError) => ApiGatewayResponse.internalServerError(unknownError)
}).toApiGatewayOp.withLogging("GetByEmail")
zuoraAccountForEmail <- getSingleZuoraAccountForEmail(emailAddress)
_ <- noZuoraAccountsForIdentityId(identityId)
_ <- syncableSFToIdentity(zuoraAccountForEmail.sfContactId)
} yield PreReqResult(zuoraAccountForEmail.accountId, zuoraAccountForEmail.sfContactId, identityId)
}
def noZuoraAccountsForIdentityId(
countZuoraAccountsForIdentityId: ClientFailableOp[Int]
): ApiGatewayOp[Unit] = {
for {
zuoraAccountsForIdentityId <- countZuoraAccountsForIdentityId.toApiGatewayOp("zuora issue")
_ <- (zuoraAccountsForIdentityId == 0)
.toApiGatewayContinueProcessing(ApiGatewayResponse.notFound("already used that identity id"))
} yield ()
}
def getSingleZuoraAccountForEmail(
getZuoraAccountsForEmail: ClientFailableOp[List[ZuoraAccountIdentitySFContact]]
): ApiGatewayOp[ZuoraAccountIdentitySFContact] = {
for {
zuoraAccountsForEmail <- getZuoraAccountsForEmail.toApiGatewayOp("zuora issue")
zuoraAccountForEmail <- zuoraAccountsForEmail match {
case one :: Nil => ContinueProcessing(one);
case _ => ReturnWithResponse(ApiGatewayResponse.notFound("should have exactly one zuora account per email at this stage"))
}
_ <- zuoraAccountForEmail match {
case zuoraAccount if zuoraAccount.identityId.isEmpty => ContinueProcessing(());
case _ => ReturnWithResponse(ApiGatewayResponse.notFound("the account we found was already populated with an identity id"))
}
} yield zuoraAccountForEmail
}
def acceptableReaderType(function: ClientFailableOp[List[GetZuoraSubTypeForAccount.ReaderType]]): ApiGatewayOp[Unit] = {
for {
readerTypes <- function.toApiGatewayOp("zuora issue")
incorrectReaderTypes = readerTypes.collect {
case ReaderTypeValue(readerType) if readerType != "Direct" => readerType // it's bad
}
_ <- incorrectReaderTypes.isEmpty
.toApiGatewayContinueProcessing(ApiGatewayResponse.notFound(s"had an incorrect reader type(s): ${incorrectReaderTypes.mkString(",")}"))
} yield ()
}
}