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


(async () => {
    const result = [];
    const byForm = {};

    for (const data of list) {
        try {
            const id = data.eventSessionId || data.eventId;
            const resourceType = data.eventSessionId
                ? 'event-sessions'
                : 'events';

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

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

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

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

            const url = fillForm?.formUrl || null;

            let formLinkId = null;

            if (url) {
                const match = url.match(/form-passing\/([^/]+)\//);
                formLinkId = match ? match[1] : null;
            }

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

            if (!byForm[data.formId]) {
                byForm[data.formId] = {
                    total: 0,
                    ok: 0,
                    uniqueLinks: new Set()
                };
            }

            byForm[data.formId].total++;

            if (url) {
                byForm[data.formId].ok++;

                if (formLinkId) {
                    byForm[data.formId].uniqueLinks.add(formLinkId);
                }
            }

        } catch (e) {
            result.push({
                id: data.eventSessionId || data.eventId,
                formId: data.formId,
                status: 'ERROR',
                error: String(e)
            });
        }
    }

    const summary = {};

    for (const [formId, stat] of Object.entries(byForm)) {
        summary[formId] = {
            totalEvents: stat.total,
            withCertificateForm: stat.ok,
            uniqueFormLinks: stat.uniqueLinks.size
        };
    }

    console.table(summary);

    console.table(
        result.filter(x => x.status !== 'OK')
    );

    window.certificateAudit = result;

    console.log(
        'Полный результат сохранён в window.certificateAudit'
    );
})();