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

IMN-657 GET EService descriptor by ID #712

Merged
merged 34 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a02dc84
Implements get Eservice's descriptor by id
Viktor-K Jul 1, 2024
b6d1ce8
Fix assertion on GetEserivcesDescriptorById
Viktor-K Jul 10, 2024
af35559
Fix types from API clients package
Viktor-K Jul 10, 2024
56a0277
remove wrong changes
Viktor-K Jul 10, 2024
b1ac408
Fix linting
Viktor-K Jul 10, 2024
8f82ffe
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 10, 2024
87effa8
Move converter to properly ApiConverter file
Viktor-K Jul 10, 2024
6b080a5
Fix linting
Viktor-K Jul 10, 2024
107daaa
Create getter help methods in catalog service
Viktor-K Jul 16, 2024
17be40e
Rename converter catalog API converter method
Viktor-K Jul 16, 2024
6006856
Fix var naming
Viktor-K Jul 16, 2024
33b8648
Fix toBffCatalogApiEserviceRiskAnalysis conversion in Bff tuility
Viktor-K Jul 16, 2024
392cb8c
Fix toBffCatalogApiEserviceRiskAnalysis conversion in Bff tuility
Viktor-K Jul 16, 2024
43848ef
uniform tenant client creation
Viktor-K Jul 17, 2024
480d433
Reduce unsafeBrandId usages
Viktor-K Jul 17, 2024
5bb768c
Minor fix rename variable name
Viktor-K Jul 17, 2024
dfba69b
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 17, 2024
6429d36
Fix linting
Viktor-K Jul 17, 2024
9cf1835
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 17, 2024
7dc73a2
Refactorize validators and mapper on catalogService
Viktor-K Jul 22, 2024
846effd
move agreement upgredeable veirification in mapper file
Viktor-K Jul 22, 2024
98a7bad
Move getTenant email to mappers
Viktor-K Jul 22, 2024
748d8c6
Add proper type during risk anlysis converter
Viktor-K Jul 22, 2024
9fe126b
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 23, 2024
9e9d46e
fix linting
Viktor-K Jul 23, 2024
3d974f2
Rename BFF validator
Viktor-K Jul 23, 2024
00e0562
Minor Fix : renaming function and vars
Viktor-K Jul 24, 2024
1b30f5a
Update packages/bff/src/model/modelMappingUtils.ts
Viktor-K Jul 24, 2024
28954da
Fix typo
Viktor-K Jul 24, 2024
d3d4657
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 24, 2024
5ba6040
Rename const variable
Viktor-K Jul 25, 2024
31c5dcd
IMN-656 get producer eservice detail by ID (#730)
Viktor-K Jul 25, 2024
11026a3
Add operative log
Viktor-K Jul 25, 2024
c41d3ec
Merge branch 'main' into IMN-657-getEserviceDescriptorById
Viktor-K Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function toBffCatalogApiDescriptorAttribute(
});
}

export function toBffCatalogApiDescriptorInterface(
export function toBffCatalogApiDescriptorDoc(
document: catalogApi.EServiceDoc
): bffApi.EServiceDoc {
return {
Expand All @@ -123,24 +123,27 @@ export function toBffCatalogApiDescriptorInterface(
export function toBffCatalogApiEserviceRiskAnalysis(
riskAnalysis: catalogApi.EServiceRiskAnalysis
): bffApi.EServiceRiskAnalysis {
const answers: { [key: string]: string[] } = {};

riskAnalysis.riskAnalysisForm.singleAnswers
.concat(riskAnalysis.riskAnalysisForm.multiAnswers)
.map((answer) => ({
questionId: answer.questionId,
answer: answer.value,
}))
.forEach((QA) => {
if (answers[`${QA.questionId}`] && QA.answer) {
answers[`${QA.questionId}`] = [
...answers[`${QA.questionId}`],
QA.answer,
];
} else {
answers[`${QA.questionId}`] = [];
}
});
const answers: { [key: string]: string[] } =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reuse type from bffApi.RiskAnalysisForm?

Suggested change
const answers: { [key: string]: string[] } =
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: { [key: string]: string[] }, answer) => {
const key = `${answer.key}`;
Viktor-K marked this conversation as resolved.
Show resolved Hide resolved
if (answers[key] && answer.value) {
answers[key] = [...answers[key], answer.value];
} else {
answers[key] = [];
AsterITA marked this conversation as resolved.
Show resolved Hide resolved
}

return answers;
}, {});

const riskAnalysisForm: bffApi.RiskAnalysisForm = {
riskAnalysisId: riskAnalysis.id,
Expand All @@ -164,7 +167,7 @@ export function toBffCatalogApiProducerDescriptorEService(
(m) => m.kind === tenantMailKind.ContactEmail
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the values are the same and that TS probably helps with this, but here you are comparing enums of different types: tenantApi.Tenant and our internal TenantMailKind. To be safer I would refactor as follows:

Suggested change
(m) => m.kind === tenantMailKind.ContactEmail
(m) => m.kind === tenantApi.MailKind.Values.CONTACT_EMAIL

Copy link
Collaborator Author

@Viktor-K Viktor-K Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Furthermore this commit move it to separate file

);

const notDraftDecriptor: bffApi.CompactDescriptor[] =
const notDraftDecriptors: bffApi.CompactDescriptor[] =
eservice.descriptors.filter((d) => d.state !== descriptorApiState.DRAFT);

const draftDescriptor: bffApi.CompactDescriptor | undefined =
Expand All @@ -184,7 +187,7 @@ export function toBffCatalogApiProducerDescriptorEService(
riskAnalysis: eservice.riskAnalysis.map(
toBffCatalogApiEserviceRiskAnalysis
),
descriptors: notDraftDecriptor,
descriptors: notDraftDecriptors,
};
}

Expand Down
2 changes: 2 additions & 0 deletions packages/bff/src/model/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

export const errorCodes = {
purposeNotFound: "0001",
userNotFound: "0002",
selfcareEntityNotFilled: "0003",
descriptorNotFound: "0006",
attributeNotExists: "0008",
invalidEserviceRequester: "0009",
Expand Down
15 changes: 8 additions & 7 deletions packages/bff/src/providers/clientProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
} from "pagopa-interop-api-clients";
import { config } from "../config/config.js";

export type TenantProcessClient = ReturnType<
typeof tenantApi.createTenantApiClient
>;
export type TenantProcessClient = {
tenant: ReturnType<typeof tenantApi.createTenantApiClient>;
selfcare: ReturnType<typeof tenantApi.createSelfcareApiClient>;
};

export type AttributeProcessClient = ReturnType<
typeof attributeRegistryApi.createAttributeApiClient
Expand Down Expand Up @@ -37,10 +38,10 @@ export type PagoPAInteropBeClients = {

export function getInteropBeClients(): PagoPAInteropBeClients {
return {
tenantProcessClient: tenantApi.createTenantApiClient(
config.tenantProcessUrl
),

tenantProcessClient: {
tenant: tenantApi.createTenantApiClient(config.tenantProcessUrl),
selfcare: tenantApi.createSelfcareApiClient(config.tenantProcessUrl),
},
agreementProcessClient: agreementApi.createAgreementApiClient(
config.agreementProcessUrl
),
Expand Down
5 changes: 3 additions & 2 deletions packages/bff/src/routers/catalogRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
zodiosValidationErrorToApiProblem,
} from "pagopa-interop-commons";
import { bffApi } from "pagopa-interop-api-clients";
import { unsafeBrandId } from "pagopa-interop-models";
import { PagoPAInteropBeClients } from "../providers/clientProvider.js";
import { catalogServiceBuilder } from "../services/catalogService.js";
import { toEserviceCatalogProcessQueryParams } from "../model/api/converters/catalogClientApiConverter.js";
Expand Down Expand Up @@ -62,8 +63,8 @@ const catalogRouter = (

try {
const response = await catalogService.getProducerEServiceDescriptor(
req.params.eserviceId,
req.params.descriptorId,
unsafeBrandId(req.params.eserviceId),
unsafeBrandId(req.params.descriptorId),
ctx
);
return res.status(200).json(response).send();
Expand Down
83 changes: 43 additions & 40 deletions packages/bff/src/services/catalogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import {
bffApi,
attributeRegistryApi,
} from "pagopa-interop-api-clients";
import {
DescriptorId,
EServiceId,
TenantId,
unsafeBrandId,
} from "pagopa-interop-models";
import { DescriptorId, EServiceId, unsafeBrandId } from "pagopa-interop-models";
import { descriptorApiState } from "../model/api/catalogTypes.js";
import {
toBffCatalogApiDescriptorAttribute,
toBffCatalogApiDescriptorInterface,
toBffCatalogApiDescriptorDoc,
toBffCatalogApiEService,
toBffCatalogApiProducerDescriptorEService,
} from "../model/api/converters/catalogClientApiConverter.js";
Expand Down Expand Up @@ -50,7 +45,7 @@ const enhanceCatalogEService =
requesterId: string
): ((eservice: catalogApi.EService) => Promise<bffApi.CatalogEService>) =>
async (eservice: catalogApi.EService): Promise<bffApi.CatalogEService> => {
const producerTenant = await tenantProcessClient.getTenant({
const producerTenant = await tenantProcessClient.tenant.getTenant({
headers,
params: {
id: eservice.producerId,
Expand All @@ -59,7 +54,7 @@ const enhanceCatalogEService =

const requesterTenant: tenantApi.Tenant =
requesterId !== eservice.producerId
? await tenantProcessClient.getTenant({
? await tenantProcessClient.tenant.getTenant({
headers,
params: {
id: requesterId,
Expand Down Expand Up @@ -129,6 +124,36 @@ const getBulkAttributes = async (
return await attributesBulk(0, []);
};

const getEserviceDesciptor = (
Copy link
Contributor

@AsterITA AsterITA Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const getEserviceDesciptor = (
const retrieveEserviceDescriptor = (

We can align this name with the other retrieves like this and we can fix the typo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with this commit

eservice: catalogApi.EService,
descriptorId: DescriptorId
): catalogApi.EServiceDescriptor => {
const descriptor = eservice.descriptors.find((e) => e.id === descriptorId);

if (!descriptor) {
throw eserviceDescriptorNotFound(
unsafeBrandId<EServiceId>(eservice.id), // catalogApi.EService type is missing branded types
descriptorId
);
}

return descriptor;
};

const getAttributeIds = (
descriptor: catalogApi.EServiceDescriptor
): string[] => [
...descriptor.attributes.certified.flatMap((atts) =>
atts.map((att) => att.id)
),
...descriptor.attributes.declared.flatMap((atts) =>
atts.map((att) => att.id)
),
...descriptor.attributes.verified.flatMap((atts) =>
atts.map((att) => att.id)
),
];

export function catalogServiceBuilder(
catalogProcessClient: CatalogProcessClient,
tenantProcessClient: TenantProcessClient,
Expand Down Expand Up @@ -176,8 +201,8 @@ export function catalogServiceBuilder(
return response;
},
getProducerEServiceDescriptor: async (
eServiceId: string,
descriptorId: string,
eserviceId: EServiceId,
AsterITA marked this conversation as resolved.
Show resolved Hide resolved
descriptorId: DescriptorId,
context: WithLogger<BffAppContext>
): Promise<bffApi.ProducerEServiceDescriptor> => {
const requesterId = context.authData.organizationId;
Expand All @@ -186,40 +211,18 @@ export function catalogServiceBuilder(
const eservice: catalogApi.EService =
await catalogProcessClient.getEServiceById({
params: {
eServiceId,
eServiceId: eserviceId,
},
headers,
});

if (eservice.producerId !== requesterId) {
throw invalidEServiceRequester(
unsafeBrandId<EServiceId>(eServiceId),
unsafeBrandId<TenantId>(requesterId)
);
throw invalidEServiceRequester(eserviceId, requesterId);
}

const descriptor = eservice.descriptors.find(
(e) => e.id === descriptorId
);

if (!descriptor) {
throw eserviceDescriptorNotFound(
unsafeBrandId<EServiceId>(eServiceId),
unsafeBrandId<DescriptorId>(descriptorId)
);
}
const descriptor = getEserviceDesciptor(eservice, descriptorId);

const descriptorAttributeIds: string[] = [
...descriptor.attributes.certified.flatMap((atts) =>
atts.map((att) => att.id)
),
...descriptor.attributes.declared.flatMap((atts) =>
atts.map((att) => att.id)
),
...descriptor.attributes.verified.flatMap((atts) =>
atts.map((att) => att.id)
),
];
const descriptorAttributeIds = getAttributeIds(descriptor);

const attributes = await getBulkAttributes(
attributeProcessClient,
Expand Down Expand Up @@ -248,7 +251,7 @@ export function catalogServiceBuilder(
],
};

const requesterTenant = await tenantProcessClient.getTenant({
const requesterTenant = await tenantProcessClient.tenant.getTenant({
headers,
params: {
id: requesterId,
Expand All @@ -261,8 +264,8 @@ export function catalogServiceBuilder(
description: descriptor.description,
interface:
descriptor.interface &&
toBffCatalogApiDescriptorInterface(descriptor.interface),
docs: descriptor.docs.map(toBffCatalogApiDescriptorInterface),
toBffCatalogApiDescriptorDoc(descriptor.interface),
docs: descriptor.docs.map(toBffCatalogApiDescriptorDoc),
state: descriptor.state,
audience: descriptor.audience,
voucherLifespan: descriptor.voucherLifespan,
Expand Down