|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl> |
| 3 | + * SPDX-License-Identifier: EUPL-1.2 |
| 4 | + */ |
| 5 | +/** |
| 6 | + * Client side of the ArchiMate round-trip check: the request to |
| 7 | + * `POST /api/archimate/round-trip-check` and the rows and summary the admin |
| 8 | + * settings page shows from its report. |
| 9 | + * |
| 10 | + * @spec openspec/changes/architecture-round-trip-check/specs/archimate-round-trip-check/spec.md#requirement-req-art-004-the-admin-settings-page-shall-show-the-round-trip-report |
| 11 | + */ |
| 12 | + |
| 13 | +import axios from '@nextcloud/axios' |
| 14 | +import { translate as t } from '@nextcloud/l10n' |
| 15 | +import { generateUrl } from '@nextcloud/router' |
| 16 | + |
| 17 | +/** Convert the file and generate it back; nothing is written. */ |
| 18 | +export const MODE_BEFORE_IMPORT = 'before-import' |
| 19 | + |
| 20 | +/** Generate the file back from the stored objects of its model. */ |
| 21 | +export const MODE_AGAINST_IMPORTED = 'against-imported' |
| 22 | + |
| 23 | +/** |
| 24 | + * The categories of the report, in the order the server reports them. |
| 25 | + * |
| 26 | + * @type {string[]} |
| 27 | + */ |
| 28 | +export const CATEGORIES = ['elements', 'relationships', 'views', 'viewNodes', 'propertyDefinitions', 'organizations'] |
| 29 | + |
| 30 | +/** |
| 31 | + * The translated name of a report category. |
| 32 | + * |
| 33 | + * @param {string} category The category key. |
| 34 | + * @return {string} |
| 35 | + * |
| 36 | + * @spec openspec/changes/architecture-round-trip-check/specs/archimate-round-trip-check/spec.md#requirement-req-art-004-the-admin-settings-page-shall-show-the-round-trip-report |
| 37 | + */ |
| 38 | +export function categoryLabel(category) { |
| 39 | + const labels = { |
| 40 | + elements: t('stackiq', 'Elements'), |
| 41 | + relationships: t('stackiq', 'Relationships'), |
| 42 | + views: t('stackiq', 'Views'), |
| 43 | + viewNodes: t('stackiq', 'View nodes'), |
| 44 | + propertyDefinitions: t('stackiq', 'Property definitions'), |
| 45 | + organizations: t('stackiq', 'Organization folders'), |
| 46 | + } |
| 47 | + return labels[category] ?? category |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * One row per category: the counts and the examples. |
| 52 | + * |
| 53 | + * @param {object} report The report the server answered. |
| 54 | + * @return {Array<{key: string, label: string, source: number, result: number, missing: number, extra: number, changed: number, examples: Array<object>, lossy: boolean}>} |
| 55 | + * |
| 56 | + * @spec openspec/changes/architecture-round-trip-check/specs/archimate-round-trip-check/spec.md#requirement-req-art-004-the-admin-settings-page-shall-show-the-round-trip-report |
| 57 | + */ |
| 58 | +export function roundTripRows(report) { |
| 59 | + const categories = report?.categories ?? {} |
| 60 | + return CATEGORIES.filter((key) => categories[key] !== undefined).map((key) => { |
| 61 | + const category = categories[key] |
| 62 | + const missing = Number(category.missing ?? 0) |
| 63 | + const extra = Number(category.extra ?? 0) |
| 64 | + const changed = Number(category.changed ?? 0) |
| 65 | + return { |
| 66 | + key, |
| 67 | + label: categoryLabel(key), |
| 68 | + source: Number(category.source ?? 0), |
| 69 | + result: Number(category.result ?? 0), |
| 70 | + missing, |
| 71 | + extra, |
| 72 | + changed, |
| 73 | + examples: Array.isArray(category.examples) ? category.examples : [], |
| 74 | + lossy: missing + extra + changed > 0, |
| 75 | + } |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * The summary line: "No losses found", the categories with losses, or that the model is not imported. |
| 81 | + * |
| 82 | + * @param {object} report The report the server answered. |
| 83 | + * @return {string} |
| 84 | + * |
| 85 | + * @spec openspec/changes/architecture-round-trip-check/specs/archimate-round-trip-check/spec.md#requirement-req-art-004-the-admin-settings-page-shall-show-the-round-trip-report |
| 86 | + */ |
| 87 | +export function roundTripSummary(report) { |
| 88 | + if (report?.imported === false) { |
| 89 | + return t('stackiq', 'This model is not imported.') |
| 90 | + } |
| 91 | + const lossy = roundTripRows(report).filter((row) => row.lossy).map((row) => row.label) |
| 92 | + if (lossy.length === 0) { |
| 93 | + return t('stackiq', 'No losses found') |
| 94 | + } |
| 95 | + return t('stackiq', 'Losses in: {categories}', { categories: lossy.join(', ') }) |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Upload a file to the round-trip check and answer its report. |
| 100 | + * |
| 101 | + * @param {File} file The exchange file. |
| 102 | + * @param {string} mode MODE_BEFORE_IMPORT or MODE_AGAINST_IMPORTED. |
| 103 | + * @return {Promise<object>} The report. |
| 104 | + * @throws {Error} With the server's message when the check fails. |
| 105 | + * |
| 106 | + * @spec openspec/changes/architecture-round-trip-check/specs/archimate-round-trip-check/spec.md#requirement-req-art-004-the-admin-settings-page-shall-show-the-round-trip-report |
| 107 | + */ |
| 108 | +export async function checkRoundTrip(file, mode) { |
| 109 | + const formData = new FormData() |
| 110 | + formData.append('archiMateFile', file) |
| 111 | + formData.append('mode', mode) |
| 112 | + try { |
| 113 | + const response = await axios.post(generateUrl('/apps/stackiq/api/archimate/round-trip-check'), formData) |
| 114 | + return response.data |
| 115 | + } catch (error) { |
| 116 | + throw new Error(error?.response?.data?.message || t('stackiq', 'The check failed'), { cause: error }) |
| 117 | + } |
| 118 | +} |
0 commit comments