Skip to content

Commit

Permalink
Merge branch 'main' into feature/incaricato
Browse files Browse the repository at this point in the history
  • Loading branch information
ecamellini committed Nov 12, 2024
2 parents 226013e + 717cdc5 commit 51e291c
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 157 deletions.
2 changes: 1 addition & 1 deletion packages/delegation-process/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import delegationProducerRouter from "./routers/DelegationProducerRouter.js";
import delegationRouter from "./routers/DelegationRouter.js";
import { config } from "./config/config.js";

const serviceName = "delgation-process";
const serviceName = "delegation-process";

const app = zodiosCtx.app();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ZodiosEndpointDefinitions } from "@zodios/core";
import { ZodiosRouter } from "@zodios/express";
import { delegationApi } from "pagopa-interop-api-clients";
import {
Expand All @@ -24,7 +23,7 @@ import {
revokeDelegationErrorMapper,
approveDelegationErrorMapper,
rejectDelegationErrorMapper,
} from "../utilites/errorMappers.js";
} from "../utilities/errorMappers.js";

const readModelService = readModelServiceBuilder(
ReadModelRepository.init(config)
Expand Down Expand Up @@ -52,7 +51,7 @@ const { ADMIN_ROLE } = userRoles;

const delegationProducerRouter = (
ctx: ZodiosContext
): ZodiosRouter<ZodiosEndpointDefinitions, ExpressContext> => {
): ZodiosRouter<typeof delegationApi.producerApi.api, ExpressContext> => {
const delegationProducerRouter = ctx.router(delegationApi.producerApi.api, {
validationErrorHandler: zodiosValidationErrorToApiProblem,
});
Expand Down Expand Up @@ -124,10 +123,8 @@ const delegationProducerRouter = (

try {
await delegationProducerService.approveProducerDelegation(
ctx.authData.organizationId,
unsafeBrandId(delegationId),
ctx.correlationId,
ctx.logger
ctx
);

return res.status(204).send();
Expand All @@ -153,10 +150,9 @@ const delegationProducerRouter = (

try {
await delegationProducerService.rejectProducerDelegation(
ctx.authData.organizationId,
unsafeBrandId(delegationId),
ctx.correlationId,
rejectionReason
rejectionReason,
ctx
);

return res.status(204).send();
Expand Down
40 changes: 23 additions & 17 deletions packages/delegation-process/src/routers/DelegationRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ZodiosEndpointDefinitions } from "@zodios/core";
import { ZodiosRouter } from "@zodios/express";
import { delegationApi } from "pagopa-interop-api-clients";
import {
Expand All @@ -19,7 +18,10 @@ import {
delegationToApiDelegation,
} from "../model/domain/apiConverter.js";
import { makeApiProblem } from "../model/domain/errors.js";
import { getDelegationErrorMapper } from "../utilites/errorMappers.js";
import {
getDelegationsErrorMapper,
getDelegationByIdErrorMapper,
} from "../utilities/errorMappers.js";
import { delegationServiceBuilder } from "../services/delegationService.js";

const readModelService = readModelServiceBuilder(
Expand All @@ -33,7 +35,7 @@ const { ADMIN_ROLE, API_ROLE, SECURITY_ROLE, M2M_ROLE, SUPPORT_ROLE } =

const delegationRouter = (
ctx: ZodiosContext
): ZodiosRouter<ZodiosEndpointDefinitions, ExpressContext> => {
): ZodiosRouter<typeof delegationApi.delegationApi.api, ExpressContext> => {
const delegationRouter = ctx.router(delegationApi.delegationApi.api, {
validationErrorHandler: zodiosValidationErrorToApiProblem,
});
Expand Down Expand Up @@ -61,17 +63,20 @@ const delegationRouter = (
} = req.query;

try {
const delegations = await delegationService.getDelegations({
delegateIds: delegateIds.map(unsafeBrandId<TenantId>),
delegatorIds: delegatorIds.map(unsafeBrandId<TenantId>),
delegationStates: delegationStates.map(
apiDelegationStateToDelegationState
),
eserviceIds: eserviceIds.map(unsafeBrandId<EServiceId>),
kind: kind && apiDelegationKindToDelegationKind(kind),
offset,
limit,
});
const delegations = await delegationService.getDelegations(
{
delegateIds: delegateIds.map(unsafeBrandId<TenantId>),
delegatorIds: delegatorIds.map(unsafeBrandId<TenantId>),
delegationStates: delegationStates.map(
apiDelegationStateToDelegationState
),
eserviceIds: eserviceIds.map(unsafeBrandId<EServiceId>),
kind: kind && apiDelegationKindToDelegationKind(kind),
offset,
limit,
},
ctx.logger
);

return res.status(200).send(
delegationApi.Delegations.parse({
Expand All @@ -84,7 +89,7 @@ const delegationRouter = (
} catch (error) {
const errorRes = makeApiProblem(
error,
getDelegationErrorMapper,
getDelegationsErrorMapper,
ctx.logger,
ctx.correlationId
);
Expand All @@ -108,7 +113,8 @@ const delegationRouter = (

try {
const delegation = await delegationService.getDelegationById(
unsafeBrandId(delegationId)
unsafeBrandId(delegationId),
ctx.logger
);

return res
Expand All @@ -121,7 +127,7 @@ const delegationRouter = (
} catch (error) {
const errorRes = makeApiProblem(
error,
getDelegationErrorMapper,
getDelegationByIdErrorMapper,
ctx.logger,
ctx.correlationId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import {
DB,
eventRepository,
FileManager,
Logger,
PDFGenerator,
WithLogger,
} from "pagopa-interop-commons";
import {
CorrelationId,
Delegation,
DelegationId,
delegationEventToBinaryDataV2,
Expand Down Expand Up @@ -78,7 +76,7 @@ export function delegationProducerServiceBuilder(
const eserviceId = unsafeBrandId<EServiceId>(delegationSeed.eserviceId);

logger.info(
`Creating a delegation for tenant:${delegationSeed.delegateId} by producer:${delegatorId}`
`Creating a delegation for tenant ${delegationSeed.delegateId} by producer ${delegatorId}`
);

assertDelegatorIsNotDelegate(delegatorId, delegateId);
Expand Down Expand Up @@ -126,7 +124,7 @@ export function delegationProducerServiceBuilder(
): Promise<Delegation> {
const delegatorId = unsafeBrandId<TenantId>(authData.organizationId);
logger.info(
`Revoking delegation:${delegationId} by producer:${delegatorId}`
`Revoking delegation ${delegationId} by producer ${delegatorId}`
);

const currentDelegation = await retrieveDelegationById(
Expand Down Expand Up @@ -180,25 +178,29 @@ export function delegationProducerServiceBuilder(
return revokedDelegation;
},
async approveProducerDelegation(
delegateId: TenantId,
delegationId: DelegationId,
correlationId: CorrelationId,
logger: Logger
{ logger, correlationId, authData }: WithLogger<AppContext>
): Promise<void> {
const delegateId = unsafeBrandId<TenantId>(authData.organizationId);

logger.info(
`Approving delegation ${delegationId} by delegate ${delegateId}`
);

const { data: delegation, metadata } = await retrieveDelegationById(
readModelService,
delegationId
);

assertIsDelegate(delegation, delegateId);
assertIsState(delegationState.waitingForApproval, delegation);

const [delegator, delegate, eservice] = await Promise.all([
retrieveTenantById(delegation.delegatorId),
retrieveTenantById(delegation.delegateId),
retrieveEserviceById(delegation.eserviceId),
]);

assertIsDelegate(delegation, delegateId);
assertIsState(delegationState.waitingForApproval, delegation);

const activationContract = await contractBuilder.createActivationContract(
{
delegation,
Expand Down Expand Up @@ -237,11 +239,16 @@ export function delegationProducerServiceBuilder(
);
},
async rejectProducerDelegation(
delegateId: TenantId,
delegationId: DelegationId,
correlationId: CorrelationId,
rejectionReason: string
rejectionReason: string,
{ logger, correlationId, authData }: WithLogger<AppContext>
): Promise<void> {
const delegateId = unsafeBrandId<TenantId>(authData.organizationId);

logger.info(
`Rejecting delegation ${delegationId} by delegate ${delegateId}`
);

const { data: delegation, metadata } = await retrieveDelegationById(
readModelService,
delegationId
Expand Down
50 changes: 31 additions & 19 deletions packages/delegation-process/src/services/delegationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TenantId,
WithMetadata,
} from "pagopa-interop-models";
import { Logger } from "pagopa-interop-commons";
import { delegationNotFound } from "../model/domain/errors.js";
import { ReadModelService } from "./readModelService.js";

Expand All @@ -24,31 +25,42 @@ export const retrieveDelegationById = async (
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function delegationServiceBuilder(readModelService: ReadModelService) {
return {
async getDelegationById(delegationId: DelegationId): Promise<Delegation> {
async getDelegationById(
delegationId: DelegationId,
logger: Logger
): Promise<Delegation> {
logger.info(`Retrieving delegation by id ${delegationId}`);

const delegation = await retrieveDelegationById(
readModelService,
delegationId
);
return delegation.data;
},
// eslint-disable-next-line max-params
async getDelegations({
delegateIds,
delegatorIds,
delegationStates,
eserviceIds,
kind,
offset,
limit,
}: {
delegateIds: TenantId[];
delegatorIds: TenantId[];
delegationStates: DelegationState[];
eserviceIds: EServiceId[];
kind: DelegationKind | undefined;
offset: number;
limit: number;
}): Promise<Delegation[]> {
async getDelegations(
{
delegateIds,
delegatorIds,
delegationStates,
eserviceIds,
kind,
offset,
limit,
}: {
delegateIds: TenantId[];
delegatorIds: TenantId[];
delegationStates: DelegationState[];
eserviceIds: EServiceId[];
kind: DelegationKind | undefined;
offset: number;
limit: number;
},
logger: Logger
): Promise<Delegation[]> {
logger.info(
`Retrieving delegations with filters: delegateIds=${delegateIds}, delegatorIds=${delegatorIds}, delegationStates=${delegationStates}, eserviceIds=${eserviceIds}, kind=${kind}, offset=${offset}, limit=${limit}`
);

return readModelService.getDelegations({
delegateIds,
delegatorIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const {
HTTP_STATUS_UNAUTHORIZED,
} = constants;

export const getDelegationByIdsrrorMapper = (
export const getDelegationsErrorMapper = (
error: ApiError<ErrorCodes>
): number =>
match(error.code).otherwise(() => HTTP_STATUS_INTERNAL_SERVER_ERROR);

export const getDelegationByIdErrorMapper = (
error: ApiError<ErrorCodes>
): number =>
match(error.code)
Expand All @@ -36,11 +41,6 @@ export const createProducerDelegationErrorMapper = (
)
.otherwise(() => HTTP_STATUS_INTERNAL_SERVER_ERROR);

export const getDelegationErrorMapper = (error: ApiError<ErrorCodes>): number =>
match(error.code)
.with("delegationNotFound", () => HTTP_STATUS_NOT_FOUND)
.otherwise(() => HTTP_STATUS_INTERNAL_SERVER_ERROR);

export const revokeDelegationErrorMapper = (
error: ApiError<ErrorCodes>
): number =>
Expand Down
Loading

0 comments on commit 51e291c

Please sign in to comment.