Skip to content

Commit

Permalink
Use assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrotaje committed Jan 14, 2025
1 parent 575ae62 commit 5250562
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/purpose-process/src/routers/PurposeRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ const purposeRouter = (
const { purpose, isRiskAnalysisValid } =
await purposeService.clonePurpose({
purposeId: unsafeBrandId(req.params.purposeId),
organizationId: req.ctx.authData.organizationId,
authData: req.ctx.authData,
seed: req.body,
correlationId: req.ctx.correlationId,
logger: ctx.logger,
Expand Down
26 changes: 14 additions & 12 deletions packages/purpose-process/src/services/purposeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ async function retrieveTenantKind(
return tenant.kind;
}

async function retrieveDelegation(
async function retrieveActiveDelegation(
delegationId: DelegationId,
readModelService: ReadModelService
): Promise<Delegation> {
const delegation = await readModelService.getDelegationById(delegationId);
const delegation = await readModelService.getActiveDelegationById(
delegationId
);
if (delegation === undefined) {
throw delegationNotFound(delegationId);
}
Expand Down Expand Up @@ -1175,19 +1177,21 @@ export function purposeServiceBuilder(
},
async clonePurpose({
purposeId,
organizationId,
authData,
seed,
correlationId,
logger,
}: {
purposeId: PurposeId;
organizationId: TenantId;
authData: AuthData;
seed: purposeApi.PurposeCloneSeed;
correlationId: CorrelationId;
logger: Logger;
}): Promise<{ purpose: Purpose; isRiskAnalysisValid: boolean }> {
logger.info(`Cloning Purpose ${purposeId}`);

const organizationId = authData.organizationId;

const tenantKind = await retrieveTenantKind(
organizationId,
readModelService
Expand All @@ -1196,19 +1200,17 @@ export function purposeServiceBuilder(
const purposeToClone = await retrievePurpose(purposeId, readModelService);

const activeConsumerDelegation = purposeToClone.data.delegationId
? await retrieveDelegation(
? await retrieveActiveDelegation(
purposeToClone.data.delegationId,
readModelService
)
: undefined;

if (
activeConsumerDelegation &&
activeConsumerDelegation.delegateId === organizationId &&
purposeToClone.data.consumerId !== organizationId
) {
throw purposeCannotBeCloned(purposeId);
}
assertRequesterCanActAsConsumer(
purposeToClone.data,
authData,
activeConsumerDelegation
);

if (purposeIsDraft(purposeToClone.data)) {
throw purposeCannotBeCloned(purposeId);
Expand Down
3 changes: 2 additions & 1 deletion packages/purpose-process/src/services/readModelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,12 @@ export function readModelServiceBuilder(
"data.kind": delegationKind.delegatedConsumer,
});
},
async getDelegationById(
async getActiveDelegationById(
delegationId: DelegationId
): Promise<Delegation | undefined> {
return getDelegation(delegations, {
"data.id": delegationId,
"data.state": delegationState.active,
});
},
};
Expand Down
15 changes: 8 additions & 7 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,7 +75,7 @@ describe("clonePurpose", async () => {

const { purpose, isRiskAnalysisValid } = await purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -144,7 +145,7 @@ describe("clonePurpose", async () => {

const { purpose, isRiskAnalysisValid } = await purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -214,7 +215,7 @@ describe("clonePurpose", async () => {
expect(
purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -250,7 +251,7 @@ describe("clonePurpose", async () => {
expect(
purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -286,7 +287,7 @@ describe("clonePurpose", async () => {
expect(
purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -332,7 +333,7 @@ describe("clonePurpose", async () => {
expect(
purposeService.clonePurpose({
purposeId: mockPurposeToClone.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down Expand Up @@ -370,7 +371,7 @@ describe("clonePurpose", async () => {
expect(
purposeService.clonePurpose({
purposeId: mockPurpose.id,
organizationId: mockTenant.id,
authData: getMockAuthData(mockTenant.id),
seed: {
eserviceId: mockEService.id,
},
Expand Down

0 comments on commit 5250562

Please sign in to comment.