@openlup/platform 0.28.0 → 0.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +369 -156
- package/README.md +220 -4
- package/dist/composition/index.d.ts +1 -0
- package/dist/composition/index.js +1 -0
- package/dist/composition/marketingContribution.d.ts +21 -0
- package/dist/composition/marketingContribution.js +29 -0
- package/dist/composition/nodeHost.d.ts +1 -1
- package/dist/composition/nodeHost.js +3 -2
- package/dist/lifecycle/appUpgrade.d.ts +52 -0
- package/dist/lifecycle/appUpgrade.js +180 -0
- package/dist/lifecycle/index.d.ts +2 -0
- package/dist/lifecycle/index.js +2 -0
- package/dist/lifecycle/postgresInstallQualification.d.ts +51 -0
- package/dist/lifecycle/postgresInstallQualification.js +74 -0
- package/dist/marketing/acquisitionCaseSchemas.d.ts +103 -0
- package/dist/marketing/acquisitionCaseSchemas.js +53 -0
- package/dist/marketing/acquisitionEvidenceSchemas.d.ts +105 -0
- package/dist/marketing/acquisitionEvidenceSchemas.js +55 -0
- package/dist/marketing/analytics/dataLayer.d.ts +18 -0
- package/dist/marketing/analytics/dataLayer.js +111 -0
- package/dist/marketing/analytics/noop.d.ts +9 -0
- package/dist/marketing/analytics/noop.js +8 -0
- package/dist/marketing/analytics/purchaseFacts.d.ts +4 -0
- package/dist/marketing/analytics/purchaseFacts.js +69 -0
- package/dist/marketing/analytics/routePolicy.d.ts +10 -0
- package/dist/marketing/analytics/routePolicy.js +59 -0
- package/dist/marketing/consentEvidence.d.ts +8 -0
- package/dist/marketing/consentEvidence.js +32 -0
- package/dist/marketing/contactLegal.d.ts +14 -0
- package/dist/marketing/contactLegal.js +19 -0
- package/dist/marketing/contracts/acquisition.d.ts +88 -0
- package/dist/marketing/contracts/acquisition.js +1 -0
- package/dist/marketing/contracts/acquisitionPorts.d.ts +49 -0
- package/dist/marketing/contracts/acquisitionPorts.js +1 -0
- package/dist/marketing/contracts/analytics.d.ts +144 -0
- package/dist/marketing/contracts/analytics.js +1 -0
- package/dist/marketing/contracts/contactLegal.d.ts +53 -0
- package/dist/marketing/contracts/contactLegal.js +1 -0
- package/dist/marketing/contracts/index.d.ts +6 -0
- package/dist/marketing/contracts/index.js +1 -0
- package/dist/marketing/contracts/prelaunch.d.ts +39 -0
- package/dist/marketing/contracts/prelaunch.js +1 -0
- package/dist/marketing/index.d.ts +16 -0
- package/dist/marketing/index.js +14 -0
- package/dist/marketing/prelaunchAcquisitionSchemas.d.ts +116 -0
- package/dist/marketing/prelaunchAcquisitionSchemas.js +51 -0
- package/dist/marketing/seo/jsonLd.d.ts +157 -0
- package/dist/marketing/seo/jsonLd.js +127 -0
- package/dist/marketing/seo/metadata.d.ts +41 -0
- package/dist/marketing/seo/metadata.js +36 -0
- package/dist/marketing/seo/serializeJsonLd.d.ts +1 -0
- package/dist/marketing/seo/serializeJsonLd.js +6 -0
- package/dist/marketing/seo/urls.d.ts +14 -0
- package/dist/marketing/seo/urls.js +23 -0
- package/dist/marketing/server/acquisitionCaseDirect.d.ts +4 -0
- package/dist/marketing/server/acquisitionCaseDirect.js +88 -0
- package/dist/marketing/server/acquisitionCaseHandlers.d.ts +28 -0
- package/dist/marketing/server/acquisitionCaseHandlers.js +40 -0
- package/dist/marketing/server/acquisitionCasePorts.d.ts +42 -0
- package/dist/marketing/server/acquisitionCasePorts.js +1 -0
- package/dist/marketing/server/acquisitionEvidenceDirect.d.ts +5 -0
- package/dist/marketing/server/acquisitionEvidenceDirect.js +100 -0
- package/dist/marketing/server/analyticsContext.d.ts +3 -0
- package/dist/marketing/server/analyticsContext.js +65 -0
- package/dist/marketing/server/analyticsContextPorts.d.ts +31 -0
- package/dist/marketing/server/analyticsContextPorts.js +1 -0
- package/dist/marketing/server/bindings.d.ts +14 -0
- package/dist/marketing/server/bindings.js +18 -0
- package/dist/marketing/server/directPorts.d.ts +26 -0
- package/dist/marketing/server/directPorts.js +1 -0
- package/dist/marketing/server/index.d.ts +14 -0
- package/dist/marketing/server/index.js +11 -0
- package/dist/marketing/server/postgres/acquisitionCaseRoutines.d.ts +10 -0
- package/dist/marketing/server/postgres/acquisitionCaseRoutines.js +204 -0
- package/dist/marketing/server/postgres/acquisitionEvidenceRoutines.d.ts +10 -0
- package/dist/marketing/server/postgres/acquisitionEvidenceRoutines.js +107 -0
- package/dist/marketing/server/postgres/index.d.ts +3 -0
- package/dist/marketing/server/postgres/index.js +3 -0
- package/dist/marketing/server/prelaunchAcquisitionDirect.d.ts +4 -0
- package/dist/marketing/server/prelaunchAcquisitionDirect.js +122 -0
- package/dist/marketing/server/privacyContributor.d.ts +66 -0
- package/dist/marketing/server/privacyContributor.js +87 -0
- package/dist/marketing/server/research/adminSurveyResponsesHandler.d.ts +9 -0
- package/dist/marketing/server/research/adminSurveyResponsesHandler.js +53 -0
- package/dist/marketing/server/research/managedSurveyResponseSubmit.d.ts +39 -0
- package/dist/marketing/server/research/managedSurveyResponseSubmit.js +102 -0
- package/dist/marketing/server/research/surveyResponseSubmitHandler.d.ts +8 -0
- package/dist/marketing/server/research/surveyResponseSubmitHandler.js +41 -0
- package/dist/marketing/validation.d.ts +2 -0
- package/dist/marketing/validation.js +3 -0
- package/dist/migrations/postgresRunner.d.ts +9 -0
- package/dist/migrations/postgresRunner.js +17 -10
- package/dist/operations/adminAlertsSchemas.d.ts +153 -0
- package/dist/operations/adminAlertsSchemas.js +69 -0
- package/dist/operations/adminAlertsView.d.ts +73 -0
- package/dist/operations/adminAlertsView.js +184 -0
- package/dist/operations/alertPagingPolicy.d.ts +15 -0
- package/dist/operations/alertPagingPolicy.js +43 -0
- package/dist/operations/alertThrottlePolicy.d.ts +3 -0
- package/dist/operations/alertThrottlePolicy.js +18 -0
- package/dist/operations/clients.d.ts +17 -0
- package/dist/operations/clients.js +32 -0
- package/dist/operations/contracts/admin.d.ts +93 -0
- package/dist/operations/contracts/admin.js +1 -0
- package/dist/operations/contracts/adminAlertsContracts.d.ts +40 -0
- package/dist/operations/contracts/adminAlertsContracts.js +1 -0
- package/dist/operations/contracts/alertHumanContext.d.ts +7 -0
- package/dist/operations/contracts/alertHumanContext.js +1 -0
- package/dist/operations/contracts/index.d.ts +6 -0
- package/dist/operations/contracts/index.js +6 -0
- package/dist/operations/contracts/observabilityContracts.d.ts +41 -0
- package/dist/operations/contracts/observabilityContracts.js +1 -0
- package/dist/operations/contracts/observabilityPorts.d.ts +18 -0
- package/dist/operations/contracts/observabilityPorts.js +1 -0
- package/dist/operations/contracts/ports.d.ts +19 -0
- package/dist/operations/contracts/ports.js +1 -0
- package/dist/operations/errors.d.ts +6 -0
- package/dist/operations/errors.js +6 -0
- package/dist/operations/index.d.ts +9 -0
- package/dist/operations/index.js +9 -0
- package/dist/operations/schemas.d.ts +138 -0
- package/dist/operations/schemas.js +104 -0
- package/dist/operations/server/adminAlertsHandlers.d.ts +16 -0
- package/dist/operations/server/adminAlertsHandlers.js +35 -0
- package/dist/operations/server/adminPipelineHandlers.d.ts +12 -0
- package/dist/operations/server/adminPipelineHandlers.js +48 -0
- package/dist/operations/server/adminSettingsHandlers.d.ts +14 -0
- package/dist/operations/server/adminSettingsHandlers.js +82 -0
- package/dist/operations/server/controlPlaneHandler.d.ts +7 -0
- package/dist/operations/server/controlPlaneHandler.js +54 -0
- package/dist/operations/server/controlPlaneStore.d.ts +12 -0
- package/dist/operations/server/controlPlaneStore.js +138 -0
- package/dist/operations/server/controlPlaneWatchdog.d.ts +7 -0
- package/dist/operations/server/controlPlaneWatchdog.js +20 -0
- package/dist/operations/server/index.d.ts +12 -0
- package/dist/operations/server/index.js +12 -0
- package/dist/operations/server/migrations.d.ts +14 -0
- package/dist/operations/server/migrations.js +7 -0
- package/dist/operations/server/platformWatchdogDecision.d.ts +34 -0
- package/dist/operations/server/platformWatchdogDecision.js +71 -0
- package/dist/operations/server/platformWatchdogDeliveryPolicy.d.ts +10 -0
- package/dist/operations/server/platformWatchdogDeliveryPolicy.js +53 -0
- package/dist/operations/server/platformWatchdogQueue.d.ts +11 -0
- package/dist/operations/server/platformWatchdogQueue.js +94 -0
- package/dist/operations/server/platformWatchdogService.d.ts +47 -0
- package/dist/operations/server/platformWatchdogService.js +105 -0
- package/dist/operations/validation.d.ts +2 -0
- package/dist/operations/validation.js +3 -0
- package/dist/portability/filesystemObjects.d.ts +12 -0
- package/dist/portability/filesystemObjects.js +191 -0
- package/dist/portability/index.d.ts +5 -0
- package/dist/portability/index.js +4 -0
- package/dist/portability/objects.d.ts +63 -0
- package/dist/portability/objects.js +138 -0
- package/dist/portability/postgresRows.d.ts +64 -0
- package/dist/portability/postgresRows.js +182 -0
- package/dist/scheduler/advisoryLock.js +20 -2
- package/dist/scheduler/nodeScheduler.d.ts +2 -2
- package/dist/scheduler/nodeScheduler.js +35 -6
- package/package.json +47 -2
- package/release-gates.json +143 -9
- package/sql/20260814010000_platform_control_plane.sql +286 -0
- package/src/composition/index.ts +1 -0
- package/src/composition/marketingContribution.ts +37 -0
- package/src/composition/nodeHost.ts +4 -3
- package/src/lifecycle/appUpgrade.ts +186 -0
- package/src/lifecycle/index.ts +19 -0
- package/src/lifecycle/postgresInstallQualification.ts +125 -0
- package/src/marketing/acquisitionCaseSchemas.ts +75 -0
- package/src/marketing/acquisitionEvidenceSchemas.ts +72 -0
- package/src/marketing/analytics/dataLayer.ts +100 -0
- package/src/marketing/analytics/noop.ts +14 -0
- package/src/marketing/analytics/purchaseFacts.ts +63 -0
- package/src/marketing/analytics/routePolicy.ts +55 -0
- package/src/marketing/consentEvidence.ts +36 -0
- package/src/marketing/contactLegal.ts +22 -0
- package/src/marketing/contracts/acquisition.ts +50 -0
- package/src/marketing/contracts/acquisitionPorts.ts +48 -0
- package/src/marketing/contracts/analytics.ts +88 -0
- package/src/marketing/contracts/contactLegal.ts +41 -0
- package/src/marketing/contracts/index.ts +17 -0
- package/src/marketing/contracts/prelaunch.ts +15 -0
- package/src/marketing/index.ts +16 -0
- package/src/marketing/prelaunchAcquisitionSchemas.ts +64 -0
- package/src/marketing/seo/jsonLd.ts +153 -0
- package/src/marketing/seo/metadata.ts +62 -0
- package/src/marketing/seo/serializeJsonLd.ts +6 -0
- package/src/marketing/seo/urls.ts +34 -0
- package/src/marketing/server/acquisitionCaseDirect.ts +67 -0
- package/src/marketing/server/acquisitionCaseHandlers.ts +57 -0
- package/src/marketing/server/acquisitionCasePorts.ts +42 -0
- package/src/marketing/server/acquisitionEvidenceDirect.ts +76 -0
- package/src/marketing/server/analyticsContext.ts +55 -0
- package/src/marketing/server/analyticsContextPorts.ts +28 -0
- package/src/marketing/server/bindings.ts +27 -0
- package/src/marketing/server/directPorts.ts +27 -0
- package/src/marketing/server/index.ts +14 -0
- package/src/marketing/server/postgres/acquisitionCaseRoutines.ts +218 -0
- package/src/marketing/server/postgres/acquisitionEvidenceRoutines.ts +103 -0
- package/src/marketing/server/postgres/index.ts +3 -0
- package/src/marketing/server/prelaunchAcquisitionDirect.ts +121 -0
- package/src/marketing/server/privacyContributor.ts +123 -0
- package/src/marketing/server/research/adminSurveyResponsesHandler.ts +78 -0
- package/src/marketing/server/research/managedSurveyResponseSubmit.ts +158 -0
- package/src/marketing/server/research/surveyResponseSubmitHandler.ts +57 -0
- package/src/marketing/validation.ts +3 -0
- package/src/migrations/postgresRunner.ts +24 -9
- package/src/operations/adminAlertsSchemas.ts +78 -0
- package/src/operations/adminAlertsView.ts +238 -0
- package/src/operations/alertPagingPolicy.ts +50 -0
- package/src/operations/alertThrottlePolicy.ts +22 -0
- package/src/operations/clients.ts +44 -0
- package/src/operations/contracts/admin.ts +24 -0
- package/src/operations/contracts/adminAlertsContracts.ts +13 -0
- package/src/operations/contracts/alertHumanContext.ts +17 -0
- package/src/operations/contracts/index.ts +6 -0
- package/src/operations/contracts/observabilityContracts.ts +40 -0
- package/src/operations/contracts/observabilityPorts.ts +30 -0
- package/src/operations/contracts/ports.ts +22 -0
- package/src/operations/errors.ts +6 -0
- package/src/operations/index.ts +9 -0
- package/src/operations/schemas.ts +122 -0
- package/src/operations/server/adminAlertsHandlers.ts +61 -0
- package/src/operations/server/adminPipelineHandlers.ts +73 -0
- package/src/operations/server/adminSettingsHandlers.ts +114 -0
- package/src/operations/server/controlPlaneHandler.ts +56 -0
- package/src/operations/server/controlPlaneStore.ts +150 -0
- package/src/operations/server/controlPlaneWatchdog.ts +28 -0
- package/src/operations/server/index.ts +12 -0
- package/src/operations/server/migrations.ts +7 -0
- package/src/operations/server/platformWatchdogDecision.ts +93 -0
- package/src/operations/server/platformWatchdogDeliveryPolicy.ts +92 -0
- package/src/operations/server/platformWatchdogQueue.ts +96 -0
- package/src/operations/server/platformWatchdogService.ts +209 -0
- package/src/operations/validation.ts +3 -0
- package/src/portability/filesystemObjects.ts +138 -0
- package/src/portability/index.ts +12 -0
- package/src/portability/objects.ts +160 -0
- package/src/portability/postgresRows.ts +169 -0
- package/src/scheduler/advisoryLock.ts +20 -5
- package/src/scheduler/nodeScheduler.ts +28 -8
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AcquisitionCaseListRequest } from "../contracts/index.js";
|
|
2
|
+
import { type AcquisitionCaseSubmitRequest, type AcquisitionCaseTransitionRequest } from "./acquisitionCasePorts.js";
|
|
3
|
+
import type { AcquisitionCasePort } from "./acquisitionCasePorts.js";
|
|
4
|
+
export interface AcquisitionCaseHandlerOptions {
|
|
5
|
+
now?: () => string;
|
|
6
|
+
}
|
|
7
|
+
export declare function createAcquisitionCaseHandlers(port: AcquisitionCasePort, options?: AcquisitionCaseHandlerOptions): {
|
|
8
|
+
submit(request: AcquisitionCaseSubmitRequest, context: {
|
|
9
|
+
idempotencyKey: string;
|
|
10
|
+
requesterKey: string;
|
|
11
|
+
}): Promise<import("./acquisitionCasePorts.js").AcquisitionCaseResult<import("../contracts/acquisition.js").AcquisitionCaseProjection>>;
|
|
12
|
+
list(actorRef: string, request: AcquisitionCaseListRequest): Promise<import("./acquisitionCasePorts.js").AcquisitionCaseResult<import("../contracts/acquisition.js").AcquisitionCaseListResponse>>;
|
|
13
|
+
get(actorRef: string, caseRef: string): Promise<import("./acquisitionCasePorts.js").AcquisitionCaseResult<import("../contracts/acquisition.js").AcquisitionCaseProjection>>;
|
|
14
|
+
transition(actorRef: string, idempotencyKey: string, request: AcquisitionCaseTransitionRequest): Promise<import("./acquisitionCasePorts.js").AcquisitionCaseResult<import("../contracts/acquisition.js").AcquisitionCaseProjection>>;
|
|
15
|
+
activeCount(): Promise<{
|
|
16
|
+
ok: false;
|
|
17
|
+
error: {
|
|
18
|
+
kind: import("./acquisitionCasePorts.js").AcquisitionCaseFailureKind;
|
|
19
|
+
};
|
|
20
|
+
} | {
|
|
21
|
+
ok: true;
|
|
22
|
+
value: {
|
|
23
|
+
activeTesterCount: number;
|
|
24
|
+
};
|
|
25
|
+
}>;
|
|
26
|
+
contractVersion: "tester_application_v1";
|
|
27
|
+
};
|
|
28
|
+
export type AcquisitionCaseHandlers = ReturnType<typeof createAcquisitionCaseHandlers>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ACQUISITION_CASE_CONTRACT_VERSION, ACQUISITION_CASE_SOURCE } from "../acquisitionCaseSchemas.js";
|
|
2
|
+
const SCOPE = "public_tester_application";
|
|
3
|
+
export function createAcquisitionCaseHandlers(port, options = {}) {
|
|
4
|
+
const now = options.now ?? (() => new Date().toISOString());
|
|
5
|
+
return {
|
|
6
|
+
submit(request, context) {
|
|
7
|
+
return port.submit({
|
|
8
|
+
scope: SCOPE,
|
|
9
|
+
sourceKind: ACQUISITION_CASE_SOURCE,
|
|
10
|
+
idempotencyKey: context.idempotencyKey,
|
|
11
|
+
requesterKey: `${SCOPE}:${context.requesterKey}`,
|
|
12
|
+
acceptedAt: now(),
|
|
13
|
+
sourcePath: "/tester-application",
|
|
14
|
+
request,
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
list(actorRef, request) {
|
|
18
|
+
return port.list({
|
|
19
|
+
actorRef,
|
|
20
|
+
scope: SCOPE,
|
|
21
|
+
...(request.cursor ? { cursor: request.cursor } : {}),
|
|
22
|
+
limit: request.limit,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
get(actorRef, caseRef) {
|
|
26
|
+
return port.get({ actorRef, scope: SCOPE, caseRef });
|
|
27
|
+
},
|
|
28
|
+
transition(actorRef, idempotencyKey, request) {
|
|
29
|
+
return port.transition({ actorRef, scope: SCOPE, idempotencyKey, request });
|
|
30
|
+
},
|
|
31
|
+
async activeCount() {
|
|
32
|
+
const result = await port.activeCount(SCOPE);
|
|
33
|
+
return result.ok === false ? result : {
|
|
34
|
+
ok: true,
|
|
35
|
+
value: { activeTesterCount: result.value },
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
contractVersion: ACQUISITION_CASE_CONTRACT_VERSION,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AcquisitionCaseProjection, AcquisitionCaseResult, AcquisitionCasePort as PortableAcquisitionCasePort } from "../contracts/index.js";
|
|
2
|
+
import type { AcquisitionAddressReference, AcquisitionCaseProjection as Projection } from "../contracts/index.js";
|
|
3
|
+
/** Historical commands retained by the server adapter; public admission routes remain host-owned. */
|
|
4
|
+
export interface AcquisitionCaseSubmitRequest {
|
|
5
|
+
contact: {
|
|
6
|
+
email: string;
|
|
7
|
+
};
|
|
8
|
+
consent: {
|
|
9
|
+
accepted: true;
|
|
10
|
+
consentVersion: string;
|
|
11
|
+
policyVersion: string;
|
|
12
|
+
locale: Projection["consent"]["locale"];
|
|
13
|
+
};
|
|
14
|
+
addressReference: AcquisitionAddressReference;
|
|
15
|
+
}
|
|
16
|
+
export interface AcquisitionCaseTransitionRequest {
|
|
17
|
+
view: "acquisition_case_v1";
|
|
18
|
+
caseRef: string;
|
|
19
|
+
expectedVersion: number;
|
|
20
|
+
transition: "approve" | "activate" | "reject" | "withdraw";
|
|
21
|
+
reason?: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface AcquisitionCaseSubmitCommand {
|
|
24
|
+
scope: "public_tester_application";
|
|
25
|
+
sourceKind: "tester_application";
|
|
26
|
+
idempotencyKey: string;
|
|
27
|
+
requesterKey: string;
|
|
28
|
+
acceptedAt: string;
|
|
29
|
+
sourcePath: "/tester-application";
|
|
30
|
+
request: AcquisitionCaseSubmitRequest;
|
|
31
|
+
}
|
|
32
|
+
export interface AcquisitionCaseTransitionCommand {
|
|
33
|
+
actorRef: string;
|
|
34
|
+
scope: "public_tester_application";
|
|
35
|
+
idempotencyKey: string;
|
|
36
|
+
request: AcquisitionCaseTransitionRequest;
|
|
37
|
+
}
|
|
38
|
+
export interface AcquisitionCasePort extends PortableAcquisitionCasePort {
|
|
39
|
+
submit(command: AcquisitionCaseSubmitCommand): Promise<AcquisitionCaseResult<AcquisitionCaseProjection>>;
|
|
40
|
+
transition(command: AcquisitionCaseTransitionCommand): Promise<AcquisitionCaseResult<AcquisitionCaseProjection>>;
|
|
41
|
+
}
|
|
42
|
+
export type { AcquisitionCaseFailureKind, AcquisitionCaseResult, AcquisitionCaseListQuery, AcquisitionCaseGetQuery, } from "../contracts/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { HttpRequest, HttpResponse } from "../../http/index.js";
|
|
2
|
+
import type { AcquisitionEvidenceDirectDeps } from "./directPorts.js";
|
|
3
|
+
export declare function createAcquisitionSurveySubmitDirectHandler(options: AcquisitionEvidenceDirectDeps): (req: HttpRequest, res: HttpResponse) => Promise<void>;
|
|
4
|
+
export declare function createAcquisitionSurveyListDirectHandler(options: AcquisitionEvidenceDirectDeps): (req: HttpRequest, res: HttpResponse) => Promise<void>;
|
|
5
|
+
export declare function createAcquisitionNewsletterConsentDirectHandler(options: AcquisitionEvidenceDirectDeps): (value: unknown, res: HttpResponse) => Promise<void>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { acquisitionNewsletterConsentEventSchema, acquisitionSurveyListQuerySchema, acquisitionSurveySubmitSchema } from "../acquisitionEvidenceSchemas.js";
|
|
2
|
+
export function createAcquisitionSurveySubmitDirectHandler(options) {
|
|
3
|
+
const { respond, resolveBinding } = options;
|
|
4
|
+
const { sendBffSuccess, sendMethodNotAllowed } = respond;
|
|
5
|
+
return async (req, res) => {
|
|
6
|
+
if (req.method !== "POST")
|
|
7
|
+
return sendMethodNotAllowed(res, ["POST"]);
|
|
8
|
+
const request = acquisitionSurveySubmitSchema.safeParse(req.body);
|
|
9
|
+
const idempotencyKey = firstHeader(req.headers["idempotency-key"]);
|
|
10
|
+
if (!request.success || !idempotencyKey)
|
|
11
|
+
return badRequest(res, respond, request.success ? undefined : request.error.flatten());
|
|
12
|
+
const binding = await bindingOrError(res, respond, resolveBinding);
|
|
13
|
+
if (!binding)
|
|
14
|
+
return;
|
|
15
|
+
const result = await safeOperation(() => binding.submitSurvey({ ...request.data, idempotencyKey }));
|
|
16
|
+
if (!result)
|
|
17
|
+
return unavailable(res, respond);
|
|
18
|
+
if (result.ok === false)
|
|
19
|
+
return failure(res, respond, result.error.kind);
|
|
20
|
+
sendBffSuccess(res, result.value);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function createAcquisitionSurveyListDirectHandler(options) {
|
|
24
|
+
const { respond, authorize, resolveBinding } = options;
|
|
25
|
+
const { sendBffSuccess, sendMethodNotAllowed } = respond;
|
|
26
|
+
return async (req, res) => {
|
|
27
|
+
if (req.method !== "GET")
|
|
28
|
+
return sendMethodNotAllowed(res, ["GET"]);
|
|
29
|
+
const actorReference = await authorize(req, res);
|
|
30
|
+
if (!actorReference)
|
|
31
|
+
return;
|
|
32
|
+
const request = acquisitionSurveyListQuerySchema.safeParse({
|
|
33
|
+
view: first(req.query.view), surveyType: first(req.query.surveyType), limit: first(req.query.limit) ?? "50",
|
|
34
|
+
});
|
|
35
|
+
if (!request.success)
|
|
36
|
+
return badRequest(res, respond, request.error.flatten());
|
|
37
|
+
const binding = await bindingOrError(res, respond, resolveBinding);
|
|
38
|
+
if (!binding)
|
|
39
|
+
return;
|
|
40
|
+
const result = await safeOperation(() => binding.listSurveys({ ...request.data, actorReference }));
|
|
41
|
+
if (!result)
|
|
42
|
+
return unavailable(res, respond);
|
|
43
|
+
if (result.ok === false)
|
|
44
|
+
return failure(res, respond, result.error.kind);
|
|
45
|
+
sendBffSuccess(res, result.value);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function createAcquisitionNewsletterConsentDirectHandler(options) {
|
|
49
|
+
const { respond, resolveBinding } = options;
|
|
50
|
+
const { sendBffSuccess } = respond;
|
|
51
|
+
return async (value, res) => {
|
|
52
|
+
const request = acquisitionNewsletterConsentEventSchema.safeParse(value);
|
|
53
|
+
if (!request.success)
|
|
54
|
+
return badRequest(res, respond, request.error.flatten());
|
|
55
|
+
const binding = await bindingOrError(res, respond, resolveBinding);
|
|
56
|
+
if (!binding)
|
|
57
|
+
return;
|
|
58
|
+
const result = await safeOperation(() => binding.applyNewsletterConsent(request.data));
|
|
59
|
+
if (!result)
|
|
60
|
+
return unavailable(res, respond);
|
|
61
|
+
if (result.ok === false)
|
|
62
|
+
return failure(res, respond, result.error.kind);
|
|
63
|
+
sendBffSuccess(res, result.value);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async function bindingOrError(res, respond, resolve) {
|
|
67
|
+
try {
|
|
68
|
+
const binding = await resolve();
|
|
69
|
+
if (binding)
|
|
70
|
+
return binding;
|
|
71
|
+
}
|
|
72
|
+
catch { /* fail closed below */ }
|
|
73
|
+
unavailable(res, respond);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
async function safeOperation(work) {
|
|
77
|
+
try {
|
|
78
|
+
return await work();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function failure(res, respond, kind) {
|
|
85
|
+
if (kind === "invalid")
|
|
86
|
+
return badRequest(res, respond);
|
|
87
|
+
if (kind === "not_found")
|
|
88
|
+
return respond.sendBffError(res, "NOT_FOUND", "Acquisition evidence subject not found");
|
|
89
|
+
if (kind === "conflict")
|
|
90
|
+
return respond.sendBffError(res, "CONFLICT", "Acquisition evidence request conflicts with prior state");
|
|
91
|
+
unavailable(res, respond);
|
|
92
|
+
}
|
|
93
|
+
function badRequest(res, respond, details) {
|
|
94
|
+
respond.sendBffError(res, "BAD_REQUEST", "Invalid acquisition evidence request", { details });
|
|
95
|
+
}
|
|
96
|
+
function unavailable(res, respond) {
|
|
97
|
+
respond.sendBffError(res, "UPSTREAM_UNAVAILABLE", "Acquisition evidence service unavailable");
|
|
98
|
+
}
|
|
99
|
+
function first(value) { return Array.isArray(value) ? value[0] : value; }
|
|
100
|
+
function firstHeader(value) { return first(value); }
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { HttpRequest, HttpResponse } from "../../http/index.js";
|
|
2
|
+
import type { AnalyticsContextHandlerDeps } from "./analyticsContextPorts.js";
|
|
3
|
+
export declare function createAnalyticsContextHandler(deps: AnalyticsContextHandlerDeps): (req: HttpRequest, res: HttpResponse) => Promise<void>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { z } from "../validation.js";
|
|
2
|
+
export function createAnalyticsContextHandler(deps) {
|
|
3
|
+
const { respond, isEnabled, resolveStore, readAccessToken, resolveTrustedSubject, capabilityCrypto, cookieName, clientIdField } = deps;
|
|
4
|
+
const { sendBffError, sendBffSuccess, sendMethodNotAllowed } = respond;
|
|
5
|
+
const requestSchema = z.object({
|
|
6
|
+
granted: z.boolean(),
|
|
7
|
+
[clientIdField]: z.string().regex(/^\d+\.\d+$/).max(64).optional(),
|
|
8
|
+
explicitGrant: z.boolean().default(false),
|
|
9
|
+
}).strict();
|
|
10
|
+
return async (req, res) => {
|
|
11
|
+
if (req.method !== "POST")
|
|
12
|
+
return sendMethodNotAllowed(res, ["POST"]);
|
|
13
|
+
if (!isEnabled())
|
|
14
|
+
return sendBffError(res, "NOT_FOUND", "Analytics context unavailable");
|
|
15
|
+
// No URL, IP, contact, session, form or account data is accepted or logged.
|
|
16
|
+
const origin = typeof req.headers.origin === "string" ? req.headers.origin : "";
|
|
17
|
+
if (!origin || origin !== `https://${req.headers.host}`)
|
|
18
|
+
return sendBffError(res, "FORBIDDEN", "Same-origin request required");
|
|
19
|
+
const parsed = requestSchema.safeParse(req.body);
|
|
20
|
+
const clientId = parsed.success ? parsed.data[clientIdField] : undefined;
|
|
21
|
+
if (!parsed.success || (parsed.data.granted && !clientId))
|
|
22
|
+
return sendBffError(res, "BAD_REQUEST", "Invalid analytics context");
|
|
23
|
+
const store = resolveStore();
|
|
24
|
+
if (!store)
|
|
25
|
+
return sendBffError(res, "UPSTREAM_UNAVAILABLE", "Analytics context unavailable");
|
|
26
|
+
// The computed shape key widens inference; the strict schema validates these fixed fields.
|
|
27
|
+
const data = parsed.data;
|
|
28
|
+
let hash = contextHash(typeof req.headers.cookie === "string" ? req.headers.cookie : undefined, cookieName, (value) => capabilityCrypto.hashToken(value));
|
|
29
|
+
// Explicit user re-consent gets a new capability; withdrawn old bindings stay withdrawn.
|
|
30
|
+
let token = null;
|
|
31
|
+
if (data.granted && !hash) {
|
|
32
|
+
token = capabilityCrypto.mintToken();
|
|
33
|
+
hash = capabilityCrypto.hashToken(token);
|
|
34
|
+
}
|
|
35
|
+
if (!hash) {
|
|
36
|
+
sendBffSuccess(res, { active: false });
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const accessToken = readAccessToken(req);
|
|
41
|
+
const subject = data.granted && accessToken ? await resolveTrustedSubject(accessToken) : null;
|
|
42
|
+
if (accessToken && data.granted && !subject)
|
|
43
|
+
return sendBffError(res, "UNAUTHORIZED", "Customer session required");
|
|
44
|
+
let active = await store.syncContext({ tokenHash: hash, granted: data.granted, clientId: clientId ?? null });
|
|
45
|
+
if (active !== true && data.granted && data.explicitGrant && !token) {
|
|
46
|
+
token = capabilityCrypto.mintToken();
|
|
47
|
+
hash = capabilityCrypto.hashToken(token);
|
|
48
|
+
active = await store.syncContext({ tokenHash: hash, granted: true, clientId: clientId });
|
|
49
|
+
}
|
|
50
|
+
if (active === true && subject)
|
|
51
|
+
await store.enrollSubscriptions({ subject, contextHash: hash });
|
|
52
|
+
if (token && active === true)
|
|
53
|
+
res.setHeader("Set-Cookie", `${cookieName}=${token}; Path=/; Max-Age=31536000; HttpOnly; Secure; SameSite=Lax`);
|
|
54
|
+
res.setHeader("Cache-Control", "no-store");
|
|
55
|
+
sendBffSuccess(res, { active: active === true });
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
sendBffError(res, "UPSTREAM_UNAVAILABLE", "Analytics context unavailable");
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function contextHash(cookie, cookieName, hashToken) {
|
|
63
|
+
const token = cookie?.split(";").map((part) => part.trim()).find((part) => part.startsWith(`${cookieName}=`))?.split("=")[1];
|
|
64
|
+
return token && /^[a-f0-9]{64}$/.test(token) ? hashToken(token) : null;
|
|
65
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { HttpRequest } from "../../http/index.js";
|
|
2
|
+
import type { BffResponder } from "../../bff/index.js";
|
|
3
|
+
/** Host persistence rejects on failure; only literal true means an active context. */
|
|
4
|
+
export interface AnalyticsContextPersistencePort {
|
|
5
|
+
syncContext(input: {
|
|
6
|
+
tokenHash: string;
|
|
7
|
+
granted: boolean;
|
|
8
|
+
clientId: string | null;
|
|
9
|
+
}): Promise<unknown>;
|
|
10
|
+
enrollSubscriptions(input: {
|
|
11
|
+
subject: string;
|
|
12
|
+
contextHash: string;
|
|
13
|
+
}): Promise<unknown>;
|
|
14
|
+
}
|
|
15
|
+
export type AnalyticsContextStoreResolver = () => AnalyticsContextPersistencePort | null;
|
|
16
|
+
/** The host supplies opaque 32-byte hexadecimal tokens and their cryptographic hash. */
|
|
17
|
+
export interface AnalyticsContextCapabilityCrypto {
|
|
18
|
+
mintToken(): string;
|
|
19
|
+
hashToken(token: string): string;
|
|
20
|
+
}
|
|
21
|
+
export interface AnalyticsContextHandlerDeps {
|
|
22
|
+
respond: BffResponder;
|
|
23
|
+
isEnabled: () => boolean;
|
|
24
|
+
resolveStore: AnalyticsContextStoreResolver;
|
|
25
|
+
readAccessToken: (req: HttpRequest) => string | null;
|
|
26
|
+
resolveTrustedSubject: (accessToken: string) => Promise<string | null>;
|
|
27
|
+
capabilityCrypto: AnalyticsContextCapabilityCrypto;
|
|
28
|
+
cookieName: string;
|
|
29
|
+
/** Historical request field name supplied by the host. */
|
|
30
|
+
clientIdField: string;
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReadinessIssue } from "@openlup/core/readiness";
|
|
2
|
+
import { type MarketingPrivacyContributor, type MarketingPrivacyDependencies } from "./privacyContributor.js";
|
|
3
|
+
/** Select one fully bound privacy contributor; absent activation leaves the host registry unchanged. */
|
|
4
|
+
export declare function createMarketingPrivacyBindings<Context>(input: MarketingPrivacyDependencies<Context> & {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}): {
|
|
7
|
+
contributor: MarketingPrivacyContributor<Context> | null;
|
|
8
|
+
issues: ReadinessIssue[];
|
|
9
|
+
callables: Readonly<{
|
|
10
|
+
exportPersonalData: MarketingPrivacyContributor<Context>["export"] | null;
|
|
11
|
+
assessErasure: MarketingPrivacyContributor<Context>["assessErasure"] | null;
|
|
12
|
+
eraseOrRetain: MarketingPrivacyContributor<Context>["eraseOrRetain"] | null;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createMarketingPrivacyContributor } from "./privacyContributor.js";
|
|
2
|
+
/** Select one fully bound privacy contributor; absent activation leaves the host registry unchanged. */
|
|
3
|
+
export function createMarketingPrivacyBindings(input) {
|
|
4
|
+
const acquisition = input.acquisition && [input.acquisition.export, input.acquisition.assessErasure, input.acquisition.eraseOrRetain].every(fn => typeof fn === "function") ? input.acquisition : null;
|
|
5
|
+
const remoteErasure = input.remoteErasure && [input.remoteErasure.read, input.remoteErasure.openAndFence].every(fn => typeof fn === "function") ? input.remoteErasure : null;
|
|
6
|
+
const issues = [];
|
|
7
|
+
for (const [name, port] of [["acquisition", acquisition], ["remoteErasure", remoteErasure]]) {
|
|
8
|
+
if (!port)
|
|
9
|
+
issues.push({ code: "OPENLUP_E_PORT_MISSING", package: "@openlup/platform", module: "marketing", subject: name, fix: `Bind the marketing ${name} owner port.` });
|
|
10
|
+
}
|
|
11
|
+
const selected = input.enabled === true && issues.length === 0
|
|
12
|
+
? createMarketingPrivacyContributor({ ...input, acquisition, remoteErasure }) : null;
|
|
13
|
+
return { contributor: selected, issues, callables: Object.freeze({
|
|
14
|
+
exportPersonalData: selected ? selected.export : null,
|
|
15
|
+
assessErasure: selected ? selected.assessErasure : null,
|
|
16
|
+
eraseOrRetain: selected ? selected.eraseOrRetain : null,
|
|
17
|
+
}) };
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HttpRequest, HttpResponse } from "../../http/index.js";
|
|
2
|
+
import type { BffResponder } from "../../bff/index.js";
|
|
3
|
+
import type { AcquisitionCaseHandlers } from "./acquisitionCaseHandlers.js";
|
|
4
|
+
import type { AcquisitionEvidencePort } from "../contracts/index.js";
|
|
5
|
+
export interface AcquisitionCaseReadBinding {
|
|
6
|
+
handlers: Pick<AcquisitionCaseHandlers, "list" | "get" | "activeCount">;
|
|
7
|
+
}
|
|
8
|
+
export type AcquisitionCaseBindingResolver = () => Promise<AcquisitionCaseReadBinding | null>;
|
|
9
|
+
export type AcquisitionEvidenceBindingResolver = () => Promise<AcquisitionEvidencePort | null>;
|
|
10
|
+
export type AcquisitionAuthorize = (req: HttpRequest, res: HttpResponse) => Promise<string | null>;
|
|
11
|
+
export interface AcquisitionCaseDirectDeps {
|
|
12
|
+
respond: BffResponder;
|
|
13
|
+
authorize: AcquisitionAuthorize;
|
|
14
|
+
resolveBinding: AcquisitionCaseBindingResolver;
|
|
15
|
+
isDirect: () => boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface PrelaunchAcquisitionDirectDeps {
|
|
18
|
+
respond: BffResponder;
|
|
19
|
+
authorize: AcquisitionAuthorize;
|
|
20
|
+
resolveBinding: AcquisitionCaseBindingResolver;
|
|
21
|
+
}
|
|
22
|
+
export interface AcquisitionEvidenceDirectDeps {
|
|
23
|
+
respond: BffResponder;
|
|
24
|
+
authorize: AcquisitionAuthorize;
|
|
25
|
+
resolveBinding: AcquisitionEvidenceBindingResolver;
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Node-only acquisition and research algorithms. Hosts own routing, authorization and presentation. */
|
|
2
|
+
export * from "./acquisitionCaseHandlers.js";
|
|
3
|
+
export type * from "./acquisitionCasePorts.js";
|
|
4
|
+
export type * from "./directPorts.js";
|
|
5
|
+
export * from "./acquisitionCaseDirect.js";
|
|
6
|
+
export * from "./prelaunchAcquisitionDirect.js";
|
|
7
|
+
export * from "./acquisitionEvidenceDirect.js";
|
|
8
|
+
export * from "./research/surveyResponseSubmitHandler.js";
|
|
9
|
+
export * from "./research/adminSurveyResponsesHandler.js";
|
|
10
|
+
export * from "./research/managedSurveyResponseSubmit.js";
|
|
11
|
+
export { createAnalyticsContextHandler } from "./analyticsContext.js";
|
|
12
|
+
export type * from "./analyticsContextPorts.js";
|
|
13
|
+
export * from "./privacyContributor.js";
|
|
14
|
+
export * from "./bindings.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Node-only acquisition and research algorithms. Hosts own routing, authorization and presentation. */
|
|
2
|
+
export * from "./acquisitionCaseHandlers.js";
|
|
3
|
+
export * from "./acquisitionCaseDirect.js";
|
|
4
|
+
export * from "./prelaunchAcquisitionDirect.js";
|
|
5
|
+
export * from "./acquisitionEvidenceDirect.js";
|
|
6
|
+
export * from "./research/surveyResponseSubmitHandler.js";
|
|
7
|
+
export * from "./research/adminSurveyResponsesHandler.js";
|
|
8
|
+
export * from "./research/managedSurveyResponseSubmit.js";
|
|
9
|
+
export { createAnalyticsContextHandler } from "./analyticsContext.js";
|
|
10
|
+
export * from "./privacyContributor.js";
|
|
11
|
+
export * from "./bindings.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Pool } from "pg";
|
|
2
|
+
import type { AcquisitionCasePort } from "../acquisitionCasePorts.js";
|
|
3
|
+
type Env = {
|
|
4
|
+
connectionString: string;
|
|
5
|
+
};
|
|
6
|
+
type Options = {
|
|
7
|
+
poolFactory?: (env: Env) => Pool | Promise<Pool>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createPostgresAcquisitionCasePort(env: Env, options?: Options): AcquisitionCasePort;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { Pool } from "pg";
|
|
3
|
+
import { ACQUISITION_CASE_CONTRACT_VERSION, acquisitionCaseListResponseSchema, acquisitionCaseProjectionSchema } from "../../acquisitionCaseSchemas.js";
|
|
4
|
+
const ROLE = "SET LOCAL ROLE platform_acquisition_runtime";
|
|
5
|
+
const SUBMIT = `SELECT public.acquisition_case_submit_v1(
|
|
6
|
+
$1::text,$2::text,$3::text,$4::text,$5::boolean,$6::text,$7::text,
|
|
7
|
+
$8::text,$9::text,$10::text,$11::text,$12::text,$13::text,$14::timestamptz
|
|
8
|
+
) AS response`;
|
|
9
|
+
const LIST = "SELECT public.acquisition_case_operator_list_v1($1::uuid,$2::timestamptz,$3::uuid,$4::integer) AS response";
|
|
10
|
+
const GET = "SELECT public.acquisition_case_operator_get_v1($1::uuid,$2::text) AS response";
|
|
11
|
+
const TRANSITION = `SELECT public.acquisition_case_transition_v1(
|
|
12
|
+
$1::uuid,$2::text,$3::bigint,$4::text,$5::text,$6::text,$7::timestamptz
|
|
13
|
+
) AS response`;
|
|
14
|
+
const ACTIVE_COUNT = "SELECT public.acquisition_case_active_count_v1() AS response";
|
|
15
|
+
export function createPostgresAcquisitionCasePort(env, options = {}) {
|
|
16
|
+
if (!env.connectionString.trim())
|
|
17
|
+
throw new Error("acquisition_case_database_url_required");
|
|
18
|
+
let poolPromise = null;
|
|
19
|
+
const pool = () => poolPromise ??= Promise.resolve(options.poolFactory ? options.poolFactory(env) : new Pool({ connectionString: env.connectionString }));
|
|
20
|
+
const call = async (sql, values) => withRole(await pool(), async (client) => {
|
|
21
|
+
const result = await client.query(sql, values);
|
|
22
|
+
return object(result.rows[0])?.response ?? null;
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
async submit(command) {
|
|
26
|
+
if (command.scope !== "public_tester_application" || command.sourceKind !== "tester_application") {
|
|
27
|
+
return failure("invalid");
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const request = command.request;
|
|
31
|
+
const response = object(await call(SUBMIT, [
|
|
32
|
+
command.idempotencyKey,
|
|
33
|
+
command.requesterKey,
|
|
34
|
+
request.contact.email.trim().toLowerCase(),
|
|
35
|
+
null,
|
|
36
|
+
request.consent.accepted,
|
|
37
|
+
request.consent.consentVersion,
|
|
38
|
+
request.consent.policyVersion,
|
|
39
|
+
request.consent.locale,
|
|
40
|
+
command.sourcePath,
|
|
41
|
+
request.addressReference.source,
|
|
42
|
+
request.addressReference.reference,
|
|
43
|
+
request.addressReference.revision,
|
|
44
|
+
request.addressReference.provenance,
|
|
45
|
+
command.acceptedAt,
|
|
46
|
+
]));
|
|
47
|
+
return projectionResult(response);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
return databaseFailure(error);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
async list(query) {
|
|
54
|
+
if (query.scope !== "public_tester_application")
|
|
55
|
+
return failure("invalid");
|
|
56
|
+
const cursor = decodeCursor(query.cursor);
|
|
57
|
+
if (cursor === undefined)
|
|
58
|
+
return failure("invalid");
|
|
59
|
+
try {
|
|
60
|
+
const response = object(await call(LIST, [
|
|
61
|
+
query.actorRef,
|
|
62
|
+
cursor?.createdAt ?? null,
|
|
63
|
+
cursor?.id ?? null,
|
|
64
|
+
query.limit,
|
|
65
|
+
]));
|
|
66
|
+
if (!response || !Array.isArray(response.cases))
|
|
67
|
+
return failure("unavailable");
|
|
68
|
+
const parsed = acquisitionCaseListResponseSchema.safeParse({
|
|
69
|
+
contractVersion: response.contractVersion ?? ACQUISITION_CASE_CONTRACT_VERSION,
|
|
70
|
+
cases: response.cases,
|
|
71
|
+
nextCursor: response.nextCursor === null ? null : encodeCursor(response.nextCursor),
|
|
72
|
+
});
|
|
73
|
+
return parsed.success ? { ok: true, value: parsed.data } : failure("unavailable");
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return databaseFailure(error);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async get(query) {
|
|
80
|
+
if (query.scope !== "public_tester_application")
|
|
81
|
+
return failure("invalid");
|
|
82
|
+
try {
|
|
83
|
+
const response = object(await call(GET, [query.actorRef, query.caseRef]));
|
|
84
|
+
if (response?.outcome === "not_found")
|
|
85
|
+
return failure("not_found");
|
|
86
|
+
return projectionResult(response);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
return databaseFailure(error);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
async transition(command) {
|
|
93
|
+
if (command.scope !== "public_tester_application")
|
|
94
|
+
return failure("invalid");
|
|
95
|
+
const target = transitionTarget(command);
|
|
96
|
+
try {
|
|
97
|
+
const response = object(await call(TRANSITION, [
|
|
98
|
+
command.actorRef,
|
|
99
|
+
command.request.caseRef,
|
|
100
|
+
command.request.expectedVersion,
|
|
101
|
+
target,
|
|
102
|
+
command.request.reason ?? null,
|
|
103
|
+
command.idempotencyKey,
|
|
104
|
+
new Date().toISOString(),
|
|
105
|
+
]));
|
|
106
|
+
return projectionResult(response);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
return databaseFailure(error);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
async activeCount(scope) {
|
|
113
|
+
if (scope !== "public_tester_application")
|
|
114
|
+
return failure("invalid");
|
|
115
|
+
try {
|
|
116
|
+
const raw = await call(ACTIVE_COUNT, []);
|
|
117
|
+
const count = typeof raw === "number" ? raw : Number(raw);
|
|
118
|
+
return Number.isSafeInteger(count) && count >= 0
|
|
119
|
+
? { ok: true, value: count }
|
|
120
|
+
: failure("unavailable");
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return databaseFailure(error);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
async close() {
|
|
127
|
+
if (poolPromise)
|
|
128
|
+
await (await poolPromise).end();
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function withRole(pool, work) {
|
|
133
|
+
const client = await pool.connect();
|
|
134
|
+
try {
|
|
135
|
+
await client.query("BEGIN");
|
|
136
|
+
await client.query(ROLE);
|
|
137
|
+
const value = await work(client);
|
|
138
|
+
await client.query("COMMIT");
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
await client.query("ROLLBACK").catch(() => { });
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
client.release();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function projectionResult(response) {
|
|
150
|
+
if (!response)
|
|
151
|
+
return failure("unavailable");
|
|
152
|
+
const outcome = response.outcome;
|
|
153
|
+
if (outcome === "created" || outcome === "replayed" || outcome === "transitioned" || outcome === "found") {
|
|
154
|
+
const parsed = acquisitionCaseProjectionSchema.safeParse(response.acquisitionCase);
|
|
155
|
+
return parsed.success
|
|
156
|
+
? { ok: true, value: parsed.data, replayed: response.replayed === true }
|
|
157
|
+
: failure("unavailable");
|
|
158
|
+
}
|
|
159
|
+
return failure(outcomeFailure(outcome));
|
|
160
|
+
}
|
|
161
|
+
function outcomeFailure(outcome) {
|
|
162
|
+
if (outcome === "invalid_address")
|
|
163
|
+
return "invalid";
|
|
164
|
+
if (outcome === "rate_limited")
|
|
165
|
+
return "rate_limited";
|
|
166
|
+
if (outcome === "not_found")
|
|
167
|
+
return "not_found";
|
|
168
|
+
if (outcome === "idempotency_conflict" || outcome === "contact_conflict"
|
|
169
|
+
|| outcome === "version_conflict" || outcome === "transition_conflict")
|
|
170
|
+
return "conflict";
|
|
171
|
+
return "unavailable";
|
|
172
|
+
}
|
|
173
|
+
function databaseFailure(error) {
|
|
174
|
+
return failure(object(error)?.code === "22023" ? "invalid" : "unavailable");
|
|
175
|
+
}
|
|
176
|
+
function failure(kind) {
|
|
177
|
+
return { ok: false, error: { kind } };
|
|
178
|
+
}
|
|
179
|
+
function transitionTarget(command) {
|
|
180
|
+
return { approve: "approved", activate: "active", reject: "rejected", withdraw: "withdrawn" }[command.request.transition];
|
|
181
|
+
}
|
|
182
|
+
function encodeCursor(value) {
|
|
183
|
+
const cursor = object(value);
|
|
184
|
+
if (!cursor || typeof cursor.createdAt !== "string" || typeof cursor.id !== "string") {
|
|
185
|
+
throw new Error("acquisition_case_cursor_invalid");
|
|
186
|
+
}
|
|
187
|
+
return Buffer.from(JSON.stringify({ createdAt: cursor.createdAt, id: cursor.id }), "utf8").toString("base64url");
|
|
188
|
+
}
|
|
189
|
+
function decodeCursor(value) {
|
|
190
|
+
if (value === undefined)
|
|
191
|
+
return null;
|
|
192
|
+
try {
|
|
193
|
+
const parsed = object(JSON.parse(Buffer.from(value, "base64url").toString("utf8")));
|
|
194
|
+
return parsed && typeof parsed.createdAt === "string" && typeof parsed.id === "string"
|
|
195
|
+
? { createdAt: parsed.createdAt, id: parsed.id }
|
|
196
|
+
: undefined;
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function object(value) {
|
|
203
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
204
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Pool } from "pg";
|
|
2
|
+
import type { AcquisitionEvidencePort } from "../../contracts/index.js";
|
|
3
|
+
type Env = {
|
|
4
|
+
connectionString: string;
|
|
5
|
+
};
|
|
6
|
+
type Options = {
|
|
7
|
+
poolFactory?: (env: Env) => Pool | Promise<Pool>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createPostgresAcquisitionEvidencePort(env: Env, options?: Options): AcquisitionEvidencePort;
|
|
10
|
+
export {};
|