Skip to content

Commit

Permalink
FIX compute-agreement-consumer crash on startup (#952)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Gregnanin <[email protected]>
  • Loading branch information
Carminepo2 and rGregnanin authored Sep 4, 2024
1 parent dc02141 commit b49cdf1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
76 changes: 76 additions & 0 deletions packages/compute-agreements-consumer/src/converters.ts
Original file line number Diff line number Diff line change
@@ -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<agreementApi.TenantAttribute>()
.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),
};
}
3 changes: 2 additions & 1 deletion packages/compute-agreements-consumer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,7 +65,7 @@ async function processMessage({
await agreementProcessClient.computeAgreementsByAttribute(
{
attributeId: unsafeBrandId(attributeId),
consumer: fromTenantV2(tenant),
consumer: toApiCompactTenant(fromTenantV2(tenant)),
},
{
headers: {
Expand Down

0 comments on commit b49cdf1

Please sign in to comment.