Загрузка данных


window.certificateAudit = [];

window.certificateAuditPromise = (async () => {
    console.log(`AUDIT START. Всего записей: ${list.length}`);

    const result = window.certificateAudit;

    for (let i = 0; i < list.length; i++) {
        const data = list[i];

        const id = data.eventSessionId || data.eventId;
        const resourceType = data.eventSessionId
            ? 'event-sessions'
            : 'events';

        try {
            const response = await fetch(
                `/api/${resourceType}/${id}/certificate-settings`,
                {
                    credentials: 'include'
                }
            );

            if (!response.ok) {
                result.push({
                    index: i + 1,
                    id,
                    formId: data.formId,
                    status: 'HTTP_ERROR',
                    http: response.status
                });

                console.error(
                    `[${i + 1}/${list.length}] ${id}: HTTP ${response.status}`
                );

                continue;
            }

            const json = await response.json();
            const settings = json.data;

            const fillForm = settings?.issueConditions?.find(
                c => c.type === 'fillForm'
            );

            const formUrl = fillForm?.formUrl || null;

            let formLinkId = null;

            if (formUrl) {
                const match = formUrl.match(
                    /\/form-passing\/([^/]+)\/view/
                );

                if (match) {
                    formLinkId = match[1];
                }
            }

            result.push({
                index: i + 1,
                id,
                formId: data.formId,
                status: formUrl ? 'OK' : 'NO_FILL_FORM',
                formLinkId,
                formUrl
            });

            console.log(
                `[${i + 1}/${list.length}]`,
                id,
                formUrl ? 'OK' : 'NO_FILL_FORM',
                formLinkId || ''
            );

        } catch (e) {
            result.push({
                index: i + 1,
                id,
                formId: data.formId,
                status: 'ERROR',
                error: String(e)
            });

            console.error(
                `[${i + 1}/${list.length}] ${id}:`,
                e
            );
        }
    }

    const ok = result.filter(x => x.status === 'OK');
    const failed = result.filter(x => x.status !== 'OK');

    const uniqueLinks = new Set(
        ok
            .map(x => x.formLinkId)
            .filter(Boolean)
    );

    console.log('==============================');
    console.log('AUDIT FINISHED');
    console.log('Всего:', result.length);
    console.log('OK:', ok.length);
    console.log('Ошибки / без формы:', failed.length);
    console.log('Уникальных formLinkId:', uniqueLinks.size);
    console.log('==============================');

    console.table(failed);

    return {
        total: result.length,
        ok: ok.length,
        failed: failed.length,
        uniqueLinks: uniqueLinks.size,
        result
    };
})();

window.certificateAuditPromise.then(summary => {
    console.log('ИТОГ:', summary);
});