Skip to content

Commit

Permalink
Fix conflicts on agreement services and processors
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor-K committed May 27, 2024
1 parent 93e1258 commit de4d157
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 522 deletions.
310 changes: 60 additions & 250 deletions packages/agreement-process/src/services/agreementActivationProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,152 +1,64 @@
import {
AuthData,
CreateEvent,
FileManager,
Logger,
PDFGenerator,
} from "pagopa-interop-commons";
/* eslint-disable max-params */
import { AuthData, CreateEvent } from "pagopa-interop-commons";
import {
Agreement,
AgreementEvent,
AgreementId,
Descriptor,
EService,
SelfcareId,
Tenant,
WithMetadata,
agreementState,
AgreementEvent,
AgreementState,
Descriptor,
genericError,
AgreementEventV2,
WithMetadata,
UserId,
} from "pagopa-interop-models";
import {
agreementArchivableStates,
assertActivableState,
assertAgreementExist,
assertEServiceExist,
assertRequesterIsConsumerOrProducer,
assertTenantExist,
failOnActivationFailure,
matchingCertifiedAttributes,
matchingDeclaredAttributes,
matchingVerifiedAttributes,
validateActivationOnDescriptor,
verifyConsumerDoesNotActivatePending,
agreementArchivableStates,
} from "../model/domain/validators.js";
import {
toCreateEventAgreementActivated,
toCreateEventAgreementUnsuspendedByConsumer,
toCreateEventAgreementUnsuspendedByProducer,
} from "../model/domain/toEvent.js";
import { UpdateAgreementSeed } from "../model/domain/models.js";
import { ApiAgreementDocumentSeed } from "../model/types.js";
import { apiAgreementDocumentToAgreementDocument } from "../model/domain/apiConverter.js";
import { AgreementProcessConfig } from "../utilities/config.js";
import { contractBuilder } from "./agreementContractBuilder.js";
import {
createStamp,
suspendedByConsumerStamp,
suspendedByProducerStamp,
} from "./agreementStampUtils.js";
import {
agreementStateByFlags,
nextState,
suspendedByConsumerFlag,
suspendedByProducerFlag,
} from "./agreementStateProcessor.js";
import { AgreementQuery } from "./readmodel/agreementQuery.js";
import { AttributeQuery } from "./readmodel/attributeQuery.js";
import { EserviceQuery } from "./readmodel/eserviceQuery.js";
import { TenantQuery } from "./readmodel/tenantQuery.js";
import { createAgreementArchivedByUpgradeEvent } from "./agreementService.js";
import { ReadModelService } from "./readModelService.js";
import { ContractBuilder } from "./agreementContractBuilder.js";

export async function activateAgreementLogic(
agreementId: AgreementId,
agreementQuery: AgreementQuery,
eserviceQuery: EserviceQuery,
tenantQuery: TenantQuery,
attributeQuery: AttributeQuery,
authData: AuthData,
correlationId: string,
fileManager: FileManager,
pdfGenerator: PDFGenerator,
config: AgreementProcessConfig,
logger: Logger
): Promise<[Agreement, Array<CreateEvent<AgreementEvent>>]> {
const agreement = await agreementQuery.getAgreementById(agreementId);
assertAgreementExist(agreementId, agreement);

assertRequesterIsConsumerOrProducer(agreement.data, authData);
verifyConsumerDoesNotActivatePending(agreement.data, authData);
assertActivableState(agreement.data);

const eservice = await eserviceQuery.getEServiceById(
agreement.data.eserviceId
);
assertEServiceExist(agreement.data.eserviceId, eservice);

const descriptor = validateActivationOnDescriptor(
eservice,
agreement.data.descriptorId
);

const tenant = await tenantQuery.getTenantById(agreement.data.consumerId);
assertTenantExist(agreement.data.consumerId, tenant);

return activateAgreement(
agreement,
eservice,
descriptor,
tenant,
authData,
tenantQuery,
agreementQuery,
attributeQuery,
correlationId,
fileManager,
pdfGenerator,
config,
logger
);
}

async function activateAgreement(
agreementData: WithMetadata<Agreement>,
eservice: EService,
descriptor: Descriptor,
consumer: Tenant,
authData: AuthData,
tenantQuery: TenantQuery,
agreementQuery: AgreementQuery,
attributeQuery: AttributeQuery,
correlationId: string,
fileManager: FileManager,
pdfGenerator: PDFGenerator,
config: AgreementProcessConfig,
logger: Logger
): Promise<[Agreement, Array<CreateEvent<AgreementEvent>>]> {
const agreement = agreementData.data;
const nextAttributesState = nextState(agreement, descriptor, consumer);

const suspendedByConsumer = suspendedByConsumerFlag(
agreement,
authData.organizationId,
agreementState.active
);
const suspendedByProducer = suspendedByProducerFlag(
agreement,
authData.organizationId,
agreementState.active
);

const newState = agreementStateByFlags(
nextAttributesState,
suspendedByProducer,
suspendedByConsumer
);

failOnActivationFailure(newState, agreement);

const firstActivation =
agreement.state === agreementState.pending &&
newState === agreementState.active;
export function createActivationUpdateAgreementSeed({
firstActivation,
newState,
descriptor,
consumer,
eservice,
authData,
agreement,
suspendedByConsumer,
suspendedByProducer,
}: {
firstActivation: boolean;
newState: AgreementState;
descriptor: Descriptor;
consumer: Tenant;
eservice: EService;
authData: AuthData;
agreement: Agreement;
suspendedByConsumer: boolean | undefined;
suspendedByProducer: boolean | undefined;
}): UpdateAgreementSeed {
const stamp = createStamp(authData.userId);

const updatedAgreementSeed: UpdateAgreementSeed = firstActivation
return firstActivation
? {
state: newState,
certifiedAttributes: matchingCertifiedAttributes(descriptor, consumer),
Expand Down Expand Up @@ -187,105 +99,35 @@ async function activateAgreement(
? undefined
: agreement.suspendedAt,
};

const updatedAgreement = {
...agreement,
...updatedAgreementSeed,
};

const activationEvent = await match(firstActivation)
.with(true, async () => {
const contract = apiAgreementDocumentToAgreementDocument(
await createContract(
updatedAgreement,
updatedAgreementSeed,
eservice,
consumer,
attributeQuery,
tenantQuery,
authData.selfcareId,
fileManager,
pdfGenerator,
config,
logger
)
);

return toCreateEventAgreementActivated(
{ ...updatedAgreement, contract },
agreementData.metadata.version,
correlationId
);
})
.with(false, () => {
if (authData.organizationId === agreement.producerId) {
return toCreateEventAgreementUnsuspendedByProducer(
updatedAgreement,
agreementData.metadata.version,
correlationId
);
} else if (authData.organizationId === agreement.consumerId) {
return toCreateEventAgreementUnsuspendedByConsumer(
updatedAgreement,
agreementData.metadata.version,
correlationId
);
} else {
throw new Error(
`Unexpected organizationId ${authData.organizationId} in activateAgreement`
);
}
})
.exhaustive();

const archiveEvents = await archiveRelatedToAgreements(
agreement,
authData,
agreementQuery,
correlationId
);

return [updatedAgreement, [activationEvent, ...archiveEvents]];
}

export async function createActivationEvent({
firstActivation,
agreement,
updatedAgreement,
updatedAgreementSeed,
eservice,
consumer,
authData,
correlationId,
readModelService,
storeFile,
logger,
}: {
firstActivation: boolean;
agreement: WithMetadata<Agreement>;
updatedAgreement: Agreement;
updatedAgreementSeed: UpdateAgreementSeed;
eservice: EService;
consumer: Tenant;
authData: AuthData;
correlationId: string;
readModelService: ReadModelService;
storeFile: FileManager["storeBytes"];
logger: Logger;
}): Promise<CreateEvent<AgreementEventV2>> {
export async function createActivationEvent(
firstActivation: boolean,
agreement: WithMetadata<Agreement>,
updatedAgreement: Agreement,
updatedAgreementSeed: UpdateAgreementSeed,
eservice: EService,
consumer: Tenant,
producer: Tenant,
authData: AuthData,
correlationId: string,
contractBuilder: ContractBuilder
): Promise<CreateEvent<AgreementEventV2>> {
if (firstActivation) {
const contract = await createContract({
agreement: updatedAgreement,
updateSeed: updatedAgreementSeed,
const agreementContract = await contractBuilder.createContract(
authData.selfcareId,
updatedAgreement,
eservice,
consumer,
readModelService,
selfcareId: authData.selfcareId,
storeFile,
logger,
});
producer,
updatedAgreementSeed
);

return toCreateEventAgreementActivated(
{ ...updatedAgreement, contract },
{
...updatedAgreement,
contract: apiAgreementDocumentToAgreementDocument(agreementContract),
},
agreement.metadata.version,
correlationId
);
Expand Down Expand Up @@ -331,35 +173,3 @@ export const archiveRelatedToAgreements = async (
createAgreementArchivedByUpgradeEvent(agreementData, userId, correlationId)
);
};

const createContract = async (
agreement: Agreement,
updateSeed: UpdateAgreementSeed,
eservice: EService,
consumer: Tenant,
attributeQuery: AttributeQuery,
tenantQuery: TenantQuery,
selfcareId: SelfcareId,
fileManager: FileManager,
pdfGenerator: PDFGenerator,
config: AgreementProcessConfig,
logger: Logger
): Promise<ApiAgreementDocumentSeed> => {
const producer = await tenantQuery.getTenantById(agreement.producerId);
assertTenantExist(agreement.producerId, producer);

return await contractBuilder(
attributeQuery,
pdfGenerator,
fileManager,
config,
logger
).createContract(
selfcareId,
agreement,
eservice,
consumer,
producer,
updateSeed
);
};
Loading

0 comments on commit de4d157

Please sign in to comment.