|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 3 | + * SPDX-License-Identifier: EUPL-1.2 |
| 4 | + */ |
| 5 | +/** |
| 6 | + * The facts of the organisation sync card on the Data tab (board |
| 7 | + * SkSettingsData): the schedule, the last run, the next run and the last |
| 8 | + * run's result, from what `GET /api/sync/status` answers. |
| 9 | + * |
| 10 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 11 | + */ |
| 12 | + |
| 13 | +import axios from '@nextcloud/axios' |
| 14 | +import { getLanguage, translate as t } from '@nextcloud/l10n' |
| 15 | +import { generateUrl } from '@nextcloud/router' |
| 16 | + |
| 17 | +/** |
| 18 | + * Read the organisation sync's schedule and last run. |
| 19 | + * |
| 20 | + * @return {Promise<{interval: number, enabled: boolean, lastRun: object|null}>} |
| 21 | + * @throws {Error} With the server's message when the request fails. |
| 22 | + * |
| 23 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 24 | + */ |
| 25 | +export async function fetchSyncFacts() { |
| 26 | + try { |
| 27 | + const response = await axios.get(generateUrl('/apps/stackiq/api/sync/status')) |
| 28 | + return response.data |
| 29 | + } catch (error) { |
| 30 | + throw new Error(error?.response?.data?.message || t('stackiq', 'The synchronisation status could not be read'), { cause: error }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A server time (`Y-m-d H:i:s`, the server's clock) as a Date. |
| 36 | + * |
| 37 | + * @param {string} value The time. |
| 38 | + * @return {Date|null} |
| 39 | + */ |
| 40 | +function parseServerTime(value) { |
| 41 | + if (typeof value !== 'string' || value === '') { |
| 42 | + return null |
| 43 | + } |
| 44 | + const date = new Date(value.includes('T') ? value : value.replace(' ', 'T')) |
| 45 | + return Number.isNaN(date.getTime()) ? null : date |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * The interval as words: "Every hour", "Every 6 hours". |
| 50 | + * |
| 51 | + * @param {number} seconds The interval. |
| 52 | + * @return {string} |
| 53 | + * |
| 54 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 55 | + */ |
| 56 | +export function scheduleLabel(seconds) { |
| 57 | + const value = Number(seconds) || 0 |
| 58 | + if (value <= 0) { |
| 59 | + return t('stackiq', 'Not scheduled') |
| 60 | + } |
| 61 | + if (value === 3600) { |
| 62 | + return t('stackiq', 'Every hour') |
| 63 | + } |
| 64 | + if (value % 3600 === 0) { |
| 65 | + return t('stackiq', 'Every {hours} hours', { hours: value / 3600 }) |
| 66 | + } |
| 67 | + return t('stackiq', 'Every {minutes} minutes', { minutes: Math.round(value / 60) }) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * The last run as the board writes it: "30 Nov 2026, 10:00 · 2 minutes · 412 organisations, 1,034 contact persons". |
| 72 | + * |
| 73 | + * @param {object|null} lastRun The last run record. |
| 74 | + * @param {string} [locale] The locale for the time and the numbers. |
| 75 | + * @return {string} |
| 76 | + * |
| 77 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 78 | + */ |
| 79 | +export function lastRunLine(lastRun, locale = getLanguage()) { |
| 80 | + const finished = parseServerTime(lastRun?.finishedAt) |
| 81 | + if (finished === null) { |
| 82 | + return t('stackiq', 'The sync has not run yet.') |
| 83 | + } |
| 84 | + const parts = [finished.toLocaleString(locale, { dateStyle: 'medium', timeStyle: 'short' })] |
| 85 | + const started = parseServerTime(lastRun.startedAt) |
| 86 | + if (started !== null && finished >= started) { |
| 87 | + const seconds = Math.round((finished - started) / 1000) |
| 88 | + parts.push(seconds < 60 |
| 89 | + ? t('stackiq', '{seconds} s', { seconds }) |
| 90 | + : t('stackiq', '{minutes} min', { minutes: Math.round(seconds / 60) })) |
| 91 | + } |
| 92 | + if (lastRun.organizationsProcessed !== undefined) { |
| 93 | + parts.push(t('stackiq', '{organisations} organisations, {contacts} contact persons', { |
| 94 | + organisations: Number(lastRun.organizationsProcessed || 0).toLocaleString(locale), |
| 95 | + contacts: Number(lastRun.contactPersonsProcessed || 0).toLocaleString(locale), |
| 96 | + })) |
| 97 | + } |
| 98 | + return parts.join(' · ') |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * When the next scheduled run is due: the last run's end plus the interval, or null when it is off or never ran. |
| 103 | + * |
| 104 | + * @param {object} facts What the status answered. |
| 105 | + * @param {string} [locale] The locale for the time. |
| 106 | + * @return {string} |
| 107 | + * |
| 108 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 109 | + */ |
| 110 | +export function nextRunLine(facts, locale = getLanguage()) { |
| 111 | + if (!facts?.enabled) { |
| 112 | + return t('stackiq', 'Off') |
| 113 | + } |
| 114 | + const finished = parseServerTime(facts.lastRun?.finishedAt) |
| 115 | + const interval = Number(facts.interval) || 0 |
| 116 | + if (finished === null || interval <= 0) { |
| 117 | + return t('stackiq', 'At the next cron run') |
| 118 | + } |
| 119 | + return new Date(finished.getTime() + interval * 1000).toLocaleString(locale, { dateStyle: 'medium', timeStyle: 'short' }) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * The last run's result: "Last run completed: 3 new, 11 updated, 0 errors", or an empty string without a counted run. |
| 124 | + * |
| 125 | + * @param {object|null} lastRun The last run record. |
| 126 | + * @return {string} |
| 127 | + * |
| 128 | + * @spec openspec/changes/admin-settings-tabs/specs/fe-settings-ui/spec.md#requirement-the-organisation-sync-card-shows-its-schedule-and-last-run-req-fe-312 |
| 129 | + */ |
| 130 | +export function resultLine(lastRun) { |
| 131 | + if (!lastRun || lastRun.entitiesCreated === undefined) { |
| 132 | + return '' |
| 133 | + } |
| 134 | + return t('stackiq', 'Last run completed: {created} new, {updated} updated, {errors} errors', { |
| 135 | + created: Number(lastRun.entitiesCreated || 0), |
| 136 | + updated: Number(lastRun.entitiesUpdated || 0), |
| 137 | + errors: Number(lastRun.errorCount || 0), |
| 138 | + }) |
| 139 | +} |
0 commit comments