All files / src/lib agreement.ts

94.11% Statements 32/34
82.81% Branches 53/64
100% Functions 5/5
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                  1x 1x                     8x         8x       3x 1x 1x           10x 3x                       5x 5x 5x 4x 4x 4x 4x 3x 4x 4x 2x       1x 1x 1x       2x           2x 2x 2x 2x                    
/**
 * A contract agreement as the transfer page shows it (#876): when it was
 * signed, until when it allows use, and the policy both parties agreed to.
 */
 
export interface AgreementView {
  agreementId: string;
  assetId: string;
  providerId: string;
  consumerId: string;
  /** ISO time the connectors signed it, or "" when the connector gives none. */
  signedAt: string;
  /** ISO time the agreed period ends, or "" when the policy sets none. */
  validUntil: string;
  policy: Record<string, unknown>;
}
 
const DAY_MS = 86_400_000;
const UNIT_MS: Record<string, number> = {
  s: 1000,
  m: 60_000,
  h: 3_600_000,
  d: DAY_MS,
  w: 7 * DAY_MS,
};
 
/** "inForceDate", "edc:inForceDate", or the full IRI, as its local name. */
function localName(v: unknown): string {
  const raw =
    typeof v === "string"
      ? v
      : v && typeof v === "object" && "@id" in v
        ? String((v as { "@id": unknown })["@id"])
        : "";
  return raw.split(/[:/#]/).pop() ?? "";
}
 
function value(v: unknown): string {
  if (typeof v === "string" || typeof v === "number") return String(v);
  Eif (v && typeof v === "object" && "@value" in v) {
    return String((v as { "@value": unknown })["@value"]);
  }
  return "";
}
 
function list(v: unknown): Record<string, unknown>[] {
  if (Array.isArray(v)) return v as Record<string, unknown>[];
  return v && typeof v === "object" ? [v as Record<string, unknown>] : [];
}
 
/**
 * The end of the period the policy allows, counted from the signing:
 * `contractAgreement+365d` is a year after it, a date is that date. Empty
 * when no permission sets an upper limit on inForceDate or dateTime.
 */
export function agreementValidUntil(
  policy: Record<string, unknown>,
  signedAtMs: number,
): string {
  for (const perm of list(policy.permission ?? policy["odrl:permission"])) {
    const constraints = list(perm.constraint ?? perm["odrl:constraint"]);
    for (const c of constraints) {
      const left = localName(c.leftOperand ?? c["odrl:leftOperand"]);
      const op = localName(c.operator ?? c["odrl:operator"]);
      Iif (!["inForceDate", "dateTime"].includes(left)) continue;
      if (!["lteq", "lt", "eq"].includes(op)) continue;
      const right = value(c.rightOperand ?? c["odrl:rightOperand"]).trim();
      const period = /^contractAgreement\s*\+\s*(\d+)\s*([smhdw])$/.exec(right);
      if (period && signedAtMs) {
        return new Date(
          signedAtMs + Number(period[1]) * UNIT_MS[period[2]],
        ).toISOString();
      }
      Eif (/^\d{4}-\d{2}-\d{2}/.test(right)) {
        const d = new Date(right);
        Eif (!Number.isNaN(d.getTime())) return d.toISOString();
      }
    }
  }
  return "";
}
 
/** The connector's agreement in the shape the page reads. */
export function toAgreementView(raw: Record<string, unknown>): AgreementView {
  // EDC gives the signing time in seconds since the epoch.
  const signed = Number(raw.contractSigningDate ?? 0);
  const signedAtMs = signed > 0 && signed < 1e12 ? signed * 1000 : signed;
  const policy = (raw.policy ?? {}) as Record<string, unknown>;
  return {
    agreementId: String(raw["@id"] ?? ""),
    assetId: String(raw.assetId ?? ""),
    providerId: String(raw.providerId ?? ""),
    consumerId: String(raw.consumerId ?? ""),
    signedAt: signedAtMs ? new Date(signedAtMs).toISOString() : "",
    validUntil: agreementValidUntil(policy, signedAtMs),
    policy,
  };
}