All files / src/app/teacher-page/components/class-roster studentProfileLookup.js

100% Statements 10/10
100% Branches 13/13
100% Functions 5/5
100% Lines 8/8

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                                                19x                                 26x 9x   17x 20x                   11x 9x             9x      
/**
 * Pure helpers for the class-roster student profile lookup.
 *
 * Extracted from ClassRoster.jsx so the matching rule can be unit tested. The
 * roster renders through MaterialReactTable, and reaching this logic through a
 * rendered row is not practical in jsdom — while the rule itself is the thing
 * that stops a teacher being shown the WRONG student's records.
 *
 * Developer: Allan Ninal
 */
 
import { API_ENDPOINTS } from "@/api/endpoints";
 
/**
 * Builds the account-lookup URL for a student email.
 *
 * The email MUST be encoded: a plus-addressed address (`ada+test@example.com`)
 * is otherwise read by the server as `ada test@example.com`, so the exact-match
 * check below can never succeed and the teacher is told no student was found.
 *
 * @param {string} email
 * @returns {string}
 */
export function buildStudentLookupUrl(email) {
    return `${API_ENDPOINTS.teacher.account.find}?email=${encodeURIComponent(email ?? "")}&role=student`;
}
 
/**
 * Picks the student whose email matches EXACTLY (case-insensitively).
 *
 * The API search is fuzzy — looking up `ada@example.com` can return
 * `adam@example.com` — so trusting the first result would show one student
 * another student's name, photo, email and class list. Returns null when there
 * is no exact match, which the caller surfaces as "no exact match found" rather
 * than opening a profile.
 *
 * @param {Array<{email?: string}>|null|undefined} results
 * @param {string} email
 * @returns {object|null}
 */
export function findExactStudentByEmail(results, email) {
    if (!Array.isArray(results) || typeof email !== "string" || email.trim() === "") {
        return null;
    }
    const target = email.trim().toLowerCase();
    return results.find((student) => student?.email?.trim?.().toLowerCase() === target) ?? null;
}
 
/**
 * Maps a matched account onto the shape the profile dialog renders.
 *
 * @param {object|null|undefined} student
 * @returns {object|null}
 */
export function toStudentProfile(student) {
    if (!student) return null;
    return {
        first_name: student.first_name,
        middle_name: student.middle_name,
        last_name: student.last_name,
        status: student.status,
        email: student.email,
        photo: student.profile_picture,
        subjects: (student.classes ?? []).map((cls, index) => ({ id: index, title: cls?.title })),
    };
}