diff --git a/packages/compute-agreements-consumer/src/converters.ts b/packages/compute-agreements-consumer/src/converters.ts new file mode 100644 index 0000000000..38bad42035 --- /dev/null +++ b/packages/compute-agreements-consumer/src/converters.ts @@ -0,0 +1,76 @@ +import { match } from "ts-pattern"; +import { agreementApi } from "pagopa-interop-api-clients"; +import { + CertifiedTenantAttribute, + DeclaredTenantAttribute, + Tenant, + TenantAttribute, + tenantAttributeType, + VerifiedTenantAttribute, +} from "pagopa-interop-models"; + +function toApiCompactTenantCertifiedDeclaredAttribute( + attr: CertifiedTenantAttribute +): agreementApi.CertifiedTenantAttribute; +function toApiCompactTenantCertifiedDeclaredAttribute( + attr: DeclaredTenantAttribute +): agreementApi.DeclaredTenantAttribute; +function toApiCompactTenantCertifiedDeclaredAttribute( + attr: CertifiedTenantAttribute | DeclaredTenantAttribute +): + | agreementApi.CertifiedTenantAttribute + | agreementApi.DeclaredTenantAttribute { + return { + id: attr.id, + assignmentTimestamp: attr.assignmentTimestamp.toISOString(), + revocationTimestamp: attr.revocationTimestamp?.toISOString(), + }; +} + +function toApiCompactTenantVerifiedAttribute( + attr: VerifiedTenantAttribute +): agreementApi.VerifiedTenantAttribute { + return { + id: attr.id, + assignmentTimestamp: attr.assignmentTimestamp.toISOString(), + verifiedBy: attr.verifiedBy.map((v) => ({ + id: v.id, + extensionDate: v.extensionDate?.toISOString(), + verificationDate: v.verificationDate?.toISOString(), + expirationDate: v.expirationDate?.toISOString(), + })), + revokedBy: attr.revokedBy.map((v) => ({ + id: v.id, + extensionDate: v.extensionDate?.toISOString(), + verificationDate: v.verificationDate.toISOString(), + revocationDate: v.verificationDate.toISOString(), + expirationDate: v.expirationDate?.toISOString(), + })), + }; +} + +function toCompactTenantAttribute( + attribute: TenantAttribute +): agreementApi.TenantAttribute { + return match(attribute) + .returnType() + .with( + { type: tenantAttributeType.CERTIFIED }, + toApiCompactTenantCertifiedDeclaredAttribute + ) + .with( + { type: tenantAttributeType.DECLARED }, + toApiCompactTenantCertifiedDeclaredAttribute + ) + .with( + { type: tenantAttributeType.VERIFIED }, + toApiCompactTenantVerifiedAttribute + ); +} + +export function toApiCompactTenant(tenant: Tenant): agreementApi.CompactTenant { + return { + id: tenant.id, + attributes: tenant.attributes.map(toCompactTenantAttribute), + }; +} diff --git a/packages/compute-agreements-consumer/src/index.ts b/packages/compute-agreements-consumer/src/index.ts index 8c98495780..3d5f1d5b9d 100644 --- a/packages/compute-agreements-consumer/src/index.ts +++ b/packages/compute-agreements-consumer/src/index.ts @@ -16,6 +16,7 @@ import { match, P } from "ts-pattern"; import { v4 as uuidv4 } from "uuid"; import { agreementApi } from "pagopa-interop-api-clients"; import { config } from "./config/config.js"; +import { toApiCompactTenant } from "./converters.js"; const agreementProcessClient = agreementApi.createAgreementApiClient( config.agreementProcessUrl @@ -64,7 +65,7 @@ async function processMessage({ await agreementProcessClient.computeAgreementsByAttribute( { attributeId: unsafeBrandId(attributeId), - consumer: fromTenantV2(tenant), + consumer: toApiCompactTenant(fromTenantV2(tenant)), }, { headers: {