import { ParmanaClient, ParmanaApiError } from "@parmanasystems/sdk-client";
const client = new ParmanaClient({
baseUrl: process.env.PARMANA_URL ?? "http://localhost:3000",
apiKey: process.env.PARMANA_API_KEY,
});
interface RefundSignals {
refundAmount: number;
orderAge: number; // days since order
customerRefundCount: number; // refunds in last 90 days
refundReason: string;
originalOrderAmount: number;
accountStanding: "good" | "warned" | "suspended";
}
interface RefundResult {
refundId: string;
approved: boolean;
pendingSupervisor: boolean;
action: string;
reason: string;
attestationSignature: string;
}
async function requestRefund(
refundId: string,
signals: RefundSignals
): Promise<RefundResult> {
const attestation = await client.execute({
executionId: refundId,
policyId: "refund-approval",
policyVersion: "1.0.0",
signals,
});
// Verify — do not process refund without a valid attestation
const verification = await client.verify(attestation);
if (!verification.valid) {
throw new Error(`Attestation verification failed for refund ${refundId}`);
}
return {
refundId,
approved:
attestation.execution_state === "completed" &&
attestation.decision.action === "approve",
pendingSupervisor:
attestation.execution_state === "pending_override",
action: attestation.decision.action,
reason: attestation.decision.reason,
attestationSignature: attestation.signature,
};
}
async function supervisorApproveRefund(
refundId: string,
supervisorId: string,
reason: string
) {
return client._request<{
status: string;
overrideId: string;
}>("/override", {
method: "POST",
body: JSON.stringify({
executionId: refundId,
approved: true,
approvedBy: supervisorId,
approverRole: "customer_service_supervisor",
reason,
}),
});
}
async function supervisorDenyRefund(
refundId: string,
supervisorId: string,
reason: string
) {
return client._request<{
status: string;
executionId: string;
}>("/override", {
method: "POST",
body: JSON.stringify({
executionId: refundId,
approved: false,
approvedBy: supervisorId,
approverRole: "customer_service_supervisor",
reason,
}),
});
}
// Example usage
async function main() {
// Auto-approved
const small = await requestRefund("REFUND-2024-00881", {
refundAmount: 35,
orderAge: 5,
customerRefundCount: 0,
refundReason: "wrong_item",
originalOrderAmount: 35,
accountStanding: "good",
});
console.log(small.action); // "approve"
console.log(small.approved); // true
// Denied: order too old
const expired = await requestRefund("REFUND-2024-00882", {
refundAmount: 80,
orderAge: 120,
customerRefundCount: 1,
refundReason: "defective",
originalOrderAmount: 80,
accountStanding: "good",
});
console.log(expired.action); // "reject"
console.log(expired.approved); // false
// Pending supervisor: high value
const highValue = await requestRefund("REFUND-2024-00883", {
refundAmount: 750,
orderAge: 10,
customerRefundCount: 1,
refundReason: "not_as_described",
originalOrderAmount: 750,
accountStanding: "good",
});
console.log(highValue.action); // "supervisor_review"
console.log(highValue.pendingSupervisor); // true
// Supervisor approves
if (highValue.pendingSupervisor) {
const approved = await supervisorApproveRefund(
"REFUND-2024-00883",
"supervisor-maria-santos",
"Customer provided photos. Defect confirmed. Refund approved."
);
console.log(approved.status); // "approved"
}
}
main().catch(console.error);