Skip to content

Commit

Permalink
PIN-5619 clone purpose (#1355)
Browse files Browse the repository at this point in the history
Co-authored-by: AsterITA <[email protected]>
  • Loading branch information
sandrotaje and AsterITA authored Jan 17, 2025
1 parent 348b5c0 commit 270d163
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
4 changes: 1 addition & 3 deletions packages/purpose-process/src/routers/PurposeRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 16 additions & 7 deletions packages/purpose-process/src/services/purposeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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<AppContext>;
}): Promise<{ purpose: Purpose; isRiskAnalysisValid: boolean }> {
const { correlationId, authData, logger } = ctx;
const organizationId = authData.organizationId;

logger.info(`Cloning Purpose ${purposeId}`);

const tenantKind = await retrieveTenantKind(
Expand All @@ -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);
}
Expand Down Expand Up @@ -1255,6 +1263,7 @@ export function purposeServiceBuilder(
isFreeOfCharge: purposeToClone.data.isFreeOfCharge,
freeOfChargeReason: purposeToClone.data.freeOfChargeReason,
riskAnalysisForm: clonedRiskAnalysisForm,
delegationId: purposeToClone.data.delegationId,
};

const isRiskAnalysisValid = clonedRiskAnalysisForm
Expand Down
64 changes: 43 additions & 21 deletions packages/purpose-process/test/clonePurpose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
decodeProtobufPayload,
getMockAgreement,
getMockAuthData,
getMockEService,
getMockPurpose,
getMockPurposeVersion,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
});
Expand Down

0 comments on commit 270d163

Please sign in to comment.