Skip to content

Commit

Permalink
Package lifecycle (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor-K authored Jul 8, 2024
1 parent f7068b0 commit 51a9b3a
Show file tree
Hide file tree
Showing 16 changed files with 5,822 additions and 4,476 deletions.
32 changes: 32 additions & 0 deletions packages/agreement-lifecycle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "pagopa-interop-agreement-lifecycle",
"version": "1.0.0",
"private": true,
"description": "PagoPA Interoperability agreement lifecycle",
"main": "dist",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"scripts": {
"lint": "eslint . --ext .ts,.tsx",
"lint:autofix": "eslint . --ext .ts,.tsx --fix",
"format:check": "prettier --check src",
"format:write": "prettier --write src",
"start": "node --loader ts-node/esm --watch ./src/index.ts",
"build": "tsc",
"check": "tsc --project tsconfig.check.json"
},
"keywords": [],
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@types/node": "20.14.6",
"pagopa-interop-models": "workspace:*"
},
"devDependencies": {
"eslint": "8.57.0",
"prettier": "2.8.8",
"typescript": "5.4.5"
}
}
41 changes: 41 additions & 0 deletions packages/agreement-lifecycle/src/filters/attributesFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ========= FILTERS ========= */

import {
CertifiedTenantAttribute,
CompactTenant,
DeclaredTenantAttribute,
Tenant,
TenantId,
VerifiedTenantAttribute,
tenantAttributeType,
} from "pagopa-interop-models";

export const filterVerifiedAttributes = (
producerId: TenantId,
tenant: Tenant | CompactTenant
): VerifiedTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.VERIFIED &&
att.verifiedBy.find(
(v) =>
v.id === producerId &&
(!v.extensionDate || v.extensionDate > new Date())
)
) as VerifiedTenantAttribute[];

export const filterCertifiedAttributes = (
tenant: Tenant | CompactTenant
): CertifiedTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.CERTIFIED && !att.revocationTimestamp
) as CertifiedTenantAttribute[];

export const filterDeclaredAttributes = (
tenant: Tenant | CompactTenant
): DeclaredTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.DECLARED && !att.revocationTimestamp
) as DeclaredTenantAttribute[];
2 changes: 2 additions & 0 deletions packages/agreement-lifecycle/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./validator/attributesValidator.js";
export * from "./filters/attributesFilter.js";
65 changes: 65 additions & 0 deletions packages/agreement-lifecycle/src/validator/attributesValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
Descriptor,
CompactTenant,
Tenant,
TenantId,
EServiceAttribute,
TenantAttribute,
} from "pagopa-interop-models";
import {
filterCertifiedAttributes,
filterDeclaredAttributes,
filterVerifiedAttributes,
} from "../filters/attributesFilter.js";

const attributesSatisfied = (
descriptorAttributes: EServiceAttribute[][],
consumerAttributeIds: Array<TenantAttribute["id"]>
): boolean =>
descriptorAttributes.every((attributeList) => {
const attributes = attributeList.map((a) => a.id);
return (
attributes.filter((a) => consumerAttributeIds.includes(a)).length > 0
);
});

export const certifiedAttributesSatisfied = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const certifiedAttributes = filterCertifiedAttributes(tenant).map(
(a) => a.id
);

return attributesSatisfied(
descriptor.attributes.certified,
certifiedAttributes
);
};

export const declaredAttributesSatisfied = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const declaredAttributes = filterDeclaredAttributes(tenant).map((a) => a.id);

return attributesSatisfied(
descriptor.attributes.declared,
declaredAttributes
);
};

export const verifiedAttributesSatisfied = (
producerId: TenantId,
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const verifiedAttributes = filterVerifiedAttributes(producerId, tenant).map(
(a) => a.id
);

return attributesSatisfied(
descriptor.attributes.verified,
verifiedAttributes
);
};
7 changes: 7 additions & 0 deletions packages/agreement-lifecycle/tsconfig.check.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true,
},
"include": ["src"]
}
9 changes: 9 additions & 0 deletions packages/agreement-lifecycle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"outDir": "dist"
},
"include": ["src"]
}
3 changes: 3 additions & 0 deletions packages/agreement-process/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ COPY pnpm-workspace.yaml /app/
COPY ./packages/agreement-process/package.json /app/packages/agreement-process/package.json
COPY ./packages/commons/package.json /app/packages/commons/package.json
COPY ./packages/models/package.json /app/packages/models/package.json
COPY ./packages/lifecycle/package.json /app/packages/lifecycle/package.json
COPY ./packages/selfcare-v2-client/package.json /app/packages/selfcare-v2-client/package.json

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
Expand All @@ -19,6 +20,7 @@ COPY turbo.json /app/
COPY ./packages/agreement-process /app/packages/agreement-process
COPY ./packages/commons /app/packages/commons
COPY ./packages/models /app/packages/models
COPY ./packages/lifecycle /app/packages/lifecycle
COPY ./packages/selfcare-v2-client /app/packages/selfcare-v2-client

RUN pnpm build && \
Expand All @@ -30,6 +32,7 @@ RUN pnpm build && \
package*.json packages/agreement-process/package*.json \
packages/commons/ \
packages/models/ \
packages/lifecycle/ \
packages/selfcare-v2-client \
packages/agreement-process/dist && \
find /out -exec touch -h --date=@0 {} \;
Expand Down
1 change: 1 addition & 0 deletions packages/agreement-process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"mongodb": "6.7.0",
"pagopa-interop-commons": "workspace:*",
"pagopa-interop-models": "workspace:*",
"pagopa-interop-agreement-lifecycle": "workspace:*",
"pagopa-interop-selfcare-v2-client": "workspace:*",
"ts-pattern": "5.2.0",
"uuid": "10.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TenantVerifier,
TenantRevoker,
badRequestError,
CompactTenant,
} from "pagopa-interop-models";
import { P, match } from "ts-pattern";

Expand All @@ -23,7 +24,6 @@ import {
ApiTenantRevoker,
ApiTenantVerifier,
} from "../types.js";
import { CompactTenant } from "./models.js";

export function agreementStateToApiAgreementState(
input: AgreementState
Expand Down
8 changes: 0 additions & 8 deletions packages/agreement-process/src/model/domain/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {
AgreementAttribute,
AgreementStamps,
AgreementState,
TenantAttribute,
TenantId,
} from "pagopa-interop-models";
import { z } from "zod";

Expand Down Expand Up @@ -50,9 +48,3 @@ export const CompactEService = z.object({
name: z.string(),
});
export type CompactEService = z.infer<typeof CompactEService>;

export const CompactTenant = z.object({
id: TenantId,
attributes: z.array(TenantAttribute),
});
export type CompactTenant = z.infer<typeof CompactTenant>;
96 changes: 6 additions & 90 deletions packages/agreement-process/src/model/domain/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import {
Agreement,
AgreementState,
AttributeId,
CertifiedTenantAttribute,
DeclaredTenantAttribute,
Descriptor,
DescriptorState,
EService,
EServiceAttribute,
Tenant,
VerifiedTenantAttribute,
agreementState,
descriptorState,
tenantAttributeType,
AgreementId,
DescriptorId,
EServiceId,
unsafeBrandId,
TenantAttribute,
TenantId,
} from "pagopa-interop-models";
import { AuthData } from "pagopa-interop-commons";
import {
certifiedAttributesSatisfied,
filterCertifiedAttributes,
filterDeclaredAttributes,
filterVerifiedAttributes,
} from "pagopa-interop-agreement-lifecycle";
import { ApiAgreementPayload } from "../types.js";
import { ReadModelService } from "../../services/readModelService.js";
import {
Expand All @@ -37,7 +38,6 @@ import {
} from "./errors.js";
import {
CertifiedAgreementAttribute,
CompactTenant,
DeclaredAgreementAttribute,
VerifiedAgreementAttribute,
} from "./models.js";
Expand Down Expand Up @@ -294,58 +294,6 @@ export const validateActiveOrPendingAgreement = (
}
};

const attributesSatisfied = (
descriptorAttributes: EServiceAttribute[][],
consumerAttributeIds: Array<TenantAttribute["id"]>
): boolean =>
descriptorAttributes.every((attributeList) => {
const attributes = attributeList.map((a) => a.id);
return (
attributes.filter((a) => consumerAttributeIds.includes(a)).length > 0
);
});

export const certifiedAttributesSatisfied = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const certifiedAttributes = filterCertifiedAttributes(tenant).map(
(a) => a.id
);

return attributesSatisfied(
descriptor.attributes.certified,
certifiedAttributes
);
};

export const declaredAttributesSatisfied = (
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const declaredAttributes = filterDeclaredAttributes(tenant).map((a) => a.id);

return attributesSatisfied(
descriptor.attributes.declared,
declaredAttributes
);
};

export const verifiedAttributesSatisfied = (
producerId: TenantId,
descriptor: Descriptor,
tenant: Tenant | CompactTenant
): boolean => {
const verifiedAttributes = filterVerifiedAttributes(producerId, tenant).map(
(a) => a.id
);

return attributesSatisfied(
descriptor.attributes.verified,
verifiedAttributes
);
};

export const verifyConflictingAgreements = async (
consumerId: TenantId,
eserviceId: EServiceId,
Expand Down Expand Up @@ -457,35 +405,3 @@ export const matchingVerifiedAttributes = (
verifiedAttributes
).map((id) => ({ id } as VerifiedAgreementAttribute));
};

/* ========= FILTERS ========= */

export const filterVerifiedAttributes = (
producerId: TenantId,
tenant: Tenant | CompactTenant
): VerifiedTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.VERIFIED &&
att.verifiedBy.find(
(v) =>
v.id === producerId &&
(!v.extensionDate || v.extensionDate > new Date())
)
) as VerifiedTenantAttribute[];

export const filterCertifiedAttributes = (
tenant: Tenant | CompactTenant
): CertifiedTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.CERTIFIED && !att.revocationTimestamp
) as CertifiedTenantAttribute[];

export const filterDeclaredAttributes = (
tenant: Tenant | CompactTenant
): DeclaredTenantAttribute[] =>
tenant.attributes.filter(
(att) =>
att.type === tenantAttributeType.DECLARED && !att.revocationTimestamp
) as DeclaredTenantAttribute[];
Loading

0 comments on commit 51a9b3a

Please sign in to comment.