All files / src/utils/features teacherMadeAssignmentQuota.js

99.04% Statements 104/105
97.12% Branches 135/139
100% Functions 12/12
100% Lines 92/92

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238    13x 13x     13x 8x     13x                           13x 312x 238x 238x     13x 11x         13x 28x   24x 13x 12x 11x 10x 9x 7x   7x       13x 169x   156x 156x   169x 74x                 82x 1x                 81x 81x   81x                 13x 13x 13x       13x 50x   45x 28x 25x     28x               13x 145x   139x   145x 74x               65x 3x     62x               13x 147x 12x     135x 10x             125x 74x     51x             13x 23x 1x     22x 22x 23x 23x     23x         23x 4x     18x 10x 10x         22x                   13x 16x 16x   16x       16x   16x 16x 16x   16x   16x 16x 5x 1x 4x 2x   2x   11x 3x 8x 7x     16x                
import { getFeatureMeta, isFeatureEnabled } from "@/utils/features/featureAccess";
 
export const TEACHER_MADE_TESTS_UNLIMITED_KEY = "teacher.teacher_made_tests.unlimited";
export const TEACHER_MADE_TESTS_LIMIT_10_KEY = "teacher.teacher_made_tests.limit_10";
 
/** Summary analytics API — requires limit_10 or unlimited (not item_analysis). */
export const isTeacherMadeAssignmentAnalyticsEnabled = (features) =>
    isFeatureEnabled(features, TEACHER_MADE_TESTS_LIMIT_10_KEY) ||
    isFeatureEnabled(features, TEACHER_MADE_TESTS_UNLIMITED_KEY);
 
export const TEACHER_MADE_ASSIGNMENT_BLOCK_REASONS = {
    NOT_ENABLED: "teacher_made_assignments_not_enabled",
    LIMIT_REACHED: "teacher_made_assignment_limit_reached",
};
 
/**
 * @typedef {Object} TeacherMadeAssignmentQuota
 * @property {boolean} can_create
 * @property {number|null} max_assignments
 * @property {number} current_count
 * @property {number|null} remaining
 * @property {string|null} enabled_feature
 */
 
const toNumberOrNull = (value) => {
    if (value === null || value === undefined || value === "") return null;
    const num = Number(value);
    return Number.isFinite(num) ? num : null;
};
 
const isQuotaShape = (value) =>
    value &&
    typeof value === "object" &&
    ("can_create" in value || "max_assignments" in value || "current_count" in value);
 
/** Pick quota object from API responses (effective features, quota endpoint, create error, etc.). */
export const pickTeacherMadeAssignmentQuotaRaw = (payload) => {
    if (!payload || typeof payload !== "object") return null;
 
    if (payload.teacher_made_assignments) return payload.teacher_made_assignments;
    if (payload.teacherMadeAssignments) return payload.teacherMadeAssignments;
    if (payload.data?.teacher_made_assignments) return payload.data.teacher_made_assignments;
    if (payload.data?.teacherMadeAssignments) return payload.data.teacherMadeAssignments;
    if (payload.quota && isQuotaShape(payload.quota)) return payload.quota;
    if (isQuotaShape(payload)) return payload;
    Iif (payload.data && isQuotaShape(payload.data)) return payload.data;
 
    return null;
};
 
/** Normalize quota from dedicated quota API (preferred) or nested response fields. */
export const normalizeTeacherMadeAssignmentQuota = (raw) => {
    if (!raw || typeof raw !== "object") return null;
 
    const maxAssignments = toNumberOrNull(raw.max_assignments);
    const currentCount = toNumberOrNull(raw.current_count) ?? 0;
 
    if (maxAssignments === null) {
        return {
            can_create: raw.can_create !== false,
            max_assignments: null,
            current_count: currentCount,
            remaining: null,
            enabled_feature: raw.enabled_feature ?? null,
        };
    }
 
    if (maxAssignments === 0) {
        return {
            can_create: false,
            max_assignments: 0,
            current_count: currentCount,
            remaining: 0,
            enabled_feature: raw.enabled_feature ?? null,
        };
    }
 
    const remaining = Math.max(0, maxAssignments - currentCount);
    const canCreate = currentCount < maxAssignments;
 
    return {
        can_create: canCreate,
        max_assignments: maxAssignments,
        current_count: currentCount,
        remaining,
        enabled_feature: raw.enabled_feature ?? null,
    };
};
 
export const extractTeacherMadeAssignmentQuotaFromPayload = (payload) => {
    const raw = pickTeacherMadeAssignmentQuotaRaw(payload);
    return raw ? normalizeTeacherMadeAssignmentQuota(raw) : null;
};
 
/** Live teacher-made assignments in one class (current category, not deleted). */
export const countClassTeacherMadeAssignments = (assignments) => {
    if (!Array.isArray(assignments)) return 0;
 
    return assignments.filter((assignment) => {
        if (assignment?.deleted === true) return false;
        const category = String(assignment?.category ?? "current")
            .trim()
            .toLowerCase();
        return !category || category === "current";
    }).length;
};
 
/**
 * Apply plan entitlement from API with per-class usage (limit is per class, per teacher).
 * `planQuota.max_assignments` is the cap for each class; `classAssignmentCount` is this class only.
 */
export const resolveClassTeacherMadeAssignmentQuota = (planQuota, classAssignmentCount = 0) => {
    if (!planQuota) return null;
 
    const classCount = Math.max(0, Number(classAssignmentCount) || 0);
 
    if (planQuota.max_assignments === null) {
        return normalizeTeacherMadeAssignmentQuota({
            max_assignments: null,
            current_count: classCount,
            enabled_feature: planQuota.enabled_feature,
            can_create: true,
        });
    }
 
    if (planQuota.max_assignments === 0) {
        return { ...planQuota, current_count: classCount, remaining: 0, can_create: false };
    }
 
    return normalizeTeacherMadeAssignmentQuota({
        max_assignments: planQuota.max_assignments,
        current_count: classCount,
        enabled_feature: planQuota.enabled_feature,
    });
};
 
/** UI state for create button / banners — trust API quota over client counting. */
export const getTeacherMadeAssignmentCreateUi = (quota) => {
    if (!quota) {
        return { showCreate: true, message: null, upgradeRequired: false };
    }
 
    if (quota.max_assignments === 0 || !quota.can_create) {
        return {
            showCreate: false,
            message: "Upgrade to create assignments",
            upgradeRequired: true,
        };
    }
 
    if (quota.max_assignments === null) {
        return { showCreate: true, message: null, upgradeRequired: false };
    }
 
    return {
        showCreate: quota.can_create,
        message: `${quota.current_count} of ${quota.max_assignments} assignments used in this class`,
        upgradeRequired: !quota.can_create,
    };
};
 
export const parseTeacherMadeAssignmentError = (errorPayload) => {
    if (!errorPayload) {
        return { reason: null, message: "Failed to create assignment", quota: null };
    }
 
    const detail = errorPayload.detail;
    const detailObj = detail && typeof detail === "object" ? detail : null;
    const reason = detailObj?.reason ?? errorPayload.reason ?? null;
    const quota = normalizeTeacherMadeAssignmentQuota(detailObj?.quota ?? errorPayload.quota);
 
    let message =
        (typeof detail === "string" && detail) ||
        detailObj?.message ||
        errorPayload.message ||
        "Failed to create assignment";
 
    if (reason === TEACHER_MADE_ASSIGNMENT_BLOCK_REASONS.NOT_ENABLED) {
        message =
            detailObj?.hint ||
            "Your plan does not include teacher-made assignments. Contact your district administrator to upgrade.";
    } else if (reason === TEACHER_MADE_ASSIGNMENT_BLOCK_REASONS.LIMIT_REACHED) {
        const max = quota?.max_assignments ?? 10;
        message =
            detailObj?.hint ||
            `Teacher-made assignment limit reached (${max} per class). Delete an assignment in this class before creating a new one.`;
    }
 
    return {
        reason,
        message,
        quota,
        upgradeRequired:
            reason === TEACHER_MADE_ASSIGNMENT_BLOCK_REASONS.NOT_ENABLED ||
            (reason === TEACHER_MADE_ASSIGNMENT_BLOCK_REASONS.LIMIT_REACHED && quota && !quota.can_create),
    };
};
 
export const getUpgradeDialogOptionsForQuota = (quota, features = [], options = {}) => {
    const { intent = "create" } = options;
    const enabledKey = quota?.enabled_feature;
    const preferredKey =
        enabledKey === TEACHER_MADE_TESTS_LIMIT_10_KEY
            ? TEACHER_MADE_TESTS_UNLIMITED_KEY
            : TEACHER_MADE_TESTS_UNLIMITED_KEY;
 
    const featureMeta = getFeatureMeta(features, preferredKey) || getFeatureMeta(features, TEACHER_MADE_TESTS_LIMIT_10_KEY);
 
    const notEnabled = !quota || quota.max_assignments === 0;
    const atLimit = !notEnabled && quota.max_assignments != null && !quota.can_create;
    const featureKey = atLimit ? TEACHER_MADE_TESTS_UNLIMITED_KEY : TEACHER_MADE_TESTS_LIMIT_10_KEY;
 
    const defaultFeatureLabel = intent === "reuse" ? "Re-use assignment" : "Teacher-made assignments";
 
    let title = "Create assignments on a higher plan";
    if (intent === "reuse") {
        if (notEnabled) {
            title = "Re-use assignment is not on your plan";
        } else if (atLimit) {
            title = "Assignment limit reached";
        } else {
            title = "Upgrade to re-use this assignment";
        }
    } else if (notEnabled) {
        title = "Assignment creation is not on your plan";
    } else if (atLimit) {
        title = "Assignment limit reached";
    }
 
    return {
        title,
        featureKey,
        featureLabel: defaultFeatureLabel,
        featureMeta,
        minPlan: featureMeta?.min_plan ?? (atLimit ? "premium" : "intermediate"),
    };
};