-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IMN-657 GET EService descriptor by ID (#712)
Co-authored-by: Eric Camellini <eric.camellini@gmail.com>
- Loading branch information
1 parent
3c2519e
commit a9561da
Showing
10 changed files
with
665 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
packages/bff/src/model/api/converters/catalogClientApiConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/* 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 { attributeNotExists } from "../../domain/errors.js"; | ||
import { getTenantEmail, isUpgradable } from "../../modelMappingUtils.js"; | ||
import { catalogApiDescriptorState } from "../apiTypes.js"; | ||
|
||
export function toEserviceCatalogProcessQueryParams( | ||
queryParams: bffApi.BffGetCatalogQueryParam | ||
): catalogApi.GetCatalogQueryParam { | ||
return { | ||
...queryParams, | ||
eservicesIds: [], | ||
name: queryParams.q, | ||
}; | ||
} | ||
|
||
export function toBffCatalogApiEService( | ||
eservice: catalogApi.EService, | ||
producerTenant: tenantApi.Tenant, | ||
hasCertifiedAttributes: boolean, | ||
isRequesterEqProducer: boolean, | ||
activeDescriptor?: catalogApi.EServiceDescriptor, | ||
agreement?: agreementApi.Agreement | ||
): bffApi.CatalogEService { | ||
const partialEnhancedEservice = { | ||
id: eservice.id, | ||
name: eservice.name, | ||
description: eservice.description, | ||
producer: { | ||
id: eservice.producerId, | ||
name: producerTenant.name, | ||
}, | ||
isMine: isRequesterEqProducer, | ||
hasCertifiedAttributes, | ||
}; | ||
|
||
return { | ||
...partialEnhancedEservice, | ||
...(activeDescriptor | ||
? { | ||
activeDescriptor: { | ||
id: activeDescriptor.id, | ||
version: activeDescriptor.version, | ||
audience: activeDescriptor.audience, | ||
state: activeDescriptor.state, | ||
}, | ||
} | ||
: {}), | ||
...(agreement | ||
? { | ||
agreement: { | ||
id: agreement.id, | ||
state: agreement.state, | ||
canBeUpgraded: isUpgradable(eservice, agreement), | ||
}, | ||
} | ||
: {}), | ||
}; | ||
} | ||
|
||
export function toBffCatalogApiDescriptorAttribute( | ||
attributes: attributeRegistryApi.Attribute[], | ||
descriptorAttributes: catalogApi.Attribute[] | ||
): bffApi.DescriptorAttribute[] { | ||
return descriptorAttributes.map((attribute) => { | ||
const foundAttribute = attributes.find((att) => att.id === attribute.id); | ||
if (!foundAttribute) { | ||
throw attributeNotExists(unsafeBrandId(attribute.id)); | ||
} | ||
|
||
return { | ||
id: attribute.id, | ||
name: foundAttribute.name, | ||
description: foundAttribute.description, | ||
explicitAttributeVerification: attribute.explicitAttributeVerification, | ||
}; | ||
}); | ||
} | ||
|
||
export function toBffCatalogApiDescriptorDoc( | ||
document: catalogApi.EServiceDoc | ||
): bffApi.EServiceDoc { | ||
return { | ||
id: document.id, | ||
name: document.name, | ||
contentType: document.contentType, | ||
prettyName: document.prettyName, | ||
}; | ||
} | ||
|
||
export function toBffCatalogApiEserviceRiskAnalysis( | ||
riskAnalysis: catalogApi.EServiceRiskAnalysis | ||
): bffApi.EServiceRiskAnalysis { | ||
const answers: bffApi.RiskAnalysisForm["answers"] = | ||
riskAnalysis.riskAnalysisForm.singleAnswers | ||
.concat( | ||
riskAnalysis.riskAnalysisForm.multiAnswers.flatMap((multiAnswer) => | ||
multiAnswer.values.map((answerValue) => ({ | ||
id: multiAnswer.id, | ||
value: answerValue, | ||
key: multiAnswer.key, | ||
})) | ||
) | ||
) | ||
.reduce((answers: bffApi.RiskAnalysisForm["answers"], answer) => { | ||
const key = answer.key; | ||
if (answers[key] && answer.value) { | ||
answers[key] = [...answers[key], answer.value]; | ||
} else { | ||
answers[key] = []; | ||
} | ||
|
||
return answers; | ||
}, {}); | ||
|
||
const riskAnalysisForm: bffApi.RiskAnalysisForm = { | ||
riskAnalysisId: riskAnalysis.id, | ||
version: riskAnalysis.riskAnalysisForm.version, | ||
answers, | ||
}; | ||
|
||
return { | ||
id: riskAnalysis.id, | ||
name: riskAnalysis.name, | ||
createdAt: riskAnalysis.createdAt, | ||
riskAnalysisForm, | ||
}; | ||
} | ||
|
||
export function toBffCatalogApiProducerDescriptorEService( | ||
eservice: catalogApi.EService, | ||
producer: tenantApi.Tenant | ||
): bffApi.ProducerDescriptorEService { | ||
const producerMail = getTenantEmail(producer); | ||
|
||
const notDraftDecriptors: bffApi.CompactDescriptor[] = | ||
eservice.descriptors.filter( | ||
(d) => d.state !== catalogApiDescriptorState.DRAFT | ||
); | ||
|
||
const draftDescriptor: bffApi.CompactDescriptor | undefined = | ||
eservice.descriptors.find( | ||
(d) => d.state === catalogApiDescriptorState.DRAFT | ||
); | ||
|
||
return { | ||
id: eservice.id, | ||
name: eservice.name, | ||
description: eservice.description, | ||
technology: eservice.technology, | ||
mode: eservice.mode, | ||
mail: producerMail && { | ||
address: producerMail.address, | ||
description: producerMail.description, | ||
}, | ||
draftDescriptor, | ||
riskAnalysis: eservice.riskAnalysis.map( | ||
toBffCatalogApiEserviceRiskAnalysis | ||
), | ||
descriptors: notDraftDecriptors, | ||
}; | ||
} | ||
|
||
export function toEserviceAttribute( | ||
attributes: catalogApi.Attribute[] | ||
): EServiceAttribute[] { | ||
return attributes.map((attribute) => ({ | ||
...attribute, | ||
id: unsafeBrandId(attribute.id), | ||
})); | ||
} | ||
|
||
export function toDescriptorWithOnlyAttributes( | ||
descriptor: catalogApi.EServiceDescriptor | ||
): DescriptorWithOnlyAttributes { | ||
return { | ||
...descriptor, | ||
attributes: { | ||
certified: descriptor.attributes.certified.map(toEserviceAttribute), | ||
declared: descriptor.attributes.declared.map(toEserviceAttribute), | ||
verified: descriptor.attributes.verified.map(toEserviceAttribute), | ||
}, | ||
}; | ||
} | ||
|
||
export function toBffCatalogApiDescriptorAttributes( | ||
attributes: attributeRegistryApi.Attribute[], | ||
descriptor: catalogApi.EServiceDescriptor | ||
): bffApi.DescriptorAttributes { | ||
return { | ||
certified: [ | ||
toBffCatalogApiDescriptorAttribute( | ||
attributes, | ||
descriptor.attributes.certified.flat() | ||
), | ||
], | ||
declared: [ | ||
toBffCatalogApiDescriptorAttribute( | ||
attributes, | ||
descriptor.attributes.declared.flat() | ||
), | ||
], | ||
verified: [ | ||
toBffCatalogApiDescriptorAttribute( | ||
attributes, | ||
descriptor.attributes.verified.flat() | ||
), | ||
], | ||
}; | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/bff/src/model/api/converters/tenantClientApiConverters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { TenantWithOnlyAttributes } from "pagopa-interop-agreement-lifecycle"; | ||
import { tenantApi } from "pagopa-interop-api-clients"; | ||
import { | ||
CertifiedTenantAttribute, | ||
DeclaredTenantAttribute, | ||
TenantAttribute, | ||
VerifiedTenantAttribute, | ||
tenantAttributeType, | ||
unsafeBrandId, | ||
} from "pagopa-interop-models"; | ||
|
||
export function toTenantAttribute( | ||
att: tenantApi.TenantAttribute | ||
): TenantAttribute[] { | ||
const certified: CertifiedTenantAttribute | undefined = att.certified && { | ||
id: unsafeBrandId(att.certified.id), | ||
type: tenantAttributeType.CERTIFIED, | ||
revocationTimestamp: att.certified.revocationTimestamp | ||
? new Date(att.certified.revocationTimestamp) | ||
: undefined, | ||
assignmentTimestamp: new Date(att.certified.assignmentTimestamp), | ||
}; | ||
|
||
const verified: VerifiedTenantAttribute | undefined = att.verified && { | ||
id: unsafeBrandId(att.verified.id), | ||
type: tenantAttributeType.VERIFIED, | ||
assignmentTimestamp: new Date(att.verified.assignmentTimestamp), | ||
verifiedBy: att.verified.verifiedBy.map((v) => ({ | ||
id: v.id, | ||
verificationDate: new Date(v.verificationDate), | ||
expirationDate: v.expirationDate ? new Date(v.expirationDate) : undefined, | ||
extensionDate: v.extensionDate ? new Date(v.extensionDate) : undefined, | ||
})), | ||
revokedBy: att.verified.revokedBy.map((r) => ({ | ||
id: r.id, | ||
verificationDate: new Date(r.verificationDate), | ||
revocationDate: new Date(r.revocationDate), | ||
expirationDate: r.expirationDate ? new Date(r.expirationDate) : undefined, | ||
extensionDate: r.extensionDate ? new Date(r.extensionDate) : undefined, | ||
})), | ||
}; | ||
|
||
const declared: DeclaredTenantAttribute | undefined = att.declared && { | ||
id: unsafeBrandId(att.declared.id), | ||
type: tenantAttributeType.DECLARED, | ||
assignmentTimestamp: new Date(att.declared.assignmentTimestamp), | ||
revocationTimestamp: att.declared.revocationTimestamp | ||
? new Date(att.declared.revocationTimestamp) | ||
: undefined, | ||
}; | ||
|
||
return [certified, verified, declared].filter( | ||
(a): a is TenantAttribute => !!a | ||
); | ||
} | ||
|
||
export function toTenantWithOnlyAttributes( | ||
tenant: tenantApi.Tenant | ||
): TenantWithOnlyAttributes { | ||
return { | ||
...tenant, | ||
attributes: tenant.attributes.map(toTenantAttribute).flat(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.