All files / webapp/model supportEndThresholds.ts

91.66% Statements 11/12
62.5% Branches 5/8
100% Functions 3/3
91.66% Lines 11/12

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                            47x           47x                           1970x 1970x 1970x                         77x 10x     67x       67x 84x     67x    
/** One "support end approaching" threshold: within `months`, highlight as `state`. */
export interface SupportEndThreshold {
    months: number;
    state: "Error" | "Warning";
}
 
/**
 * Central, configurable color coding for "support end approaching"
 * highlighting. Used by the product detail page (lifecycle milestones), the
 * row highlighting in the product table (Main view) and the support status
 * view. Order matters: the first matching (smallest) threshold wins. A
 * date that has already passed automatically gets the most urgent (first)
 * state.
 */
export const SUPPORT_END_THRESHOLDS: SupportEndThreshold[] = [
    { months: 6, state: "Error" },
    { months: 12, state: "Warning" }
];
 
/** Display color per `sap.ui.core.ValueState` name used for the thresholds above. */
export const SUPPORT_END_STATE_COLOR: Record<string, string> = {
    Error: "#dc2626",
    Warning: "#f59e0b",
    None: "#9ca3af"
};
 
/**
 * Returns the end of the period that is `months` months from `today`: the
 * last day of the target month, at 23:59:59.999.
 * @param today The reference date
 * @param months How many months from today the period extends
 * @returns The end of that period
 */
export function getRelativePeriodEnd(today: Date, months: number): Date {
    const periodEnd = new Date(today.getFullYear(), today.getMonth() + months + 1, 0);
    periodEnd.setHours(23, 59, 59, 999);
    return periodEnd;
}
 
/**
 * Returns the ValueState ("Error"/"Warning"/"None") for a date relative to
 * "today", according to {@link SUPPORT_END_THRESHOLDS}. A date that has
 * already passed is automatically treated as the most urgent case (first
 * threshold).
 * @param date The date to classify, or `null` if none is set
 * @param today The reference date
 * @returns The matching ValueState name, or "None" if no threshold matches
 */
export function getSupportEndState(date: Date | null, today: Date): string {
    if (date === null) {
        return "None";
    }
 
    Iif (date.getTime() < today.getTime()) {
        return SUPPORT_END_THRESHOLDS[0]?.state ?? "Error";
    }
 
    const match = SUPPORT_END_THRESHOLDS.find((threshold) => {
        return date.getTime() <= getRelativePeriodEnd(today, threshold.months).getTime();
    });
 
    return match?.state ?? "None";
}