Skip to content

Commit

Permalink
Merge branch 'main' into IMN-495_change-kafka-rebalancing-log-severity
Browse files Browse the repository at this point in the history
  • Loading branch information
MalpenZibo committed Jun 6, 2024
2 parents 8fe8e3e + bb22681 commit 41aea4a
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 376 deletions.
2 changes: 1 addition & 1 deletion packages/agreement-process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@anatine/zod-mock": "3.13.4",
"@pagopa/eslint-config": "3.0.0",
"@types/express": "4.17.21",
"@types/node": "20.14.1",
"@types/node": "20.14.2",
"@types/uuid": "9.0.8",
"cpx2": "7.0.1",
"openapi-zod-client": "1.18.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/agreement-process/src/model/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
makeApiProblemBuilder,
UserId,
SelfcareId,
AttributeId,
} from "pagopa-interop-models";

export const errorCodes = {
Expand Down Expand Up @@ -297,3 +298,13 @@ export function userNotFound(
title: "User not found",
});
}

export function attributeNotFound(
attributeId: AttributeId
): ApiError<ErrorCodes> {
return new ApiError({
detail: `Attribute ${attributeId} not found`,
code: "attributeNotFound",
title: "Attribute not found",
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@ import {
toCreateEventAgreementUnsuspendedByProducer,
} from "../model/domain/toEvent.js";
import { UpdateAgreementSeed } from "../model/domain/models.js";
import { apiAgreementDocumentToAgreementDocument } from "../model/domain/apiConverter.js";
import {
createStamp,
suspendedByConsumerStamp,
suspendedByProducerStamp,
} from "./agreementStampUtils.js";
import { createAgreementArchivedByUpgradeEvent } from "./agreementService.js";
import { ReadModelService } from "./readModelService.js";
import { ContractBuilder } from "./agreementContractBuilder.js";

export function createActivationUpdateAgreementSeed({
firstActivation,
isFirstActivation,
newState,
descriptor,
consumer,
Expand All @@ -49,7 +47,7 @@ export function createActivationUpdateAgreementSeed({
suspendedByProducer,
suspendedByPlatform,
}: {
firstActivation: boolean;
isFirstActivation: boolean;
newState: AgreementState;
descriptor: Descriptor;
consumer: Tenant;
Expand All @@ -62,7 +60,7 @@ export function createActivationUpdateAgreementSeed({
}): UpdateAgreementSeed {
const stamp = createStamp(authData.userId);

return firstActivation
return isFirstActivation
? {
state: newState,
certifiedAttributes: matchingCertifiedAttributes(descriptor, consumer),
Expand Down Expand Up @@ -108,36 +106,20 @@ export function createActivationUpdateAgreementSeed({
}

export async function createActivationEvent(
firstActivation: boolean,
isFirstActivation: boolean,
updatedAgreement: Agreement,
updatedAgreementSeed: UpdateAgreementSeed,
eservice: EService,
consumer: Tenant,
producer: Tenant,
originalSuspendedByPlatform: boolean | undefined,
suspendedByPlatformChanged: boolean,
agreementEventStoreVersion: number,
authData: AuthData,
correlationId: string,
contractBuilder: ContractBuilder
correlationId: string
): Promise<Array<CreateEvent<AgreementEventV2>>> {
if (firstActivation) {
if (isFirstActivation) {
// Pending >>> Active
const agreementContract = await contractBuilder.createContract(
authData.selfcareId,
updatedAgreement,
eservice,
consumer,
producer,
updatedAgreementSeed
);

return [
toCreateEventAgreementActivated(
{
...updatedAgreement,
contract: apiAgreementDocumentToAgreementDocument(agreementContract),
},
updatedAgreement,
agreementEventStoreVersion,
correlationId
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {
VerifiedTenantAttribute,
UserId,
generateId,
genericError,
tenantAttributeType,
unsafeBrandId,
AgreementDocument,
} from "pagopa-interop-models";
import {
UserResponse,
Expand All @@ -37,10 +37,10 @@ import {
agreementMissingUserInfo,
agreementSelfcareIdNotFound,
agreementStampNotFound,
attributeNotFound,
userNotFound,
} from "../model/domain/errors.js";
import { UpdateAgreementSeed } from "../model/domain/models.js";
import { ApiAgreementDocumentSeed } from "../model/types.js";
import { AgreementProcessConfig } from "../utilities/config.js";
import { ReadModelService } from "./readModelService.js";

Expand All @@ -64,10 +64,11 @@ const retrieveUser = async (

const createAgreementDocumentName = (
consumerId: TenantId,
producerId: TenantId
producerId: TenantId,
documentCreatedAt: Date
): string =>
`${consumerId}_${producerId}_${formatDateyyyyMMddHHmmss(
new Date()
documentCreatedAt
)}_agreement_contract.pdf`;

const getAttributeInvolved = async (
Expand Down Expand Up @@ -104,7 +105,7 @@ const getAttributeInvolved = async (
tenantAttribute.id
);
if (!attribute) {
throw genericError(`Attribute ${tenantAttribute.id} not found`);
throw attributeNotFound(tenantAttribute.id);
}
return [attribute, tenantAttribute as unknown as T];
})
Expand Down Expand Up @@ -314,7 +315,7 @@ export const contractBuilder = (
consumer: Tenant,
producer: Tenant,
seed: UpdateAgreementSeed
): Promise<ApiAgreementDocumentSeed> => {
): Promise<AgreementDocument> => {
const templateFilePath = path.resolve(
dirname,
"..",
Expand All @@ -338,9 +339,11 @@ export const contractBuilder = (
);

const documentId = generateId<AgreementDocumentId>();
const documentCreatedAt = new Date();
const documentName = createAgreementDocumentName(
agreement.consumerId,
agreement.producerId
agreement.producerId,
documentCreatedAt
);

const documentPath = await fileManager.storeBytes(
Expand All @@ -358,6 +361,7 @@ export const contractBuilder = (
contentType: CONTENT_TYPE_PDF,
prettyName: AGREEMENT_CONTRACT_PRETTY_NAME,
path: documentPath,
createdAt: documentCreatedAt,
};
},
};
Expand Down
72 changes: 58 additions & 14 deletions packages/agreement-process/src/services/agreementService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AppContext,
AuthData,
CreateEvent,
DB,
FileManager,
Expand Down Expand Up @@ -109,7 +110,10 @@ import {
createActivationEvent,
createActivationUpdateAgreementSeed,
} from "./agreementActivationProcessor.js";
import { contractBuilder } from "./agreementContractBuilder.js";
import {
ContractBuilder,
contractBuilder,
} from "./agreementContractBuilder.js";
import { createStamp } from "./agreementStampUtils.js";
import {
agreementStateByFlags,
Expand All @@ -120,7 +124,6 @@ import {
suspendedByProducerFlag,
} from "./agreementStateProcessor.js";
import {
addContractOnFirstActivation,
createSubmissionUpdateAgreementSeed,
isActiveOrSuspended,
validateConsumerEmail,
Expand Down Expand Up @@ -468,15 +471,19 @@ export function agreementServiceBuilder(
logger
);

const isFirstActivation =
updatedAgreement.state === agreementState.active &&
!hasRelatedAgreements;

const submittedAgreement = await addContractOnFirstActivation(
isFirstActivation,
contractBuilderInstance,
eservice,
consumer,
producer,
updateSeed,
authData,
updatedAgreement,
hasRelatedAgreements
authData
);

const agreementEvent =
Expand Down Expand Up @@ -979,13 +986,13 @@ export function agreementServiceBuilder(

failOnActivationFailure(newState, agreement.data);

const firstActivation =
const isFirstActivation =
agreement.data.state === agreementState.pending &&
newState === agreementState.active;

const updatedAgreementSeed: UpdateAgreementSeed =
createActivationUpdateAgreementSeed({
firstActivation,
isFirstActivation,
newState,
descriptor,
consumer,
Expand All @@ -997,27 +1004,34 @@ export function agreementServiceBuilder(
suspendedByPlatform,
});

const updatedAgreement: Agreement = {
const updatedAgreementWithoutContract: Agreement = {
...agreement.data,
...updatedAgreementSeed,
};

const updatedAgreement: Agreement = await addContractOnFirstActivation(
isFirstActivation,
contractBuilderInstance,
eservice,
consumer,
producer,
updatedAgreementSeed,
updatedAgreementWithoutContract,
authData
);

const suspendedByPlatformChanged =
agreement.data.suspendedByPlatform !==
updatedAgreement.suspendedByPlatform;

const activationEvents = await createActivationEvent(
firstActivation,
isFirstActivation,
updatedAgreement,
updatedAgreementSeed,
eservice,
consumer,
producer,
agreement.data.suspendedByPlatform,
suspendedByPlatformChanged,
agreement.metadata.version,
authData,
correlationId,
contractBuilderInstance
correlationId
);

const archiveEvents = await archiveRelatedToAgreements(
Expand Down Expand Up @@ -1176,3 +1190,33 @@ function maybeCreateSetToMissingCertifiedAttributesByPlatformEvent(
}
return undefined;
}

// eslint-disable-next-line max-params
async function addContractOnFirstActivation(
isFirstActivation: boolean,
contractBuilder: ContractBuilder,
eservice: EService,
consumer: Tenant,
producer: Tenant,
updateSeed: UpdateAgreementSeed,
agreement: Agreement,
authData: AuthData
): Promise<Agreement> {
if (isFirstActivation) {
const contract = await contractBuilder.createContract(
authData.selfcareId,
agreement,
eservice,
consumer,
producer,
updateSeed
);

return {
...agreement,
contract,
};
}

return agreement;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-params */
import { AuthData, CreateEvent } from "pagopa-interop-commons";
import { CreateEvent } from "pagopa-interop-commons";
import {
Agreement,
AgreementEvent,
Expand All @@ -14,7 +14,6 @@ import {
tenantMailKind,
} from "pagopa-interop-models";
import { match } from "ts-pattern";
import { apiAgreementDocumentToAgreementDocument } from "../model/domain/apiConverter.js";
import {
agreementNotInExpectedState,
consumerWithNotValidEmail,
Expand All @@ -26,7 +25,6 @@ import {
matchingVerifiedAttributes,
} from "../model/domain/validators.js";
import { ApiAgreementSubmissionPayload } from "../model/types.js";
import { ContractBuilder } from "./agreementContractBuilder.js";
import { retrieveTenant } from "./agreementService.js";
import { createStamp } from "./agreementStampUtils.js";
import { ReadModelService } from "./readModelService.js";
Expand Down Expand Up @@ -117,35 +115,3 @@ export const calculateStamps = (
.otherwise(() => {
throw agreementNotInExpectedState(agreement.id, state);
});

export const addContractOnFirstActivation = async (
contractBuilder: ContractBuilder,
eservice: EService,
consumer: Tenant,
producer: Tenant,
updateSeed: UpdateAgreementSeed,
authData: AuthData,
agreement: Agreement,
hasRelatedAgreements: boolean
): Promise<Agreement> => {
const isFirstActivation =
agreement.state === agreementState.active && !hasRelatedAgreements;

if (isFirstActivation) {
const contract = await contractBuilder.createContract(
authData.selfcareId,
agreement,
eservice,
consumer,
producer,
updateSeed
);

return {
...agreement,
contract: apiAgreementDocumentToAgreementDocument(contract),
};
}

return agreement;
};
2 changes: 1 addition & 1 deletion packages/agreement-readmodel-writer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@pagopa/eslint-config": "3.0.0",
"@types/node": "20.14.1",
"@types/node": "20.14.2",
"@types/uuid": "9.0.8",
"pagopa-interop-commons-test": "workspace:*",
"prettier": "2.8.8",
Expand Down
Loading

0 comments on commit 41aea4a

Please sign in to comment.