Skip to content

Commit

Permalink
IMN-851 - Removing with attributes models and converters (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecamellini authored Oct 3, 2024
1 parent 7eb6d3b commit efbf9ac
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 86 deletions.
14 changes: 7 additions & 7 deletions packages/agreement-lifecycle/src/filters/attributesFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import {
CertifiedTenantAttribute,
DeclaredTenantAttribute,
TenantAttribute,
TenantId,
VerifiedTenantAttribute,
tenantAttributeType,
} from "pagopa-interop-models";
import { TenantWithOnlyAttributes } from "../models/models.js";

export const filterVerifiedAttributes = (
producerId: TenantId,
tenant: TenantWithOnlyAttributes
tenantAttributes: TenantAttribute[]
): VerifiedTenantAttribute[] =>
tenant.attributes.filter(
tenantAttributes.filter(
(att) =>
att.type === tenantAttributeType.VERIFIED &&
att.verifiedBy.find(
Expand All @@ -24,17 +24,17 @@ export const filterVerifiedAttributes = (
) as VerifiedTenantAttribute[];

export const filterCertifiedAttributes = (
tenant: TenantWithOnlyAttributes
tenantAttributes: TenantAttribute[]
): CertifiedTenantAttribute[] =>
tenant.attributes.filter(
tenantAttributes.filter(
(att) =>
att.type === tenantAttributeType.CERTIFIED && !att.revocationTimestamp
) as CertifiedTenantAttribute[];

export const filterDeclaredAttributes = (
tenant: TenantWithOnlyAttributes
tenantAttributes: TenantAttribute[]
): DeclaredTenantAttribute[] =>
tenant.attributes.filter(
tenantAttributes.filter(
(att) =>
att.type === tenantAttributeType.DECLARED && !att.revocationTimestamp
) as DeclaredTenantAttribute[];
1 change: 0 additions & 1 deletion packages/agreement-lifecycle/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./validator/attributesValidator.js";
export * from "./filters/attributesFilter.js";
export * from "./models/models.js";
4 changes: 0 additions & 4 deletions packages/agreement-lifecycle/src/models/models.ts

This file was deleted.

49 changes: 19 additions & 30 deletions packages/agreement-lifecycle/src/validator/attributesValidator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
Descriptor,
CompactTenant,
Tenant,
TenantId,
EServiceAttribute,
TenantAttribute,
Expand All @@ -11,61 +9,52 @@ import {
filterDeclaredAttributes,
filterVerifiedAttributes,
} from "../filters/attributesFilter.js";
import {
DescriptorWithOnlyAttributes,
TenantWithOnlyAttributes,
} from "../models/models.js";

const attributesSatisfied = (
descriptorAttributes: EServiceAttribute[][],
consumerAttributeIds: Array<TenantAttribute["id"]>
tenantAttributes: Array<TenantAttribute["id"]>
): boolean =>
descriptorAttributes
.filter((attGroup) => attGroup.length > 0)
.every((attributeList) => {
const attributes = attributeList.map((a) => a.id);
return (
attributes.filter((a) => consumerAttributeIds.includes(a)).length > 0
);
return attributes.filter((a) => tenantAttributes.includes(a)).length > 0;
});

export const certifiedAttributesSatisfied = (
descriptor: DescriptorWithOnlyAttributes,
tenant: TenantWithOnlyAttributes
descriptorAttributes: Descriptor["attributes"],
tenantAttributes: TenantAttribute[]
): boolean => {
const certifiedAttributes = filterCertifiedAttributes(tenant).map(
const certifiedAttributes = filterCertifiedAttributes(tenantAttributes).map(
(a) => a.id
);

return attributesSatisfied(
descriptor.attributes.certified,
descriptorAttributes.certified,
certifiedAttributes
);
};

export const declaredAttributesSatisfied = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
descriptorAttributes: Descriptor["attributes"],
tenantAttributes: TenantAttribute[]
): boolean => {
const declaredAttributes = filterDeclaredAttributes(tenant).map((a) => a.id);

return attributesSatisfied(
descriptor.attributes.declared,
declaredAttributes
const declaredAttributes = filterDeclaredAttributes(tenantAttributes).map(
(a) => a.id
);

return attributesSatisfied(descriptorAttributes.declared, declaredAttributes);
};

export const verifiedAttributesSatisfied = (
producerId: TenantId,
descriptor: Descriptor,
tenant: Tenant | CompactTenant
descriptorAttributes: Descriptor["attributes"],
tenantAttributes: TenantAttribute[]
): boolean => {
const verifiedAttributes = filterVerifiedAttributes(producerId, tenant).map(
(a) => a.id
);
const verifiedAttributes = filterVerifiedAttributes(
producerId,
tenantAttributes
).map((a) => a.id);

return attributesSatisfied(
descriptor.attributes.verified,
verifiedAttributes
);
return attributesSatisfied(descriptorAttributes.verified, verifiedAttributes);
};
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ export const validateCertifiedAttributes = ({
descriptor: Descriptor;
consumer: Tenant;
}): void => {
if (!certifiedAttributesSatisfied(descriptor, consumer)) {
if (
!certifiedAttributesSatisfied(descriptor.attributes, consumer.attributes)
) {
throw missingCertifiedAttributesError(descriptor.id, consumer.id);
}
};
Expand Down Expand Up @@ -366,9 +368,9 @@ export const matchingCertifiedAttributes = (
descriptor: Descriptor,
consumer: Tenant
): CertifiedAgreementAttribute[] => {
const certifiedAttributes = filterCertifiedAttributes(consumer).map(
(a) => a.id
);
const certifiedAttributes = filterCertifiedAttributes(
consumer.attributes
).map((a) => a.id);

return matchingAttributes(
descriptor.attributes.certified,
Expand All @@ -380,7 +382,7 @@ export const matchingDeclaredAttributes = (
descriptor: Descriptor,
consumer: Tenant
): DeclaredAgreementAttribute[] => {
const declaredAttributes = filterDeclaredAttributes(consumer).map(
const declaredAttributes = filterDeclaredAttributes(consumer.attributes).map(
(a) => a.id
);

Expand All @@ -397,7 +399,7 @@ export const matchingVerifiedAttributes = (
): VerifiedAgreementAttribute[] => {
const verifiedAttributes = filterVerifiedAttributes(
eservice.producerId,
consumer
consumer.attributes
).map((a) => a.id);

return matchingAttributes(
Expand Down
8 changes: 4 additions & 4 deletions packages/agreement-process/src/services/agreementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,13 @@ export function agreementServiceBuilder(

const verifiedValid = verifiedAttributesSatisfied(
agreementToBeUpgraded.data.producerId,
newDescriptor,
consumer
newDescriptor.attributes,
consumer.attributes
);

const declaredValid = declaredAttributesSatisfied(
newDescriptor,
consumer
newDescriptor.attributes,
consumer.attributes
);

const contractBuilderInstance = contractBuilder(
Expand Down
36 changes: 25 additions & 11 deletions packages/agreement-process/src/services/agreementStateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ const nextStateFromDraft = (
if (agreement.consumerId === agreement.producerId) {
return active;
}
if (!certifiedAttributesSatisfied(descriptor, tenant)) {
if (!certifiedAttributesSatisfied(descriptor.attributes, tenant.attributes)) {
return missingCertifiedAttributes;
}

if (
descriptor.agreementApprovalPolicy === agreementApprovalPolicy.automatic &&
declaredAttributesSatisfied(descriptor, tenant) &&
verifiedAttributesSatisfied(agreement.producerId, descriptor, tenant)
declaredAttributesSatisfied(descriptor.attributes, tenant.attributes) &&
verifiedAttributesSatisfied(
agreement.producerId,
descriptor.attributes,
tenant.attributes
)
) {
return active;
}
if (declaredAttributesSatisfied(descriptor, tenant)) {
if (declaredAttributesSatisfied(descriptor.attributes, tenant.attributes)) {
return pending;
}
return draft;
Expand All @@ -67,13 +71,19 @@ const nextStateFromPending = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): AgreementState => {
if (!certifiedAttributesSatisfied(descriptor, tenant)) {
if (!certifiedAttributesSatisfied(descriptor.attributes, tenant.attributes)) {
return missingCertifiedAttributes;
}
if (!declaredAttributesSatisfied(descriptor, tenant)) {
if (!declaredAttributesSatisfied(descriptor.attributes, tenant.attributes)) {
return draft;
}
if (!verifiedAttributesSatisfied(agreement.producerId, descriptor, tenant)) {
if (
!verifiedAttributesSatisfied(
agreement.producerId,
descriptor.attributes,
tenant.attributes
)
) {
return pending;
}
return active;
Expand All @@ -88,9 +98,13 @@ const nextStateFromActiveOrSuspended = (
return active;
}
if (
certifiedAttributesSatisfied(descriptor, tenant) &&
declaredAttributesSatisfied(descriptor, tenant) &&
verifiedAttributesSatisfied(agreement.producerId, descriptor, tenant)
certifiedAttributesSatisfied(descriptor.attributes, tenant.attributes) &&
declaredAttributesSatisfied(descriptor.attributes, tenant.attributes) &&
verifiedAttributesSatisfied(
agreement.producerId,
descriptor.attributes,
tenant.attributes
)
) {
return active;
}
Expand All @@ -101,7 +115,7 @@ const nextStateFromMissingCertifiedAttributes = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): AgreementState => {
if (certifiedAttributesSatisfied(descriptor, tenant)) {
if (certifiedAttributesSatisfied(descriptor.attributes, tenant.attributes)) {
return draft;
}
return missingCertifiedAttributes;
Expand Down
23 changes: 12 additions & 11 deletions packages/backend-for-frontend/src/api/catalogApiConverter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/* eslint-disable functional/immutable-data */
/* eslint-disable max-params */
import { DescriptorWithOnlyAttributes } from "pagopa-interop-agreement-lifecycle";
import {
agreementApi,
attributeRegistryApi,
bffApi,
catalogApi,
tenantApi,
} from "pagopa-interop-api-clients";
import { EServiceAttribute, unsafeBrandId } from "pagopa-interop-models";
import {
Descriptor,
EServiceAttribute,
unsafeBrandId,
} from "pagopa-interop-models";
import { attributeNotExists } from "../model/errors.js";
import {
getLatestActiveDescriptor,
Expand Down Expand Up @@ -272,16 +275,14 @@ export function toEserviceAttribute(
}));
}

export function toDescriptorWithOnlyAttributes(
descriptor: catalogApi.EServiceDescriptor
): DescriptorWithOnlyAttributes {
export function descriptorAttributesFromApi(
catalogApiDescriptorAttributes: catalogApi.EServiceDescriptor["attributes"]
): Descriptor["attributes"] {
return {
...descriptor,
attributes: {
certified: descriptor.attributes.certified.map(toEserviceAttribute),
declared: descriptor.attributes.declared.map(toEserviceAttribute),
verified: descriptor.attributes.verified.map(toEserviceAttribute),
},
certified:
catalogApiDescriptorAttributes.certified.map(toEserviceAttribute),
declared: catalogApiDescriptorAttributes.declared.map(toEserviceAttribute),
verified: catalogApiDescriptorAttributes.verified.map(toEserviceAttribute),
};
}

Expand Down
13 changes: 5 additions & 8 deletions packages/backend-for-frontend/src/api/tenantApiConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TenantWithOnlyAttributes } from "pagopa-interop-agreement-lifecycle";
import {
attributeRegistryApi,
bffApi,
Expand All @@ -8,6 +7,7 @@ import { isDefined } from "pagopa-interop-commons";
import {
CertifiedTenantAttribute,
DeclaredTenantAttribute,
Tenant,
TenantAttribute,
TenantMail,
VerifiedTenantAttribute,
Expand Down Expand Up @@ -61,13 +61,10 @@ export function toTenantAttribute(
);
}

export function toTenantWithOnlyAttributes(
tenant: tenantApi.Tenant
): TenantWithOnlyAttributes {
return {
...tenant,
attributes: tenant.attributes.map(toTenantAttribute).flat(),
};
export function tenantAttributesFromApi(
tenantApiAttributes: tenantApi.Tenant["attributes"]
): Tenant["attributes"] {
return tenantApiAttributes.map(toTenantAttribute).flat();
}

export const toBffApiRequesterCertifiedAttributes = (
Expand Down
8 changes: 4 additions & 4 deletions packages/backend-for-frontend/src/services/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
tenantApi,
} from "pagopa-interop-api-clients";
import { TenantId } from "pagopa-interop-models";
import { toDescriptorWithOnlyAttributes } from "../api/catalogApiConverter.js";
import { toTenantWithOnlyAttributes } from "../api/tenantApiConverter.js";
import { descriptorAttributesFromApi } from "../api/catalogApiConverter.js";
import { tenantAttributesFromApi } from "../api/tenantApiConverter.js";
import {
invalidEServiceRequester,
notValidDescriptor,
Expand Down Expand Up @@ -73,8 +73,8 @@ export function hasCertifiedAttributes(
return (
descriptor !== undefined &&
certifiedAttributesSatisfied(
toDescriptorWithOnlyAttributes(descriptor),
toTenantWithOnlyAttributes(requesterTenant)
descriptorAttributesFromApi(descriptor.attributes),
tenantAttributesFromApi(requesterTenant.attributes)
)
);
}
Expand Down

0 comments on commit efbf9ac

Please sign in to comment.