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

PIN-4256 Added SubUnitType #69

Merged
merged 12 commits into from
Nov 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,22 @@ object JsonFormats {
}
}

private implicit val ptutFormat: RootJsonFormat[PersistentTenantUnitType] =
new RootJsonFormat[PersistentTenantUnitType] {
override def read(json: JsValue): PersistentTenantUnitType = json match {
case JsString("AOO") => PersistentTenantUnitType.Aoo
case JsString("UO") => PersistentTenantUnitType.Uo
case x =>
throw new DeserializationException(s"Unable to deserialize PersistentTenantUnitType: unmapped type $x")
}
override def write(obj: PersistentTenantUnitType): JsValue = obj match {
case PersistentTenantUnitType.Aoo => JsString("AOO")
case PersistentTenantUnitType.Uo => JsString("UO")
}
}

implicit val ptmFormat: RootJsonFormat[PersistentTenantMail] = jsonFormat5(PersistentTenantMail.apply)
implicit val ptFormat: RootJsonFormat[PersistentTenant] = jsonFormat11(PersistentTenant.apply)
implicit val ptFormat: RootJsonFormat[PersistentTenant] = jsonFormat12(PersistentTenant.apply)

implicit val pcFormat: RootJsonFormat[TenantCreated] = jsonFormat1(TenantCreated.apply)
implicit val puFormat: RootJsonFormat[TenantUpdated] = jsonFormat1(TenantUpdated.apply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,23 @@ object TenantEventsSerde {
}
}

private implicit val ptutFormat: RootJsonFormat[PersistentTenantUnitType] =
new RootJsonFormat[PersistentTenantUnitType] {
override def read(json: JsValue): PersistentTenantUnitType = json match {
case JsString("AOO") => PersistentTenantUnitType.Aoo
case JsString("UO") => PersistentTenantUnitType.Uo
case x =>
throw new DeserializationException(s"Unable to deserialize PersistentTenantUnitType: unmapped type $x")
}
override def write(obj: PersistentTenantUnitType): JsValue = obj match {
case PersistentTenantUnitType.Aoo => JsString("AOO")
case PersistentTenantUnitType.Uo => JsString("UO")
}
}

private implicit val ptmFormat: RootJsonFormat[PersistentTenantMail] = jsonFormat5(PersistentTenantMail.apply)
private implicit val pexFormat: RootJsonFormat[PersistentExternalId] = jsonFormat2(PersistentExternalId.apply)
private implicit val ptFormat: RootJsonFormat[PersistentTenant] = jsonFormat11(PersistentTenant.apply)
private implicit val ptFormat: RootJsonFormat[PersistentTenant] = jsonFormat12(PersistentTenant.apply)
private implicit val tcFormat: RootJsonFormat[TenantCreated] = jsonFormat1(TenantCreated.apply)
private implicit val tuFormat: RootJsonFormat[TenantUpdated] = jsonFormat1(TenantUpdated.apply)
private implicit val tdFormat: RootJsonFormat[TenantDeleted] = jsonFormat1(TenantDeleted.apply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ final case class PersistentTenant(
updatedAt: Option[OffsetDateTime],
mails: List[PersistentTenantMail],
name: String,
onboardedAt: Option[OffsetDateTime]
onboardedAt: Option[OffsetDateTime],
subUnitType: Option[PersistentTenantUnitType]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.pagopa.interop.tenantmanagement.model.tenant

sealed trait PersistentTenantUnitType

object PersistentTenantUnitType {
case object Aoo extends PersistentTenantUnitType
case object Uo extends PersistentTenantUnitType
}
6 changes: 6 additions & 0 deletions src/main/protobuf/v1/tenant.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message TenantV1 {
optional string name = 9;
optional TenantKindV1 kind = 10;
optional int64 onboardedAt= 11;
optional TenantUnitTypeV1 subUnitType = 12;
}

enum TenantKindV1 {
Expand All @@ -42,6 +43,11 @@ enum TenantMailKindV1 {
DIGITAL_ADDRESS = 2;
}

enum TenantUnitTypeV1 {
AOO = 1;
UO = 2;
}

message TenantFeatureV1 {
oneof sealed_value {
CertifierV1 certifier = 1;
Expand Down
13 changes: 11 additions & 2 deletions src/main/resources/interface-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,9 @@ components:
type: string
onboardedAt:
type: string
format: date-time
format: date-time
subUnitType:
$ref: '#/components/schemas/TenantUnitType'
required:
- id
- certifier
Expand Down Expand Up @@ -648,6 +650,11 @@ components:
- kind
- address
- createdAt
TenantUnitType:
type: string
enum:
- AOO
- UO
TenantKind:
type: string
enum:
Expand Down Expand Up @@ -691,7 +698,9 @@ components:
$ref: '#/components/schemas/TenantKind'
onboardedAt:
type: string
format: date-time
format: date-time
subUnitType:
$ref: '#/components/schemas/TenantUnitType'
required:
- externalId
- features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ package object impl extends SprayJsonSupport with DefaultJsonProtocol {
implicit val tenantFeatureFormat: RootJsonFormat[TenantFeature] = jsonFormat1(TenantFeature)
implicit val mailSeedFormat: RootJsonFormat[MailSeed] = jsonFormat4(MailSeed)
implicit val mailFormat: RootJsonFormat[Mail] = jsonFormat5(Mail)
implicit val tenantFormat: RootJsonFormat[Tenant] = jsonFormat11(Tenant)
implicit val tenantFormat: RootJsonFormat[Tenant] = jsonFormat12(Tenant)
implicit val tenantDeltaFormat: RootJsonFormat[TenantDelta] = jsonFormat3(TenantDelta)
implicit val tenantSeedFormat: RootJsonFormat[TenantSeed] = jsonFormat7(TenantSeed)
implicit val tenantSeedFormat: RootJsonFormat[TenantSeed] = jsonFormat8(TenantSeed)
implicit val problemErrorFormat: RootJsonFormat[ProblemError] = jsonFormat2(ProblemError)
implicit val problemFormat: RootJsonFormat[Problem] = jsonFormat6(Problem)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package it.pagopa.interop.tenantmanagement.model.persistence
import cats.implicits._
import it.pagopa.interop.commons.utils.service.OffsetDateTimeSupplier
import it.pagopa.interop.tenantmanagement.error.TenantManagementErrors.{InvalidAttributeStructure, InvalidFeature}
import it.pagopa.interop.tenantmanagement.model.MailKind.{DIGITAL_ADDRESS, CONTACT_EMAIL}
import it.pagopa.interop.tenantmanagement.model.MailKind.{CONTACT_EMAIL, DIGITAL_ADDRESS}
import it.pagopa.interop.tenantmanagement.model.TenantUnitType.{AOO, UO}
import it.pagopa.interop.tenantmanagement.model._
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantMailKind.{DigitalAddress, ContactEmail}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantMailKind.{ContactEmail, DigitalAddress}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantUnitType.{Aoo, Uo}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantKind
import it.pagopa.interop.tenantmanagement.model.tenant._

Expand Down Expand Up @@ -145,6 +147,20 @@ object Adapters {
)
}

implicit class PersistentTenantUnitTypeWrapper(private val ptut: PersistentTenantUnitType) extends AnyVal {
def toApi: TenantUnitType = ptut match {
case Aoo => TenantUnitType.AOO
case Uo => TenantUnitType.UO
}
}

implicit class PersistentTenantUnitTypeObjectWrapper(private val p: PersistentTenantUnitType.type) extends AnyVal {
def fromApi(subUnitType: TenantUnitType): PersistentTenantUnitType = subUnitType match {
case AOO => Aoo
case UO => Uo
}
}

implicit class PersistentTenantMailObjectWrapper(private val p: PersistentTenantMail.type) extends AnyVal {
def fromAPI(ms: MailSeed, createdAt: OffsetDateTime): PersistentTenantMail = PersistentTenantMail(
id = ms.id,
Expand All @@ -167,7 +183,8 @@ object Adapters {
updatedAt = p.updatedAt,
mails = p.mails.map(_.toApi),
name = p.name,
onboardedAt = p.onboardedAt
onboardedAt = p.onboardedAt,
subUnitType = p.subUnitType.map(_.toApi)
)

def update(ptd: PersistentTenantDelta, time: OffsetDateTime): PersistentTenant =
Expand Down Expand Up @@ -202,7 +219,8 @@ object Adapters {
updatedAt = None,
mails = Nil,
name = seed.name,
onboardedAt = seed.onboardedAt
onboardedAt = seed.onboardedAt,
subUnitType = seed.subUnitType.map(PersistentTenantUnitType.fromApi)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import it.pagopa.interop.tenantmanagement.model.persistence.serializer.v1.tenant
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantFeature.PersistentCertifier
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantKind.{GSP, PA, PRIVATE}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantMailKind.{DigitalAddress, ContactEmail}
import it.pagopa.interop.tenantmanagement.model.tenant.PersistentTenantUnitType.{Aoo, Uo}
import it.pagopa.interop.tenantmanagement.model.tenant._

object protobufUtils {
Expand Down Expand Up @@ -38,6 +39,7 @@ object protobufUtils {
updatedAt <- protobufTenant.updatedAt.traverse(_.toOffsetDateTime.toEither)
onboardedAt <- protobufTenant.onboardedAt.traverse(_.toOffsetDateTime.toEither)
mails <- protobufTenant.mails.traverse(toPersistentTenantMail)
subUnitType <- protobufTenant.subUnitType.traverse(toPersistentTenantUnitType)
} yield PersistentTenant(
id = id,
kind = kind,
Expand All @@ -49,7 +51,8 @@ object protobufUtils {
updatedAt = updatedAt,
mails = mails,
name = protobufTenant.name.getOrElse(""),
onboardedAt = onboardedAt
onboardedAt = onboardedAt,
subUnitType = subUnitType
)

def toProtobufTenant(persistentTenant: PersistentTenant): Either[Throwable, TenantV1] = for {
Expand All @@ -65,7 +68,8 @@ object protobufUtils {
updatedAt = persistentTenant.updatedAt.map(_.toMillis),
mails = persistentTenant.mails.map(toProtobufTenantMail),
name = persistentTenant.name.some,
onboardedAt = persistentTenant.onboardedAt.map(_.toMillis)
onboardedAt = persistentTenant.onboardedAt.map(_.toMillis),
subUnitType = persistentTenant.subUnitType.map(toProtobufTenantUnitType)
)

def toPersistentTenantMail(protobufTenantMail: TenantMailV1): Either[Throwable, PersistentTenantMail] = for {
Expand Down Expand Up @@ -212,4 +216,20 @@ object protobufUtils {
case GSP => TenantKindV1.GSP
case PRIVATE => TenantKindV1.PRIVATE
}

def toPersistentTenantUnitType(
protobufTenantUnitType: TenantUnitTypeV1
): Either[Throwable, PersistentTenantUnitType] =
protobufTenantUnitType match {
case TenantUnitTypeV1.AOO => Aoo.asRight[Throwable]
case TenantUnitTypeV1.UO => Uo.asRight[Throwable]
case TenantUnitTypeV1.Unrecognized(unrecognizedValue) =>
new Exception(s"Unable to deserialize TenantUnitTypeV1 $unrecognizedValue").asLeft[PersistentTenantUnitType]
}

def toProtobufTenantUnitType(persistentTenantUnitType: PersistentTenantUnitType): TenantUnitTypeV1 =
persistentTenantUnitType match {
case Aoo => TenantUnitTypeV1.AOO
case Uo => TenantUnitTypeV1.UO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ object Generators {
)
)

val unitTypeGenerator: Gen[(PersistentTenantUnitType, TenantUnitTypeV1)] =
Gen.oneOf((PersistentTenantUnitType.Aoo, TenantUnitTypeV1.AOO), (PersistentTenantUnitType.Uo, TenantUnitTypeV1.UO))

val kindGenerator: Gen[(PersistentTenantKind, TenantKindV1)] =
Gen.oneOf(
(PersistentTenantKind.PA, TenantKindV1.PA),
Expand All @@ -150,6 +153,7 @@ object Generators {
(mails, protoMails) <- listOf(mailGenerator).map(_.separate)
name <- stringGen
(onboardedAt, onboardedAtV1) <- Gen.option(offsetDatetimeGen).map(_.separate)
(unitType, unitTypeV1) <- unitTypeGenerator
} yield (
PersistentTenant(
id,
Expand All @@ -162,7 +166,8 @@ object Generators {
updatedAt,
mails,
name,
onboardedAt
onboardedAt,
unitType.some
),
TenantV1(
id.toString(),
Expand All @@ -175,7 +180,8 @@ object Generators {
protoMails,
name.some,
kindV1.some,
onboardedAtV1
onboardedAtV1,
unitTypeV1.some
)
)

Expand Down