From 270d16384dd1c4d5df0f5372c9072dab0af7e76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Taj=C3=A8?= Date: Fri, 17 Jan 2025 18:04:43 +0100 Subject: [PATCH] PIN-5619 clone purpose (#1355) Co-authored-by: AsterITA --- .../src/routers/PurposeRouter.ts | 4 +- .../src/services/purposeService.ts | 23 +++++-- .../purpose-process/test/clonePurpose.test.ts | 64 +++++++++++++------ 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/packages/purpose-process/src/routers/PurposeRouter.ts b/packages/purpose-process/src/routers/PurposeRouter.ts index 3ff24bd771..3785c46dd7 100644 --- a/packages/purpose-process/src/routers/PurposeRouter.ts +++ b/packages/purpose-process/src/routers/PurposeRouter.ts @@ -454,10 +454,8 @@ const purposeRouter = ( const { purpose, isRiskAnalysisValid } = await purposeService.clonePurpose({ purposeId: unsafeBrandId(req.params.purposeId), - organizationId: req.ctx.authData.organizationId, seed: req.body, - correlationId: req.ctx.correlationId, - logger: ctx.logger, + ctx, }); return res .status(200) diff --git a/packages/purpose-process/src/services/purposeService.ts b/packages/purpose-process/src/services/purposeService.ts index bceac92f66..7944cb1fd0 100644 --- a/packages/purpose-process/src/services/purposeService.ts +++ b/packages/purpose-process/src/services/purposeService.ts @@ -760,8 +760,8 @@ export function purposeServiceBuilder( const newPurposeVersion: PurposeVersion = { id: generateId(), - state: purposeVersionState.active, riskAnalysis: riskAnalysisDocument, + state: purposeVersionState.active, dailyCalls: seed.dailyCalls, firstActivationAt: new Date(), createdAt: new Date(), @@ -1169,17 +1169,16 @@ export function purposeServiceBuilder( }, async clonePurpose({ purposeId, - organizationId, seed, - correlationId, - logger, + ctx, }: { purposeId: PurposeId; - organizationId: TenantId; seed: purposeApi.PurposeCloneSeed; - correlationId: CorrelationId; - logger: Logger; + ctx: WithLogger; }): Promise<{ purpose: Purpose; isRiskAnalysisValid: boolean }> { + const { correlationId, authData, logger } = ctx; + const organizationId = authData.organizationId; + logger.info(`Cloning Purpose ${purposeId}`); const tenantKind = await retrieveTenantKind( @@ -1189,6 +1188,15 @@ export function purposeServiceBuilder( const purposeToClone = await retrievePurpose(purposeId, readModelService); + assertRequesterCanActAsConsumer( + purposeToClone.data, + authData, + purposeToClone.data.delegationId && + (await readModelService.getActiveConsumerDelegationByDelegationId( + purposeToClone.data.delegationId + )) + ); + if (purposeIsDraft(purposeToClone.data)) { throw purposeCannotBeCloned(purposeId); } @@ -1255,6 +1263,7 @@ export function purposeServiceBuilder( isFreeOfCharge: purposeToClone.data.isFreeOfCharge, freeOfChargeReason: purposeToClone.data.freeOfChargeReason, riskAnalysisForm: clonedRiskAnalysisForm, + delegationId: purposeToClone.data.delegationId, }; const isRiskAnalysisValid = clonedRiskAnalysisForm diff --git a/packages/purpose-process/test/clonePurpose.test.ts b/packages/purpose-process/test/clonePurpose.test.ts index e401dc3718..0dba4b58d1 100644 --- a/packages/purpose-process/test/clonePurpose.test.ts +++ b/packages/purpose-process/test/clonePurpose.test.ts @@ -3,6 +3,7 @@ import { decodeProtobufPayload, getMockAgreement, + getMockAuthData, getMockEService, getMockPurpose, getMockPurposeVersion, @@ -74,12 +75,15 @@ describe("clonePurpose", async () => { const { purpose, isRiskAnalysisValid } = await purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }); const writtenEvent = await readLastPurposeEvent(purpose.id); @@ -144,12 +148,15 @@ describe("clonePurpose", async () => { const { purpose, isRiskAnalysisValid } = await purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }); const writtenEvent = await readLastPurposeEvent(purpose.id); @@ -214,12 +221,15 @@ describe("clonePurpose", async () => { expect( purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }) ).rejects.toThrowError(purposeNotFound(mockPurpose.id)); }); @@ -250,12 +260,15 @@ describe("clonePurpose", async () => { expect( purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }) ).rejects.toThrowError(purposeCannotBeCloned(mockPurpose.id)); }); @@ -286,12 +299,15 @@ describe("clonePurpose", async () => { expect( purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }) ).rejects.toThrowError(purposeCannotBeCloned(mockPurpose.id)); }); @@ -332,12 +348,15 @@ describe("clonePurpose", async () => { expect( purposeService.clonePurpose({ purposeId: mockPurposeToClone.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }) ).rejects.toThrowError( duplicatedPurposeTitle(mockPurposeWithSameName.title) @@ -370,12 +389,15 @@ describe("clonePurpose", async () => { expect( purposeService.clonePurpose({ purposeId: mockPurpose.id, - organizationId: mockTenant.id, seed: { eserviceId: mockEService.id, }, - correlationId: generateId(), - logger: genericLogger, + ctx: { + authData: getMockAuthData(mockTenant.id), + correlationId: generateId(), + logger: genericLogger, + serviceName: "test", + }, }) ).rejects.toThrowError(tenantKindNotFound(mockTenant.id)); });