diff --git a/CHANGELOG.md b/CHANGELOG.md index 87505881a..69d2c85b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ sheets becomes or updates a module, its vendor, a usage for the municipality and a contact person for its owner; a repeat import matches on APPID, so it updates instead of duplicating. See `docs/features/cmdb-import.md`. +- CMDB import, repeat imports: applications that left the CMDB sheets are + archived when the sheet "Gearchiveerde Applicaties" lists them and moved to + OpenRegister's trash when no sheet does; one that returns is unarchived or + restored, never duplicated. The new option `missingRecords` (`archive`, the + default, or `keep`) is a choice in the CMDB import section, and the report + counts archived, unarchived, deleted and restored applications. The module + (0.3.9) and usage (1.5.7) schemas opt into OpenRegister's archive state, and + the Applications and Applications in use pages get an "Archived" quick + filter. (cmdb-import-archive-reconciliation) ### Changed - EOL feed: the register and schema slugs the feature reads from are corrected. diff --git a/docs/features/README.md b/docs/features/README.md index 7fa964f0a..c57ff8486 100644 --- a/docs/features/README.md +++ b/docs/features/README.md @@ -158,7 +158,7 @@ The ArchiMate integration maps GEMMA Softwarecatalogus objects to ArchiMate appl ## CMDB Import -Import a TOPdesk CMDB export (`.xlsx`) for one municipality from the **CMDB import** section of the admin settings. Every application row of the two CMDB sheets ("Onbeh Applicaties CMDB" and "Beheerde Applicaties CMDB") becomes or updates a module, its vendor as a Supplier organisation, a usage that links it to the municipality and records whether maintenance is arranged, and a contact person for its owner (identity in Nextcloud Contacts, never public). A repeat import matches on APPID per municipality, so it updates instead of duplicating, and leaves applications missing from the newer export as they are. The column mapping is declarative JSON executed by OpenRegister's mapping engine. +Import a TOPdesk CMDB export (`.xlsx`) for one municipality from the **CMDB import** section of the admin settings. Every application row of the two CMDB sheets ("Onbeh Applicaties CMDB" and "Beheerde Applicaties CMDB") becomes or updates a module, its vendor as a Supplier organisation, a usage that links it to the municipality and records whether maintenance is arranged, and a contact person for its owner (identity in Nextcloud Contacts, never public). A repeat import matches on APPID per municipality, so it updates instead of duplicating. By default it archives the applications that moved to the sheet "Gearchiveerde Applicaties" and moves those on no sheet any more to OpenRegister's trash, and brings either back when it returns; the admin can choose to keep them instead. The Applications and Applications in use pages list the archive under the quick filter "Archived". The column mapping is declarative JSON executed by OpenRegister's mapping engine. See [CMDB import](cmdb-import.md) for the steps, the expected file structure, the error codes and how to adjust the mapping. diff --git a/docs/features/cmdb-import.md b/docs/features/cmdb-import.md index 32550932b..e71756d73 100644 --- a/docs/features/cmdb-import.md +++ b/docs/features/cmdb-import.md @@ -17,9 +17,11 @@ updates: in Nextcloud Contacts. Owners are never readable by the public. All of it is stored as OpenRegister objects in the stackiq register. Import a -newer export later and the same applications are updated, not duplicated. +newer export later and the same applications are updated, not duplicated; +applications that left the export are archived or moved to the trash (see +[Repeat imports](#repeat-imports)). -Specification: [`openspec/specs/cmdb-export-import/spec.md`](https://github.com/ConductionNL/stackiq/blob/development/openspec/specs/cmdb-export-import/spec.md) (change archived in [`openspec/changes/archive/2026-10-05-cmdb-export-import/`](https://github.com/ConductionNL/stackiq/tree/development/openspec/changes/archive/2026-10-05-cmdb-export-import)). +Specification: [`openspec/specs/cmdb-export-import/spec.md`](https://github.com/ConductionNL/stackiq/blob/development/openspec/specs/cmdb-export-import/spec.md) (change archived in [`openspec/changes/archive/2026-10-05-cmdb-export-import/`](https://github.com/ConductionNL/stackiq/tree/development/openspec/changes/archive/2026-10-05-cmdb-export-import); archiving and deleting on a repeat import in [`openspec/changes/cmdb-import-archive-reconciliation/`](https://github.com/ConductionNL/stackiq/tree/development/openspec/changes/cmdb-import-archive-reconciliation)). ## Who can import @@ -77,9 +79,16 @@ page in stackiq. and the summary counts them as *created unpublished*. Applications that were imported before keep their publication as it is, whichever you choose. -6. Press **Import**. A progress bar shows how many rows have been processed. - **Cancel import** stops the import before the next row; rows that were - already processed stay imported. The section says whether the server +6. **Applications missing from the export.** **Archive or delete them** is + the default: an application of this municipality that is on neither CMDB + sheet any more is archived or moved to the trash after the last row (see + [Repeat imports](#repeat-imports)). Choose **Keep them as they are** to + leave such applications untouched. +7. Press **Import**. A progress bar shows how many rows have been processed. + **Cancel import** stops the import before the next row, or, while the + applications are compared with the export, before the next application; + what was already processed stays: the rows imported, and the applications + archived or deleted so far. The section says whether the server accepted the cancel; one pressed before the server has started on the rows cannot take effect yet, and the section says so. @@ -92,11 +101,16 @@ When the import finishes, the section shows: - the **summary**: rows read, created, updated, unchanged, skipped, failed and warnings, and, when publishing was off, how many applications were - created unpublished; + created unpublished; after a repeat import also how many applications + were archived, unarchived, deleted (moved to the trash) or restored, when + there are any. The sentence above it, which a screen reader announces, + names those counts too, for example *Applications: 1 archived, 1 deleted.*; - **warnings for the whole file**, for example an optional column that is missing; - the **rows** table: sheet, row number, APPID, application, outcome, - and the reasons and warnings for that row. Filter it with **Show rows with + and the reasons and warnings for that row. An application archived or + deleted because it left the export has no row number; its sheet is + `Gearchiveerde Applicaties` when that sheet listed it. Filter it with **Show rows with outcome**, and sort it by clicking a column header. It shows 100 rows at a time; **Show 100 more rows** adds the next ones. The application name links to the module in stackiq. @@ -106,8 +120,15 @@ the same organisation. ## The file -The import reads the two CMDB sheets of the export and ignores all others, -including the `Invoer` sheets they are derived from: +The import reads the two CMDB sheets of the export, and of the sheet +`Gearchiveerde Applicaties` only the `APPID` column (see +[Repeat imports](#repeat-imports)). It ignores all other sheets, including +the `Invoer` sheets they are derived from. The archive sheet is read in every +import, also with **Keep them as they are**, and it is bounded like the two +CMDB sheets (see [Limits](#limits)): an archive sheet over the limits refuses +the import even when you keep missing applications. Delete the sheet from a +copy of the export to import it without one; the import then archives and +never deletes. | Sheet | What it holds | Recorded on the usage | |---|---|---| @@ -120,8 +141,8 @@ surrounding spaces and a trailing `:` or `⚡` do not matter, and the order of the columns does not matter. A column that one sheet has and the other has not (such as `Nickname`, only on `Beheerde Applicaties CMDB`) is optional on the sheet that lacks it. Empty rows, including formatted rows below the data, -are ignored and not counted. A sheet may hold at most 10,000 rows with data -by default (see [Limits](#limits)). +are ignored and not counted. A sheet, the archive sheet included, may hold at +most 10,000 rows with data by default (see [Limits](#limits)). Two columns are **required** on every CMDB sheet that is present: `APPID` and `Applicatie Naam`. Every other column is optional; when one is missing, @@ -213,9 +234,9 @@ colliding. - **Known APPID, nothing changed**: nothing is saved; the row is reported as *unchanged*. Importing the same export twice creates nothing the second time. -- **APPID missing from a newer export**: the application, its usage and - its contact persons are left as they are. They are not changed, depublished - or deleted. +- **APPID missing from a newer export**: see + [Applications that left the export](#applications-that-left-the-export) + below. - Each application keeps exactly one usage for the municipality. - **Known APPID, but the application belongs to another organisation**: an application found by its match key is only updated when the municipality @@ -243,6 +264,61 @@ OpenRegister refuses to save it, that row is reported as *failed* with the step that failed, and the other rows are imported. Importing again completes the failed row. +### Applications that left the export + +With **Archive or delete them** (the default; `missingRecords=archive` in +the API), the import compares the municipality's applications with the +export after the last row. It looks only at the municipality's own usages +and the applications its import created, never at an application only +another organisation uses or one added by hand. For an application whose +APPID is on neither CMDB sheet: + +| The APPID is | The application and its usage | +|---|---| +| on the sheet `Gearchiveerde Applicaties` | are **archived**: OpenRegister's archive state, with the reason `cmdb-import: on sheet "Gearchiveerde Applicaties"`. Reported as *archived*. | +| on no sheet at all | are **deleted**: moved to OpenRegister's trash, where they stay restorable for 30 days. Reported as *deleted*. | +| on no sheet, but the archive sheet cannot be relied on: the workbook has no sheet `Gearchiveerde Applicaties`, the sheet has no `APPID` column, or an `APPID` in it has no cached value (a formula whose result the file does not store), or an `APPID` on one of the two CMDB sheets has no cached value (the import cannot tell which application that row stands for) | are **archived**, never deleted. Each case has its own warning in the result: *sheet not found*, *no column APPID*, or *an APPID without a cached value in row …* (for more rows: *in N rows (…)*, naming the first ten; for a CMDB sheet the warning names that sheet). Such a workbook therefore never moves anything to the trash. | + +An archived or deleted application disappears from the stackiq lists, from +OpenCatalogi and from Portaliq's **Software we use**. In stackiq, the quick +filter **Archived** on the **Applications** and **Applications in use** pages +lists the archive. On the **Applications** page the **Archived** tab lists +the whole archive: it ignores the GEMMA filters and the search box (the page says so in a note under the search box while that tab is active), because +their counts and matches describe the working set, which leaves archived +applications out. An OpenCatalogi caller finds archived applications by +adding `_archived=true` to its request. Contact persons are never archived +or deleted: an owner is shared with the municipality's other applications. +An application that is already in the state the export asks for is left +alone and not reported again. + +When an archived or deleted application **returns** in a later export, it is +taken out of the archive (*unarchived*) or out of the trash (*restored*), +and then updated as usual. No second application is created. With **Update +existing records** off, it is skipped as `exists` and stays archived or in +the trash. When an application was deleted by hand and a later import +created it again, the working one is updated and the deleted one stays in +the trash. + +The comparison runs only when the import went through all rows: an import +cancelled or failed before the comparison archives or deletes nothing. +**Cancel import** during the comparison stops it before the next +application; what was archived or deleted until then stays so, and the +result lists it. During the comparison the progress line reads +*Checking the applications of this municipality against the export* without +a count, and the bar stays full: the comparison covers every application of +the municipality, not only the ones missing from the export, so its length +is not known in rows. Each +application is handled on its own; one that cannot be archived is reported +as *failed* with the step (`archive`, `restore` or `delete`) and the others +continue. Archiving needs module schema 0.3.9 and usage schema 1.5.7 or +later, which declare that their objects can be archived; on an older register configuration each application fails +with the step `archive`, and **Force Update** at the top of the stackiq admin +settings fixes it. + +With **Keep them as they are** (`missingRecords=keep`), the application, its +usage and its contact persons are left as they are: not changed, archived, +depublished or deleted. + ## Owners The owner becomes a **contact person of the municipality**, never a @@ -281,19 +357,20 @@ and the section shows the reason and the error code. | Error code | What it means | What to do | |---|---|---| | `NOT_XLSX` | The file is not an Excel workbook: wrong extension, or the content is not an `.xlsx` package. | Save the export as Excel workbook (`.xlsx`). | -| `FILE_TOO_LARGE` | The file is larger than the upload limit (10 MB by default). The message names the limit in force. | Remove sheets the import does not read, or split the export. | +| `FILE_TOO_LARGE` | The file is larger than the upload limit (10 MB by default). The message names the limit in force. | Remove sheets the import does not read. Or split the export and import every part with **Keep them as they are**: an import that archives treats the applications of the other parts as missing. Applications that left the export are then not archived or deleted. | | `NO_FILE_UPLOADED` | No file arrived. | Choose the file again. | | `MUNICIPALITY_REQUIRED` | No municipality was chosen. | Pick or type a municipality. | | `MUNICIPALITY_INVALID` | The chosen organisation does not exist or is not of type Municipality. | Pick an organisation of type Municipality, or type a new name. | | `NO_SOURCE_SHEET` | Neither `Onbeh Applicaties CMDB` nor `Beheerde Applicaties CMDB` is in the workbook. | Check the sheet names; they must match exactly. | | `MISSING_COLUMN` | A present CMDB sheet has no `APPID` or `Applicatie Naam` column. The message names the sheet and the column. | Add the column to that sheet. | -| `TOO_MANY_ROWS` | A CMDB sheet has more rows with data than the row limit (10,000 by default). The message names the sheet and the limit. | Split the export and import the parts one after the other. | -| `WORKBOOK_TOO_LARGE` | Unpacked, the workbook is larger than the import reads: 50 MB in all, 10 MB for one sheet the import reads or for the table of texts the sheets share, or more than 200,000 different texts, by default. Sheets the import does not read may be larger than 10 MB. An `.xlsx` is a compressed package, so a file of a few MB can unpack to far more. The message names the limit, and for a sheet the sheet and its unpacked size. | For a sheet that is too large: split its rows over two copies of the export and import them one after the other. For a workbook that is too large in all: delete the sheets the import does not read from a copy and import that copy. | +| `TOO_MANY_ROWS` | A CMDB sheet has more rows with data than the row limit (10,000 by default). The message names the sheet and the limit. | Split the export and import the parts one after the other with **Keep them as they are**: an import that archives treats the applications of the other parts as missing. Applications that left the export are then not archived or deleted. | +| `WORKBOOK_TOO_LARGE` | Unpacked, the workbook is larger than the import reads: 50 MB in all, 10 MB for one sheet the import reads (the two CMDB sheets and the archive sheet, in every import) or for the table of texts the sheets share, or more than 200,000 different texts, by default. Sheets the import does not read may be larger than 10 MB. An `.xlsx` is a compressed package, so a file of a few MB can unpack to far more. The message names the limit, and for a sheet the sheet and its unpacked size. | For a sheet that is too large: split its rows over two copies of the export and import both with **Keep them as they are**. For a workbook that is too large in all: delete the sheets the import does not read from a copy and import that copy. | | `MUNICIPALITY_AMBIGUOUS` | More than one municipality has the typed name. The import does not guess which one. | Pick the municipality from the list instead of typing its name. | | `IMPORT_IN_PROGRESS` | Another CMDB import is running. Only one import runs at a time. | Wait until it has finished and try again. | | `FIELD_INVALID` | A form field of the request has a value the import does not accept, for example an `updateExisting` or `publish` that is neither `true` nor `false`. The message names the field. | Not reachable from the section; reported for API callers. | | `UPLOAD_FAILED` | The file reached the server but could not be stored there. | Try again; the Nextcloud log has the details. | -| `MISSING_RECORDS_UNSUPPORTED` | The request asked to mark or remove records missing from the export. Only keeping them is supported. | Not reachable from the section; reported for API callers. | +| `MISSING_RECORDS_UNSUPPORTED` | The request asked for something other than `keep` or `archive` for records missing from the export, for example `mark` or `remove`. The details list the accepted values. | Not reachable from the section; reported for API callers. | +| `ARCHIVE_UNAVAILABLE` | **Archive or delete them** was chosen, but the OpenRegister on this server cannot archive or restore objects (it is missing or too old). Nothing was read or written. | Choose **Keep them as they are** and import again, or update OpenRegister. | | `MAPPING_UNAVAILABLE` | OpenRegister's mapping engine is missing, or one of the mapping files is invalid. | Update OpenRegister. If you changed a mapping file, check it against the Nextcloud log. | | `READER_UNAVAILABLE` | The Excel reader that ships with OpenRegister cannot be loaded. | Make sure OpenRegister is installed and enabled. | | `NOT_CONFIGURED` | The stackiq register or its schemas cannot be found. | Run **Auto Configure** at the top of the stackiq admin settings. | @@ -322,9 +399,9 @@ every import: | Setting | Default | What it limits | |---|---|---| | `maxFileBytes` | `10485760` (10 MB) | the size of the uploaded file | -| `maxRowsPerSheet` | `10000` | the rows with data on one CMDB sheet | +| `maxRowsPerSheet` | `10000` | the rows with data on one CMDB sheet, and on the archive sheet | | `maxUncompressedBytes` | `52428800` (50 MB) | the size of the workbook once unpacked, checked before a sheet is parsed | -| `maxPartBytes` | `10485760` (10 MB) | the size of one part the import parses once unpacked, such as a source sheet or the shared-strings table, checked before a sheet is parsed; a sheet the import does not read may be larger, and is read as an empty sheet | +| `maxPartBytes` | `10485760` (10 MB) | the size of one part the import parses once unpacked, such as a source sheet or the shared-strings table, checked before a sheet is parsed; a sheet the import does not read may be larger, and is read as an empty sheet; the archive sheet is read, so it is bounded like a CMDB sheet | | `maxSharedStrings` | `200000` | the number of different texts in the workbook's shared-strings table, each formatted piece of a text with mixed formatting counted as one more, also in text written in a cell itself, counted before a sheet is parsed | | `maxReferencedStringBytes` | `67108864` (64 MB) | the shared text all cells of the workbook reference together, counted once per cell and once per sheet the import reads (the two CMDB sheets and the archive sheet, also when the workbook has no archive sheet) before a sheet is parsed; a long text that many cells repeat counts as often as it is repeated | diff --git a/l10n/en.js b/l10n/en.js index 8dd84a2f4..61a8eb769 100644 --- a/l10n/en.js +++ b/l10n/en.js @@ -982,7 +982,7 @@ OC.L10N.register( "Import for {name} cancelled after {read} rows.": "Import for {name} cancelled after {read} rows.", "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.": "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.", "Import progress": "Import progress", - "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.": "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.", + "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.": "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.", "Importing the export…": "Importing the export…", "Importing…": "Importing…", "APPID": "APPID", @@ -999,7 +999,7 @@ OC.L10N.register( "Reasons and warnings": "Reasons and warnings", "Records missing from the export can only be kept.": "Records missing from the export can only be kept.", "Reload the page and try again.": "Reload the page and try again.", - "Remove sheets the import does not read, or split the export, and try again.": "Remove sheets the import does not read, or split the export, and try again.", + "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".": "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".", "Row": "Row", "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.": "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.", "Rows": "Rows", @@ -1017,7 +1017,7 @@ OC.L10N.register( "The import failed unexpectedly.": "The import failed unexpectedly.", "The import mapping cannot run.": "The import mapping cannot run.", "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.": "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.", - "The import was cancelled. The rows processed before it stopped are kept.": "The import was cancelled. The rows processed before it stopped are kept.", + "The import was cancelled. The rows and applications processed before it stopped are kept.": "The import was cancelled. The rows and applications processed before it stopped are kept.", "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.": "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.", "The server could not be reached.": "The server could not be reached.", "The sheet \"{sheet}\" has no column \"{column}\".": "The sheet \"{sheet}\" has no column \"{column}\".", @@ -1038,7 +1038,23 @@ OC.L10N.register( "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.": "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.", "Choose a municipality or enter the name of a new one.": "Choose a municipality or enter the name of a new one.", "No running CMDB import has this id.": "No running CMDB import has this id.", - "Only keeping records that are missing from the export is supported.": "Only keeping records that are missing from the export is supported.", + "Applications missing from the export can only be archived or kept.": "Applications missing from the export can only be archived or kept.", + "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.": "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.", + "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.", + "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.", + "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.", + "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.": "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.", + "Unarchived": "Unarchived", + "Restored": "Restored", + "Archived": "Archived", + "Deleted": "Deleted", + "Applications missing from the export": "Applications missing from the export", + "Archive or delete them": "Archive or delete them", + "Keep them as they are": "Keep them as they are", + "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.": "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.", + "Applications missing from the export cannot be archived.": "Applications missing from the export cannot be archived.", + "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.": "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.", + "Checking the applications of this municipality against the export": "Checking the applications of this municipality against the export", "Sheet \"%1$s\" has more than %2$s rows.": "Sheet \"%1$s\" has more than %2$s rows.", "Sheet \"%1$s\" has no column \"%2$s\".": "Sheet \"%1$s\" has no column \"%2$s\".", "Stackiq is not configured: the register or its schemas cannot be found.": "Stackiq is not configured: the register or its schemas cannot be found.", @@ -1086,15 +1102,15 @@ OC.L10N.register( "The server could not store the uploaded file. The details are in the Nextcloud log.": "The server could not store the uploaded file. The details are in the Nextcloud log.", "The file is larger than {size}, the most the import accepts.": "The file is larger than {size}, the most the import accepts.", "The file is larger than the server accepts.": "The file is larger than the server accepts.", - "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.": "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.", - "Split the export and import the parts one after the other.": "Split the export and import the parts one after the other.", + "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".": "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".", + "Split the export and import the parts one after the other with \"Keep them as they are\".": "Split the export and import the parts one after the other with \"Keep them as they are\".", "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.": "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.", - "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.": "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.", + "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.": "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.", "The page got no answer from the import.": "The page got no answer from the import.", "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.": "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.", "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.": "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.", "The import could not be cancelled: {reason}": "The import could not be cancelled: {reason}", - "Cancelling the import. It stops before the next row; the rows already processed stay imported.": "Cancelling the import. It stops before the next row; the rows already processed stay imported.", + "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.": "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.", "Showing {shown} of {total} rows.": "Showing {shown} of {total} rows.", "Show {count} more rows": "Show {count} more rows", "The request field \"{field}\" has a value the import does not accept.": "The request field \"{field}\" has a value the import does not accept.", @@ -1125,14 +1141,21 @@ OC.L10N.register( "conflict: the application with this import key is used by another organisation, so it is not changed": "conflict: the application with this import key is used by another organisation, so it is not changed", "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.": "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.", "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.": "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.", - "Split the rows over two copies of the export and import them one after the other.": "Split the rows over two copies of the export and import them one after the other.", + "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.": "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.", "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.": "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.", "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.": "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.", - "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.": "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.", + "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.": "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.", "The workbook holds more than {count} different texts, the most the import reads.": "The workbook holds more than {count} different texts, the most the import reads.", "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.": "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.", "In development since": "In development since", - "In use since": "In use since" + "In use since": "In use since", + "{count} archived": "{count} archived", + "{count} unarchived": "{count} unarchived", + "{count} deleted": "{count} deleted", + "{count} restored": "{count} restored", + "Applications: {changes}.": "Applications: {changes}.", + "This tab lists the whole archive. It ignores the GEMMA filters and the search term.": "This tab lists the whole archive. It ignores the GEMMA filters and the search term.", + "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when.": "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when." }, "nplurals=2; plural=(n != 1);" ) diff --git a/l10n/en.json b/l10n/en.json index 313f968ea..efb114389 100644 --- a/l10n/en.json +++ b/l10n/en.json @@ -981,7 +981,7 @@ "Import for {name} cancelled after {read} rows.": "Import for {name} cancelled after {read} rows.", "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.": "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.", "Import progress": "Import progress", - "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.": "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.", + "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.": "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.", "Importing the export…": "Importing the export…", "Importing…": "Importing…", "APPID": "APPID", @@ -998,7 +998,7 @@ "Reasons and warnings": "Reasons and warnings", "Records missing from the export can only be kept.": "Records missing from the export can only be kept.", "Reload the page and try again.": "Reload the page and try again.", - "Remove sheets the import does not read, or split the export, and try again.": "Remove sheets the import does not read, or split the export, and try again.", + "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".": "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".", "Row": "Row", "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.": "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.", "Rows": "Rows", @@ -1016,7 +1016,7 @@ "The import failed unexpectedly.": "The import failed unexpectedly.", "The import mapping cannot run.": "The import mapping cannot run.", "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.": "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.", - "The import was cancelled. The rows processed before it stopped are kept.": "The import was cancelled. The rows processed before it stopped are kept.", + "The import was cancelled. The rows and applications processed before it stopped are kept.": "The import was cancelled. The rows and applications processed before it stopped are kept.", "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.": "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.", "The server could not be reached.": "The server could not be reached.", "The sheet \"{sheet}\" has no column \"{column}\".": "The sheet \"{sheet}\" has no column \"{column}\".", @@ -1037,7 +1037,23 @@ "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.": "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.", "Choose a municipality or enter the name of a new one.": "Choose a municipality or enter the name of a new one.", "No running CMDB import has this id.": "No running CMDB import has this id.", - "Only keeping records that are missing from the export is supported.": "Only keeping records that are missing from the export is supported.", + "Applications missing from the export can only be archived or kept.": "Applications missing from the export can only be archived or kept.", + "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.": "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.", + "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.", + "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.", + "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.": "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.", + "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.": "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.", + "Unarchived": "Unarchived", + "Restored": "Restored", + "Archived": "Archived", + "Deleted": "Deleted", + "Applications missing from the export": "Applications missing from the export", + "Archive or delete them": "Archive or delete them", + "Keep them as they are": "Keep them as they are", + "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.": "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.", + "Applications missing from the export cannot be archived.": "Applications missing from the export cannot be archived.", + "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.": "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.", + "Checking the applications of this municipality against the export": "Checking the applications of this municipality against the export", "Sheet \"%1$s\" has more than %2$s rows.": "Sheet \"%1$s\" has more than %2$s rows.", "Sheet \"%1$s\" has no column \"%2$s\".": "Sheet \"%1$s\" has no column \"%2$s\".", "Stackiq is not configured: the register or its schemas cannot be found.": "Stackiq is not configured: the register or its schemas cannot be found.", @@ -1085,15 +1101,15 @@ "The server could not store the uploaded file. The details are in the Nextcloud log.": "The server could not store the uploaded file. The details are in the Nextcloud log.", "The file is larger than {size}, the most the import accepts.": "The file is larger than {size}, the most the import accepts.", "The file is larger than the server accepts.": "The file is larger than the server accepts.", - "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.": "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.", - "Split the export and import the parts one after the other.": "Split the export and import the parts one after the other.", + "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".": "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".", + "Split the export and import the parts one after the other with \"Keep them as they are\".": "Split the export and import the parts one after the other with \"Keep them as they are\".", "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.": "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.", - "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.": "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.", + "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.": "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.", "The page got no answer from the import.": "The page got no answer from the import.", "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.": "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.", "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.": "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.", "The import could not be cancelled: {reason}": "The import could not be cancelled: {reason}", - "Cancelling the import. It stops before the next row; the rows already processed stay imported.": "Cancelling the import. It stops before the next row; the rows already processed stay imported.", + "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.": "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.", "Showing {shown} of {total} rows.": "Showing {shown} of {total} rows.", "Show {count} more rows": "Show {count} more rows", "The request field \"{field}\" has a value the import does not accept.": "The request field \"{field}\" has a value the import does not accept.", @@ -1124,13 +1140,20 @@ "conflict: the application with this import key is used by another organisation, so it is not changed": "conflict: the application with this import key is used by another organisation, so it is not changed", "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.": "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.", "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.": "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.", - "Split the rows over two copies of the export and import them one after the other.": "Split the rows over two copies of the export and import them one after the other.", + "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.": "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.", "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.": "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.", "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.": "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.", - "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.": "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.", + "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.": "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.", "The workbook holds more than {count} different texts, the most the import reads.": "The workbook holds more than {count} different texts, the most the import reads.", "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.": "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.", "In development since": "In development since", - "In use since": "In use since" + "In use since": "In use since", + "{count} archived": "{count} archived", + "{count} unarchived": "{count} unarchived", + "{count} deleted": "{count} deleted", + "{count} restored": "{count} restored", + "Applications: {changes}.": "Applications: {changes}.", + "This tab lists the whole archive. It ignores the GEMMA filters and the search term.": "This tab lists the whole archive. It ignores the GEMMA filters and the search term.", + "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when.": "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when." } } diff --git a/l10n/nl.js b/l10n/nl.js index 3151813d6..cdf48b52b 100644 --- a/l10n/nl.js +++ b/l10n/nl.js @@ -1050,7 +1050,7 @@ OC.L10N.register( "Import for {name} cancelled after {read} rows.": "Import voor {name} geannuleerd na {read} rijen.", "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.": "Import voor {name} voltooid. {read} rijen gelezen: {created} aangemaakt, {updated} bijgewerkt, {unchanged} ongewijzigd.", "Import progress": "Voortgang van de import", - "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.": "Een nieuwere export opnieuw importeren werkt dezelfde applicaties bij, herkend aan het APPID per gemeente. Applicaties die er niet meer in staan, blijven zoals ze zijn.", + "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.": "Een nieuwere export opnieuw importeren werkt dezelfde applicaties bij, herkend aan het APPID per gemeente. Applicaties die er niet meer in staan, worden gearchiveerd of verwijderd, tenzij je kiest ze te laten zoals ze zijn.", "Importing the export…": "De export wordt geïmporteerd…", "Importing…": "Importeren…", "APPID": "APPID", @@ -1067,7 +1067,7 @@ OC.L10N.register( "Reasons and warnings": "Redenen en waarschuwingen", "Records missing from the export can only be kept.": "Records die in de export ontbreken, kunnen alleen worden behouden.", "Reload the page and try again.": "Laad de pagina opnieuw en probeer het nog eens.", - "Remove sheets the import does not read, or split the export, and try again.": "Verwijder tabbladen die de import niet leest, of splits de export, en probeer het opnieuw.", + "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".": "Verwijder tabbladen die de import niet leest en probeer het opnieuw. Of splits de export en importeer elk deel met \"Laat ze zoals ze zijn\".", "Row": "Rij", "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.": "Rij 1 bevat de kolomnamen. \"APPID\" en \"Applicatie Naam\" zijn verplicht; de volgorde van de kolommen maakt niet uit.", "Rows": "Rijen", @@ -1085,7 +1085,7 @@ OC.L10N.register( "The import failed unexpectedly.": "De import is onverwacht mislukt.", "The import mapping cannot run.": "De mapping van de import kan niet worden uitgevoerd.", "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.": "De import leest werkmappen met de spreadsheetbibliotheek die met OpenRegister wordt meegeleverd. Zorg dat OpenRegister is geïnstalleerd en ingeschakeld.", - "The import was cancelled. The rows processed before it stopped are kept.": "De import is geannuleerd. De rijen die vóór het stoppen zijn verwerkt, blijven behouden.", + "The import was cancelled. The rows and applications processed before it stopped are kept.": "De import is geannuleerd. De rijen en applicaties die vóór het stoppen zijn verwerkt, blijven behouden.", "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.": "Het organisatieregister is niet ingesteld, dus bestaande gemeenten kunnen niet worden getoond. U kunt nog steeds de naam van een gemeente typen.", "The server could not be reached.": "De server is niet bereikbaar.", "The sheet \"{sheet}\" has no column \"{column}\".": "Het tabblad \"{sheet}\" heeft geen kolom \"{column}\".", @@ -1106,7 +1106,23 @@ OC.L10N.register( "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.": "Het stackiq-register of de schema's ervan zijn niet gevonden. Voer bovenaan deze pagina Auto Configure uit en probeer het daarna opnieuw.", "Choose a municipality or enter the name of a new one.": "Kies een gemeente of voer de naam van een nieuwe in.", "No running CMDB import has this id.": "Er loopt geen CMDB-import met deze id.", - "Only keeping records that are missing from the export is supported.": "Alleen het behouden van records die in de export ontbreken, wordt ondersteund.", + "Applications missing from the export can only be archived or kept.": "Applicaties die in de export ontbreken, kunnen alleen worden gearchiveerd of behouden.", + "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.": "Applicaties die in de export ontbreken, kunnen niet worden gearchiveerd: OpenRegister ontbreekt of is te oud. Kies ervoor ze te behouden, of werk OpenRegister bij.", + "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft geen kolom \"%2$s\", dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft een APPID zonder opgeslagen waarde in rij %2$s, dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft een APPID zonder opgeslagen waarde in %2$s rijen (%3$s), dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.": "Tabblad \"%s\" is niet gevonden, dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Unarchived": "Uit archief gehaald", + "Restored": "Hersteld", + "Archived": "Gearchiveerd", + "Deleted": "Verwijderd", + "Applications missing from the export": "Applicaties die in de export ontbreken", + "Archive or delete them": "Archiveren of verwijderen", + "Keep them as they are": "Laat ze zoals ze zijn", + "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.": "Een applicatie van deze gemeente die op geen van beide CMDB-tabbladen meer staat, wordt gearchiveerd als het tabblad \"Gearchiveerde Applicaties\" haar noemt, en naar de prullenbak verplaatst als geen enkel tabblad dat doet. Beide verdwijnen uit de lijsten, uit OpenCatalogi en uit Portaliq; een applicatie die in een latere export terugkomt, wordt teruggezet. Ontbreekt dat tabblad, heeft het geen kolom APPID of heeft een APPID geen opgeslagen waarde, dan worden applicaties alleen gearchiveerd en zegt het resultaat dat. Kies Laat ze zoals ze zijn om ze ongemoeid te laten.", + "Applications missing from the export cannot be archived.": "Applicaties die in de export ontbreken, kunnen niet worden gearchiveerd.", + "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.": "OpenRegister ontbreekt of is te oud om objecten te archiveren. Kies \"Laat ze zoals ze zijn\" en importeer opnieuw, of werk OpenRegister bij. Er is niets geïmporteerd.", + "Checking the applications of this municipality against the export": "De applicaties van deze gemeente met de export vergelijken", "Sheet \"%1$s\" has more than %2$s rows.": "Tabblad \"%1$s\" heeft meer dan %2$s rijen.", "Sheet \"%1$s\" has no column \"%2$s\".": "Tabblad \"%1$s\" heeft geen kolom \"%2$s\".", "Stackiq is not configured: the register or its schemas cannot be found.": "Stackiq is niet ingesteld: het register of de schema's ervan zijn niet gevonden.", @@ -1154,15 +1170,15 @@ OC.L10N.register( "The server could not store the uploaded file. The details are in the Nextcloud log.": "De server kon het geüploade bestand niet opslaan. De details staan in het Nextcloud-logboek.", "The file is larger than {size}, the most the import accepts.": "Het bestand is groter dan {size}, het maximum dat de import accepteert.", "The file is larger than the server accepts.": "Het bestand is groter dan de server accepteert.", - "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.": "Een brontabblad mag hoogstens {limit} rijen bevatten. Splits de export en importeer de delen na elkaar.", - "Split the export and import the parts one after the other.": "Splits de export en importeer de delen na elkaar.", + "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".": "Een brontabblad mag hoogstens {limit} rijen bevatten. Splits de export en importeer de delen na elkaar met \"Laat ze zoals ze zijn\".", + "Split the export and import the parts one after the other with \"Keep them as they are\".": "Splits de export en importeer de delen na elkaar met \"Laat ze zoals ze zijn\".", "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.": "Excel-werkmap (.xlsx) met het tabblad \"{first}\" of \"{second}\". Standaard mag het bestand hoogstens {size} zijn.", - "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.": "De tabbladen \"{first}\" (applicaties zonder geregeld beheer) en \"{second}\" (met geregeld beheer) worden gelezen; andere tabbladen, ook de \"Invoer\"-tabbladen, worden genegeerd.", + "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.": "De tabbladen \"{first}\" (applicaties zonder geregeld beheer) en \"{second}\" (met geregeld beheer) worden gelezen, en van het tabblad \"{archive}\" alleen de kolom APPID; andere tabbladen, ook de \"Invoer\"-tabbladen, worden genegeerd.", "The page got no answer from the import.": "De pagina kreeg geen antwoord van de import.", "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.": "De verbinding werd verbroken voordat de import antwoordde, dus de import kan nog bezig zijn of al klaar zijn. Wacht een paar minuten en controleer de applicaties van de gemeente voordat u opnieuw importeert. Hetzelfde bestand opnieuw importeren maakt geen dubbele records aan.", "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.": "De import kan nu niet worden geannuleerd: de server is nog niet aan de rijen begonnen, of is er al mee klaar. Loopt de import door, druk dan zo dadelijk opnieuw op Import annuleren.", "The import could not be cancelled: {reason}": "De import kon niet worden geannuleerd: {reason}", - "Cancelling the import. It stops before the next row; the rows already processed stay imported.": "De import wordt geannuleerd. Hij stopt voor de volgende rij; de rijen die al verwerkt zijn blijven geïmporteerd.", + "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.": "De import wordt geannuleerd. Hij stopt voor de volgende rij of, tijdens het vergelijken van de applicaties met de export, voor de volgende applicatie; wat al verwerkt is blijft staan.", "Showing {shown} of {total} rows.": "{shown} van {total} rijen weergegeven.", "Show {count} more rows": "Toon nog {count} rijen", "The request field \"{field}\" has a value the import does not accept.": "Het veld \"{field}\" van het verzoek heeft een waarde die de import niet accepteert.", @@ -1193,14 +1209,21 @@ OC.L10N.register( "conflict: the application with this import key is used by another organisation, so it is not changed": "conflict: de applicatie met deze importsleutel wordt door een andere organisatie gebruikt en wordt daarom niet gewijzigd", "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.": "Staat dit aan, dan overschrijft een herhaalde import de naam, de omschrijvingen, het applicatietype, het hostingmodel, het BBN-niveau, de bronvelden en de leverancier van de applicatie, en de status, de uitfaseerdatum en de functioneel eigenaar van het gebruik, met de waarden uit de export. De TIME-classificatie en de interne notitie van het gebruik worden alleen gezet wanneer het gebruik wordt aangemaakt of het veld leeg is, zodat wijzigingen in stackiq blijven staan.", "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.": "Een .xlsx-bestand is gecomprimeerd: een bestand van een paar MB kan uitgepakt veel groter zijn. Er is niets geïmporteerd.", - "Split the rows over two copies of the export and import them one after the other.": "Verdeel de rijen over twee kopieën van de export en importeer die na elkaar.", + "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.": "Verdeel de rijen over twee kopieën van de export en importeer beide met \"Laat ze zoals ze zijn\": een import die archiveert, ziet de applicaties van de andere kopie als ontbrekend.", "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.": "Het tabblad \"{sheet}\" is te groot om te importeren: uitgepakt is het {size}, en de import leest van één tabblad hooguit {limit}.", "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.": "Het onderdeel {part} van de werkmap is te groot om te importeren: uitgepakt is het {size}, en de import leest van één onderdeel hooguit {limit}.", - "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.": "Verwijder in een kopie van de export de tabbladen die de import niet leest (hij leest alleen \"{first}\" en \"{second}\"), en importeer die kopie.", + "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.": "Verwijder in een kopie van de export de tabbladen die de import niet leest (hij leest \"{first}\" en \"{second}\", en de kolom APPID van \"{archive}\"), en importeer die kopie.", "The workbook holds more than {count} different texts, the most the import reads.": "De werkmap bevat meer dan {count} verschillende teksten, het maximum dat de import leest.", "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.": "Samen verwijzen de cellen van de werkmap naar meer dan {size} gedeelde tekst, het maximum dat de import leest.", "In development since": "In ontwikkeling sinds", - "In use since": "In gebruik sinds" + "In use since": "In gebruik sinds", + "{count} archived": "{count} gearchiveerd", + "{count} unarchived": "{count} uit het archief gehaald", + "{count} deleted": "{count} verwijderd", + "{count} restored": "{count} teruggezet", + "Applications: {changes}.": "Applicaties: {changes}.", + "This tab lists the whole archive. It ignores the GEMMA filters and the search term.": "Dit tabblad toont het hele archief. De GEMMA-filters en de zoekterm worden genegeerd.", + "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when.": "Applicaties die niet meer in de export staan, worden gearchiveerd of naar de prullenbak verplaatst. De (i)-knop bovenaan legt uit wanneer." }, "nplurals=2; plural=(n != 1);" ) diff --git a/l10n/nl.json b/l10n/nl.json index 680f6f853..88f4a002a 100644 --- a/l10n/nl.json +++ b/l10n/nl.json @@ -1049,7 +1049,7 @@ "Import for {name} cancelled after {read} rows.": "Import voor {name} geannuleerd na {read} rijen.", "Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.": "Import voor {name} voltooid. {read} rijen gelezen: {created} aangemaakt, {updated} bijgewerkt, {unchanged} ongewijzigd.", "Import progress": "Voortgang van de import", - "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are left as they are.": "Een nieuwere export opnieuw importeren werkt dezelfde applicaties bij, herkend aan het APPID per gemeente. Applicaties die er niet meer in staan, blijven zoals ze zijn.", + "Importing a newer export again updates the same applications, matched on APPID per municipality. Applications missing from it are archived or deleted, unless you choose to keep them.": "Een nieuwere export opnieuw importeren werkt dezelfde applicaties bij, herkend aan het APPID per gemeente. Applicaties die er niet meer in staan, worden gearchiveerd of verwijderd, tenzij je kiest ze te laten zoals ze zijn.", "Importing the export…": "De export wordt geïmporteerd…", "Importing…": "Importeren…", "APPID": "APPID", @@ -1066,7 +1066,7 @@ "Reasons and warnings": "Redenen en waarschuwingen", "Records missing from the export can only be kept.": "Records die in de export ontbreken, kunnen alleen worden behouden.", "Reload the page and try again.": "Laad de pagina opnieuw en probeer het nog eens.", - "Remove sheets the import does not read, or split the export, and try again.": "Verwijder tabbladen die de import niet leest, of splits de export, en probeer het opnieuw.", + "Remove sheets the import does not read and try again. Or split the export and import every part with \"Keep them as they are\".": "Verwijder tabbladen die de import niet leest en probeer het opnieuw. Of splits de export en importeer elk deel met \"Laat ze zoals ze zijn\".", "Row": "Rij", "Row 1 holds the column names. \"APPID\" and \"Applicatie Naam\" are required; column order does not matter.": "Rij 1 bevat de kolomnamen. \"APPID\" en \"Applicatie Naam\" zijn verplicht; de volgorde van de kolommen maakt niet uit.", "Rows": "Rijen", @@ -1084,7 +1084,7 @@ "The import failed unexpectedly.": "De import is onverwacht mislukt.", "The import mapping cannot run.": "De mapping van de import kan niet worden uitgevoerd.", "The import reads workbooks with the spreadsheet library that ships with OpenRegister. Make sure OpenRegister is installed and enabled.": "De import leest werkmappen met de spreadsheetbibliotheek die met OpenRegister wordt meegeleverd. Zorg dat OpenRegister is geïnstalleerd en ingeschakeld.", - "The import was cancelled. The rows processed before it stopped are kept.": "De import is geannuleerd. De rijen die vóór het stoppen zijn verwerkt, blijven behouden.", + "The import was cancelled. The rows and applications processed before it stopped are kept.": "De import is geannuleerd. De rijen en applicaties die vóór het stoppen zijn verwerkt, blijven behouden.", "The organisation register is not configured, so existing municipalities cannot be listed. You can still type the name of a municipality.": "Het organisatieregister is niet ingesteld, dus bestaande gemeenten kunnen niet worden getoond. U kunt nog steeds de naam van een gemeente typen.", "The server could not be reached.": "De server is niet bereikbaar.", "The sheet \"{sheet}\" has no column \"{column}\".": "Het tabblad \"{sheet}\" heeft geen kolom \"{column}\".", @@ -1105,7 +1105,23 @@ "The stackiq register or its schemas cannot be found. Run Auto Configure at the top of this page, then try again.": "Het stackiq-register of de schema's ervan zijn niet gevonden. Voer bovenaan deze pagina Auto Configure uit en probeer het daarna opnieuw.", "Choose a municipality or enter the name of a new one.": "Kies een gemeente of voer de naam van een nieuwe in.", "No running CMDB import has this id.": "Er loopt geen CMDB-import met deze id.", - "Only keeping records that are missing from the export is supported.": "Alleen het behouden van records die in de export ontbreken, wordt ondersteund.", + "Applications missing from the export can only be archived or kept.": "Applicaties die in de export ontbreken, kunnen alleen worden gearchiveerd of behouden.", + "Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.": "Applicaties die in de export ontbreken, kunnen niet worden gearchiveerd: OpenRegister ontbreekt of is te oud. Kies ervoor ze te behouden, of werk OpenRegister bij.", + "Sheet \"%1$s\" has no column \"%2$s\", so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft geen kolom \"%2$s\", dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%1$s\" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft een APPID zonder opgeslagen waarde in rij %2$s, dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%1$s\" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.": "Tabblad \"%1$s\" heeft een APPID zonder opgeslagen waarde in %2$s rijen (%3$s), dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Sheet \"%s\" was not found, so applications missing from the export are archived, not deleted.": "Tabblad \"%s\" is niet gevonden, dus applicaties die in de export ontbreken, worden gearchiveerd en niet verwijderd.", + "Unarchived": "Uit archief gehaald", + "Restored": "Hersteld", + "Archived": "Gearchiveerd", + "Deleted": "Verwijderd", + "Applications missing from the export": "Applicaties die in de export ontbreken", + "Archive or delete them": "Archiveren of verwijderen", + "Keep them as they are": "Laat ze zoals ze zijn", + "An application of this municipality that is on neither CMDB sheet any more is archived when the sheet \"Gearchiveerde Applicaties\" lists it, and moved to the trash when no sheet does. Both disappear from the lists, from OpenCatalogi and from Portaliq; one that returns in a later export is brought back. When that sheet is missing, has no APPID column, or has an APPID without a cached value, applications are only archived and the result says so. Choose Keep to leave them as they are.": "Een applicatie van deze gemeente die op geen van beide CMDB-tabbladen meer staat, wordt gearchiveerd als het tabblad \"Gearchiveerde Applicaties\" haar noemt, en naar de prullenbak verplaatst als geen enkel tabblad dat doet. Beide verdwijnen uit de lijsten, uit OpenCatalogi en uit Portaliq; een applicatie die in een latere export terugkomt, wordt teruggezet. Ontbreekt dat tabblad, heeft het geen kolom APPID of heeft een APPID geen opgeslagen waarde, dan worden applicaties alleen gearchiveerd en zegt het resultaat dat. Kies Laat ze zoals ze zijn om ze ongemoeid te laten.", + "Applications missing from the export cannot be archived.": "Applicaties die in de export ontbreken, kunnen niet worden gearchiveerd.", + "OpenRegister is missing or too old to archive objects. Choose \"Keep them as they are\" and import again, or update OpenRegister. Nothing was imported.": "OpenRegister ontbreekt of is te oud om objecten te archiveren. Kies \"Laat ze zoals ze zijn\" en importeer opnieuw, of werk OpenRegister bij. Er is niets geïmporteerd.", + "Checking the applications of this municipality against the export": "De applicaties van deze gemeente met de export vergelijken", "Sheet \"%1$s\" has more than %2$s rows.": "Tabblad \"%1$s\" heeft meer dan %2$s rijen.", "Sheet \"%1$s\" has no column \"%2$s\".": "Tabblad \"%1$s\" heeft geen kolom \"%2$s\".", "Stackiq is not configured: the register or its schemas cannot be found.": "Stackiq is niet ingesteld: het register of de schema's ervan zijn niet gevonden.", @@ -1153,15 +1169,15 @@ "The server could not store the uploaded file. The details are in the Nextcloud log.": "De server kon het geüploade bestand niet opslaan. De details staan in het Nextcloud-logboek.", "The file is larger than {size}, the most the import accepts.": "Het bestand is groter dan {size}, het maximum dat de import accepteert.", "The file is larger than the server accepts.": "Het bestand is groter dan de server accepteert.", - "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.": "Een brontabblad mag hoogstens {limit} rijen bevatten. Splits de export en importeer de delen na elkaar.", - "Split the export and import the parts one after the other.": "Splits de export en importeer de delen na elkaar.", + "A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with \"Keep them as they are\".": "Een brontabblad mag hoogstens {limit} rijen bevatten. Splits de export en importeer de delen na elkaar met \"Laat ze zoals ze zijn\".", + "Split the export and import the parts one after the other with \"Keep them as they are\".": "Splits de export en importeer de delen na elkaar met \"Laat ze zoals ze zijn\".", "Excel workbook (.xlsx) with the sheet \"{first}\" or \"{second}\". By default the file may be at most {size}.": "Excel-werkmap (.xlsx) met het tabblad \"{first}\" of \"{second}\". Standaard mag het bestand hoogstens {size} zijn.", - "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read; other sheets, including the \"Invoer\" sheets, are ignored.": "De tabbladen \"{first}\" (applicaties zonder geregeld beheer) en \"{second}\" (met geregeld beheer) worden gelezen; andere tabbladen, ook de \"Invoer\"-tabbladen, worden genegeerd.", + "The sheets \"{first}\" (applications without arranged maintenance) and \"{second}\" (with arranged maintenance) are read, and of the sheet \"{archive}\" only the APPID column; other sheets, including the \"Invoer\" sheets, are ignored.": "De tabbladen \"{first}\" (applicaties zonder geregeld beheer) en \"{second}\" (met geregeld beheer) worden gelezen, en van het tabblad \"{archive}\" alleen de kolom APPID; andere tabbladen, ook de \"Invoer\"-tabbladen, worden genegeerd.", "The page got no answer from the import.": "De pagina kreeg geen antwoord van de import.", "The connection was cut off before the import answered, so it may still be running or may have finished. Wait a few minutes and check the applications of the municipality before you import again. Importing the same file again creates no duplicates.": "De verbinding werd verbroken voordat de import antwoordde, dus de import kan nog bezig zijn of al klaar zijn. Wacht een paar minuten en controleer de applicaties van de gemeente voordat u opnieuw importeert. Hetzelfde bestand opnieuw importeren maakt geen dubbele records aan.", "The import cannot be cancelled now: the server has not started its rows yet, or has already finished them. If the import keeps running, press Cancel import again in a moment.": "De import kan nu niet worden geannuleerd: de server is nog niet aan de rijen begonnen, of is er al mee klaar. Loopt de import door, druk dan zo dadelijk opnieuw op Import annuleren.", "The import could not be cancelled: {reason}": "De import kon niet worden geannuleerd: {reason}", - "Cancelling the import. It stops before the next row; the rows already processed stay imported.": "De import wordt geannuleerd. Hij stopt voor de volgende rij; de rijen die al verwerkt zijn blijven geïmporteerd.", + "Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.": "De import wordt geannuleerd. Hij stopt voor de volgende rij of, tijdens het vergelijken van de applicaties met de export, voor de volgende applicatie; wat al verwerkt is blijft staan.", "Showing {shown} of {total} rows.": "{shown} van {total} rijen weergegeven.", "Show {count} more rows": "Toon nog {count} rijen", "The request field \"{field}\" has a value the import does not accept.": "Het veld \"{field}\" van het verzoek heeft een waarde die de import niet accepteert.", @@ -1192,13 +1208,20 @@ "conflict: the application with this import key is used by another organisation, so it is not changed": "conflict: de applicatie met deze importsleutel wordt door een andere organisatie gebruikt en wordt daarom niet gewijzigd", "When on, a re-import overwrites the application's name, descriptions, application type, hosting model, BBN level, source fields and supplier, and the usage's status, phase-out date and business owner, with the values from the export. The usage's TIME classification and internal note are only set when the usage is created or the field is empty, so changes made in stackiq stay.": "Staat dit aan, dan overschrijft een herhaalde import de naam, de omschrijvingen, het applicatietype, het hostingmodel, het BBN-niveau, de bronvelden en de leverancier van de applicatie, en de status, de uitfaseerdatum en de functioneel eigenaar van het gebruik, met de waarden uit de export. De TIME-classificatie en de interne notitie van het gebruik worden alleen gezet wanneer het gebruik wordt aangemaakt of het veld leeg is, zodat wijzigingen in stackiq blijven staan.", "An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported.": "Een .xlsx-bestand is gecomprimeerd: een bestand van een paar MB kan uitgepakt veel groter zijn. Er is niets geïmporteerd.", - "Split the rows over two copies of the export and import them one after the other.": "Verdeel de rijen over twee kopieën van de export en importeer die na elkaar.", + "Split the rows over two copies of the export and import both with \"Keep them as they are\": an import that archives treats the applications of the other copy as missing.": "Verdeel de rijen over twee kopieën van de export en importeer beide met \"Laat ze zoals ze zijn\": een import die archiveert, ziet de applicaties van de andere kopie als ontbrekend.", "The sheet \"{sheet}\" is too large to import: unpacked it is {size}, and the import reads at most {limit} of one sheet.": "Het tabblad \"{sheet}\" is te groot om te importeren: uitgepakt is het {size}, en de import leest van één tabblad hooguit {limit}.", "The part {part} of the workbook is too large to import: unpacked it is {size}, and the import reads at most {limit} of one part.": "Het onderdeel {part} van de werkmap is te groot om te importeren: uitgepakt is het {size}, en de import leest van één onderdeel hooguit {limit}.", - "Delete the sheets the import does not read from a copy of the export (it reads only \"{first}\" and \"{second}\"), and import that copy.": "Verwijder in een kopie van de export de tabbladen die de import niet leest (hij leest alleen \"{first}\" en \"{second}\"), en importeer die kopie.", + "Delete the sheets the import does not read from a copy of the export (it reads \"{first}\" and \"{second}\", and the APPID column of \"{archive}\"), and import that copy.": "Verwijder in een kopie van de export de tabbladen die de import niet leest (hij leest \"{first}\" en \"{second}\", en de kolom APPID van \"{archive}\"), en importeer die kopie.", "The workbook holds more than {count} different texts, the most the import reads.": "De werkmap bevat meer dan {count} verschillende teksten, het maximum dat de import leest.", "Together, the cells of the workbook reference more than {size} of shared text, the most the import reads.": "Samen verwijzen de cellen van de werkmap naar meer dan {size} gedeelde tekst, het maximum dat de import leest.", "In development since": "In ontwikkeling sinds", - "In use since": "In gebruik sinds" + "In use since": "In gebruik sinds", + "{count} archived": "{count} gearchiveerd", + "{count} unarchived": "{count} uit het archief gehaald", + "{count} deleted": "{count} verwijderd", + "{count} restored": "{count} teruggezet", + "Applications: {changes}.": "Applicaties: {changes}.", + "This tab lists the whole archive. It ignores the GEMMA filters and the search term.": "Dit tabblad toont het hele archief. De GEMMA-filters en de zoekterm worden genegeerd.", + "Applications no longer in the export are archived or moved to the trash. The (i) button above explains when.": "Applicaties die niet meer in de export staan, worden gearchiveerd of naar de prullenbak verplaatst. De (i)-knop bovenaan legt uit wanneer." } } diff --git a/lib/Controller/CmdbImportController.php b/lib/Controller/CmdbImportController.php index 7d1c00a8f..bc7869cbe 100644 --- a/lib/Controller/CmdbImportController.php +++ b/lib/Controller/CmdbImportController.php @@ -86,7 +86,8 @@ public function __construct( * Import a TOPdesk CMDB export for one municipality. * * Multipart fields: `cmdbFile`, `municipalityUuid` or `municipalityName`, - * `updateExisting` (default true), `publish` (default true), `missingRecords` (only `keep`) and + * `updateExisting` (default true), `publish` (default true), `missingRecords` (`keep` or `archive`, + * default `archive`) and * `operationId` (pattern `cmdb-` plus 8 to 64 letters, digits or hyphens). * * @return JSONResponse The report (200), or an error envelope with the contract code. @@ -94,6 +95,7 @@ public function __construct( * @auth admin-only the import writes with RBAC and multitenancy off, across tenants, so only a full Nextcloud admin may run it. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-8 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 */ public function import(): JSONResponse { try { @@ -247,15 +249,21 @@ protected function iniBytes(string $name): ?int { * @return array{path: string, options: array}|JSONResponse The import input, or the first error. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-8 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 */ private function readOptions(string $path, string $fileName): array|JSONResponse { - $missingRecords = $this->stringParam(name: 'missingRecords', default: 'keep'); + $missingRecords = $this->stringParam(name: 'missingRecords', default: CmdbExportImportService::DEFAULT_MISSING_RECORDS); if ($missingRecords === null) { return $this->invalidField(field: 'missingRecords'); } - if ($this->importService->supportsMissingRecords(mode: $missingRecords) === false) { - return $this->error(code: 'MISSING_RECORDS_UNSUPPORTED', status: Http::STATUS_UNPROCESSABLE_ENTITY, details: ['accepted' => ['keep']]); + $accepted = $this->importService->missingRecordsModes(); + if (in_array($missingRecords, $accepted, true) === false) { + return $this->error( + code: 'MISSING_RECORDS_UNSUPPORTED', + status: Http::STATUS_UNPROCESSABLE_ENTITY, + details: ['accepted' => $accepted] + ); } // An unrecognised value is refused rather than read as true: true is the mode that overwrites. @@ -293,6 +301,7 @@ private function readOptions(string $path, string $fileName): array|JSONResponse 'municipalityName' => $municipalityName, 'updateExisting' => $updateExisting, 'publish' => $publish, + 'missingRecords' => $missingRecords, 'operationId' => $this->request->getParam('operationId'), 'fileName' => basename(str_replace('\\', '/', $fileName)), ], @@ -394,7 +403,7 @@ private function message(string $code, array $details): string { 'NO_FILE_UPLOADED' => $this->l10n->t('No file was uploaded.'), 'NOT_XLSX' => $this->l10n->t('The file is not an Excel workbook (.xlsx).'), 'FILE_TOO_LARGE' => $this->l10n->t('The file is larger than the maximum of %s MB.', [$megabytes]), - 'MISSING_RECORDS_UNSUPPORTED' => $this->l10n->t('Only keeping records that are missing from the export is supported.'), + 'MISSING_RECORDS_UNSUPPORTED' => $this->l10n->t('Applications missing from the export can only be archived or kept.'), 'FIELD_INVALID' => $this->fieldMessage(field: (string)($details['field'] ?? ''), accepted: $accepted), 'MUNICIPALITY_REQUIRED' => $this->l10n->t('Choose a municipality or enter the name of a new one.'), 'MUNICIPALITY_INVALID' => $this->l10n->t('The chosen organisation is not a municipality.'), @@ -410,6 +419,9 @@ private function message(string $code, array $details): string { 'SCHEMA_OUTDATED' => $this->l10n->t('The stackiq register is out of date; import its configuration again.'), 'IMPORT_IN_PROGRESS' => $this->l10n->t('Another CMDB import is running; try again when it has finished.'), 'MUNICIPALITY_AMBIGUOUS' => $this->l10n->t('Several municipalities have this name; choose one from the list.'), + 'ARCHIVE_UNAVAILABLE' => $this->l10n->t( + 'Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.' + ), default => $this->l10n->t('The import failed. The details are in the Nextcloud log.'), }; }//end message() diff --git a/lib/Exception/CmdbImportException.php b/lib/Exception/CmdbImportException.php index 31404c505..65127a9fe 100644 --- a/lib/Exception/CmdbImportException.php +++ b/lib/Exception/CmdbImportException.php @@ -50,6 +50,7 @@ class CmdbImportException extends RuntimeException { public const SCHEMA_OUTDATED = 'SCHEMA_OUTDATED'; public const IMPORT_IN_PROGRESS = 'IMPORT_IN_PROGRESS'; public const MUNICIPALITY_AMBIGUOUS = 'MUNICIPALITY_AMBIGUOUS'; + public const ARCHIVE_UNAVAILABLE = 'ARCHIVE_UNAVAILABLE'; /** * HTTP status per error code. @@ -70,6 +71,7 @@ class CmdbImportException extends RuntimeException { self::SCHEMA_OUTDATED => 503, self::IMPORT_IN_PROGRESS => 409, self::MUNICIPALITY_AMBIGUOUS => 422, + self::ARCHIVE_UNAVAILABLE => 503, ]; /** diff --git a/lib/Service/Cmdb/CmdbImportReport.php b/lib/Service/Cmdb/CmdbImportReport.php index 730365fde..2615c8b07 100644 --- a/lib/Service/Cmdb/CmdbImportReport.php +++ b/lib/Service/Cmdb/CmdbImportReport.php @@ -36,6 +36,10 @@ class CmdbImportReport { public const UNCHANGED = 'unchanged'; public const SKIPPED = 'skipped'; public const FAILED = 'failed'; + public const ARCHIVED = 'archived'; + public const UNARCHIVED = 'unarchived'; + public const DELETED = 'deleted'; + public const RESTORED = 'restored'; /** * The row entries, in processing order. @@ -90,7 +94,7 @@ public function __construct( * Add one row outcome. * * @param string $sheet The sheet name. - * @param int $row The 1-based sheet row number. + * @param int $row The 1-based sheet row number, or 0 for an application the reconciliation archived or deleted. * @param string $appId The APPID ('' when missing). * @param string $name The application name ('' when missing). * @param string $outcome One of the outcome constants. @@ -180,14 +184,25 @@ public function markCancelled(): void { }//end markCancelled() /** - * The number of rows processed so far. + * Whether the run stopped on a cancel. + * + * @return bool + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function isCancelled(): bool { + return $this->cancelled; + }//end isCancelled() + + /** + * The number of sheet rows processed so far; reconciliation entries (row 0) are not sheet rows. * * @return int * * @spec openspec/changes/cmdb-export-import/tasks.md#task-7 */ public function processed(): int { - return count($this->rows); + return count(array_filter($this->rows, static fn (array $row): bool => $row['row'] > 0)); }//end processed() /** @@ -195,21 +210,28 @@ public function processed(): int { * * `unpublished` counts the modules created without a publication date; * a row whose module was created but whose usage then failed counts too. + * `processed` counts sheet rows only; every outcome, also of a + * reconciliation entry, is counted under its own key. * * @return array{rowsRead: int, processed: int, created: int, updated: int, unchanged: int, skipped: int, failed: int, - * warnings: int, unpublished: int} + * archived: int, unarchived: int, deleted: int, restored: int, warnings: int, unpublished: int} * * @spec openspec/changes/cmdb-export-import/tasks.md#task-7 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 */ public function summary(): array { $summary = [ 'rowsRead' => $this->rowsRead, - 'processed' => count($this->rows), + 'processed' => $this->processed(), self::CREATED => 0, self::UPDATED => 0, self::UNCHANGED => 0, self::SKIPPED => 0, self::FAILED => 0, + self::ARCHIVED => 0, + self::UNARCHIVED => 0, + self::DELETED => 0, + self::RESTORED => 0, 'warnings' => 0, 'unpublished' => $this->unpublished, ]; diff --git a/lib/Service/Cmdb/CmdbWorkbookReader.php b/lib/Service/Cmdb/CmdbWorkbookReader.php index 3ba065a4d..4b80393b8 100644 --- a/lib/Service/Cmdb/CmdbWorkbookReader.php +++ b/lib/Service/Cmdb/CmdbWorkbookReader.php @@ -175,8 +175,11 @@ public function isAvailable(): bool { * @param CmdbImportProfile $profile The import profile. * * @return array `rows` (list of {sheet, row, cells, uncached}), `importWarnings` - * (list of {sheet, message}), `date1904` (bool) and `archive` - * ({sheet, present, keyColumnMissing, appIds, uncachedRows}). + * (list of {sheet, message}), `date1904` (bool), `archive` + * ({sheet, present, keyColumnMissing, appIds, uncachedRows}) and `uncachedKeyRows` + * (source sheet => numbers of the rows dropped as empty because their key is a + * formula without a cached value; a row with other content is in `rows`, its + * `uncached` lists the key). * * @throws CmdbImportException WORKBOOK_TOO_LARGE, READER_UNAVAILABLE, NOT_XLSX, NO_SOURCE_SHEET, * MISSING_COLUMN or TOO_MANY_ROWS. @@ -369,7 +372,15 @@ private function readBounded(string $path, CmdbImportProfile $profile, object $s $spreadsheet = $this->load(path: $path, sheetNames: $loaded, filter: new CmdbReadFilter(lastRow: $lastRow, columns: $letters)); try { - $rows = $this->readSourceRows(spreadsheet: $spreadsheet, sheetNames: $present, columnsPerSheet: $resolved['columns'], limit: $limit); + $uncachedKeyRows = []; + $rows = $this->readSourceRows( + spreadsheet: $spreadsheet, + sheetNames: $present, + columnsPerSheet: $resolved['columns'], + limit: $limit, + keyColumn: $profile->keyColumn(), + uncachedKeyRows: $uncachedKeyRows + ); if ($archiveColumns !== []) { $unseen = []; $archiveRows = $this->readRows( @@ -389,7 +400,13 @@ private function readBounded(string $path, CmdbImportProfile $profile, object $s $spreadsheet->disconnectWorksheets(); } - return ['rows' => $rows, 'importWarnings' => $resolved['warnings'], 'date1904' => $date1904, 'archive' => $archive]; + return [ + 'rows' => $rows, + 'importWarnings' => $resolved['warnings'], + 'date1904' => $date1904, + 'archive' => $archive, + 'uncachedKeyRows' => $uncachedKeyRows, + ]; }//end readBounded() /** @@ -434,20 +451,38 @@ private function sourceSheets(string $path, CmdbImportProfile $profile): array { * @param array $sheetNames The source sheets the workbook has. * @param array> $columnsPerSheet Sheet name => column letter => column name. * @param int $limit The row limit per sheet. + * @param string $keyColumn The key column's name. + * @param array> $uncachedKeyRows Filled, per sheet, with the number of each row that was + * dropped as empty only because its key is a formula + * without a cached value. * * @return array> See read(). * * @throws CmdbImportException TOO_MANY_ROWS. */ - private function readSourceRows(object $spreadsheet, array $sheetNames, array $columnsPerSheet, int $limit): array { + private function readSourceRows( + object $spreadsheet, + array $sheetNames, + array $columnsPerSheet, + int $limit, + string $keyColumn, + array &$uncachedKeyRows, + ): array { $rows = []; foreach ($sheetNames as $sheetName) { + $unseen = []; $sheetRows = $this->readRows( worksheet: $spreadsheet->getSheetByName($sheetName), columns: $columnsPerSheet[$sheetName], sheetName: $sheetName, - limit: $limit + limit: $limit, + unseen: $unseen, + keyColumn: $keyColumn ); + if ($unseen !== []) { + $uncachedKeyRows[$sheetName] = array_values((array)$unseen); + } + array_push($rows, ...$sheetRows); } @@ -732,12 +767,20 @@ private function resolveColumns(object $worksheet, array $referenced): array { * @param int $limit The maximum number of non-empty rows. * @param array|null $unseen Filled with the number of each row that read as empty only because * a formula in it has no cached value; null to not collect them. + * @param string|null $keyColumn When set, only a row whose uncached columns include this one is collected. * * @return array, uncached: array}> * * @throws CmdbImportException TOO_MANY_ROWS. */ - private function readRows(object $worksheet, array $columns, string $sheetName, int $limit, ?array &$unseen = null): array { + private function readRows( + object $worksheet, + array $columns, + string $sheetName, + int $limit, + ?array &$unseen = null, + ?string $keyColumn = null, + ): array { $rows = []; $lastRow = (int)$worksheet->getHighestDataRow(); for ($rowNumber = 2; $rowNumber <= $lastRow; $rowNumber++) { @@ -747,7 +790,7 @@ private function readRows(object $worksheet, array $columns, string $sheetName, rowNumber: $rowNumber ); if ($empty === true) { - if ($uncached !== [] && $unseen !== null) { + if ($uncached !== [] && $unseen !== null && ($keyColumn === null || in_array($keyColumn, $uncached, true) === true)) { $unseen[] = $rowNumber; } diff --git a/lib/Service/CmdbExportImportService.php b/lib/Service/CmdbExportImportService.php index 1efc049d7..1cce85f47 100644 --- a/lib/Service/CmdbExportImportService.php +++ b/lib/Service/CmdbExportImportService.php @@ -30,7 +30,15 @@ * - `publicationDate` is set to the import's start on create, unless the * admin chose not to publish (`publish` false), and never written on * update; neither is `depublicationDate`. - * - Records missing from a newer export are left untouched. + * - Records missing from a newer export are left untouched with + * `missingRecords` `keep`. With `archive` (the default) the municipality's + * usages and their modules are reconciled after the last row: archived + * when the APPID is on the archive sheet, soft-deleted when it is on no + * sheet, archived only when the workbook has no archive sheet, the sheet + * has no APPID column or an APPID in it has no cached value + * (openspec/changes/cmdb-import-archive-reconciliation, design D5). A + * module or usage matched among archived or soft-deleted objects is + * unarchived or restored before it is updated, never duplicated. * - Owners become contact persons, never Nextcloud user accounts, and no * report entry or log line carries an owner name or e-mail address. * - One import runs per register at a time: every match is find-then-create, @@ -55,6 +63,7 @@ use DateTimeImmutable; use DateTimeZone; +use JsonSerializable; use OCA\OpenRegister\Contract\ObjectServiceInterface; use OCA\Stackiq\Exception\CmdbImportException; use OCA\Stackiq\Service\Cmdb\CmdbImportProfile; @@ -68,6 +77,7 @@ use OCP\Lock\LockedException; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use RuntimeException; use Throwable; /** @@ -75,6 +85,12 @@ * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 * + * @phpstan-type ArchiveSheet array{sheet: string, present: bool, keyColumnMissing: bool, uncachedRows: array, keys: array} + * + * @phpstan-type ScopeExtra array{schemaKey: string, uuid: string, state: array{archived: bool, deleted: bool}} + * @phpstan-type ScopeStates array + * @phpstan-type ScopeEntry array{key: string, usage: object, module: object, states: ScopeStates, extras?: array} + * * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) One import run resolves four linked * object kinds per row (supplier, module, owners, usage), each with its own match rule, * create-only fields and run cache. Splitting them over several classes would hand the @@ -111,6 +127,58 @@ class CmdbExportImportService { */ public const SCHEMA_MAPPER_CLASS = 'OCA\OpenRegister\Db\SchemaMapper'; + /** + * OpenRegister's archive handler, behind POST and DELETE .../archive (not a public contract). + */ + public const ARCHIVE_HANDLER_CLASS = 'OCA\OpenRegister\Service\Object\ArchiveHandler'; + + /** + * OpenRegister's object mapper, whose restoreObject() empties the trash of one object (not a public contract). + */ + public const OBJECT_MAPPER_CLASS = 'OCA\OpenRegister\Db\MagicMapper'; + + /** + * What happens to records missing from the export when the request does not say. + */ + public const DEFAULT_MISSING_RECORDS = 'archive'; + + /** + * The start of every archive reason the import writes, so the audit trail says who archived and why. + */ + private const REASON_PREFIX = 'cmdb-import: '; + + /** + * The search lens that lists the archive alone (OpenRegister's `_archived=true`). + * + * The state of a stored object is read from the lens that found it, never + * from the archive marker of a search result: OpenRegister's list search + * does not hydrate that marker (design D9). + * + * @var array + */ + private const ARCHIVED_LENS = ['_archived' => 'true']; + + /** + * The state of an object found without a lens. + * + * @var array{archived: bool, deleted: bool} + */ + private const STATE_WORKING = ['archived' => false, 'deleted' => false]; + + /** + * The state of an object found with ARCHIVED_LENS. + * + * @var array{archived: bool, deleted: bool} + */ + private const STATE_ARCHIVED = ['archived' => true, 'deleted' => false]; + + /** + * The state of an object found in the trash; whether it was archived too is read when it is restored. + * + * @var array{archived: bool, deleted: bool} + */ + private const STATE_DELETED = ['archived' => false, 'deleted' => true]; + /** * The properties every match search and the module key rely on, per schema. * @@ -177,6 +245,13 @@ class CmdbExportImportService { */ private const UNMATCHED_STATUSES = ['merged', 'Inactive']; + /** + * How many row numbers an archive-sheet warning names before it cuts the list. + * + * @var int + */ + private const WARNED_ROWS = 10; + /** * Page size for loading the organisations a name may match. */ @@ -222,6 +297,27 @@ class CmdbExportImportService { */ private array $winningSheets = []; + /** + * The CMDB-sheet rows of this run whose key column the reader could not see (a formula without a cached value). + * + * @var array> Sheet name => row numbers. + */ + private array $unreadableKeyRows = []; + + /** + * OpenRegister's archive handler and object mapper, once resolved. + * + * @var array{handler: object, mapper: object}|null + */ + private ?array $archiveServices = null; + + /** + * The register's soft-deleted modules and usages, read once per run: schema key => uuid => entity. + * + * @var array{module: array, usage: array}|null + */ + private ?array $trash = null; + /** * Contact UIDs by e-mail or display name, per run. * @@ -300,17 +396,16 @@ public function assertXlsx(string $path, string $fileName): void { }//end assertXlsx() /** - * Whether a `missingRecords` value is supported (only `keep`). - * - * @param string $mode The requested value. + * The accepted `missingRecords` values: the profile's modes (`keep`, `archive`); any other is refused. * - * @return bool + * @return array * * @spec openspec/changes/cmdb-export-import/tasks.md#task-8 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 */ - public function supportsMissingRecords(string $mode): bool { - return $mode === 'keep'; - }//end supportsMissingRecords() + public function missingRecordsModes(): array { + return $this->profile->missingRecordsModes(); + }//end missingRecordsModes() /** * Ask a running import to stop between rows. @@ -349,19 +444,21 @@ public function requestCancel(string $operationId): bool { * * @param string $path The xlsx file, already checked by assertXlsx(). * @param array $options municipalityUuid, municipalityName, updateExisting, publish, - * operationId, and fileName (the upload's name, for the audit log line). + * missingRecords (keep or archive, default archive), operationId, + * and fileName (the upload's name, for the audit log line). * * @return array The report (contract.md). * * @throws CmdbImportException MAPPING_UNAVAILABLE, NOT_CONFIGURED, SCHEMA_OUTDATED, IMPORT_IN_PROGRESS, * WORKBOOK_TOO_LARGE, READER_UNAVAILABLE, NOT_XLSX, NO_SOURCE_SHEET, * MISSING_COLUMN, TOO_MANY_ROWS, MUNICIPALITY_REQUIRED, - * MUNICIPALITY_INVALID or MUNICIPALITY_AMBIGUOUS. + * MUNICIPALITY_INVALID, MUNICIPALITY_AMBIGUOUS or ARCHIVE_UNAVAILABLE. * @throws \Exception An unexpected OpenRegister error outside a row, such as * creating the municipality; rows catch their own. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 * @spec openspec/changes/cmdb-export-import/tasks.md#task-7 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 */ public function import(string $path, array $options): array { $this->resetRun(); @@ -371,6 +468,10 @@ public function import(string $path, array $options): array { $this->profile->load(); $this->engine = $this->resolveEngine(); $this->coordinates = $this->resolveCoordinates(); + if (self::missingRecordsFrom(options: $options) === 'archive') { + // Refused before the lock and the file: an import that cannot reconcile must not half-run. + $this->archiveServices(); + } $lock = $this->acquireImportLock(register: $this->coordinates['register']); try { @@ -442,29 +543,21 @@ private function acquireImportLock(int $register): string { * @return array The report. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-7 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 */ private function runImport(string $path, array $options, string $startedAt): array { $workbook = $this->reader->read(path: $path, profile: $this->profile); $municipality = $this->resolveMunicipality(options: $options); $operationId = $this->operationIdFrom(options: $options); + $missingRecords = self::missingRecordsFrom(options: $options); $rows = array_values($workbook['rows']); $report = new CmdbImportReport(operationId: $operationId, rowsRead: count($rows)); $report->setMunicipality(uuid: $municipality['uuid'], name: $municipality['name'], created: $municipality['created']); $report->addImportWarnings(warnings: $this->translateImportWarnings(warnings: $workbook['importWarnings'])); - if ($municipality['created'] === true) { - $report->addImportWarnings( - warnings: [ - [ - 'sheet' => '', - 'message' => $this->l10n->t( - 'No municipality named "%s" was found, so it was created. Check the name if you meant an existing one.', - [$municipality['name']] - ), - ], - ] - ); - } + $archive = $this->archiveSheet(workbook: $workbook); + $this->unreadableKeyRows = $this->unreadableKeyRows(rows: $rows, droppedRows: (array)($workbook['uncachedKeyRows'] ?? [])); + $this->addRunWarnings(report: $report, municipality: $municipality, archive: $archive, missingRecords: $missingRecords); $this->progressTracker->startOperation( operationType: self::OPERATION_TYPE, @@ -488,6 +581,7 @@ private function runImport(string $path, array $options, string $startedAt): arr 'municipalityCreated' => $municipality['created'], 'updateExisting' => $updateExisting, 'publish' => $publish, + 'missingRecords' => $missingRecords, ]; $this->logger->info('CmdbExportImportService: import started', array_merge($audit, ['rows' => count($rows)])); try { @@ -507,6 +601,16 @@ private function runImport(string $path, array $options, string $startedAt): arr $this->progressTracker->updateProgress(processedItems: ($index + 1)); } + if ($missingRecords === 'archive' && $report->isCancelled() === false) { + $this->reconcile( + municipalityUuid: $municipality['uuid'], + archive: $archive, + operationId: $operationId, + processedItems: count($rows), + report: $report + ); + } + $result = $report->toArray(); $this->finishOperation(report: $report->toStoredArray(maxRows: self::STORED_REPORT_ROWS)); } catch (Throwable $e) { @@ -528,6 +632,86 @@ private function runImport(string $path, array $options, string $startedAt): arr return $result; }//end runImport() + /** + * Add the run's own warnings. + * + * An archive sheet that is missing, has no key column or has unreadable keys, a CMDB sheet with + * unreadable keys, and a created municipality. + * + * @param CmdbImportReport $report The report. + * @param array{uuid: string, name: string, created: bool} $municipality The consumer, as resolveMunicipality() found it. + * @param ArchiveSheet $archive The archive sheet. + * @param string $missingRecords The import's `missingRecords` mode. + * + * @return void + * + * @spec openspec/changes/archive/2026-10-05-cmdb-export-import/tasks.md#task-7 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function addRunWarnings(CmdbImportReport $report, array $municipality, array $archive, string $missingRecords): void { + if ($missingRecords === 'archive' && ($archive['present'] === false || $archive['uncachedRows'] !== [])) { + $report->addImportWarnings(warnings: [['sheet' => $archive['sheet'], 'message' => $this->archiveSheetWarning(archive: $archive)]]); + } + + if ($missingRecords === 'archive') { + foreach ($this->unreadableKeyRows as $sheet => $rowNumbers) { + $cmdbSheet = ['sheet' => $sheet, 'keyColumnMissing' => false, 'uncachedRows' => $rowNumbers]; + $report->addImportWarnings(warnings: [['sheet' => $sheet, 'message' => $this->archiveSheetWarning(archive: $cmdbSheet)]]); + } + } + + if ($municipality['created'] === true) { + $report->addImportWarnings( + warnings: [ + [ + 'sheet' => '', + 'message' => $this->l10n->t( + 'No municipality named "%s" was found, so it was created. Check the name if you meant an existing one.', + [$municipality['name']] + ), + ], + ] + ); + } + }//end addRunWarnings() + + /** + * The CMDB-sheet rows whose key column is a formula without a cached value, per sheet. + * + * Such a row reads as empty and is skipped, so its application may be + * missing from `winningSheets` although it is on a CMDB sheet. A run with + * one is not authoritative for a deletion, as with an archive sheet that + * has an unreadable key. + * + * @param array}> $rows The reader rows. + * @param array> $droppedRows The rows the reader dropped as empty because their key + * has no cached value (they are not in `$rows`), per sheet. + * + * @return array> Sheet name => row numbers. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function unreadableKeyRows(array $rows, array $droppedRows): array { + $key = $this->profile->keyColumn(); + $found = []; + foreach ($droppedRows as $sheet => $rowNumbers) { + $found[(string)$sheet] = array_map('intval', array_values((array)$rowNumbers)); + } + + foreach ($rows as $row) { + if (in_array($key, ($row['uncached'] ?? []), true) === true) { + $found[$row['sheet']][] = $row['row']; + } + } + + foreach ($found as $sheet => $rowNumbers) { + sort($rowNumbers); + $found[$sheet] = $rowNumbers; + } + + return $found; + }//end unreadableKeyRows() + /** * Process one row in its own error boundary and add its outcome. * @@ -748,11 +932,20 @@ private function translateImportWarnings(array $warnings): array { * @param string $module The module outcome. * @param string $usage The usage outcome. * - * @return string created when the module was created, updated when anything was saved, else unchanged. + * @return string restored when either was restored from the trash, else unarchived when either was + * unarchived, else created when the module was created, updated when anything was saved, + * else unchanged. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-7 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 */ private static function rowOutcome(string $module, string $usage): string { + foreach ([CmdbImportReport::RESTORED, CmdbImportReport::UNARCHIVED] as $revived) { + if ($module === $revived || $usage === $revived) { + return $revived; + } + } + if ($module === CmdbImportReport::CREATED) { return CmdbImportReport::CREATED; } @@ -781,7 +974,7 @@ private function finishOperation(array $report): void { } $this->progressTracker->completeOperation(finalStatistics: ['report' => $report]); - // A cancel that came in after the last row's check has nothing left to stop. + // A cancel that came in after the last check has nothing left to stop. $this->progressTracker->clearCancelRequested(operationId: (string)$report['operationId']); }//end finishOperation() @@ -1028,19 +1221,19 @@ private function resolveManufacturer(array $values, int $rowNumber): ?string { * @param string $matchKey The APPID's match key. * @param array{updateExisting: bool} $options Whether a match is updated. * - * @return array{existing: object|null, uuid: string|null, skipReason: string|null} The stored module, the uuid - * to report for a skipped row, - * and why the row is skipped. + * @return array{existing: array{entity: object, state: array{archived: bool, deleted: bool}}|null, uuid: string|null, skipReason: string|null} + * The stored module with its state, the uuid to report for a skipped row, and why the row is skipped. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 */ private function matchModule(string $externalKey, string $municipalityUuid, string $matchKey, array $options): array { - $existing = $this->findOne(schemaKey: 'module', filters: ['externalKey' => $externalKey]); + $existing = $this->findInAnyState(schemaKey: 'module', filters: ['externalKey' => $externalKey]); if ($existing === null) { return ['existing' => null, 'uuid' => null, 'skipReason' => null]; } - $uuid = (string)$existing->getUuid(); + $uuid = (string)$existing['entity']->getUuid(); if ($this->moduleBelongsTo(moduleUuid: $uuid, municipalityUuid: $municipalityUuid) === false) { $this->logger->warning( 'CmdbExportImportService: import key belongs to a module another organisation uses; row not imported', @@ -1062,7 +1255,8 @@ private function matchModule(string $externalKey, string $municipalityUuid, stri * * @param array $data The mapped module fields. * @param string $externalKey The module's import key. - * @param object|null $existing The module matchModule() found, or null to create one. + * @param array{entity: object, state: array{archived: bool, deleted: bool}}|null $existing The module matchModule() + * found, or null to create one. * @param string|null $providerUuid The supplier, when there is one. * @param string|null $publicationDate ISO start time of the import for a module that is published * when created, or null to create it unpublished. @@ -1075,7 +1269,7 @@ private function matchModule(string $externalKey, string $municipalityUuid, stri private function importModule( array $data, string $externalKey, - ?object $existing, + ?array $existing, ?string $providerUuid, ?string $publicationDate, CmdbImportReport $report, @@ -1102,6 +1296,8 @@ private function importModule( * together with the usages: the module must already have a usage whose * consumer is this municipality, or, before the first import, no usage * at all. A module only another organisation uses is never taken over. + * This municipality's usage counts in every state: an archived or + * soft-deleted usage still says the module is the municipality's. * * @param string $moduleUuid The module found by its import key. * @param string $municipalityUuid The consumer of this import. @@ -1109,9 +1305,10 @@ private function importModule( * @return bool * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 */ private function moduleBelongsTo(string $moduleUuid, string $municipalityUuid): bool { - if ($this->findOne(schemaKey: 'usage', filters: ['consumer' => $municipalityUuid, 'module' => $moduleUuid]) !== null) { + if ($this->findInAnyState(schemaKey: 'usage', filters: ['consumer' => $municipalityUuid, 'module' => $moduleUuid]) !== null) { return true; } @@ -1123,19 +1320,22 @@ private function moduleBelongsTo(string $moduleUuid, string $municipalityUuid): * * @param array $data The mapped module fields. * @param string $externalKey The match key. - * @param object|null $existing The stored module, or null to create one. + * @param array{entity: object, state: array{archived: bool, deleted: bool}}|null $existing The stored module and its + * state, or null to create one. * @param string|null $providerUuid The supplier, when there is one. * @param string|null $publicationDate ISO start time of the import for a module that is published * when created, or null to create it unpublished. * - * @return array{uuid: string, outcome: string} Outcome created, updated or unchanged. + * @return array{uuid: string, outcome: string} Outcome created, updated or unchanged, or restored or + * unarchived when the stored module was revived first. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 */ private function upsertModule( array $data, string $externalKey, - ?object $existing, + ?array $existing, ?string $providerUuid, ?string $publicationDate, ): array { @@ -1154,14 +1354,15 @@ private function upsertModule( return ['uuid' => $this->save(schemaKey: 'module', data: $create, uuid: null), 'outcome' => CmdbImportReport::CREATED]; } - $uuid = (string)$existing->getUuid(); - $merged = $this->merge(target: 'module', stored: $existing->getObject(), mapped: $data); + $uuid = (string)$existing['entity']->getUuid(); + $revived = $this->revive(found: $existing); + $merged = $this->merge(target: 'module', stored: $revived['entity']->getObject(), mapped: $data); if ($merged === null) { - return ['uuid' => $uuid, 'outcome' => CmdbImportReport::UNCHANGED]; + return ['uuid' => $uuid, 'outcome' => ($revived['outcome'] ?? CmdbImportReport::UNCHANGED)]; } $this->save(schemaKey: 'module', data: $merged, uuid: $uuid); - return ['uuid' => $uuid, 'outcome' => CmdbImportReport::UPDATED]; + return ['uuid' => $uuid, 'outcome' => ($revived['outcome'] ?? CmdbImportReport::UPDATED)]; }//end upsertModule() /** @@ -1173,9 +1374,11 @@ private function upsertModule( * @param string|null $providerUuid The supplier, when there is one. * @param array{businessOwner: string|null} $owners The owner contact person. * - * @return array{uuid: string, outcome: string} + * @return array{uuid: string, outcome: string} Outcome created, updated or unchanged, or restored or + * unarchived when the stored usage was revived first. * * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 */ private function upsertUsage(array $data, string $municipalityUuid, string $moduleUuid, ?string $providerUuid, array $owners): array { if (isset($data['interneAnnotation']) === true && is_string($data['interneAnnotation']) === true) { @@ -1199,19 +1402,20 @@ private function upsertUsage(array $data, string $municipalityUuid, string $modu } } - $existing = $this->findOne(schemaKey: 'usage', filters: ['consumer' => $municipalityUuid, 'module' => $moduleUuid]); + $existing = $this->findInAnyState(schemaKey: 'usage', filters: ['consumer' => $municipalityUuid, 'module' => $moduleUuid]); if ($existing === null) { return ['uuid' => $this->save(schemaKey: 'usage', data: $data, uuid: null), 'outcome' => CmdbImportReport::CREATED]; } - $uuid = (string)$existing->getUuid(); - $merged = $this->merge(target: 'usage', stored: $existing->getObject(), mapped: $data); + $uuid = (string)$existing['entity']->getUuid(); + $revived = $this->revive(found: $existing); + $merged = $this->merge(target: 'usage', stored: $revived['entity']->getObject(), mapped: $data); if ($merged === null) { - return ['uuid' => $uuid, 'outcome' => CmdbImportReport::UNCHANGED]; + return ['uuid' => $uuid, 'outcome' => ($revived['outcome'] ?? CmdbImportReport::UNCHANGED)]; } $this->save(schemaKey: 'usage', data: $merged, uuid: $uuid); - return ['uuid' => $uuid, 'outcome' => CmdbImportReport::UPDATED]; + return ['uuid' => $uuid, 'outcome' => ($revived['outcome'] ?? CmdbImportReport::UPDATED)]; }//end upsertUsage() /** @@ -1633,7 +1837,7 @@ private function organisationsOfType(string $type): array { }//end organisationsOfType() /** - * The one object matching every filter, or null. + * The working object matching every filter, or null. * * @param string $schemaKey The coordinates key of the schema. * @param array $filters Field => exact value. @@ -1643,10 +1847,67 @@ private function organisationsOfType(string $type): array { * @spec openspec/changes/cmdb-export-import/tasks.md#task-5 */ private function findOne(string $schemaKey, array $filters): ?object { + return $this->search(schemaKey: $schemaKey, filters: $filters, lens: []); + }//end findOne() + + /** + * The object matching every filter in any state, with that state, or null. + * + * A working object wins over an archived one, and an archived one over one + * in the trash: an object deleted by hand may share its import key with the + * one a later import created, and bringing the deleted one back would + * duplicate it. The state is the lens that found the object (design D9): + * a search without a lens finds only working objects, the archive lens only + * archived ones, and the trash is read from OpenRegister's trash listing, + * because its list search cannot return a soft-deleted object. + * + * @param string $schemaKey module or usage. + * @param array $filters Field => exact value. + * + * @return array{entity: object, state: array{archived: bool, deleted: bool}}|null + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private function findInAnyState(string $schemaKey, array $filters): ?array { + $entity = $this->search(schemaKey: $schemaKey, filters: $filters, lens: []); + if ($entity !== null) { + return ['entity' => $entity, 'state' => self::STATE_WORKING]; + } + + $entity = $this->search(schemaKey: $schemaKey, filters: $filters, lens: self::ARCHIVED_LENS); + if ($entity !== null) { + return ['entity' => $entity, 'state' => self::STATE_ARCHIVED]; + } + + foreach (($this->trash()[$schemaKey] ?? []) as $entity) { + if (self::matchesFilters(entity: $entity, filters: $filters) === true) { + return ['entity' => $entity, 'state' => self::STATE_DELETED]; + } + } + + return null; + }//end findInAnyState() + + /** + * The first object a search with one lens returns that matches every filter, or null. + * + * The filters are checked again on the result: a filter OpenRegister cannot + * apply must not turn into a match. + * + * @param string $schemaKey The coordinates key of the schema. + * @param array $filters Field => exact value. + * @param array $lens No lens (working objects) or ARCHIVED_LENS. + * + * @return object|null The entity (ObjectEntityInterface). + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private function search(string $schemaKey, array $filters, array $lens): ?object { $coordinates = $this->coordinates(); $results = $coordinates['objectService']->searchObjects( query: array_merge( ['@self' => ['register' => $coordinates['register'], 'schema' => $coordinates[$schemaKey]], '_limit' => 10], + $lens, $filters ), _rbac: false, @@ -1657,22 +1918,34 @@ private function findOne(string $schemaKey, array $filters): ?object { } foreach ($results as $entity) { - $data = $entity->getObject(); - $matches = true; - foreach ($filters as $field => $value) { - if (self::relationUuid(value: ($data[$field] ?? null)) !== $value) { - $matches = false; - break; - } - } - - if ($matches === true) { + if (self::matchesFilters(entity: $entity, filters: $filters) === true) { return $entity; } } return null; - }//end findOne() + }//end search() + + /** + * Whether an object's data has every filter's value. + * + * @param object $entity The entity (ObjectEntityInterface). + * @param array $filters Field => exact value. + * + * @return bool + * + * @spec openspec/changes/archive/2026-10-05-cmdb-export-import/tasks.md#task-5 + */ + private static function matchesFilters(object $entity, array $filters): bool { + $data = $entity->getObject(); + foreach ($filters as $field => $value) { + if (self::relationUuid(value: ($data[$field] ?? null)) !== $value) { + return false; + } + } + + return true; + }//end matchesFilters() /** * Save an object through OpenRegister and return its uuid. @@ -1699,6 +1972,797 @@ private function save(string $schemaKey, array $data, ?string $uuid): string { return (string)$entity->getUuid(); }//end save() + /** + * The `missingRecords` mode of an import: the option, or DEFAULT_MISSING_RECORDS. + * + * @param array $options The import options. + * + * @return string + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 + */ + private static function missingRecordsFrom(array $options): string { + $mode = ($options['missingRecords'] ?? null); + if (is_string($mode) === false || $mode === '') { + return self::DEFAULT_MISSING_RECORDS; + } + + return $mode; + }//end missingRecordsFrom() + + /** + * The archive sheet as the reader found it, with its APPIDs as match keys. + * + * @param array $workbook What the reader returned. + * + * @return ArchiveSheet + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function archiveSheet(array $workbook): array { + $archive = ($workbook['archive'] ?? []); + $key = $this->profile->keyColumn(); + $keys = []; + foreach (($archive['appIds'] ?? []) as $appId) { + $values = $this->normaliser->normalise(cells: [$key => $appId], dateColumns: [], idColumns: $this->profile->idColumns()); + $matchKey = self::matchKey(appId: ($values[$key] ?? '')); + if ($matchKey !== '') { + $keys[$matchKey] = true; + } + } + + return [ + 'sheet' => (string)($archive['sheet'] ?? ($this->profile->archiveSheetName() ?? '')), + 'present' => (($archive['present'] ?? false) === true), + 'keyColumnMissing' => (($archive['keyColumnMissing'] ?? false) === true), + 'uncachedRows' => array_values(array_map('intval', (array)($archive['uncachedRows'] ?? []))), + 'keys' => $keys, + ]; + }//end archiveSheet() + + /** + * The report warning of an archiving import whose archive sheet is absent or has no key column, or has unreadable keys. + * + * Also the warning of a CMDB sheet with unreadable keys: it is handed the sheet's name and rows in the archive sheet's shape. + * + * @param array{sheet: string, keyColumnMissing: bool, uncachedRows: array} $archive The archive sheet, or a CMDB sheet. + * + * @return string + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function archiveSheetWarning(array $archive): string { + if ($archive['uncachedRows'] !== []) { + // A long list is cut: the warning counts the rows and names where to look, not every one. + $count = count($archive['uncachedRows']); + $rows = implode(', ', array_slice($archive['uncachedRows'], 0, self::WARNED_ROWS)); + if ($count > self::WARNED_ROWS) { + $rows .= ', …'; + } + + if ($count === 1) { + return $this->l10n->t( + 'Sheet "%1$s" has an APPID without a cached value in row %2$s, so applications missing from the export are archived, not deleted.', + [$archive['sheet'], $rows] + ); + } + + return $this->l10n->t( + 'Sheet "%1$s" has an APPID without a cached value in %2$s rows (%3$s), so applications missing from the export are archived, not deleted.', + [$archive['sheet'], $count, $rows] + ); + } + + if ($archive['keyColumnMissing'] === true) { + return $this->l10n->t( + 'Sheet "%1$s" has no column "%2$s", so applications missing from the export are archived, not deleted.', + [$archive['sheet'], $this->profile->keyColumn()] + ); + } + + return $this->l10n->t('Sheet "%s" was not found, so applications missing from the export are archived, not deleted.', [$archive['sheet']]); + }//end archiveSheetWarning() + + /** + * Resolve OpenRegister's archive handler and object mapper, failing closed. + * + * @return array{handler: object, mapper: object} The handler, with `archive()` and `unarchive()`, and the + * mapper, with `restoreObject()` and `findDeletedAcrossAllMagicTables()`. + * + * @throws CmdbImportException ARCHIVE_UNAVAILABLE. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function archiveServices(): array { + if ($this->archiveServices !== null) { + return $this->archiveServices; + } + + $previous = null; + try { + $handler = $this->container->get(static::ARCHIVE_HANDLER_CLASS); + $mapper = $this->container->get(static::OBJECT_MAPPER_CLASS); + } catch (Throwable $e) { + $this->logger->warning('CmdbExportImportService: OpenRegister archive services could not be resolved', ['exception' => get_class($e)]); + $previous = $e; + $handler = null; + $mapper = null; + } + + if (is_object($handler) === false || method_exists($handler, 'archive') === false || method_exists($handler, 'unarchive') === false + || is_object($mapper) === false || method_exists($mapper, 'restoreObject') === false + || method_exists($mapper, 'findDeletedAcrossAllMagicTables') === false + ) { + throw new CmdbImportException( + errorCode: CmdbImportException::ARCHIVE_UNAVAILABLE, + message: 'OpenRegister ArchiveHandler or MagicMapper is not available', + previous: $previous + ); + } + + $this->archiveServices = ['handler' => $handler, 'mapper' => $mapper]; + return $this->archiveServices; + }//end archiveServices() + + /** + * Whether an object as OpenRegister's mapper reads it by uuid is archived and whether it is in the trash. + * + * Read from its `@self` metadata. Only for an object the mapper read + * itself (the one restoreObject() answers): a list search result carries + * no archive marker (design D9). + * + * @param object $entity The entity (ObjectEntityInterface). + * + * @return array{archived: bool, deleted: bool} + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private static function objectState(object $entity): array { + $self = []; + if ($entity instanceof JsonSerializable) { + $serialised = $entity->jsonSerialize(); + if (is_array($serialised) === true && is_array($serialised['@self'] ?? null) === true) { + $self = $serialised['@self']; + } + } + + return [ + 'archived' => (self::isEmptyValue(value: ($self['archived'] ?? null)) === false), + 'deleted' => (self::isEmptyValue(value: ($self['deleted'] ?? null)) === false), + ]; + }//end objectState() + + /** + * Bring a matched module or usage back before it is written: out of the trash, then out of the archive. + * + * OpenRegister refuses a data write to an archived object, and the + * archive handler cannot reach one in the trash, hence the order. The + * trash listing does not say whether an object was archived before it was + * deleted; the restored object, read by the mapper, does. The update then + * starts from that object: the trash listing decodes every string that + * parses as JSON, so its copy of the data must not be written back. + * + * @param array{entity: object, state: array{archived: bool, deleted: bool}} $found The matched entity and its state. + * + * @return array{outcome: string|null, entity: object} restored, unarchived, or null when it was neither, and the + * entity whose data the update starts from. + * + * @throws CmdbImportException ARCHIVE_UNAVAILABLE when the archive services are missing. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private function revive(array $found): array { + $entity = $found['entity']; + $archived = $found['state']['archived']; + if ($found['state']['deleted'] === false && $archived === false) { + return ['outcome' => null, 'entity' => $entity]; + } + + $uuid = (string)$entity->getUuid(); + $outcome = null; + if ($found['state']['deleted'] === true) { + $restored = $this->restore(uuid: $uuid); + $outcome = CmdbImportReport::RESTORED; + if (is_object($restored) === true && method_exists($restored, 'getObject') === true) { + $entity = $restored; + $archived = self::objectState(entity: $restored)['archived']; + } + } + + if ($archived === true) { + $this->archiveServices()['handler']->unarchive(identifier: $uuid, reason: self::REASON_PREFIX . 'back in the export'); + $outcome = ($outcome ?? CmdbImportReport::UNARCHIVED); + } + + return ['outcome' => $outcome, 'entity' => $entity]; + }//end revive() + + /** + * Take one object out of the trash, and out of this run's copy of the trash. + * + * @param string $uuid The object. + * + * @return mixed What MagicMapper::restoreObject() answers: the object as it reads it. + * + * @throws CmdbImportException ARCHIVE_UNAVAILABLE when the archive services are missing. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private function restore(string $uuid): mixed { + $restored = $this->archiveServices()['mapper']->restoreObject(uuid: $uuid); + if ($this->trash !== null) { + unset($this->trash['module'][$uuid], $this->trash['usage'][$uuid]); + } + + return $restored; + }//end restore() + + /** + * The register's soft-deleted modules and usages, read once per run. + * + * OpenRegister's list search cannot return a soft-deleted object, whatever + * `_includeDeleted` says, so the trash is read from the trash listing that + * the Deleted page uses (design D9). That listing scans every magic table + * of the instance, so it is read once, lazily, and only this register's + * modules and usages are kept. Its objects carry no archive or trash + * marker; everything in it is deleted. Without the archive services (a + * `keep` import on an OpenRegister without them) the trash cannot be read: + * nothing in it is matched, and the log says so. A listing that throws is + * not cached, so the next row reads it again. + * + * It is read for `keep` as well: matching is not tied to the mode (REQ-CMDB-016), + * and an application a person soft-deleted by hand must be restored, not + * created a second time. The listing cannot be narrowed, so the cost is one + * scan per run, paid on the first row no working or archived object matches. + * + * @return array{module: array, usage: array} Schema key => uuid => entity. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private function trash(): array { + if ($this->trash !== null) { + return $this->trash; + } + + try { + $services = $this->archiveServices(); + } catch (CmdbImportException $e) { + $this->logger->warning('CmdbExportImportService: OpenRegister trash is not readable; soft-deleted modules and usages are not matched'); + $this->trash = ['module' => [], 'usage' => []]; + return $this->trash; + } + + // Cached only once the listing was read: a listing that fails fails its row, and the + // next row reads it again instead of matching against an empty trash and duplicating. + $coordinates = $this->coordinates(); + $deleted = $services['mapper']->findDeletedAcrossAllMagicTables(limit: null, offset: null, _rbac: false, _multitenancy: false); + $this->trash = self::registerTrash(deleted: $deleted, coordinates: $coordinates); + + return $this->trash; + }//end trash() + + /** + * The modules and usages of this register among the instance's soft-deleted objects. + * + * @param mixed $deleted What MagicMapper::findDeletedAcrossAllMagicTables() answered. + * @param array $coordinates The register and schema ids, from coordinates(). + * + * @return array{module: array, usage: array} Schema key => uuid => entity. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + private static function registerTrash(mixed $deleted, array $coordinates): array { + $trash = ['module' => [], 'usage' => []]; + if (is_array($deleted) === false) { + return $trash; + } + + foreach ($deleted as $entity) { + if (is_object($entity) === false || (string)$entity->getRegister() !== (string)$coordinates['register'] || $entity->getUuid() === null) { + continue; + } + + foreach (['module', 'usage'] as $schemaKey) { + if ((string)$entity->getSchema() === (string)$coordinates[$schemaKey]) { + $trash[$schemaKey][(string)$entity->getUuid()] = $entity; + } + } + } + + return $trash; + }//end registerTrash() + + /** + * Archive or soft-delete every application of the municipality whose APPID left the CMDB sheets. + * + * Runs after the last row of an import that was not cancelled, under the + * same register lock. The whole scope is loaded before anything is + * written, so the paging is not shifted by the writes. Every application + * has its own error boundary. A cancel is honoured before the scope is + * loaded and before each application: what was archived or deleted so far + * stays, and the report says the run was cancelled. + * + * @param string $municipalityUuid The consumer of this import. + * @param ArchiveSheet $archive The archive sheet. + * @param string $operationId The operation id, for the archive reason. + * @param int $processedItems The rows already counted as processed. + * @param CmdbImportReport $report The report. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function reconcile(string $municipalityUuid, array $archive, string $operationId, int $processedItems, CmdbImportReport $report): void { + // A cancel that came in during the last row stops the import before anything is archived or deleted. + if ($this->progressTracker->isCancelRequested(operationId: $operationId) === true) { + $report->markCancelled(); + return; + } + + $scope = $this->reconciliationScope(municipalityUuid: $municipalityUuid); + $this->progressTracker->setPhase(phase: 'reconciling', data: ['total_items' => ($processedItems + count($scope))]); + + foreach ($scope as $index => $application) { + if ($this->progressTracker->isCancelRequested(operationId: $operationId) === true) { + $report->markCancelled(); + return; + } + + // Every APPID on a CMDB sheet was handled by its row, whatever the row's outcome. + if (isset($this->winningSheets[$application['key']]) === false) { + $this->reconcileApplication(application: $application, archive: $archive, operationId: $operationId, report: $report); + } + + $this->progressTracker->updateProgress(processedItems: ($processedItems + $index + 1)); + } + }//end reconcile() + + /** + * The municipality's usages, in every state, each with its module when that module carries this municipality's import key. + * + * A usage of a hand-made module, or of a module another organisation + * imported, is left out: only what this municipality's import created is + * reconciled. Working and archived usages are paged with one lens each, + * soft-deleted ones come from the trash (design D9); each object keeps the + * state its lens or the trash gave it. When an import key occurs more than + * once, only the entry the import would match is kept (onePerKey()). + * + * @param string $municipalityUuid The consumer of this import. + * + * @return array}> + * Per application, the APPID's match key, the usage, the module and the state of both by schema key. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function reconciliationScope(string $municipalityUuid): array { + $usages = $this->usagesInEveryState(municipalityUuid: $municipalityUuid); + + $owned = []; + foreach ($usages as $usage) { + $data = $usage['entity']->getObject(); + $moduleUuid = self::relationUuid(value: ($data['module'] ?? null)); + // The filter is checked again: a filter OpenRegister cannot apply must not widen the scope. + if (self::relationUuid(value: ($data['consumer'] ?? null)) === $municipalityUuid && $moduleUuid !== null && $usage['entity']->getUuid() !== null) { + $owned[] = $usage + ['module' => $moduleUuid]; + } + } + + $prefix = $this->profile->externalKeyPrefix() . ':' . $municipalityUuid . ':'; + $modules = $this->modulesByUuid(uuids: array_values(array_unique(array_column($owned, 'module')))); + $scope = []; + foreach ($owned as $usage) { + $module = ($modules[$usage['module']] ?? null); + if ($module === null) { + continue; + } + + $externalKey = (string)($module['entity']->getObject()['externalKey'] ?? ''); + if (str_starts_with($externalKey, $prefix) === true && strlen($externalKey) > strlen($prefix)) { + $scope[] = [ + 'key' => substr($externalKey, strlen($prefix)), + 'usage' => $usage['entity'], + 'module' => $module['entity'], + 'states' => ['usage' => $usage['state'], 'module' => $module['state']], + ]; + } + } + + return self::onePerKey(scope: $scope); + }//end reconciliationScope() + + /** + * One application per import key: the entry the import would match, with the other usages of the key as extras. + * + * The import takes a working module before an archived one, and an archived + * one before one in the trash (design D4), so a module deleted by hand next + * to the one a later import created is never revived. The reconciliation + * follows the same order: the entry with the best module state is the + * application, the best usage state breaks a tie, and the first one listed + * breaks the rest. The other entries of the key are not dropped: their + * usages, and their modules when those are not in the trash, are carried + * as `extras` and brought to the same state, so a stray usage of a stale + * duplicate does not stay visible. Nothing in the trash is restored for an + * extra, and nothing the application itself holds is listed twice. + * + * @param array $scope The entries. + * + * @return array + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private static function onePerKey(array $scope): array { + $preferred = []; + $ranks = []; + $entries = []; + foreach ($scope as $application) { + $rank = ((self::stateRank(state: $application['states']['module']) * 3) + self::stateRank(state: $application['states']['usage'])); + $key = $application['key']; + $entries[$key][] = $application; + if (isset($ranks[$key]) === false || $rank < $ranks[$key]) { + $ranks[$key] = $rank; + $preferred[$key] = $application; + } + } + + foreach ($preferred as $key => $application) { + $preferred[$key]['extras'] = self::extrasOf(application: $application, entries: $entries[$key]); + } + + return array_values($preferred); + }//end onePerKey() + + /** + * The usages and modules of the other entries of a key that the reconciliation brings to the same state. + * + * A usage or module appears once, and not when the application holds it + * itself. One in the trash is left out: it is not visible, and restoring + * a stale duplicate is what the preference rule exists to prevent. + * + * @param ScopeEntry $application The preferred entry of the key. + * @param array $entries Every entry of the key. + * + * @return array Usages first, then modules. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private static function extrasOf(array $application, array $entries): array { + $seen = [(string)$application['usage']->getUuid() => true, (string)$application['module']->getUuid() => true]; + $found = ['usage' => [], 'module' => []]; + foreach ($entries as $entry) { + foreach (['usage', 'module'] as $schemaKey) { + $uuid = (string)$entry[$schemaKey]->getUuid(); + if (isset($seen[$uuid]) === false && $entry['states'][$schemaKey]['deleted'] === false) { + $seen[$uuid] = true; + $found[$schemaKey][] = ['schemaKey' => $schemaKey, 'uuid' => $uuid, 'state' => $entry['states'][$schemaKey]]; + } + } + } + + return array_merge($found['usage'], $found['module']); + }//end extrasOf() + + /** + * How far down the preference order a state is: working 0, archived 1, deleted 2. + * + * @param array{archived: bool, deleted: bool} $state The state. + * + * @return int + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private static function stateRank(array $state): int { + if ($state['deleted'] === true) { + return 2; + } + + if ($state['archived'] === true) { + return 1; + } + + return 0; + }//end stateRank() + + /** + * The usages that name this municipality as consumer, in every state, each with that state. + * + * Working and archived usages are paged with one lens each; soft-deleted + * ones come from the trash, which is not filtered by consumer, so the + * caller checks the consumer of every usage. + * + * @param string $municipalityUuid The consumer of this import. + * + * @return array + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function usagesInEveryState(string $municipalityUuid): array { + $coordinates = $this->coordinates(); + $usages = []; + foreach ([[[], self::STATE_WORKING], [self::ARCHIVED_LENS, self::STATE_ARCHIVED]] as [$lens, $state]) { + $offset = 0; + do { + $page = $coordinates['objectService']->searchObjects( + query: array_merge( + [ + '@self' => ['register' => $coordinates['register'], 'schema' => $coordinates['usage']], + 'consumer' => $municipalityUuid, + '_limit' => self::PAGE_SIZE, + '_offset' => $offset, + ], + $lens + ), + _rbac: false, + _multitenancy: false + ); + if (is_array($page) === false) { + $page = []; + } + + foreach ($page as $usage) { + $usages[] = ['entity' => $usage, 'state' => $state]; + } + + $offset += self::PAGE_SIZE; + $pageSize = count($page); + } while ($pageSize === self::PAGE_SIZE); + }//end foreach + + foreach ($this->trash()['usage'] as $usage) { + $usages[] = ['entity' => $usage, 'state' => self::STATE_DELETED]; + } + + return $usages; + }//end usagesInEveryState() + + /** + * The modules with these uuids, in every state, each with its state. + * + * Per chunk of PAGE_SIZE uuids one search without a lens and one with + * the archive lens; the rest is looked up in the trash. + * + * @param array $uuids The module uuids. + * + * @return array Uuid => entity and state. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function modulesByUuid(array $uuids): array { + $coordinates = $this->coordinates(); + $modules = []; + foreach (array_chunk($uuids, self::PAGE_SIZE) as $chunk) { + // The chunks are disjoint, so a uuid found in an earlier chunk is never overwritten. + $modules += $this->modulesInChunk(chunk: $chunk, coordinates: $coordinates); + } + + foreach ($uuids as $uuid) { + $trashed = ($this->trash()['module'][$uuid] ?? null); + if (isset($modules[$uuid]) === false && $trashed !== null) { + $modules[$uuid] = ['entity' => $trashed, 'state' => self::STATE_DELETED]; + } + } + + return $modules; + }//end modulesByUuid() + + /** + * The working and archived modules of one chunk of uuids; a module both lenses return keeps the first state. + * + * @param array $chunk At most PAGE_SIZE module uuids. + * @param array $coordinates The object service and register and schema ids, from coordinates(). + * + * @return array Uuid => entity and state. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function modulesInChunk(array $chunk, array $coordinates): array { + $modules = []; + foreach ([[[], self::STATE_WORKING], [self::ARCHIVED_LENS, self::STATE_ARCHIVED]] as [$lens, $state]) { + $results = $coordinates['objectService']->searchObjects( + query: array_merge( + ['@self' => ['register' => $coordinates['register'], 'schema' => $coordinates['module']], '_ids' => $chunk, '_limit' => count($chunk)], + $lens + ), + _rbac: false, + _multitenancy: false + ); + if (is_array($results) === false) { + continue; + } + + foreach ($results as $entity) { + $uuid = (string)$entity->getUuid(); + if (in_array($uuid, $chunk, true) === true && isset($modules[$uuid]) === false) { + $modules[$uuid] = ['entity' => $entity, 'state' => $state]; + } + } + } + + return $modules; + }//end modulesInChunk() + + /** + * Archive or soft-delete one application that left the CMDB sheets, usage first, in its own error boundary. + * + * The writes go in this order: the application's own usage, the other + * usages of its key, its module, the other modules. A failure stops the + * application there and reports one `failed` row (the step and the + * exception class, no list of what was already written). What it leaves: + * a failing own usage leaves everything as it was; a failing extra usage + * leaves the own usage in the new state, that extra and the module as + * they were (so the extra stays visible and the module stays working); + * a failing module leaves every usage in the new state and the module + * as it was. The next import reconciles the application again. + * An application already in the state the export asks for is not reported. + * + * @param ScopeEntry $application The application. + * @param ArchiveSheet $archive The archive sheet. + * @param string $operationId The operation id, for the archive reason. + * @param CmdbImportReport $report The report. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function reconcileApplication(array $application, array $archive, string $operationId, CmdbImportReport $report): void { + $moduleData = $application['module']->getObject(); + $onArchiveSheet = ($archive['present'] === true && isset($archive['keys'][$application['key']]) === true); + $entry = [ + 'sheet' => '', + 'row' => 0, + 'appId' => (string)($moduleData['externalNumber'] ?? $application['key']), + 'name' => (string)($moduleData['name'] ?? ''), + ]; + + $target = CmdbImportReport::ARCHIVED; + $reason = self::REASON_PREFIX . 'not in the export (' . $operationId . ')'; + if ($onArchiveSheet === true) { + $entry['sheet'] = $archive['sheet']; + $reason = self::REASON_PREFIX . 'on sheet "' . $archive['sheet'] . '" (' . $operationId . ')'; + } elseif ($archive['present'] === true && $archive['uncachedRows'] === [] && $this->unreadableKeyRows === []) { + // A sheet with an unreadable key (the archive sheet or a CMDB sheet) may list this application, so the + // workbook is not authoritative for a deletion. + $target = CmdbImportReport::DELETED; + } + + $uuids = ['module' => (string)$application['module']->getUuid(), 'usage' => (string)$application['usage']->getUuid()]; + $step = 'archive'; + $changed = false; + try { + foreach (self::itemsOf(application: $application, restoreDeleted: $onArchiveSheet) as $item) { + foreach (self::transitions(state: $item['state'], target: $target, restoreDeleted: $item['restore']) as $step) { + $this->transition(step: $step, schemaKey: $item['schemaKey'], uuid: $item['uuid'], reason: $reason); + $changed = true; + } + } + } catch (Throwable $e) { + // The module's stored values are taken out of the log line, as a row's cells are. + $values = array_map('strval', array_filter($moduleData, static fn (mixed $value): bool => is_scalar($value))); + $this->failRow(report: $report, entry: $entry, step: $step, e: $e, warnings: [], uuids: [$uuids['module'], $uuids['usage']], values: $values); + return; + } + + if ($changed === true) { + $this->addRow(report: $report, entry: $entry, outcome: $target, moduleUuid: $uuids['module'], usageUuid: $uuids['usage']); + } + }//end reconcileApplication() + + /** + * What one application brings to the state the export asks for: its usage and the other usages of its key, then its module and the other modules. + * + * Own usage, extra usages, own module, extra modules. Usages come before + * modules, so a failure leaves usages in the new state and a module at + * worst unchanged; it does not leave a module changed under a usage that + * still shows. An extra usage that fails does hold back the module. + * + * Only the application's own objects are restored from the trash, and only + * when `restoreDeleted` says so; the extras never are. + * + * @param ScopeEntry $application The application. + * @param bool $restoreDeleted Whether the application's own objects in the trash are restored to be archived. + * + * @return array + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private static function itemsOf(array $application, bool $restoreDeleted): array { + $items = []; + foreach (['usage', 'module'] as $schemaKey) { + // Every usage is written before any module, whoever holds it. + $items[] = [ + 'schemaKey' => $schemaKey, + 'uuid' => (string)$application[$schemaKey]->getUuid(), + 'state' => $application['states'][$schemaKey], + 'restore' => $restoreDeleted, + ]; + foreach (($application['extras'] ?? []) as $extra) { + if ($extra['schemaKey'] === $schemaKey) { + $items[] = $extra + ['restore' => false]; + } + } + } + + return $items; + }//end itemsOf() + + /** + * The steps that bring one module or usage to the state the export asks for (design D5). + * + * Deleted: soft-delete what is not in the trash yet. Archived: archive + * what is working; an object in the trash is restored first only when the + * archive sheet lists it, so a workbook without the sheet never takes + * anything out of the trash. + * + * @param array{archived: bool, deleted: bool} $state The object's state. + * @param string $target archived or deleted. + * @param bool $restoreDeleted Whether an object in the trash is restored to be archived. + * + * @return array delete, restore and archive, in order; empty when nothing changes. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private static function transitions(array $state, string $target, bool $restoreDeleted): array { + if ($target === CmdbImportReport::DELETED) { + if ($state['deleted'] === true) { + return []; + } + + return ['delete']; + } + + if ($state['deleted'] === true) { + if ($restoreDeleted === true) { + return ['restore', 'archive']; + } + + return []; + } + + if ($state['archived'] === true) { + return []; + } + + return ['archive']; + }//end transitions() + + /** + * Run one step through OpenRegister: soft delete, restore from the trash, or archive. + * + * @param string $step delete, restore or archive. + * @param string $schemaKey The coordinates key of the schema. + * @param string $uuid The object. + * @param string $reason The archive reason. + * + * @return void + * + * @throws RuntimeException When OpenRegister does not delete the object. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + private function transition(string $step, string $schemaKey, string $uuid, string $reason): void { + if ($step === 'delete') { + $coordinates = $this->coordinates(); + $deleted = $coordinates['objectService']->deleteObject( + uuid: $uuid, + register: $coordinates['register'], + schema: $coordinates[$schemaKey], + _rbac: false, + _multitenancy: false + ); + if ($deleted !== true) { + throw new RuntimeException('OpenRegister did not delete the object'); + } + + return; + } + + if ($step === 'restore') { + $this->restore(uuid: $uuid); + return; + } + + $this->archiveServices()['handler']->archive(identifier: $uuid, reason: $reason); + }//end transition() + /** * Resolve OpenRegister's mapping engine. * @@ -1915,6 +2979,8 @@ private function resetRun(): void { $this->winningSheets = []; $this->contactUids = []; $this->contactPersons = []; + $this->archiveServices = null; + $this->trash = null; }//end resetRun() /** diff --git a/lib/Service/ProgressTracker.php b/lib/Service/ProgressTracker.php index b0c9ce229..5a9706ada 100644 --- a/lib/Service/ProgressTracker.php +++ b/lib/Service/ProgressTracker.php @@ -62,6 +62,11 @@ class ProgressTracker { 'processing_relationships' => ['weight' => 15, 'description' => 'Processing relationships'], 'processing_organizations' => ['weight' => 10, 'description' => 'Processing organizations'], 'processing_views' => ['weight' => 10, 'description' => 'Processing views'], + // The CMDB import's reconciliation of records missing from the source + // (openspec/changes/cmdb-import-archive-reconciliation). Weight 0, so the + // weighted percentage of the ArchiMate import's phases is unchanged; the + // CMDB section computes its own percentage from the item counts. + 'reconciling' => ['weight' => 0, 'description' => 'Reconciling records missing from the source'], 'finalizing' => ['weight' => 5, 'description' => 'Finalizing import'], 'completed' => ['weight' => 0, 'description' => 'Completed'], ]; diff --git a/openapi.json b/openapi.json index 7ee628b87..f456fc209 100644 --- a/openapi.json +++ b/openapi.json @@ -62,10 +62,11 @@ "missingRecords": { "type": "string", "enum": [ - "keep" + "keep", + "archive" ], - "default": "keep", - "description": "Only keep is accepted; mark and remove are reserved" + "default": "archive", + "description": "What happens to the municipality's applications whose APPID is no longer on either CMDB sheet. archive: after the last row, archive module and usage when the sheet \"Gearchiveerde Applicaties\" lists the APPID, soft-delete them when no sheet does, and archive only when the workbook has no archive sheet, the sheet has no APPID column, an APPID in it has no cached value, or an APPID on a CMDB sheet has no cached value (that application may be the one the reader could not see); an archived or deleted application that returns is unarchived or restored. keep: leave them as they are. mark and remove are reserved and refused with 422 MISSING_RECORDS_UNSUPPORTED" }, "operationId": { "type": "string", @@ -128,7 +129,7 @@ } }, "422": { - "description": "MISSING_RECORDS_UNSUPPORTED, MUNICIPALITY_REQUIRED, MUNICIPALITY_INVALID, MUNICIPALITY_AMBIGUOUS (more than one live Municipality has the typed name; details.matches lists their uuids), NO_SOURCE_SHEET, MISSING_COLUMN or TOO_MANY_ROWS", + "description": "MISSING_RECORDS_UNSUPPORTED (details.accepted lists keep and archive), MUNICIPALITY_REQUIRED, MUNICIPALITY_INVALID, MUNICIPALITY_AMBIGUOUS (more than one live Municipality has the typed name; details.matches lists their uuids), NO_SOURCE_SHEET, MISSING_COLUMN or TOO_MANY_ROWS", "content": { "application/json": { "schema": { @@ -148,7 +149,7 @@ } }, "503": { - "description": "MAPPING_UNAVAILABLE, READER_UNAVAILABLE, NOT_CONFIGURED, or SCHEMA_OUTDATED (a stackiq schema lacks a property the import matches on, or the module schema predates 0.3.8; details.schema names it and details.missing lists the properties, and externalKey.authorization.update for a missing admin-only write rule)", + "description": "MAPPING_UNAVAILABLE, READER_UNAVAILABLE, NOT_CONFIGURED, or SCHEMA_OUTDATED (a stackiq schema lacks a property the import matches on, or the module schema predates 0.3.8; details.schema names it and details.missing lists the properties, and externalKey.authorization.update for a missing admin-only write rule), or ARCHIVE_UNAVAILABLE (missingRecords=archive, but OpenRegister's archive handler or object mapper is missing; nothing is read or written)", "content": { "application/json": { "schema": { @@ -499,6 +500,10 @@ "unchanged", "skipped", "failed", + "archived", + "unarchived", + "deleted", + "restored", "warnings", "unpublished" ], @@ -524,6 +529,22 @@ "failed": { "type": "integer" }, + "archived": { + "type": "integer", + "description": "Applications whose module and usage this import archived because they left the CMDB sheets" + }, + "unarchived": { + "type": "integer", + "description": "Rows whose archived module or usage this import unarchived before updating it" + }, + "deleted": { + "type": "integer", + "description": "Applications whose module and usage this import soft-deleted because no sheet lists them any more" + }, + "restored": { + "type": "integer", + "description": "Rows whose soft-deleted module or usage this import restored from the trash before updating it" + }, "warnings": { "type": "integer" }, @@ -571,7 +592,8 @@ "type": "string" }, "row": { - "type": "integer" + "type": "integer", + "description": "The sheet row, or 0 for an application the reconciliation archived or deleted" }, "appId": { "type": "string" @@ -586,7 +608,11 @@ "updated", "unchanged", "skipped", - "failed" + "failed", + "archived", + "unarchived", + "deleted", + "restored" ] }, "reasons": { diff --git a/openspec/changes/cmdb-import-archive-reconciliation/design.md b/openspec/changes/cmdb-import-archive-reconciliation/design.md index 53fc943f8..5329cc153 100644 --- a/openspec/changes/cmdb-import-archive-reconciliation/design.md +++ b/openspec/changes/cmdb-import-archive-reconciliation/design.md @@ -71,7 +71,7 @@ With `updateExisting=false`, a matched row is skipped as `exists` before anythin The profile names the sheet (`archiveSheet.name`: "Gearchiveerde Applicaties"). The reader lists the workbook's sheets, and when the archive sheet is present it is loaded with the CMDB sheets under the same bounds: it counts as a read sheet for `CmdbPartReferences` (an oversized archive sheet is refused, not blanked) and for the referenced-string budget, its row span is bounded like a source sheet, and `TOO_MANY_ROWS` names it. Only its APPID column is resolved and read; the allowlist does not grow. The reader returns `archive: {present, keyColumnMissing, appIds, uncachedRows}` next to `rows`. - Sheet absent, or present without an APPID column: `present` false (with `keyColumnMissing`), and the service adds a report warning. The reconciliation then archives every missing application and soft-deletes nothing: a workbook saved without the sheet must not empty the trash-bound set by accident. -- A row whose APPID is a formula without a cached value reads as empty, as on the CMDB sheets (REQ-CMDB-002), so the sheet would silently list one application fewer. The reader therefore lists such rows (their numbers) in `uncachedRows`, and the service treats the sheet like an absent one for the decision between archive and delete: it adds a warning naming the sheet and the rows, archives every missing application and soft-deletes nothing. A tool that saves formulas without cached values must not turn archived applications into trash. +- A row whose APPID is a formula without a cached value reads as empty, as on the CMDB sheets (REQ-CMDB-002), so the sheet would silently list one application fewer. The reader therefore lists such rows (their numbers) in `uncachedRows`, and the service treats the sheet like an absent one for the decision between archive and delete: it adds a warning naming the sheet and the rows, archives every missing application and soft-deletes nothing. A tool that saves formulas without cached values must not turn archived applications into trash. The same holds for a CMDB sheet: a row whose APPID has no cached value is skipped, so that application may be on a CMDB sheet and still look absent. The service notes such rows (`unreadableKeyRows`, per sheet) and, with any of them, archives instead of soft-deleting and warns naming the CMDB sheet and the rows, as for the archive sheet. - The archive sheet's other columns are not read; `Applicatie Status` there is always `Verwijderd` in the municipality's export and adds nothing. ### D4. Matching widens to archived and deleted objects, and revives what it matches @@ -94,10 +94,12 @@ Per application, with `processed` = every APPID match key on the CMDB sheets (wh | on the archive sheet | deleted | restore, then archive | `archived` | | nowhere, sheet present | working or archived | soft-delete usage, then module | `deleted` | | nowhere, sheet present | deleted | nothing | — | -| nowhere, sheet absent or incomplete (no key column, or `uncachedRows` not empty) | working | archive usage, then module | `archived` | +| nowhere, sheet absent or incomplete (no key column, `uncachedRows` not empty, or a CMDB-sheet row with an unreadable APPID) | working | archive usage, then module | `archived` | | nowhere, sheet absent or incomplete | archived or deleted | nothing | — | -The usage is written before the module, so a failure between the two leaves the municipality's usage in the new state and the module visible, never the reverse. A module another municipality still uses is archived or deleted all the same when this municipality's usage says so: the module carries this municipality's import key, so it is this municipality's record (REQ-CMDB-006 allows a shared module only when the other organisation's usage was added by hand). The archive reason is `cmdb-import: on sheet "Gearchiveerde Applicaties"` or `cmdb-import: not in the export`, plus the operation id, so the audit trail says why. +The table decides on one entry per key. When a municipality has several usages and modules under the same import key (D4 says earlier imports created exactly such duplicates, a module deleted by hand next to the one a later import created), the scope keeps the entry the import would match: the best module state wins (working, then archived, then deleted), the best usage state breaks a tie, the first listed breaks the rest. The other usages of the key follow the application, so a stray usage stays visible nowhere: a working or archived usage of a stale duplicate, a second usage of the same module, and a working or archived duplicate module are archived or soft-deleted as the key asks, once, in the same error boundary and the same report row (usages first, then modules). What is in the trash is left as it is: a stale trashed duplicate is not restored into the archive when the key is on the archive sheet, and is not touched when it is on no sheet. + +The writes go in this order: the application's own usage, the other usages of its key, its module, the other modules. A failure stops that application and gives one `failed` report row (step and exception class only). A failing own usage leaves everything as it was; a failing extra usage leaves the own usage in the new state and that extra and the module as they were (the extra stays visible, the module stays working); a failing module leaves every usage in the new state and the module as it was. Never is a module changed while a usage of the key still shows. The next import reconciles the application again. A module another municipality still uses is archived or deleted all the same when this municipality's usage says so: the module carries this municipality's import key, so it is this municipality's record (REQ-CMDB-006 allows a shared module only when the other organisation's usage was added by hand). The archive reason is `cmdb-import: on sheet "Gearchiveerde Applicaties"` or `cmdb-import: not in the export`, plus the operation id, so the audit trail says why. Each application runs in its own boundary: a failure is a `failed` row naming the step (`archive`, `restore`, `delete`) and the exception class, as row failures do, and the other applications continue. Contact persons are never touched. @@ -194,8 +196,8 @@ No seed objects change. The fragment's three seed modules stay as they are; the ## Risks / Trade-offs - [The default archives on the first re-import after the upgrade] → The section shows the choice with help text; the report lists every archived and deleted application; nothing is hard-deleted. -- [A workbook saved without the archive sheet, or whose archive sheet has no APPID column or an APPID without a cached value] → archive only, never delete, plus a warning (D3). -- [A failure between the usage and the module] → the usage is written first, so the municipality's view is right and the module is at worst still visible (D5). +- [A workbook saved without the archive sheet, or whose archive sheet has no APPID column or an APPID without a cached value, or a CMDB sheet with an APPID without a cached value] → archive only, never delete, plus a warning (D3). +- [A failure part way through an application] → usages are written before modules, so a module is never changed under a usage that still shows; but a failing extra usage leaves the own usage changed, that extra visible and the module unchanged, and the row only says `failed`. The next import retries (D5). - [Reviving before writing adds calls per returning row] → only for rows whose match is archived or deleted; a row whose module is working costs the same searches as before, a new row one more search (the archive lens). - [The trash listing scans every magic table of the instance] → read once per import, not per row (D9). - [OpenRegister internals change shape] → guarded resolution, 503 before the file is read (D7). diff --git a/openspec/changes/cmdb-import-archive-reconciliation/proposal.md b/openspec/changes/cmdb-import-archive-reconciliation/proposal.md index dd4017f30..d9685e710 100644 --- a/openspec/changes/cmdb-import-archive-reconciliation/proposal.md +++ b/openspec/changes/cmdb-import-archive-reconciliation/proposal.md @@ -34,7 +34,7 @@ None. ### In Scope - The sheet "Gearchiveerde Applicaties" (columns Applicatiecomponent, APPID, Applicatie Code, Applicatie Naam, Roepnaam, Applicatie Status, Bron, Datum Interface, Datum wijziging) is read for its APPID column only, when it is present. -- After every row of a completed import: the municipality's usages, with their modules, are compared with the APPIDs on the CMDB sheets and on the archive sheet. Missing and on the archive sheet: module and usage archived. Missing everywhere: module and usage soft-deleted. Without the archive sheet, or when it has no APPID column or an APPID the reader cannot see (a formula without a cached value): archived only, with a warning in the report. +- After every row of a completed import: the municipality's usages, with their modules, are compared with the APPIDs on the CMDB sheets and on the archive sheet. Missing and on the archive sheet: module and usage archived. Missing everywhere: module and usage soft-deleted. Without the archive sheet, or when it has no APPID column or an APPID the reader cannot see (a formula without a cached value, on the archive sheet or on a CMDB sheet): archived only, with a warning in the report. - A returning APPID: an archived module or usage is unarchived, a soft-deleted one is restored from the trash, and the row is then updated as before. - `missingRecords`: `keep` leaves everything as it is (the behaviour so far), `archive` reconciles and is the default; `remove` stays reserved and is refused. - The settings section offers the choice with help text; the report shows the new outcomes in its summary and rows. @@ -78,7 +78,7 @@ None. `ArchiveHandler` and `MagicMapper` come from OpenRegister, which stackiq a **Severity:** Medium — **Mitigation:** only the municipality's own usages and the modules they point at are in scope, never a supplier's or another municipality's module; nothing is hard-deleted; the choice is visible in the section with help text, and the report lists every archived and deleted application. A cancelled or failed import never reconciles. ### Risk 2: A workbook without a usable archive sheet soft-deletes everything that moved there -**Severity:** Medium — **Mitigation:** without the sheet, without its APPID column, or with an APPID the reader cannot see (a formula without a cached value), missing applications are archived, never soft-deleted, and the report warns, naming the sheet (and the rows). +**Severity:** Medium — **Mitigation:** without the sheet, without its APPID column, or with an APPID the reader cannot see on it or on a CMDB sheet (a formula without a cached value), missing applications are archived, never soft-deleted, and the report warns, naming the sheet (and the rows). ### Risk 3: The archive services are OpenRegister internals **Severity:** Low — **Mitigation:** resolved through the container behind a guard; when missing, an import with `missingRecords=archive` is refused with 503 `ARCHIVE_UNAVAILABLE` before the file is read, and `keep` still works. diff --git a/openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md b/openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md index 78903af22..d6e445624 100644 --- a/openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md +++ b/openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md @@ -16,7 +16,7 @@ OpenRegister: `OCA\OpenRegister\Contract\ObjectServiceInterface` (`searchObjects ### Requirement: An application missing from the CMDB sheets SHALL be archived when the archive sheet lists it and soft-deleted when no sheet does (REQ-CMDB-015) -With `missingRecords` `archive`, after every row of an import that was not cancelled and did not fail, the import SHALL reconcile, under the register lock it already holds, every `usage` whose `consumer` is the imported municipality together with the `module` it points at, including archived and soft-deleted ones, and only those whose module carries this municipality's import key (`topdesk::`). It SHALL read the APPID column of the sheet "Gearchiveerde Applicaties" when the workbook has it, under the same size and row bounds as the CMDB sheets and without reading any other column of it. For an application whose APPID is on neither CMDB sheet: when the archive sheet lists the APPID, the import SHALL archive the usage and then the module through OpenRegister's archive state with a reason that names the import, restoring a soft-deleted one from the trash first; when no sheet lists it, the import SHALL soft-delete the usage and then the module through OpenRegister's trash. When the workbook has no archive sheet, or the sheet has no APPID column, or a row of the sheet holds an APPID formula without a cached value (read as empty, as REQ-CMDB-002 has it for the CMDB sheets, and listed by the reader in `archive.uncachedRows`), the import SHALL archive and SHALL NOT soft-delete, and SHALL add a warning to the report; for an uncached row the warning SHALL name the sheet and the row numbers. An application that is already in the state the export asks for SHALL be left alone and not reported. The import SHALL NOT touch contact persons, organisations, modules that carry another municipality's import key or no import key, or usages of another organisation. Each application SHALL be reconciled in its own error boundary: a failure SHALL be a `failed` report row naming the step and the exception class, and the other applications SHALL still be reconciled. The reconciliation SHALL report its progress as the phase `reconciling` of the import's operation. A cancel SHALL also be honoured after the last row and before each application of the reconciliation: the import SHALL stop there, SHALL keep what it archived or deleted so far, and SHALL report the run as cancelled. The report SHALL count `archived` and `deleted` in its summary and SHALL list each as a row with the APPID, the application name and both uuids, never person data. When OpenRegister's archive handler or object mapper is not available, an import with `missingRecords` `archive` SHALL be refused with 503 `ARCHIVE_UNAVAILABLE` before the file is read. +With `missingRecords` `archive`, after every row of an import that was not cancelled and did not fail, the import SHALL reconcile, under the register lock it already holds, every `usage` whose `consumer` is the imported municipality together with the `module` it points at, including archived and soft-deleted ones, and only those whose module carries this municipality's import key (`topdesk::`). It SHALL read the APPID column of the sheet "Gearchiveerde Applicaties" when the workbook has it, under the same size and row bounds as the CMDB sheets and without reading any other column of it. For an application whose APPID is on neither CMDB sheet: when the archive sheet lists the APPID, the import SHALL archive the usage and then the module through OpenRegister's archive state with a reason that names the import, restoring a soft-deleted one from the trash first; when no sheet lists it, the import SHALL soft-delete the usage and then the module through OpenRegister's trash. When the workbook has no archive sheet, or the sheet has no APPID column, or a row of the sheet holds an APPID formula without a cached value (read as empty, as REQ-CMDB-002 has it for the CMDB sheets, and listed by the reader in `archive.uncachedRows`), the import SHALL archive and SHALL NOT soft-delete, and SHALL add a warning to the report; for an uncached row the warning SHALL name the sheet and the row numbers. The same holds when a row of a CMDB sheet holds an APPID formula without a cached value (the row is skipped as empty, so its application may look absent): the import SHALL archive and SHALL NOT soft-delete, and the warning SHALL name that CMDB sheet and the rows. When a municipality has more than one module or usage under the same import key (a module deleted by hand next to the one a later import created), the import SHALL reconcile as the application the one it would match, a working one before an archived one and an archived one before one in the trash, and SHALL bring every other usage of that key, and every other module of it that is not in the trash, to the same state, so no stray usage stays visible; it SHALL leave what is in the trash as it is, so a stale duplicate is never restored into the archive. An application that is already in the state the export asks for SHALL be left alone and not reported. The import SHALL NOT touch contact persons, organisations, modules that carry another municipality's import key or no import key, or usages of another organisation. Each application SHALL be reconciled in its own error boundary: a failure SHALL be a `failed` report row naming the step and the exception class, and the other applications SHALL still be reconciled. The reconciliation SHALL report its progress as the phase `reconciling` of the import's operation. A cancel SHALL also be honoured after the last row and before each application of the reconciliation: the import SHALL stop there, SHALL keep what it archived or deleted so far, and SHALL report the run as cancelled. The report SHALL count `archived` and `deleted` in its summary and SHALL list each as a row with the APPID, the application name and both uuids, never person data. When OpenRegister's archive handler or object mapper is not available, an import with `missingRecords` `archive` SHALL be refused with 503 `ARCHIVE_UNAVAILABLE` before the file is read. #### Scenario: An application that moved to the archive sheet is archived @e2e tests/e2e/spec-coverage/cmdb-import.spec.ts @@ -36,6 +36,15 @@ With `missingRecords` `archive`, after every row of an import that was not cance - **THEN** the module and the usage of `7` SHALL be archived and SHALL NOT be deleted - **AND** the report SHALL carry a warning that the sheet "Gearchiveerde Applicaties" was not found +#### Scenario: A stale duplicate under the same import key is left in the trash +@e2e exclude Data condition from earlier imports; tests/Unit/Service/CmdbExportImportServiceTest.php testAStaleDuplicateIsNeverRestoredByTheReconciliation stores a working and a trashed module and usage for APPID `7`, lists `7` on the archive sheet, and asserts the working pair is archived, no `restore` happens and the trashed pair stays deleted; testEveryOtherUsageOfAKeyFollowsTheApplication asserts the stray and second usages follow. + +- **GIVEN** a working module with APPID `7` and its usage, and a module with the same import key in the trash with its own usage +- **WHEN** an export without `7` on the CMDB sheets, with `7` on "Gearchiveerde Applicaties", is imported with `archive` +- **THEN** the working module and usage SHALL be archived +- **AND** the module and the usage in the trash SHALL stay there, and nothing SHALL be restored +- **AND** a working usage that points at the module in the trash, or a second usage of the working module, SHALL be archived as well + #### Scenario: An archive row whose APPID has no cached value never turns an archive into a deletion @e2e exclude Covered by the service test; tests/Unit/Service/CmdbExportImportServiceTest.php testAnUncachedArchiveRowArchivesInsteadOfDeleting asserts the archive state, no delete, and the warning naming the sheet and the row; tests/Unit/Service/Cmdb/CmdbWorkbookReaderTest.php testAnUncachedArchiveKeyIsListedNotDropped asserts the reader lists the row. @@ -44,6 +53,14 @@ With `missingRecords` `archive`, after every row of an import that was not cance - **THEN** the module and the usage of `7` SHALL be archived and SHALL NOT be deleted - **AND** the report SHALL carry a warning naming the sheet "Gearchiveerde Applicaties" and the row +#### Scenario: A CMDB row whose APPID has no cached value never turns an archive into a deletion +@e2e exclude Covered by the service test; tests/Unit/Service/CmdbExportImportServiceTest.php testAnUncachedCmdbRowArchivesInsteadOfDeleting asserts the archive state, no delete, and the warning naming the CMDB sheet and the row. + +- **GIVEN** the module with APPID `8` and its usage were imported, `8` is on no readable row, and a row of a CMDB sheet holds its APPID in a formula that carries no cached value +- **WHEN** the workbook is imported with `archive` and a readable archive sheet that does not list `8` +- **THEN** the module and the usage of `8` SHALL be archived and SHALL NOT be deleted +- **AND** the report SHALL carry a warning naming the CMDB sheet and the row + #### Scenario: Keeping missing records, a cancelled import and another municipality leave everything as it is @e2e exclude Covered by the service test; tests/Unit/Service/CmdbExportImportServiceTest.php testRecordsMissingFromTheExportStayWhenKept, testACancelledImportDoesNotReconcile and testAnotherMunicipalitysUsagesAreNeverReconciled assert no archive, no delete and no reconciliation row. @@ -156,7 +173,7 @@ The import SHALL accept `missingRecords` with the values `keep` and `archive`; ` ### Requirement: The admin settings SHALL offer a CMDB import section (REQ-CMDB-014) -Stackiq's admin settings page SHALL show a section "CMDB import", rendered by the settings page and not registered as an in-app route. The section SHALL let the admin choose an existing municipality or type the name of a new one, choose an `.xlsx` file, choose what happens to applications missing from the export (archive, the default, or keep) with help text that says what each does, and start the import. While the import runs it SHALL show a progress bar and a Cancel button. Afterwards it SHALL show the summary, including the archived, unarchived, deleted and restored counts when they are not zero, and a report table that can be filtered by outcome, including the four new ones; a reconciliation row SHALL show no row number. Every control SHALL have a visible label, and every string SHALL be translatable. +Stackiq's admin settings page SHALL show a section "CMDB import", rendered by the settings page and not registered as an in-app route. The section SHALL let the admin choose an existing municipality or type the name of a new one, choose an `.xlsx` file, choose what happens to applications missing from the export (archive, the default, or keep) with a one-sentence help text that says what each does and the details in the section's information panel, and start the import. While the import runs it SHALL show a progress bar and a Cancel button. Afterwards it SHALL show the summary, including the archived, unarchived, deleted and restored counts when they are not zero, and a report table that can be filtered by outcome, including the four new ones; a reconciliation row SHALL show no row number. Every control SHALL have a visible label, and every string SHALL be translatable. (Previously: the section had no choice for missing records; the request always sent `keep`.) @@ -170,7 +187,7 @@ Stackiq's admin settings page SHALL show a section "CMDB import", rendered by th - **AND** filtering the table on `created` SHALL show the two imported rows #### Scenario: The admin chooses to keep missing applications -@e2e exclude Covered by the Jest test; src/utils/cmdbImport.spec.js asserts buildImportForm() sends `archive` by default and `keep` when chosen, and src/views/settings/sections/CmdbImport.spec.js asserts the section's default. +@e2e tests/e2e/spec-coverage/cmdb-import.spec.ts - **GIVEN** the section - **WHEN** the admin selects "Keep them as they are" and starts the import diff --git a/openspec/changes/cmdb-import-archive-reconciliation/tasks.md b/openspec/changes/cmdb-import-archive-reconciliation/tasks.md index 17bee62a1..76d347861 100644 --- a/openspec/changes/cmdb-import-archive-reconciliation/tasks.md +++ b/openspec/changes/cmdb-import-archive-reconciliation/tasks.md @@ -4,7 +4,7 @@ Spec: `openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-imp ## Implementation Tasks -Status in this PR (the archive-sheet reader slice): Tasks 1 and 2 are implemented here. Tasks 3-7 are unticked because their code lands in the follow-up PRs (reconciliation in #1286, UI in #1287). +Status in this PR (the reconciliation slice, on top of the reader slice): Tasks 1-5 are implemented in #1286 (the reader's tasks 1-2 come from the base PR). Task 6 (the UI) is implemented in #1287; task 7 has its fixture, docs and e2e spec written there, and the Playwright run is the one thing not done yet. ### Task 1: Schema opt-in and the import profile - **spec_ref**: `SPEC#requirement-archived-applications-and-usages-shall-be-listable-in-stackiq-and-shall-stay-out-of-opencatalogi-and-portaliq-by-default-req-cmdb-017`, `SPEC#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012` @@ -37,22 +37,23 @@ Status in this PR (the archive-sheet reader slice): Tasks 1 and 2 are implemente - GIVEN OpenRegister's search results (no archive marker; no soft-deleted row, `_includeDeleted` or not) THEN the state comes from the lens that found the object and from the trash listing, read once per import (design D9) - GIVEN `updateExisting` false THEN the row is skipped as `exists` and nothing is revived - GIVEN a deleted and a working module (and usage) sharing one import key THEN the working one is updated and nothing is restored -- [ ] Implement -- [ ] Test +- [x] Implement +- [x] Test ### Task 4: Reconciliation after the rows - **spec_ref**: `SPEC#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015` - **files**: `lib/Service/CmdbExportImportService.php`, `lib/Service/ProgressTracker.php`, `lib/Exception/CmdbImportException.php`, `tests/Unit/Service/CmdbExportImportServiceTest.php` - **acceptance_criteria**: - GIVEN APPIDs 1 and 7 imported WHEN an export with row 1 and 7 on the archive sheet is imported THEN module and usage of 7 are archived with a reason, the report row is `archived`, and the owner contact person is untouched - - GIVEN APPID 7 on no sheet THEN module and usage are soft-deleted and the row is `deleted`; GIVEN the archive sheet absent, without an APPID column, or with an APPID that has no cached value THEN they are archived instead and the report warns + - GIVEN APPID 7 on no sheet THEN module and usage are soft-deleted and the row is `deleted`; GIVEN the archive sheet absent, without an APPID column, or with an APPID that has no cached value, or a CMDB-sheet row whose APPID has no cached value THEN they are archived instead and the report warns - GIVEN APPID 7 soft-deleted and listed on the archive sheet THEN module and usage are restored and archived; GIVEN it already archived or, when on no sheet, already deleted THEN nothing is written + - GIVEN a working pair and a trashed pair under the same import key and the key on the archive sheet THEN the working pair is archived, nothing is restored and the trashed pair stays deleted; GIVEN a further working usage of the key (of the trashed module, or a second one of the working module) THEN it is archived or deleted with the application - GIVEN `missingRecords` `keep`, a cancelled import, or a usage of another municipality THEN nothing is archived or deleted - GIVEN a cancel during the last row THEN nothing is archived or deleted; GIVEN a cancel during the reconciliation THEN it stops before the next application and the report is cancelled - GIVEN the archive of one application throws THEN that application is a `failed` row and the others are reconciled; GIVEN no ArchiveHandler THEN 503 `ARCHIVE_UNAVAILABLE` before reading - GIVEN the step runs THEN the operation is in phase `reconciling` and its progress advances per application -- [ ] Implement -- [ ] Test +- [x] Implement +- [x] Test ### Task 5: Controller option - **spec_ref**: `SPEC#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012` @@ -60,8 +61,8 @@ Status in this PR (the archive-sheet reader slice): Tasks 1 and 2 are implemente - **acceptance_criteria**: - GIVEN no `missingRecords` THEN the service receives `archive`; GIVEN `keep` or `archive` THEN it is passed through; GIVEN `remove` or `mark` THEN 422 `MISSING_RECORDS_UNSUPPORTED` with `details.accepted` `["keep", "archive"]` and no import - GIVEN `ARCHIVE_UNAVAILABLE` from the service THEN 503 with its own translated message -- [ ] Implement -- [ ] Test +- [x] Implement +- [x] Test ### Task 6: Settings section, report words and the archive quick filter - **spec_ref**: `SPEC#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014`, `SPEC#requirement-archived-applications-and-usages-shall-be-listable-in-stackiq-and-shall-stay-out-of-opencatalogi-and-portaliq-by-default-req-cmdb-017` @@ -70,9 +71,10 @@ Status in this PR (the archive-sheet reader slice): Tasks 1 and 2 are implemente - GIVEN the section THEN a labelled choice "Applications missing from the export" offers Archive (default) and Keep with help text, and `buildImportForm()` sends the choice as `missingRecords` - GIVEN a report with the new outcomes THEN the summary shows their tiles, the outcome filter offers them, and a reconciliation row shows no row number - GIVEN the Applications and Applications in use pages THEN a quick filter "Archived" sends `_archived=true` + - GIVEN the Applications page with the "Archived" tab active THEN a note under the toolbar says the tab ignores the GEMMA filters and the search term, and it is gone on any other tab - Every new string is in `l10n/en.json` and `l10n/nl.json`, and the generated `l10n/*.js` are current -- [ ] Implement -- [ ] Test +- [x] Implement +- [x] Test ### Task 7: Fixture, docs and e2e - **spec_ref**: `SPEC#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015` @@ -81,7 +83,7 @@ Status in this PR (the archive-sheet reader slice): Tasks 1 and 2 are implemente - GIVEN `build-fixtures.py` THEN it derives `topdesk-archived-applications.xlsx`: APPID 1234 on the archive sheet only, APPID 2 on no sheet, both CMDB sheets present and empty; the hygiene test passes on it - GIVEN the e2e THEN after the first import, importing the variant archives 1234 and soft-deletes 2, the Applications page lists 1234 under "Archived", and importing the original again reports 1234 `unarchived` and 2 `restored` with the object counts back to the first import's - GIVEN the docs THEN "Repeat imports" describes archive, soft delete, the archive sheet, the option and the return of an application, and the error table has `ARCHIVE_UNAVAILABLE` and the new meaning of `MISSING_RECORDS_UNSUPPORTED` -- [ ] Implement +- [x] Implement - [ ] Test - Status: PHPUnit covers the fixture round trip (testTheArchivedApplicationsFixtureArchivesDeletesAndBringsBack); the Playwright test is written and not run yet, because no Nextcloud was reachable from the worktree. diff --git a/postman/stackiq-tests.json b/postman/stackiq-tests.json index 385214b7c..0da0499b8 100644 --- a/postman/stackiq-tests.json +++ b/postman/stackiq-tests.json @@ -23073,9 +23073,10 @@ "listen": "test", "script": { "exec": [ - "pm.test(\"Only missingRecords=keep is accepted\", function () {", + "pm.test(\"Only missingRecords=keep or archive is accepted\", function () {", " pm.response.to.have.status(422);", " pm.expect(pm.response.json().error).to.eql(\"MISSING_RECORDS_UNSUPPORTED\");", + " pm.expect(pm.response.json().details.accepted).to.eql([\"keep\", \"archive\"]);", "});" ], "type": "text/javascript" diff --git a/src/icons.js b/src/icons.js index 865c514c0..34ffbfdfb 100644 --- a/src/icons.js +++ b/src/icons.js @@ -17,6 +17,7 @@ import AccountGroup from 'vue-material-design-icons/AccountGroup.vue' import AccountMultiple from 'vue-material-design-icons/AccountMultiple.vue' import AlertCircle from 'vue-material-design-icons/AlertCircle.vue' import ApplicationOutline from 'vue-material-design-icons/ApplicationOutline.vue' +import ArchiveOutline from 'vue-material-design-icons/ArchiveOutline.vue' import ArrowRight from 'vue-material-design-icons/ArrowRight.vue' import BookOpenVariant from 'vue-material-design-icons/BookOpenVariant.vue' import BookOpenVariantOutline from 'vue-material-design-icons/BookOpenVariantOutline.vue' @@ -79,6 +80,7 @@ export default { AccountMultiple, AlertCircle, ApplicationOutline, + ArchiveOutline, ArrowRight, BookOpenVariant, BookOpenVariantOutline, diff --git a/src/manifest.d/usages.json b/src/manifest.d/usages.json index 66990ad7d..eec9d1242 100644 --- a/src/manifest.d/usages.json +++ b/src/manifest.d/usages.json @@ -35,7 +35,8 @@ { "label": "Planned", "filter": { "status": "Planned" } }, { "label": "In production", "filter": { "status": "In production" }, "icon": "CheckCircle" }, { "label": "To be phased out", "filter": { "status": "To be phased out" }, "icon": "AlertCircle" }, - { "label": "Phased out", "filter": { "status": "Phased out" } } + { "label": "Phased out", "filter": { "status": "Phased out" } }, + { "label": "Archived", "filter": { "_archived": "true" }, "icon": "ArchiveOutline" } ], "sidebar": { "enabled": true, "showMetadata": true }, "documentationUrl": "https://stackiq.conduction.nl" diff --git a/src/manifest.json b/src/manifest.json index 22b321684..1541f303e 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -616,7 +616,7 @@ "type": "custom", "title": "Applications", "component": "FacetedCatalogIndexView", - "_note": "Faceted index page (gemma-faceted-search): CnFacetSidebar (live GEMMA-dimension counts from FacetService) + a standalone CnIndexPage narrowed via { _ids: matchedObjectIds } (OpenRegister's list parameter; the objects endpoint ignores a plain id filter). Columns are objects: `provider` is a reference, so it renders through the fkResolve cell widget (uuid → organisation name) and carries its own label — object-typed properties get no schema-generated column, so a bare key showed the raw uuid under the header 'provider'. Stays custom rather than type:index — see customComponents.js's FacetedCatalogIndexView entry for why CnIndexPage's own embedded facet sidebar cannot drive this narrowing. bio-compliance-assessment: bbnLevel/dpiaStatus columns + BBN/DPIA quickFilters reconciled onto this page after gemma-faceted-search (#382) merged — see FacetedCatalogIndexView.vue's quickFilters prop docblock for how the quick filter's field constraints compose with the facet { _ids: ... } narrowing (disjoint keys, additive AND, no clobber).", + "_note": "Faceted index page (gemma-faceted-search): CnFacetSidebar (live GEMMA-dimension counts from FacetService) + a standalone CnIndexPage narrowed via { _ids: matchedObjectIds } (OpenRegister's list parameter; the objects endpoint ignores a plain id filter). Columns are objects: `provider` is a reference, so it renders through the fkResolve cell widget (uuid → organisation name) and carries its own label — object-typed properties get no schema-generated column, so a bare key showed the raw uuid under the header 'provider'. Stays custom rather than type:index — see customComponents.js's FacetedCatalogIndexView entry for why CnIndexPage's own embedded facet sidebar cannot drive this narrowing. bio-compliance-assessment: bbnLevel/dpiaStatus columns + BBN/DPIA quickFilters reconciled onto this page after gemma-faceted-search (#382) merged — see FacetedCatalogIndexView.vue's quickFilters prop docblock for how the quick filter's field constraints compose with the facet { _ids: ... } narrowing (disjoint keys, additive AND, no clobber). The one exception is the Archived tab: its filter carries an empty-string `_ids` that replaces the facet id set in the merge, because that set is built from the working set, which leaves archived applications out, so ANDing it with `_archived=true` could never match. That tab lists the whole archive and ignores the GEMMA facets and the search term.", "config": { "register": "@resolve:voorzieningen_register", "schema": "module", @@ -662,6 +662,14 @@ "dpiaStatus": ["not required", "required"] }, "icon": "AlertCircle" + }, + { + "label": "Archived", + "filter": { + "_archived": "true", + "_ids": "" + }, + "icon": "ArchiveOutline" } ] } diff --git a/src/utils/cmdbImport.js b/src/utils/cmdbImport.js index ab7fa7080..2c83018a0 100644 --- a/src/utils/cmdbImport.js +++ b/src/utils/cmdbImport.js @@ -31,6 +31,8 @@ export const PROFILE_DEFAULTS = Object.freeze({ maxFileBytes: 10 * 1024 * 1024, maxRowsPerSheet: 10000, sheets: Object.freeze(['Onbeh Applicaties CMDB', 'Beheerde Applicaties CMDB']), + // Read for its APPID column only, in every import. + archiveSheet: 'Gearchiveerde Applicaties', }) /** @@ -42,8 +44,27 @@ export const PROFILE_DEFAULTS = Object.freeze({ */ export const AS_TEXT = Object.freeze({ escape: false, sanitize: false }) -/** Every row outcome the report can carry, in display order. */ -export const OUTCOMES = ['created', 'updated', 'unchanged', 'skipped', 'failed'] +/** + * Every row outcome the report can carry, in display order: the outcomes of a + * sheet row, then those of an application that left the export or came back. + */ +export const OUTCOMES = [ + 'created', + 'updated', + 'unchanged', + 'skipped', + 'failed', + 'unarchived', + 'restored', + 'archived', + 'deleted', +] + +/** + * What the import may do with applications missing from the export (the + * profile's `missingRecords`), the default first. + */ +export const MISSING_RECORDS_MODES = Object.freeze(['archive', 'keep']) /** * The words for one row outcome. @@ -51,6 +72,7 @@ export const OUTCOMES = ['created', 'updated', 'unchanged', 'skipped', 'failed'] * @param {string} outcome The outcome key from the report * @return {string} The label * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-each-row-shall-be-processed-in-isolation-and-reported-with-its-outcome-req-cmdb-011 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 */ export function outcomeLabel(outcome) { switch (outcome) { @@ -64,6 +86,14 @@ export function outcomeLabel(outcome) { return t('stackiq', 'Skipped') case 'failed': return t('stackiq', 'Failed') + case 'unarchived': + return t('stackiq', 'Unarchived') + case 'restored': + return t('stackiq', 'Restored') + case 'archived': + return t('stackiq', 'Archived') + case 'deleted': + return t('stackiq', 'Deleted') default: return String(outcome ?? '') } @@ -141,16 +171,19 @@ export function checkFile(file) { * @param {{uuid: string|null, name: string}} options.municipality The chosen municipality: an existing one has a uuid, a new one only a name * @param {boolean} options.updateExisting Whether matched rows are updated * @param {boolean} [options.publish] Whether the modules the import creates are published; true when left out + * @param {string} [options.missingRecords] What happens to applications missing from the export: 'archive' (when left out) or 'keep' * @param {string} options.operationId The progress operation id * @return {FormData} The body * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-every-import-shall-have-exactly-one-consuming-municipality-chosen-by-the-admin-req-cmdb-004 * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-a-newly-created-module-shall-get-a-publicationdate-when-the-admin-publishes-and-an-existing-one-shall-keep-its-own-req-cmdb-007 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 */ export function buildImportForm({ file, municipality, updateExisting, publish = true, + missingRecords = MISSING_RECORDS_MODES[0], operationId, }) { const form = new FormData() @@ -162,7 +195,7 @@ export function buildImportForm({ } form.append('updateExisting', updateExisting ? 'true' : 'false') form.append('publish', publish ? 'true' : 'false') - form.append('missingRecords', 'keep') + form.append('missingRecords', missingRecords) form.append('operationId', operationId) return form } @@ -465,6 +498,7 @@ const KNOWN_ERRORS = new Set([ 'SCHEMA_OUTDATED', 'IMPORT_IN_PROGRESS', 'MUNICIPALITY_AMBIGUOUS', + 'ARCHIVE_UNAVAILABLE', 'OPERATION_NOT_FOUND', 'IMPORT_INTERRUPTED', 'NOT_SIGNED_IN', @@ -503,7 +537,7 @@ function workbookTooLarge(details) { + ' ' + t( 'stackiq', - 'Split the rows over two copies of the export and import them one after the other.', + 'Split the rows over two copies of the export and import both with "Keep them as they are": an import that archives treats the applications of the other copy as missing.', ) if (Number(details.maxPartBytes) > 0) { const sizes = { @@ -532,10 +566,11 @@ function workbookTooLarge(details) { + ' ' + t( 'stackiq', - 'Delete the sheets the import does not read from a copy of the export (it reads only "{first}" and "{second}"), and import that copy.', + 'Delete the sheets the import does not read from a copy of the export (it reads "{first}" and "{second}", and the APPID column of "{archive}"), and import that copy.', { first: PROFILE_DEFAULTS.sheets[0], second: PROFILE_DEFAULTS.sheets[1], + archive: PROFILE_DEFAULTS.archiveSheet, }, AS_TEXT, ) @@ -619,14 +654,14 @@ export function errorText(error) { ), hint: t( 'stackiq', - 'Remove sheets the import does not read, or split the export, and try again.', + 'Remove sheets the import does not read and try again. Or split the export and import every part with "Keep them as they are".', ), } case 'MISSING_RECORDS_UNSUPPORTED': return { title: t( 'stackiq', - 'Records missing from the export can only be kept.', + 'Applications missing from the export can only be archived or kept.', ), hint: '', } @@ -733,13 +768,13 @@ export function errorText(error) { Number(details.limit) > 0 ? t( 'stackiq', - 'A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other.', + 'A source sheet may hold at most {limit} rows. Split the export and import the parts one after the other with "Keep them as they are".', { limit: Number(details.limit).toLocaleString() }, AS_TEXT, ) : t( 'stackiq', - 'Split the export and import the parts one after the other.', + 'Split the export and import the parts one after the other with "Keep them as they are".', ), } case 'MAPPING_UNAVAILABLE': @@ -807,6 +842,17 @@ export function errorText(error) { 'Choose the municipality from the list instead of typing its name. Nothing was imported.', ), } + case 'ARCHIVE_UNAVAILABLE': + return { + title: t( + 'stackiq', + 'Applications missing from the export cannot be archived.', + ), + hint: t( + 'stackiq', + 'OpenRegister is missing or too old to archive objects. Choose "Keep them as they are" and import again, or update OpenRegister. Nothing was imported.', + ), + } case 'OPERATION_NOT_FOUND': return { title: t('stackiq', 'This import is no longer running.'), @@ -886,9 +932,17 @@ export function cancelFailureText(error) { * server has set them, because the tracker's own percentage is weighted by * the phases of the ArchiMate import. * + * In the phase `reconciling` the server's counts add the applications of the + * municipality to the rows, and those applications include ones that are on + * the sheets and need no change. They are neither rows nor applications + * missing from the export, so the view shows no "N of M" for this phase and + * keeps the bar where the rows left it (full) instead of letting it fall + * back when the total grows. + * * @param {object|null} progress The snapshot from `GET /api/progress/{operationId}` * @return {{percentage: number, detail: string}|null} The view, or null before any progress * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-a-running-import-shall-report-its-progress-and-shall-stop-when-cancelled-req-cmdb-013 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 */ export function cmdbProgressView(progress) { if (!progress) { @@ -900,6 +954,17 @@ export function cmdbProgressView(progress) { total > 0 ? Math.min(100, Math.round((processed / total) * 100)) : Number(progress.percentage) || 0 + if (progress.phase === 'reconciling') { + return { + percentage: 100, + detail: t( + 'stackiq', + 'Checking the applications of this municipality against the export', + {}, + AS_TEXT, + ), + } + } return { percentage, detail: @@ -960,9 +1025,13 @@ export function sortReportRows(rows, key, order) { /** * The rows of the report as the table shows them. * + * An application the import archived or deleted after its rows has row 0 + * in the report and no row number in the table. + * * @param {Array} rows The report's `rows` * @return {Array} The table rows * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-each-row-shall-be-processed-in-isolation-and-reported-with-its-outcome-req-cmdb-011 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 */ export function reportRows(rows) { if (!Array.isArray(rows)) { @@ -971,7 +1040,7 @@ export function reportRows(rows) { return rows.map((row, index) => ({ key: `${row.sheet ?? ''}:${row.row ?? index}:${index}`, sheet: String(row.sheet ?? ''), - row: row.row ?? '', + row: Number(row.row) > 0 ? row.row : '', appId: String(row.appId ?? ''), name: String(row.name ?? ''), outcome: String(row.outcome ?? ''), diff --git a/src/utils/cmdbImport.spec.js b/src/utils/cmdbImport.spec.js index 8f566d6cb..a5579b985 100644 --- a/src/utils/cmdbImport.spec.js +++ b/src/utils/cmdbImport.spec.js @@ -22,6 +22,7 @@ import { interruptedImportError, isKnownError, makeCmdbOperationId, + MISSING_RECORDS_MODES, municipalityOptions, normaliseError, outcomeLabel, @@ -58,6 +59,7 @@ const SERVER_CODES = [ 'SCHEMA_OUTDATED', 'IMPORT_IN_PROGRESS', 'MUNICIPALITY_AMBIGUOUS', + 'ARCHIVE_UNAVAILABLE', 'OPERATION_NOT_FOUND', 'IMPORT_INTERRUPTED', 'NOT_SIGNED_IN', @@ -198,10 +200,26 @@ describe('buildImportForm', () => { expect(form.get('municipalityUuid')).toBe('uuid-1') expect(form.has('municipalityName')).toBe(false) expect(form.get('updateExisting')).toBe('true') - expect(form.get('missingRecords')).toBe('keep') + expect(form.get('missingRecords')).toBe('archive') expect(form.get('operationId')).toBe('cmdb-abcdefgh') }) + it('sends the choice for applications missing from the export, archive by default', () => { + const options = { + file, + municipality: { uuid: 'uuid-1', name: 'Tilburg' }, + updateExisting: true, + operationId: 'cmdb-abcdefgh', + } + expect(MISSING_RECORDS_MODES).toEqual(['archive', 'keep']) + expect(buildImportForm(options).get('missingRecords')).toBe('archive') + expect( + buildImportForm({ ...options, missingRecords: 'keep' }).get( + 'missingRecords', + ), + ).toBe('keep') + }) + it('sends a typed new municipality by name, and updateExisting as an explicit false', () => { const form = buildImportForm({ file, @@ -395,7 +413,7 @@ describe('errorText', () => { 'The sheet "Beheerde Applicaties CMDB" is too large to import: unpacked it is 14.3 MB, and the import reads at most 10 MB of one sheet.', ) expect(words.hint).toBe( - 'An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported. Split the rows over two copies of the export and import them one after the other.', + 'An .xlsx file is compressed, so a file of a few MB can be much larger once unpacked. Nothing was imported. Split the rows over two copies of the export and import both with "Keep them as they are": an import that archives treats the applications of the other copy as missing.', ) }) @@ -408,7 +426,7 @@ describe('errorText', () => { expect( errorText({ error: 'WORKBOOK_TOO_LARGE', details }).hint, ).toContain( - '(it reads only "Onbeh Applicaties CMDB" and "Beheerde Applicaties CMDB")', + '(it reads "Onbeh Applicaties CMDB" and "Beheerde Applicaties CMDB", and the APPID column of "Gearchiveerde Applicaties")', ) } }) @@ -642,6 +660,44 @@ describe('cmdbProgressView', () => { ).toEqual({ percentage: 25, detail: '1 of 4 rows processed' }) }) + it('says the import checks the applications, without counting them as rows', () => { + // The service reports rows + the municipality's applications as the total. + const view = cmdbProgressView({ + phase: 'reconciling', + processed_items: 400, + total_items: 810, + }) + expect(view.detail).toBe( + 'Checking the applications of this municipality against the export', + ) + expect(view.detail).not.toMatch(/\d/) + }) + + it('does not let the bar fall when the phase changes from the rows to the comparison', () => { + const lastOfTheRows = cmdbProgressView({ + phase: 'importing', + processed_items: 400, + total_items: 400, + }) + const firstOfTheComparison = cmdbProgressView({ + phase: 'reconciling', + processed_items: 400, + total_items: 810, + }) + const midComparison = cmdbProgressView({ + phase: 'reconciling', + processed_items: 600, + total_items: 810, + }) + expect(lastOfTheRows.percentage).toBe(100) + expect(firstOfTheComparison.percentage).toBeGreaterThanOrEqual( + lastOfTheRows.percentage, + ) + expect(midComparison.percentage).toBeGreaterThanOrEqual( + lastOfTheRows.percentage, + ) + }) + it('shows nothing before any progress', () => { expect(cmdbProgressView(null)).toBeNull() }) @@ -683,6 +739,31 @@ describe('the report table', () => { expect(reportRows(null)).toEqual([]) }) + it('shows no row number for an application archived or deleted after the rows', () => { + const [row] = reportRows([ + { + sheet: 'Gearchiveerde Applicaties', + row: 0, + appId: '7', + name: 'Zeven', + outcome: 'archived', + reasons: [], + warnings: [], + moduleUuid: 'm-7', + }, + ]) + expect(row.row).toBe('') + expect(outcomeLabel('archived')).toBe('Archived') + expect(OUTCOMES).toEqual( + expect.arrayContaining([ + 'archived', + 'unarchived', + 'deleted', + 'restored', + ]), + ) + }) + it('sorts numbers by value and keeps the report order for ties', () => { const rows = [ { key: 'a', row: 10, sheet: 'B' }, diff --git a/src/views/FacetedCatalogIndexView.vue b/src/views/FacetedCatalogIndexView.vue index f80deda08..6cb3f8aec 100644 --- a/src/views/FacetedCatalogIndexView.vue +++ b/src/views/FacetedCatalogIndexView.vue @@ -92,6 +92,18 @@ generic route-query-to-filter passthrough never sees it (see the +

+ {{ + t( + 'stackiq', + 'This tab lists the whole archive. It ignores the GEMMA filters and the search term.', + ) + }} +

+
+ @view="openDetail" + @quickFilterChange="onQuickFilterChange" />
@@ -158,6 +171,21 @@ const DIMENSION_LABELS = { domain: () => t('stackiq', 'Domain'), } +/** + * The tab CnIndexPage starts on: the first quick filter marked `default`, + * else the first one (the library's own rule in `useSelfFetchList.js`). + * + * @param {Array|null} quickFilters The quick filters + * @return {number|null} The index, or null without quick filters + */ +function initialQuickFilterIndex(quickFilters) { + if (!Array.isArray(quickFilters) || quickFilters.length === 0) { + return null + } + const index = quickFilters.findIndex((tab) => tab && tab.default === true) + return index >= 0 ? index : 0 +} + /** Debounce delay (ms) between search keystrokes and the facets/list refetch. */ const SEARCH_DEBOUNCE_MS = 400 @@ -232,7 +260,20 @@ export default { * filter's keys (`bbnLevel`, `dpiaStatus`, …) never collide with * `_ids`, so the merge is additive — both the facet-matched id set AND * the active quick filter's field constraints apply together - * (logical AND), never clobbering one another. + * (logical AND), never clobbering one another. The one exception is + * the Archived tab of the Applications page, below. + * + * The exception is the Archived tab of the Applications page + * (cmdb-import-archive-reconciliation): the facet id set is built from + * the working set, which OpenRegister's default lens limits to objects + * that are not archived, so ANDing `_ids` with `_archived=true` could + * never match an archived application. That tab's filter therefore + * carries an empty-string `_ids` (not an empty array, which the filter + * resolver drops before the merge), which wins over this view's `_ids` + * and which the request leaves out: the tab lists the whole archive + * and ignores the GEMMA selection and the search term. The tab's + * filter, not this view, owns that, so it holds however the page's + * tab state is restored. */ quickFilters: { type: Array, @@ -268,6 +309,7 @@ export default { showSaveViewModal: false, savingView: false, searchDebounceTimer: null, + activeQuickFilterIndex: initialQuickFilterIndex(this.quickFilters), } }, @@ -327,6 +369,20 @@ export default { ) }, + /** + * Whether the active quick-filter tab drops the facet id set and the + * search term: a tab whose filter carries the empty-string `_ids` + * override (the Archived tab, see the `quickFilters` prop). The facet + * sidebar and the search box stay on screen, so the page says so. + * + * @return {boolean} True while such a tab is active. + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-archived-applications-and-usages-shall-be-listable-in-stackiq-and-shall-stay-out-of-opencatalogi-and-portaliq-by-default-req-cmdb-017 + */ + archivedTabIgnoresFilters() { + const tab = (this.quickFilters ?? [])[this.activeQuickFilterIndex] + return tab?.filter?._ids === '' + }, + /** * Remount key for `CnIndexPage`: its self-fetch mode has no reactive * watcher on the `filter` prop (only route/quick-filter/tab changes @@ -342,6 +398,18 @@ export default { }, }, + watch: { + /** + * CnIndexPage is remounted when the key changes and starts on its + * default tab again, so the tab this view remembers goes back to it. + * + * @return {void} + */ + indexPageKey() { + this.activeQuickFilterIndex = initialQuickFilterIndex(this.quickFilters) + }, + }, + created() { this.facetStore.setFiltersFromQuery(this.schema, this.$route.query) }, @@ -362,6 +430,18 @@ export default { }, methods: { + /** + * Remember which quick-filter tab is active (single-select mode emits the + * tab index; multi-select emits an array, which never selects the + * Archived tab here). + * + * @param {number|number[]} index The tab index, or the selected indices + * @return {void} + */ + onQuickFilterChange(index) { + this.activeQuickFilterIndex = Number.isInteger(index) ? index : null + }, + /** * Open the detail page of a clicked row, or of the row whose View action was used. * @@ -525,6 +605,13 @@ export default { flex: 1; } +.faceted-catalog-index__archive-note { + color: var(--color-text-maxcontrast); + margin: 0; + padding: 0 calc(var(--default-grid-baseline, 8px) * 2) + calc(var(--default-grid-baseline, 8px) * 2); +} + .faceted-catalog-index__body { display: flex; align-items: flex-start; diff --git a/src/views/settings/sections/CmdbImport.spec.js b/src/views/settings/sections/CmdbImport.spec.js index 931f7edbc..f30570388 100644 --- a/src/views/settings/sections/CmdbImport.spec.js +++ b/src/views/settings/sections/CmdbImport.spec.js @@ -9,7 +9,8 @@ */ import axios from '@nextcloud/axios' -import { flushPromises, shallowMount } from '@vue/test-utils' +import { flushPromises, mount, shallowMount } from '@vue/test-utils' +import { h } from 'vue' import CmdbImport from './CmdbImport.vue' // Virtual: the package's `exports` field declares only the `import` condition, which jest does not resolve. @@ -26,25 +27,90 @@ jest.mock('@nextcloud/router', () => ({ url.replace(/{(\w+)}/g, (match, key) => String(params[key] ?? match)), generateOcsUrl: (url) => url, })) -// The component libraries are stubbed by shallowMount; virtual for the same `exports` reason. +// The component libraries are stubbed; virtual for the same `exports` reason. +// shallowMount replaces them with empty stubs. The ones below also render what +// the tests that mount the section for real (`mountFull`) need to see: the +// slots, the radio's value and the table's cell slots. jest.mock( '@nextcloud/vue', - () => ({ - NcButton: { render: () => null }, - NcCheckboxRadioSwitch: { render: () => null }, - NcLoadingIcon: { render: () => null }, - NcNoteCard: { render: () => null }, - NcProgressBar: { render: () => null }, - NcSelect: { render: () => null }, - }), + () => { + const { h } = require('vue') + const withSlot = { + render() { + return h('div', this.$slots.default?.()) + }, + } + return { + NcButton: { + render() { + return h('button', this.$slots.default?.()) + }, + }, + NcCheckboxRadioSwitch: { + props: { + modelValue: { default: null }, + value: { default: null }, + name: { default: '' }, + type: { default: 'checkbox' }, + disabled: { default: false }, + }, + emits: ['update:modelValue'], + render() { + const radio = this.type === 'radio' + return h('label', [ + h('input', { + type: radio ? 'radio' : 'checkbox', + name: this.name, + value: this.value, + disabled: this.disabled, + checked: radio + ? this.modelValue === this.value + : Boolean(this.modelValue), + onChange: (event) => + this.$emit( + 'update:modelValue', + radio ? this.value : event.target.checked, + ), + }), + this.$slots.default?.(), + ]) + }, + }, + NcLoadingIcon: { render: () => null }, + NcNoteCard: withSlot, + NcProgressBar: { render: () => null }, + NcSelect: { render: () => null }, + } + }, { virtual: true }, ) jest.mock( '@conduction/nextcloud-vue', - () => ({ - CnDataTable: { render: () => null }, - CnStatusBadge: { render: () => null }, - }), + () => { + const { h } = require('vue') + return { + // One element per row, with the cell slots the section fills. + CnDataTable: { + props: { rows: { default: () => [] } }, + render() { + return h( + 'div', + this.rows.map((row) => + h('div', { 'data-testid': 'table-row' }, [ + h( + 'span', + { 'data-testid': 'table-row-number' }, + this.$slots['column-row']?.({ row }), + ), + h('span', this.$slots['column-name']?.({ row })), + ]), + ), + ) + }, + }, + CnStatusBadge: { render: () => null }, + } + }, { virtual: true }, ) // The icon SFCs live in node_modules, which jest does not transform. @@ -55,9 +121,14 @@ jest.mock('vue-material-design-icons/DatabaseImport.vue', () => ({ jest.mock('vue-material-design-icons/TrayArrowUp.vue', () => ({ render: () => null, })) -jest.mock('../../../components/AlwaysVisibleSection.vue', () => ({ - render: () => null, -})) +jest.mock('../../../components/AlwaysVisibleSection.vue', () => { + const { h } = require('vue') + return { + render() { + return h('div', this.$slots.default?.()) + }, + } +}) const ORGANISATIONS = [ { @@ -104,6 +175,24 @@ async function mountSection() { return wrapper } +/** + * Mount the section with its child components rendering what the tests read. + * + * @return {Promise} The wrapper, after the municipalities loaded + */ +async function mountFull() { + axios.get.mockImplementation((url) => + Promise.resolve( + url.includes('/voorzieningen/config') + ? { data: { config: { register: 7, organisatie_schema: 33 } } } + : { data: { results: ORGANISATIONS } }, + ), + ) + const wrapper = mount(CmdbImport) + await flushPromises() + return wrapper +} + describe('CmdbImport municipality chooser', () => { afterEach(() => { jest.clearAllMocks() @@ -147,3 +236,333 @@ describe('CmdbImport municipality chooser', () => { expect(wrapper.vm.report).toBe(null) }) }) + +/** + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + */ +describe('CmdbImport applications missing from the export', () => { + afterEach(() => { + jest.clearAllMocks() + }) + + it('archives by default and posts the choice the admin made', async () => { + const wrapper = await mountSection() + axios.post.mockResolvedValue({ + data: { success: true, summary: {}, rows: [] }, + }) + wrapper.vm.municipality = wrapper.vm.municipalityOptions[0] + wrapper.vm.selectedFile = new File(['x'], 'export.xlsx') + + expect(wrapper.vm.missingRecords).toBe('archive') + await wrapper.vm.startImport() + wrapper.vm.missingRecords = 'keep' + await wrapper.vm.startImport() + + expect( + axios.post.mock.calls.map(([, form]) => form.get('missingRecords')), + ).toEqual(['archive', 'keep']) + }) + + it('shows the archived, unarchived, deleted and restored counts only when there are any', async () => { + const wrapper = await mountSection() + const tileKeys = () => wrapper.vm.summaryTiles.map((tile) => tile.key) + + wrapper.vm.report = { summary: { rowsRead: 2, created: 2 }, rows: [] } + expect(tileKeys()).not.toContain('archived') + + wrapper.vm.report = { + summary: { + rowsRead: 1, + archived: 1, + deleted: 1, + unarchived: 0, + restored: 2, + }, + rows: [], + } + expect(tileKeys()).toEqual( + expect.arrayContaining(['archived', 'deleted', 'restored']), + ) + expect(tileKeys()).not.toContain('unarchived') + expect(wrapper.vm.outcomeFilterOptions.map((option) => option.id)).toEqual( + expect.arrayContaining([ + 'archived', + 'unarchived', + 'deleted', + 'restored', + ]), + ) + }) +}) + +/** + * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-a-running-import-shall-report-its-progress-and-shall-stop-when-cancelled-req-cmdb-013 + */ +describe('CmdbImport cancelling', () => { + afterEach(() => { + jest.clearAllMocks() + }) + + it('says after Cancel that the comparison of the applications stops before the next application', async () => { + const wrapper = await mountFull() + axios.post.mockResolvedValue({ data: { success: true } }) + await wrapper.setData({ + importing: true, + operationId: 'cmdb-12345678', + progress: { + phase: 'reconciling', + processed_items: 400, + total_items: 810, + }, + }) + + await wrapper.vm.cancelImport() + await flushPromises() + + const status = wrapper + .find('[data-testid="cmdb-import-cancel-status"]') + .text() + expect(status).toContain('before the next row') + expect(status).toContain('before the next application') + }) + + it('says that the applications processed before the stop are kept, not only rows', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ + report: { + cancelled: true, + summary: { rowsRead: 400, archived: 3 }, + rows: [], + }, + }) + + expect(wrapper.find('[data-testid="cmdb-import-cancelled"]').text()).toMatch( + /applications/, + ) + }) +}) + +/** + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + */ +describe('CmdbImport finished announcement', () => { + afterEach(() => { + jest.clearAllMocks() + }) + + it('names the archived and deleted applications in the sentence the live region announces', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ + report: { + municipality: { name: 'Gemeente Bergen' }, + summary: { rowsRead: 0, archived: 1, deleted: 1 }, + rows: [], + }, + }) + + const announced = wrapper.find('[data-testid="cmdb-import-finished"]').text() + expect(announced).toContain('0 rows read') + expect(announced).toContain('1 archived') + expect(announced).toContain('1 deleted') + expect(announced).not.toContain('unarchived') + expect(announced).not.toContain('restored') + }) + + it('names every kind of change after a cancel too, and adds nothing when no application was touched', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ + report: { + cancelled: true, + municipality: { name: 'Gemeente Bergen' }, + summary: { + rowsRead: 5, + archived: 2, + unarchived: 3, + deleted: 4, + restored: 5, + }, + rows: [], + }, + }) + expect(wrapper.vm.finishedText).toBe( + 'Import for Gemeente Bergen cancelled after 5 rows. Applications: 2 archived, 3 unarchived, 4 deleted, 5 restored.', + ) + + // setData merges into the report; a new report replaces it. + wrapper.vm.report = { + municipality: { name: 'Gemeente Bergen' }, + summary: { rowsRead: 2, created: 2 }, + rows: [], + } + expect(wrapper.vm.finishedText).toBe( + 'Import for Gemeente Bergen finished. 2 rows read: 2 created, 0 updated, 0 unchanged.', + ) + }) +}) + +/** + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + */ +describe('CmdbImport the choice for missing applications, as the admin makes it', () => { + afterEach(() => { + jest.clearAllMocks() + }) + + const radio = (wrapper, which) => + wrapper.find(`[data-testid="cmdb-import-missing-records-${which}"] input`) + + it('starts on "Archive or delete them" and the radios share one name', async () => { + const wrapper = await mountFull() + + expect(radio(wrapper, 'archive').element.checked).toBe(true) + expect(radio(wrapper, 'keep').element.checked).toBe(false) + expect(radio(wrapper, 'archive').attributes('name')).toBe( + radio(wrapper, 'keep').attributes('name'), + ) + expect(radio(wrapper, 'archive').attributes('value')).toBe('archive') + expect(radio(wrapper, 'keep').attributes('value')).toBe('keep') + }) + + it('posts missingRecords=keep after the admin clicks "Keep them as they are", and only that field changes', async () => { + const wrapper = await mountFull() + axios.post.mockResolvedValue({ + data: { success: true, summary: {}, rows: [] }, + }) + wrapper.vm.municipality = wrapper.vm.municipalityOptions[0] + wrapper.vm.selectedFile = new File(['x'], 'export.xlsx') + + await radio(wrapper, 'keep').setValue(true) + await flushPromises() + + expect(wrapper.vm.missingRecords).toBe('keep') + expect(radio(wrapper, 'keep').element.checked).toBe(true) + expect(radio(wrapper, 'archive').element.checked).toBe(false) + // The choice is not wired to another option: they keep their defaults. + expect(wrapper.vm.publish).toBe(true) + expect(wrapper.vm.updateExisting).toBe(true) + + await wrapper.vm.startImport() + const form = axios.post.mock.calls[0][1] + expect(form.get('missingRecords')).toBe('keep') + + await radio(wrapper, 'archive').setValue(true) + expect(wrapper.vm.missingRecords).toBe('archive') + }) + + it('says in short what happens, and in the info panel when applications are only archived', async () => { + const wrapper = await mountFull() + + const help = wrapper.get('#cmdb-import-missing-records-help').text() + expect(help).toContain('archived or moved to the trash') + + // The info panel is the section's `info-content` slot; render it. + const panel = mount(CmdbImport, { + global: { + stubs: { + AlwaysVisibleSection: { + render() { + return h('div', [this.$slots['info-content']?.()]) + }, + }, + }, + }, + }) + await flushPromises() + const text = panel.text() + expect(text).toContain('is missing') + expect(text).toContain('has no APPID column') + expect(text).toContain('an APPID without a cached value') + expect(text).toContain('only archived') + }) + + it('disables both radios while an import runs', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ importing: true }) + + expect(radio(wrapper, 'archive').attributes('disabled')).toBeDefined() + expect(radio(wrapper, 'keep').attributes('disabled')).toBeDefined() + }) +}) + +/** + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + */ +describe('CmdbImport report rows of archived and deleted applications', () => { + afterEach(() => { + jest.clearAllMocks() + }) + + const reconciliationRow = (outcome, extra = {}) => ({ + sheet: 'Gearchiveerde Applicaties', + row: 0, + appId: outcome === 'deleted' ? '2' : '1234', + name: outcome === 'deleted' ? 'Naamtest' : 'Mailen', + outcome, + moduleUuid: 'module-uuid-' + outcome, + ...extra, + }) + + it('links an archived application to its module, and a deleted one to nothing', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ + report: { + summary: { rowsRead: 0, archived: 1, deleted: 1 }, + rows: [reconciliationRow('archived'), reconciliationRow('deleted')], + }, + }) + + const rows = wrapper.findAll('[data-testid="table-row"]') + expect(rows).toHaveLength(2) + const [archived, deleted] = rows + expect( + archived + .find('[data-testid="cmdb-import-module-link"]') + .attributes('href'), + ).toBe('/apps/stackiq/modules/module-uuid-archived') + expect( + deleted.find('[data-testid="cmdb-import-module-link"]').exists(), + ).toBe(false) + // The name of the deleted application is still shown, as text. + expect(deleted.text()).toContain('Naamtest') + }) + + it('shows no row number for a reconciliation row, and links one that failed', async () => { + const wrapper = await mountFull() + + await wrapper.setData({ + report: { + summary: { rowsRead: 1, failed: 1, archived: 1 }, + rows: [ + reconciliationRow('failed', { + reasons: ['archive: RuntimeException'], + }), + reconciliationRow('archived'), + { + sheet: 'Beheerde Applicaties CMDB', + row: 2, + appId: '5', + name: 'Gewoon', + outcome: 'created', + moduleUuid: 'module-uuid-created', + }, + ], + }, + }) + + const rows = wrapper.findAll('[data-testid="table-row"]') + expect( + rows.map((row) => row.get('[data-testid="table-row-number"]').text()), + ).toEqual(['', '', '2']) + expect( + rows[0].find('[data-testid="cmdb-import-module-link"]').exists(), + ).toBe(true) + expect( + rows[2].find('[data-testid="cmdb-import-module-link"]').exists(), + ).toBe(true) + }) +}) diff --git a/src/views/settings/sections/CmdbImport.vue b/src/views/settings/sections/CmdbImport.vue index f8bbc1e4b..801bc9b36 100644 --- a/src/views/settings/sections/CmdbImport.vue +++ b/src/views/settings/sections/CmdbImport.vue @@ -143,6 +143,40 @@ }}

+
+ + {{ t('stackiq', 'Applications missing from the export') }} + + + {{ t('stackiq', 'Archive or delete them') }} + + + {{ t('stackiq', 'Keep them as they are') }} + +

+ {{ + t( + 'stackiq', + 'Applications no longer in the export are archived or moved to the trash. The (i) button above explains when.', + ) + }} +

+
@@ -238,7 +272,7 @@ {{ t( 'stackiq', - 'The import was cancelled. The rows processed before it stopped are kept.', + 'The import was cancelled. The rows and applications processed before it stopped are kept.', ) }} @@ -304,7 +338,7 @@ @@ -451,6 +495,7 @@ import { interruptedImportError, isKnownError, makeCmdbOperationId, + MISSING_RECORDS_MODES, moduleUrl, municipalityOptions, normaliseError, @@ -463,6 +508,15 @@ import { typedMunicipalityOption, } from '../../../utils/cmdbImport.js' +/** The summary tiles shown only when their count is not zero. */ +const COUNTED_ONLY_TILES = [ + 'unpublished', + 'unarchived', + 'restored', + 'archived', + 'deleted', +] + /** * The "CMDB import" section of stackiq's admin settings. * @@ -472,6 +526,7 @@ import { * the text it renders; none of them may go into v-html. * * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 */ export default { name: 'CmdbImport', @@ -500,6 +555,7 @@ export default { selectedFile: null, updateExisting: true, publish: true, + missingRecords: MISSING_RECORDS_MODES[0], importing: false, cancelling: false, cancelStatus: '', @@ -522,6 +578,10 @@ export default { unchanged: 'default', skipped: 'warning', failed: 'error', + unarchived: 'info', + restored: 'info', + archived: 'default', + deleted: 'warning', }, } }, @@ -578,6 +638,7 @@ export default { * * @return {Array} One tile per count * @spec openspec/changes/cmdb-export-import/specs/cmdb-export-import/spec.md#requirement-each-row-shall-be-processed-in-isolation-and-reported-with-its-outcome-req-cmdb-011 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 */ summaryTiles() { const summary = this.report?.summary || {} @@ -594,13 +655,21 @@ export default { key: 'unpublished', label: t('stackiq', 'Created unpublished'), }, + { key: 'unarchived', label: t('stackiq', 'Unarchived') }, + { key: 'restored', label: t('stackiq', 'Restored') }, + { key: 'archived', label: t('stackiq', 'Archived') }, + { key: 'deleted', label: t('stackiq', 'Deleted') }, ] .map((tile) => ({ ...tile, value: Number(summary[tile.key]) || 0, })) - // Only an import run with publishing off leaves modules unpublished. - .filter((tile) => tile.key !== 'unpublished' || tile.value > 0) + // Only an import run with publishing off leaves modules unpublished, and + // only a re-import archives, deletes or brings back applications. + .filter( + (tile) => + !COUNTED_ONLY_TILES.includes(tile.key) || tile.value > 0, + ) ) }, @@ -619,28 +688,72 @@ export default { updated: Number(summary.updated) || 0, unchanged: Number(summary.unchanged) || 0, } + let sentence if (this.report?.cancelled) { - return t( + sentence = t( 'stackiq', 'Import for {name} cancelled after {read} rows.', counts, AS_TEXT, ) - } - if (this.report?.municipality?.created) { - return t( + } else if (this.report?.municipality?.created) { + sentence = t( 'stackiq', 'Import finished. The municipality {name} was created. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.', counts, AS_TEXT, ) + } else { + sentence = t( + 'stackiq', + 'Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.', + counts, + AS_TEXT, + ) } - return t( - 'stackiq', - 'Import for {name} finished. {read} rows read: {created} created, {updated} updated, {unchanged} unchanged.', - counts, - AS_TEXT, - ) + const applications = this.applicationChangesText + return applications === '' ? sentence : `${sentence} ${applications}` + }, + + /** + * What the import did to applications that are not on the sheets: the + * non-zero counts, for the sentence the live region announces. + * + * @return {string} A sentence, or '' when no application was touched + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-the-admin-settings-shall-offer-a-cmdb-import-section-req-cmdb-014 + */ + applicationChangesText() { + const summary = this.report?.summary || {} + const parts = [ + [ + 'archived', + (count) => t('stackiq', '{count} archived', { count }, AS_TEXT), + ], + [ + 'unarchived', + (count) => + t('stackiq', '{count} unarchived', { count }, AS_TEXT), + ], + [ + 'deleted', + (count) => t('stackiq', '{count} deleted', { count }, AS_TEXT), + ], + [ + 'restored', + (count) => t('stackiq', '{count} restored', { count }, AS_TEXT), + ], + ] + .map(([key, text]) => [Number(summary[key]) || 0, text]) + .filter(([count]) => count > 0) + .map(([count, text]) => text(count)) + return parts.length === 0 + ? '' + : t( + 'stackiq', + 'Applications: {changes}.', + { changes: parts.join(', ') }, + AS_TEXT, + ) }, /** @@ -920,6 +1033,7 @@ export default { }, updateExisting: this.updateExisting, publish: this.publish, + missingRecords: this.missingRecords, operationId: this.operationId, }) const response = await axios.post(importUrl(), form) @@ -1022,7 +1136,7 @@ export default { }) this.cancelStatus = t( 'stackiq', - 'Cancelling the import. It stops before the next row; the rows already processed stay imported.', + 'Cancelling the import. It stops before the next row or, while the applications are compared with the export, before the next application; what was already processed stays.', ) } catch (error) { // The import keeps running; say why, and let the admin press Cancel again. @@ -1091,6 +1205,17 @@ export default { color: var(--color-text-maxcontrast); } +.cmdb-import__choice { + margin: 0; + padding: 0; + border: 0; +} + +.cmdb-import__legend { + padding: 0; + font-weight: bold; +} + .cmdb-import__help--warning { color: var(--color-warning-text); } diff --git a/tests/Unit/Controller/CmdbImportControllerTest.php b/tests/Unit/Controller/CmdbImportControllerTest.php index a27b7cfbf..75af4b05f 100644 --- a/tests/Unit/Controller/CmdbImportControllerTest.php +++ b/tests/Unit/Controller/CmdbImportControllerTest.php @@ -114,7 +114,7 @@ private function controller( if ($service === null) { $service = $this->createMock(CmdbExportImportService::class); $service->method('maxFileBytes')->willReturn(10485760); - $service->method('supportsMissingRecords')->willReturnCallback(fn (string $mode): bool => $mode === 'keep'); + $service->method('missingRecordsModes')->willReturn(['keep', 'archive']); } $logger = ($logger ?? $this->createMock(LoggerInterface::class)); @@ -157,7 +157,7 @@ protected function iniBytes(string $name): ?int { private function service(): CmdbExportImportService|MockObject { $service = $this->createMock(CmdbExportImportService::class); $service->method('maxFileBytes')->willReturn(10485760); - $service->method('supportsMissingRecords')->willReturnCallback(fn (string $mode): bool => $mode === 'keep'); + $service->method('missingRecordsModes')->willReturn(['keep', 'archive']); return $service; }//end service() @@ -371,9 +371,30 @@ public function testAReservedMissingRecordsValueIsRefused(): void { $response = $this->controller(file: $this->file(path: $this->upload()), params: ['missingRecords' => $mode], service: $service)->import(); $this->assertSame(422, $response->getStatus(), $mode); $this->assertSame('MISSING_RECORDS_UNSUPPORTED', $response->getData()['error'], $mode); + $this->assertEquals((object)['accepted' => ['keep', 'archive']], $response->getData()['details'], $mode); + $this->assertSame('Applications missing from the export can only be archived or kept.', $response->getData()['message'], $mode); } }//end testAReservedMissingRecordsValueIsRefused() + /** + * Without missingRecords the import archives; keep and archive are passed through as sent. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 + */ + public function testMissingRecordsDefaultsToArchive(): void { + foreach ([[[], 'archive'], [['missingRecords' => 'keep'], 'keep'], [['missingRecords' => 'archive'], 'archive']] as [$params, $expected]) { + $service = $this->service(); + $service->expects($this->once())->method('import') + ->with($this->anything(), $this->callback(static fn (array $options): bool => $options['missingRecords'] === $expected)) + ->willReturn(['success' => true]); + + $response = $this->controller(file: $this->file(path: $this->upload()), params: array_merge(['municipalityName' => 'Gemeente Voorbeeldstad'], $params), service: $service)->import(); + $this->assertSame(200, $response->getStatus(), $expected); + } + }//end testMissingRecordsDefaultsToArchive() + /** * Without a municipality the answer is 422 MUNICIPALITY_REQUIRED and nothing is imported. * @@ -407,6 +428,7 @@ public static function serviceErrors(): array { 'schema' => [CmdbImportException::SCHEMA_OUTDATED, 503, ['schema' => 'module', 'missing' => ['externalKey']]], 'running' => [CmdbImportException::IMPORT_IN_PROGRESS, 409, []], 'ambiguous' => [CmdbImportException::MUNICIPALITY_AMBIGUOUS, 422, ['matches' => ['00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-000000000002']]], + 'archive' => [CmdbImportException::ARCHIVE_UNAVAILABLE, 503, []], ]; }//end serviceErrors() @@ -421,6 +443,10 @@ public static function engineMessages(): array { 'schema' => [CmdbImportException::SCHEMA_OUTDATED, 'The stackiq register is out of date; import its configuration again.'], 'running' => [CmdbImportException::IMPORT_IN_PROGRESS, 'Another CMDB import is running; try again when it has finished.'], 'ambiguous' => [CmdbImportException::MUNICIPALITY_AMBIGUOUS, 'Several municipalities have this name; choose one from the list.'], + 'archive' => [ + CmdbImportException::ARCHIVE_UNAVAILABLE, + 'Applications missing from the export cannot be archived: OpenRegister is missing or too old. Choose to keep them, or update OpenRegister.', + ], ]; }//end engineMessages() @@ -533,6 +559,7 @@ public function testAValidUploadReturnsTheReport(): void { 'municipalityName' => 'Gemeente Voorbeeldstad', 'updateExisting' => false, 'publish' => true, + 'missingRecords' => 'keep', 'operationId' => 'cmdb-00000000-0000-0000-0000-000000000000', 'fileName' => 'export.xlsx', ] @@ -567,6 +594,7 @@ public function testTheFieldsReachTheServiceAsSent(): void { 'municipalityName' => 'Gemeente Voorbeeldstad', 'updateExisting' => true, 'publish' => true, + 'missingRecords' => 'archive', 'operationId' => 'not-a-cmdb-id', 'fileName' => 'export.xlsx', ] diff --git a/tests/Unit/Service/Cmdb/CmdbImportReportTest.php b/tests/Unit/Service/Cmdb/CmdbImportReportTest.php index a3d7a576a..ef208a36f 100644 --- a/tests/Unit/Service/Cmdb/CmdbImportReportTest.php +++ b/tests/Unit/Service/Cmdb/CmdbImportReportTest.php @@ -69,4 +69,27 @@ public function testWithinTheLimitEveryRowIsStored(): void { $this->assertSame(array_merge($report->toArray(), ['rowsStored' => 5, 'rowsTruncated' => false]), $stored); }//end testWithinTheLimitEveryRowIsStored() + + /** + * The reconciliation outcomes are counted, and their entries (row 0) are not counted as processed sheet rows. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testTheReconciliationOutcomesAreCounted(): void { + $report = new CmdbImportReport(operationId: 'cmdb-report-02', rowsRead: 2); + $report->addRow(sheet: 'S', row: 2, appId: '1', name: 'Een', outcome: CmdbImportReport::UNARCHIVED); + $report->addRow(sheet: 'S', row: 3, appId: '2', name: 'Twee', outcome: CmdbImportReport::RESTORED); + $report->addRow(sheet: 'Gearchiveerde Applicaties', row: 0, appId: '7', name: 'Zeven', outcome: CmdbImportReport::ARCHIVED); + $report->addRow(sheet: '', row: 0, appId: '8', name: 'Acht', outcome: CmdbImportReport::DELETED); + + $summary = $report->summary(); + $this->assertSame(2, $summary['processed']); + $this->assertSame(2, $report->processed()); + $this->assertSame([1, 1, 1, 1], [$summary['archived'], $summary['unarchived'], $summary['deleted'], $summary['restored']]); + $this->assertFalse($report->isCancelled()); + $report->markCancelled(); + $this->assertTrue($report->isCancelled()); + }//end testTheReconciliationOutcomesAreCounted() }//end class diff --git a/tests/Unit/Service/Cmdb/CmdbWorkbookReaderTest.php b/tests/Unit/Service/Cmdb/CmdbWorkbookReaderTest.php index b4d561809..fa5754636 100644 --- a/tests/Unit/Service/Cmdb/CmdbWorkbookReaderTest.php +++ b/tests/Unit/Service/Cmdb/CmdbWorkbookReaderTest.php @@ -1055,6 +1055,34 @@ public function testAnUncachedArchiveKeyIsListedNotDropped(): void { $this->assertSame([2, 5], $archive['uncachedRows'], 'the rows whose APPID formula has no cached value'); }//end testAnUncachedArchiveKeyIsListedNotDropped() + /** + * A CMDB row whose only content is an APPID formula without a cached value is dropped as empty, but its row + * number is listed per sheet, so a caller knows the sheet holds an application it could not see. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAnEmptyCmdbRowWithAnUncachedKeyIsListed(): void { + $this->requireSpreadsheet(); + $path = CmdbTestSupport::buildWorkbook( + sheets: [ + 'Beheerde Applicaties CMDB' => [['APPID', 'Applicatie Naam'], [1, 'Een'], [['f' => 'Invoer!A2']], [2, 'Twee']], + 'Onbeh Applicaties CMDB' => [['APPID', 'Applicatie Naam'], [3, 'Drie']], + ] + ); + + try { + $result = (new CmdbWorkbookReader())->read(path: $path, profile: $this->profile()); + } finally { + unlink($path); + } + + $beheerd = array_filter($result['rows'], static fn (array $row): bool => $row['sheet'] === 'Beheerde Applicaties CMDB'); + $this->assertSame([2, 4], array_column($beheerd, 'row'), 'the empty row is not a row'); + $this->assertSame(['Beheerde Applicaties CMDB' => [3]], $result['uncachedKeyRows'], 'the dropped row is listed, only on its sheet'); + }//end testAnEmptyCmdbRowWithAnUncachedKeyIsListed() + /** * An archive sheet whose last used row lies beyond twice the row limit stops before any sheet is loaded. * diff --git a/tests/Unit/Service/CmdbExportImportServiceTest.php b/tests/Unit/Service/CmdbExportImportServiceTest.php index d81417ea7..2cf2e04c8 100644 --- a/tests/Unit/Service/CmdbExportImportServiceTest.php +++ b/tests/Unit/Service/CmdbExportImportServiceTest.php @@ -91,6 +91,55 @@ class CmdbExportImportServiceTest extends TestCase { */ private $beforeSave = null; + /** + * The archive and trash markers per schema id and uuid, as OpenRegister keeps them in `@self`. + * + * @var array|null, deleted?: array|null}>> + */ + private array $states = []; + + /** + * Every archive, unarchive, restore and delete: verb, schema id, uuid, reason. + * + * @var array + */ + private array $transitions = []; + + /** + * Called before every archive, unarchive, restore and delete with the verb and the uuid; may throw. + * + * @var callable|null + */ + private $beforeTransition = null; + + /** + * How often the trash listing was read. + * + * @var int + */ + private int $trashReads = 0; + + /** + * The uuid whose delete OpenRegister refuses (deleteObject() answers false), when set. + * + * @var string|null + */ + private ?string $deleteRefusedFor = null; + + /** + * How many of the next trash reads throw. + * + * @var int + */ + private int $trashFailures = 0; + + /** + * Whether the container knows OpenRegister's archive handler and object mapper. + * + * @var bool + */ + private bool $archiveServicesAvailable = true; + /** * Contacts in the fake address book: uid => name, email. * @@ -215,6 +264,13 @@ protected function setUp(): void { $this->store = [self::MODULE => [], self::ORGANIZATION => [], self::USAGE => [], self::CONTACT_PERSON => []]; $this->saves = []; $this->beforeSave = null; + $this->states = []; + $this->transitions = []; + $this->beforeTransition = null; + $this->trashReads = 0; + $this->deleteRefusedFor = null; + $this->trashFailures = 0; + $this->archiveServicesAvailable = true; $this->contacts = []; $this->addressBooks = []; $this->contactsEnabled = true; @@ -341,20 +397,26 @@ private function noteScope(string $method, bool $rbac, bool $multitenancy): void * * @param string $uuid The uuid. * @param array $data The object data. + * @param array $state The archive and trash markers, serialised under `@self`. + * @param string|null $schema The schema id, as OpenRegister's own reads set it. * * @return ObjectEntityInterface */ - private function entity(string $uuid, array $data): ObjectEntityInterface { - return new class($uuid, $data) implements ObjectEntityInterface { + private function entity(string $uuid, array $data, array $state = [], ?string $schema = null): ObjectEntityInterface { + return new class($uuid, $data, $state, $schema) implements ObjectEntityInterface { /** * Constructor. * * @param string $uuid The uuid. * @param array $data The data. + * @param array $state The markers. + * @param string|null $schema The schema id. */ public function __construct( private string $uuid, private array $data, + private array $state, + private ?string $schema, ) { } @@ -371,7 +433,7 @@ public function getRegister(): ?string { } public function getSchema(): ?string { - return null; + return $this->schema; } public function getOrganisation(): ?string { @@ -383,7 +445,7 @@ public function getOwner(): ?string { } public function jsonSerialize(): array { - return $this->data; + return array_merge($this->data, ['@self' => array_merge(['archived' => null, 'deleted' => null], $this->state)]); } }; }//end entity() @@ -403,6 +465,11 @@ function (array $object, ?array $extend = [], $register = null, $schema = null, ($this->beforeSave)($schema, $object); } + // OpenRegister refuses a data write to an archived object (ObjectStateWriteException). + if ($uuid !== null && empty($this->states[$schema][$uuid]['archived']) === false) { + throw new RuntimeException('write to an archived object'); + } + $create = ($uuid === null); if ($create === true) { $uuid = sprintf('00000000-0000-4000-8000-%012d', count($this->saves) + 1); @@ -435,12 +502,36 @@ function (array $query = [], bool $_rbac = true, bool $_multitenancy = true): ar } } + $state = ($this->states[$schema][$uuid] ?? []); + // The list search loses every soft-deleted row, `_includeDeleted` or not: + // MagicSearchHandler::convertRowToObjectEntity() parses the JSON `_deleted` + // marker as a date, fails, and skips the row (OpenRegister beta ff4dad5c). + if (empty($state['deleted']) === false) { + continue; + } + + if (self::inLens(query: $query, uuid: (string)$uuid, state: $state) === false) { + continue; + } + + // The same converter hydrates no `_archived`: a search result never carries the archive marker. $found[] = $this->entity(uuid: $uuid, data: $data); } return array_slice($found, $offset, $limit); } ); + $service->method('deleteObject')->willReturnCallback( + function (string $uuid, $register = null, $schema = null, bool $_rbac = true, bool $_multitenancy = true): bool { + $this->noteScope(method: 'deleteObject', rbac: $_rbac, multitenancy: $_multitenancy); + if ($this->deleteRefusedFor === $uuid) { + return false; + } + + $this->changeState(verb: 'delete', uuid: $uuid, reason: null); + return true; + } + ); $service->method('find')->willReturnCallback( function ($id, ?array $_extend = [], bool $files = false, $register = null, $schema = null, bool $_rbac = true, bool $_multitenancy = true): ?ObjectEntityInterface { $this->noteScope(method: 'find', rbac: $_rbac, multitenancy: $_multitenancy); @@ -459,6 +550,227 @@ function ($id, ?array $_extend = [], bool $files = false, $register = null, $sch return $service; }//end objectService() + /** + * Whether a working or archived object is in the list a query asks for, as OpenRegister's lenses decide. + * + * Archived objects are left out unless `_archived` asks for them (`true` + * only the archive, `any` both); `_ids` narrows to those uuids. + * + * @param array $query The search query. + * @param string $uuid The object. + * @param array $state Its markers. + * + * @return bool + */ + private static function inLens(array $query, string $uuid, array $state): bool { + if (isset($query['_ids']) === true && in_array($uuid, (array)$query['_ids'], true) === false) { + return false; + } + + $archived = (empty($state['archived']) === false); + $lens = ($query['_archived'] ?? null); + if ($lens === 'any') { + return true; + } + + if (filter_var($lens, FILTER_VALIDATE_BOOLEAN) === true) { + return $archived; + } + + return $archived === false; + }//end inLens() + + /** + * Archive, unarchive, restore or soft-delete a stored object, as OpenRegister's handlers do. + * + * @param string $verb archive, unarchive, restore or delete. + * @param string $uuid The object. + * @param string|null $reason The archive reason. + * + * @return array{uuid: string, archived: array|null} + */ + public function changeState(string $verb, string $uuid, ?string $reason): array { + if ($this->beforeTransition !== null) { + ($this->beforeTransition)($verb, $uuid); + } + + $schema = 0; + foreach ($this->store as $id => $objects) { + if (isset($objects[$uuid]) === true) { + $schema = $id; + } + } + + $this->assertNotSame(0, $schema, 'no stored object ' . $uuid); + $this->transitions[] = [$verb, $schema, $uuid, $reason]; + $state = ($this->states[$schema][$uuid] ?? []); + if ($verb !== 'restore' && empty($state['deleted']) === false) { + // The archive handler and the delete resolve the object without the trash. + throw new \OCP\AppFramework\Db\DoesNotExistException('object is in the trash'); + } + + match ($verb) { + 'archive' => $state['archived'] = ($state['archived'] ?? null) ?: ['by' => 'admin', 'at' => '2026-10-09T00:00:00+00:00', 'reason' => $reason], + 'unarchive' => $state['archived'] = null, + 'restore' => $state['deleted'] = null, + 'delete' => $state['deleted'] = ['deletedBy' => 'admin', 'deletedAt' => '2026-10-09T00:00:00+00:00'], + }; + $this->states[$schema][$uuid] = $state; + + return ['uuid' => $uuid, 'archived' => ($state['archived'] ?? null)]; + }//end changeState() + + /** + * OpenRegister's archive handler and object mapper over the in-memory store. + * + * @return array{0: object, 1: object} + */ + private function archiveDoubles(): array { + $test = $this; + $handler = new class($test) { + /** + * Constructor. + * + * @param CmdbExportImportServiceTest $test The test, which holds the markers. + */ + public function __construct( + private CmdbExportImportServiceTest $test, + ) { + } + + /** + * Archive an object. + * + * @param string $identifier The uuid. + * @param string|null $reason The reason. + * @param string|null $register Ignored. + * @param string|null $schema Ignored. + * + * @return array + */ + public function archive(string $identifier, ?string $reason = null, ?string $register = null, ?string $schema = null): array { + return $this->test->changeState(verb: 'archive', uuid: $identifier, reason: $reason); + } + + /** + * Unarchive an object. + * + * @param string $identifier The uuid. + * @param string|null $reason The reason. + * @param string|null $register Ignored. + * @param string|null $schema Ignored. + * + * @return array + */ + public function unarchive(string $identifier, ?string $reason = null, ?string $register = null, ?string $schema = null): array { + return $this->test->changeState(verb: 'unarchive', uuid: $identifier, reason: $reason); + } + }; + $mapper = new class($test) { + /** + * Constructor. + * + * @param CmdbExportImportServiceTest $test The test, which holds the markers. + */ + public function __construct( + private CmdbExportImportServiceTest $test, + ) { + } + + /** + * Take an object out of the trash and answer it as the mapper reads it, archive marker included. + * + * @param string $uuid The uuid. + * + * @return object + */ + public function restoreObject(string $uuid): object { + $this->test->changeState(verb: 'restore', uuid: $uuid, reason: null); + return $this->test->storedEntity(uuid: $uuid); + } + + /** + * The trash: every soft-deleted object, in every register and schema. + * + * @param int|null $limit Ignored. + * @param int|null $offset Ignored. + * @param bool $_rbac Must be false. + * @param bool $_multitenancy Must be false. + * + * @return array + */ + public function findDeletedAcrossAllMagicTables(?int $limit = null, ?int $offset = null, bool $_rbac = true, bool $_multitenancy = true): array { + return $this->test->trashListing(rbac: $_rbac, multitenancy: $_multitenancy); + } + }; + + return [$handler, $mapper]; + }//end archiveDoubles() + + /** + * A stored object as OpenRegister's mapper reads it by uuid: data plus both markers. + * + * @param string $uuid The object. + * + * @return ObjectEntityInterface + */ + public function storedEntity(string $uuid): ObjectEntityInterface { + foreach ($this->store as $schema => $objects) { + if (isset($objects[$uuid]) === true) { + return $this->entity(uuid: $uuid, data: $objects[$uuid], state: ($this->states[$schema][$uuid] ?? []), schema: (string)$schema); + } + } + + $this->fail('no stored object ' . $uuid); + }//end storedEntity() + + /** + * The trash as MagicMapper::findDeletedAcrossAllMagicTables() lists it. + * + * Its rows go through MagicMapper::rowToObjectEntity(): uuid, register, + * schema and the data, with every string that parses as JSON decoded + * ("2" becomes 2), and no archive or trash marker. + * + * @param bool $rbac The `_rbac` argument. + * @param bool $multitenancy The `_multitenancy` argument. + * + * @return array + */ + public function trashListing(bool $rbac, bool $multitenancy): array { + $this->noteScope(method: 'findDeletedAcrossAllMagicTables', rbac: $rbac, multitenancy: $multitenancy); + $this->trashReads++; + if ($this->trashFailures > 0) { + $this->trashFailures--; + throw new RuntimeException('trash listing failed'); + } + + $listed = []; + foreach ($this->states as $schema => $states) { + foreach ($states as $uuid => $state) { + if (empty($state['deleted']) === true || isset($this->store[$schema][$uuid]) === false) { + continue; + } + + $data = array_map( + static function (mixed $value): mixed { + if (is_string($value) === true) { + $decoded = json_decode($value, true); + if (json_last_error() === JSON_ERROR_NONE) { + return $decoded; + } + } + + return $value; + }, + $this->store[$schema][$uuid] + ); + $listed[] = $this->entity(uuid: (string)$uuid, data: $data, schema: (string)$schema); + } + } + + return $listed; + }//end trashListing() + /** * OpenRegister's schema mapper over the declared properties. * @@ -674,19 +986,29 @@ function (&$value): void { /** * A reader that hands out given rows, for tests that do not need the fixture. * + * The workbook has the archive sheet, listing $archived; null is a workbook without the sheet. + * * @param array}> $rows The rows. + * @param array|null $archived The APPIDs on the archive sheet, or null when there is no such sheet. + * @param array $uncachedRows The archive sheet's rows whose APPID formula has no cached value. * * @return CmdbWorkbookReader */ - private function rowsReader(array $rows): CmdbWorkbookReader { - return new class($rows) extends CmdbWorkbookReader { + private function rowsReader(array $rows, ?array $archived = [], array $uncachedRows = [], array $uncachedKeyRows = []): CmdbWorkbookReader { + return new class($rows, $archived, $uncachedRows, $uncachedKeyRows) extends CmdbWorkbookReader { /** * Constructor. * * @param array> $rows The rows. + * @param array|null $archived The archive sheet's APPIDs, or null. + * @param array $uncachedRows The archive sheet's rows without a cached APPID. + * @param array> $uncachedKeyRows The CMDB rows dropped as empty, per sheet. */ public function __construct( private array $rows, + private ?array $archived, + private array $uncachedRows, + private array $uncachedKeyRows = [], ) { } @@ -699,7 +1021,8 @@ public function __construct( * @return array */ public function read(string $path, CmdbImportProfile $profile): array { - return ['rows' => $this->rows, 'importWarnings' => [], 'date1904' => false]; + $archive = ['sheet' => 'Gearchiveerde Applicaties', 'present' => ($this->archived !== null), 'keyColumnMissing' => false, 'appIds' => ($this->archived ?? []), 'uncachedRows' => $this->uncachedRows]; + return ['rows' => $this->rows, 'importWarnings' => [], 'date1904' => false, 'archive' => $archive, 'uncachedKeyRows' => $this->uncachedKeyRows]; } }; }//end rowsReader() @@ -716,10 +1039,11 @@ public function read(string $path, CmdbImportProfile $profile): array { private function service(?CmdbWorkbookReader $reader = null, ?string $profileDir = null, array $config = ['register' => '20']): CmdbExportImportService { $objectService = $this->objectService(); $schemaMapper = $this->schemaMapper(); + [$archiveHandler, $objectMapper] = $this->archiveDoubles(); $container = $this->createMock(ContainerInterface::class); $container->method('has')->willReturn(false); $container->method('get')->willReturnCallback( - function (string $id) use ($objectService, $schemaMapper) { + function (string $id) use ($objectService, $schemaMapper, $archiveHandler, $objectMapper) { if ($id === ObjectServiceInterface::class) { return $objectService; } @@ -728,6 +1052,14 @@ function (string $id) use ($objectService, $schemaMapper) { return $schemaMapper; } + if ($this->archiveServicesAvailable === true && $id === CmdbExportImportService::ARCHIVE_HANDLER_CLASS) { + return $archiveHandler; + } + + if ($this->archiveServicesAvailable === true && $id === CmdbExportImportService::OBJECT_MAPPER_CLASS) { + return $objectMapper; + } + throw new RuntimeException('not in this container: ' . $id); } ); @@ -846,7 +1178,7 @@ public function testTheFixtureCreatesModulesUsagesAndSuppliers(): void { $this->assertTrue($report['success']); $this->assertFalse($report['cancelled']); $this->assertSame('cmdb-test-0001', $report['operationId']); - $this->assertSame(['rowsRead' => 2, 'processed' => 2, 'created' => 2, 'updated' => 0, 'unchanged' => 0, 'skipped' => 0, 'failed' => 0, 'warnings' => 0, 'unpublished' => 0], $report['summary']); + $this->assertSame(['rowsRead' => 2, 'processed' => 2, 'created' => 2, 'updated' => 0, 'unchanged' => 0, 'skipped' => 0, 'failed' => 0, 'archived' => 0, 'unarchived' => 0, 'deleted' => 0, 'restored' => 0, 'warnings' => 0, 'unpublished' => 0], $report['summary']); $this->assertSame('Gemeente Voorbeeldstad', $report['municipality']['name']); $this->assertTrue($report['municipality']['created']); $this->assertSame(['No municipality named "Gemeente Voorbeeldstad" was found, so it was created. Check the name if you meant an existing one.'], array_column($report['importWarnings'], 'message'), 'only the warning that the municipality was created'); @@ -1448,29 +1780,1083 @@ public function testUpdateExistingFalseSkipsMatches(): void { }//end testUpdateExistingFalseSkipsMatches() /** - * A module missing from a newer export, and its usage, are left as they are. + * The uuid of the stored module with this APPID. + * + * @param string $appId The APPID. + * + * @return string + */ + private function moduleOf(string $appId): string { + foreach ($this->store[self::MODULE] as $uuid => $module) { + if (($module['externalNumber'] ?? null) === $appId) { + return (string)$uuid; + } + } + + $this->fail('no module with APPID ' . $appId); + }//end moduleOf() + + /** + * The uuid of the stored usage of a module by a consumer. + * + * @param string $moduleUuid The module. + * @param string $consumer The consumer. + * + * @return string + */ + private function usageOf(string $moduleUuid, string $consumer = 'muni-1'): string { + foreach ($this->store[self::USAGE] as $uuid => $usage) { + if (($usage['module'] ?? null) === $moduleUuid && ($usage['consumer'] ?? null) === $consumer) { + return (string)$uuid; + } + } + + $this->fail('no usage of ' . $moduleUuid . ' by ' . $consumer); + }//end usageOf() + + /** + * Whether a stored object carries the archive marker, and whether the trash marker. + * + * @param int $schema The schema id. + * @param string $uuid The object. + * + * @return array{0: bool, 1: bool} Archived, deleted. + */ + private function stateOf(int $schema, string $uuid): array { + $state = ($this->states[$schema][$uuid] ?? []); + return [empty($state['archived']) === false, empty($state['deleted']) === false]; + }//end stateOf() + + /** + * Import APPIDs 1, 7 and 8 for muni-1, as a first import would. * * @return void */ - public function testRecordsMissingFromTheExportStay(): void { + private function importOneSevenAndEight(): void { $this->seedOrganisation(uuid: 'muni-1', name: 'Gemeente Voorbeeldstad', type: 'Municipality'); - $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', row: 2), $this->row(appId: '7', row: 3, sheet: 'Onbeh Applicaties CMDB')])) - ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $rows = [ + $this->row(appId: '1', row: 2), + $this->row(appId: '7', row: 3, sheet: 'Onbeh Applicaties CMDB'), + $this->row(appId: '8', row: 4, cells: ['Applicatie Eigenaar (Persoon)' => 'Achternaam, Voornaam']), + ]; + $this->service(reader: $this->rowsReader(rows: $rows))->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + $this->transitions = []; + }//end importOneSevenAndEight() + + /** + * A module missing from a newer export, and its usage, are left as they are when records are kept. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-records-missing-from-a-newer-export-shall-be-archived-by-default-or-kept-on-request-req-cmdb-012 + */ + public function testRecordsMissingFromTheExportStayWhenKept(): void { + $this->importOneSevenAndEight(); $modules = $this->store[self::MODULE]; $usages = $this->store[self::USAGE]; - $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', cells: ['Applicatie Naam' => 'Nieuw'])])) - ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', cells: ['Applicatie Naam' => 'Nieuw'])])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); foreach ($modules as $uuid => $module) { - if ($module['externalNumber'] === '7') { + if ($module['externalNumber'] !== '1') { $this->assertSame($module, $this->store[self::MODULE][$uuid]); } } $this->assertSame($usages, $this->store[self::USAGE]); - $this->assertCount(2, $this->store[self::MODULE]); - }//end testRecordsMissingFromTheExportStay() + $this->assertSame([], $this->transitions, 'nothing is archived or deleted'); + $this->assertSame([], $this->states); + $this->assertSame(['updated'], array_column($report['rows'], 'outcome')); + $this->assertSame([], $report['importWarnings'], 'keep does not need the archive sheet'); + }//end testRecordsMissingFromTheExportStayWhenKept() + + /** + * By default an application that moved to the archive sheet is archived and one on no sheet is soft-deleted, + * usage before module, and the owner contact person stays. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testMissingApplicationsAreArchivedOrDeletedByDefault(): void { + $this->importOneSevenAndEight(); + $contactPersons = $this->store[self::CONTACT_PERSON]; + $this->assertNotSame([], $contactPersons, 'APPID 8 has an owner'); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7', '1198'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-archive-01']); + + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $seven))); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $eight)); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $eight))); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $this->moduleOf(appId: '1'))); + $this->assertSame( + [ + ['archive', self::USAGE, $this->usageOf(moduleUuid: $seven)], + ['archive', self::MODULE, $seven], + ['delete', self::USAGE, $this->usageOf(moduleUuid: $eight)], + ['delete', self::MODULE, $eight], + ], + array_map(static fn (array $t): array => array_slice($t, 0, 3), $this->transitions), + 'the usage is written before its module' + ); + $this->assertSame('cmdb-import: on sheet "Gearchiveerde Applicaties" (cmdb-archive-01)', $this->transitions[0][3]); + $this->assertSame($contactPersons, $this->store[self::CONTACT_PERSON], 'contact persons are never touched'); + + $reconciled = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame( + [ + ['sheet' => 'Gearchiveerde Applicaties', 'row' => 0, 'appId' => '7', 'name' => 'Applicatie 7', 'outcome' => 'archived', 'reasons' => [], 'warnings' => [], 'moduleUuid' => $seven, 'usageUuid' => $this->usageOf(moduleUuid: $seven)], + ['sheet' => '', 'row' => 0, 'appId' => '8', 'name' => 'Applicatie 8', 'outcome' => 'deleted', 'reasons' => [], 'warnings' => [], 'moduleUuid' => $eight, 'usageUuid' => $this->usageOf(moduleUuid: $eight)], + ], + $reconciled + ); + $this->assertSame(1, $report['summary']['processed'], 'reconciliation entries are not sheet rows'); + $this->assertSame([1, 1], [$report['summary']['archived'], $report['summary']['deleted']]); + $this->assertStringNotContainsString('Achternaam', (string)json_encode($report)); + $this->assertSame([], $report['importWarnings']); + + $this->transitions = []; + $again = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $this->assertSame([], $this->transitions, 'an application already in the state the export asks for is left alone'); + $this->assertSame(['unchanged'], array_column($again['rows'], 'outcome')); + }//end testMissingApplicationsAreArchivedOrDeletedByDefault() + + /** + * Without the archive sheet a missing application is archived, never deleted, and the report warns. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testWithoutTheArchiveSheetMissingApplicationsAreArchivedNotDeleted(): void { + $this->importOneSevenAndEight(); + $eight = $this->moduleOf(appId: '8'); + $this->states[self::MODULE][$eight] = ['deleted' => ['deletedBy' => 'admin']]; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: null)) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $this->moduleOf(appId: '7'))); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $this->moduleOf(appId: '7')))); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $eight), 'what is in the trash stays there'); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $eight))); + $this->assertNotContains('delete', array_column($this->transitions, 0)); + $this->assertNotContains('restore', array_column($this->transitions, 0)); + $this->assertSame('cmdb-import: not in the export (' . $report['operationId'] . ')', $this->transitions[0][3]); + $this->assertSame(['unchanged', 'archived', 'archived'], array_column($report['rows'], 'outcome')); + $this->assertSame( + [['sheet' => 'Gearchiveerde Applicaties', 'message' => 'Sheet "Gearchiveerde Applicaties" was not found, so applications missing from the export are archived, not deleted.']], + $report['importWarnings'] + ); + }//end testWithoutTheArchiveSheetMissingApplicationsAreArchivedNotDeleted() + + /** + * An archive sheet with an APPID the reader could not see is not authoritative for a deletion: the import + * archives what it would have deleted, and warns naming the sheet and the row. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAnUncachedArchiveRowArchivesInsteadOfDeleting(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + + // APPID 7 is listed on the archive sheet; APPID 8 is on no sheet, and row 4 of the sheet holds a formula without a cached value. + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'], uncachedRows: [4])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertNotContains('delete', array_column($this->transitions, 0), 'nothing is soft-deleted'); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $eight), 'APPID 8 may be the unreadable row, so it is archived'); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $eight))); + $this->assertSame('cmdb-import: not in the export (' . $report['operationId'] . ')', $this->transitions[2][3]); + $this->assertSame( + [['sheet' => 'Gearchiveerde Applicaties', 'message' => 'Sheet "Gearchiveerde Applicaties" has an APPID without a cached value in row 4, so applications missing from the export are archived, not deleted.']], + $report['importWarnings'] + ); + $this->assertSame([2, 0], [$report['summary']['archived'], $report['summary']['deleted']]); + + // A long list of rows is cut. + $this->transitions = []; + $long = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'], uncachedRows: range(2, 13))) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'archive']); + $this->assertSame( + 'Sheet "Gearchiveerde Applicaties" has an APPID without a cached value in 12 rows (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …), so applications missing from the export are archived, not deleted.', + $long['importWarnings'][0]['message'], + 'the warning counts the rows and cuts the list' + ); + + $few = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'], uncachedRows: [3, 9])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'archive']); + $this->assertStringContainsString('in 2 rows (3, 9), so', $few['importWarnings'][0]['message']); + + // Keeping missing records needs no sheet and gives no warning. + $kept = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'], uncachedRows: [4])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + $this->assertSame([], $kept['importWarnings']); + }//end testAnUncachedArchiveRowArchivesInsteadOfDeleting() + + /** + * A CMDB-sheet row whose APPID the reader could not see is skipped as empty, so its application looks absent: + * the import archives what it would have deleted, and warns naming the CMDB sheet and the row. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAnUncachedCmdbRowArchivesInsteadOfDeleting(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + + // APPID 7 is on the readable archive sheet; APPID 8 is on no readable sheet, and row 5 of a CMDB sheet has an APPID formula without a cached value. + $report = $this->service( + reader: $this->rowsReader( + rows: [$this->row(appId: '1'), $this->row(appId: '', cells: ['APPID' => null], row: 5, uncached: ['APPID'])], + archived: ['7'] + ) + )->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertNotContains('delete', array_column($this->transitions, 0), 'nothing is soft-deleted'); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $eight), 'APPID 8 may be the unreadable row, so it is archived'); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $eight))); + $this->assertSame([2, 0], [$report['summary']['archived'], $report['summary']['deleted']]); + $this->assertSame( + [['sheet' => 'Beheerde Applicaties CMDB', 'message' => 'Sheet "Beheerde Applicaties CMDB" has an APPID without a cached value in row 5, so applications missing from the export are archived, not deleted.']], + $report['importWarnings'] + ); + + // Keeping missing records needs no warning, and without the unreadable row the same workbook deletes. + $this->transitions = []; + $this->states = array_replace_recursive($this->states, [self::MODULE => [$eight => ['archived' => null]], self::USAGE => [$this->usageOf(moduleUuid: $eight) => ['archived' => null]]]); + $kept = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '', cells: ['APPID' => null], row: 5, uncached: ['APPID'])], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + $this->assertSame([], $kept['importWarnings']); + $this->assertSame([], $this->transitions); + + $readable = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $eight), 'a readable workbook still soft-deletes what is on no sheet'); + $this->assertSame([], $readable['importWarnings']); + }//end testAnUncachedCmdbRowArchivesInsteadOfDeleting() + + /** + * A CMDB row whose only content is an uncached APPID is dropped by the reader before the service sees a row, and + * is handed over as a list of rows: the import still archives instead of deleting, and warns. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAFullyEmptyCmdbRowWithAnUncachedKeyArchivesInsteadOfDeleting(): void { + $this->importOneSevenAndEight(); + $eight = $this->moduleOf(appId: '8'); + + $report = $this->service( + reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'], uncachedKeyRows: ['Beheerde Applicaties CMDB' => [6]]) + )->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertNotContains('delete', array_column($this->transitions, 0)); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $eight)); + $this->assertSame( + [['sheet' => 'Beheerde Applicaties CMDB', 'message' => 'Sheet "Beheerde Applicaties CMDB" has an APPID without a cached value in row 6, so applications missing from the export are archived, not deleted.']], + $report['importWarnings'] + ); + }//end testAFullyEmptyCmdbRowWithAnUncachedKeyArchivesInsteadOfDeleting() + + /** + * An extra usage of a key that cannot be archived fails the application's row; its module, written after every usage, stays + * as it was, and the other applications are still reconciled. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAFailingExtraUsageFailsTheApplicationBeforeItsModule(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + $sevenUsage = $this->usageOf(moduleUuid: $seven); + $this->store[self::USAGE]['usage-second'] = array_merge($this->store[self::USAGE][$sevenUsage], ['id' => 'usage-second']); + $this->beforeTransition = static function (string $verb, string $uuid): void { + if ($uuid === 'usage-second') { + throw new RuntimeException('the extra usage cannot be archived'); + } + }; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $rows = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame(['failed', 'deleted'], array_column($rows, 'outcome')); + $this->assertSame(['step "archive" failed (RuntimeException)'], $rows[0]['reasons']); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $sevenUsage), 'the application\'s own usage went first'); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: 'usage-second'), 'the failing extra is unchanged'); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $seven), 'the module comes after every usage, so it was not reached'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $eight), 'the next application is still reconciled'); + $this->assertSame(1, $report['summary']['failed']); + }//end testAFailingExtraUsageFailsTheApplicationBeforeItsModule() + + /** + * A cancelled import reconciles nothing. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testACancelledImportDoesNotReconcile(): void { + $this->importOneSevenAndEight(); + $service = null; + $this->beforeSave = function (int $schema) use (&$service): void { + if ($schema === self::MODULE) { + $service->requestCancel(operationId: 'cmdb-cancel-02'); + } + }; + $service = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', cells: ['Applicatie Naam' => 'Nieuw']), $this->row(appId: '2', row: 3)])); + + $report = $service->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-cancel-02']); + + $this->assertTrue($report['cancelled']); + $this->assertSame([], $this->transitions); + $this->assertSame(['updated'], array_column($report['rows'], 'outcome')); + }//end testACancelledImportDoesNotReconcile() + + /** + * A cancel that comes in while the last row is processed stops the import before anything is archived or deleted. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testACancelDuringTheLastRowArchivesNothing(): void { + $this->importOneSevenAndEight(); + $service = null; + $this->beforeSave = function (int $schema) use (&$service): void { + if ($schema === self::MODULE) { + $this->assertTrue($service->requestCancel(operationId: 'cmdb-cancel-03')); + } + }; + $service = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', cells: ['Applicatie Naam' => 'Nieuw'])], archived: ['7', '8'])); + $this->trashReads = 0; + $this->searches = []; + + $report = $service->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-cancel-03']); + + $this->assertTrue($report['cancelled']); + $this->assertSame([], $this->transitions); + $this->assertSame(0, $this->trashReads, 'the scope was not loaded: the trash was not read'); + $usageScans = array_filter($this->searches, static fn (array $query): bool => ($query['consumer'] ?? null) === 'muni-1' && isset($query['_offset']) === true); + $this->assertSame([], $usageScans, 'the municipality\'s usages were not paged'); + $this->assertSame(['updated'], array_column($report['rows'], 'outcome')); + $this->assertSame('cancelled', $this->cache['progress_cmdb-cancel-03']['status']); + $this->assertArrayNotHasKey('cancel_cmdb-cancel-03', $this->cache); + }//end testACancelDuringTheLastRowArchivesNothing() + + /** + * A cancel during the reconciliation stops it before the next application and keeps what was archived. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testACancelDuringTheReconciliationStopsBeforeTheNextApplication(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + $service = null; + $this->beforeTransition = function (string $verb, string $uuid) use (&$service, $seven): void { + if ($uuid === $seven) { + $this->assertTrue($service->requestCancel(operationId: 'cmdb-cancel-04')); + } + }; + $service = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7', '8'])); + + $report = $service->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-cancel-04']); + + $this->assertTrue($report['cancelled']); + $this->assertSame([['archive', $this->usageOf(moduleUuid: $seven)], ['archive', $seven]], array_map(static fn (array $step): array => [$step[0], $step[2]], $this->transitions)); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven), 'what was archived stays archived'); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $eight)); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $eight))); + $this->assertSame(['unchanged', 'archived'], array_column($report['rows'], 'outcome')); + $this->assertSame('cancelled', $this->cache['progress_cmdb-cancel-04']['status']); + }//end testACancelDuringTheReconciliationStopsBeforeTheNextApplication() + + /** + * Only this municipality's usages and the modules its import created are reconciled. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAnotherMunicipalitysUsagesAreNeverReconciled(): void { + $this->importOneSevenAndEight(); + $this->seedOrganisation(uuid: 'muni-2', name: 'Gemeente Anders', type: 'Municipality'); + $seven = $this->moduleOf(appId: '7'); + // Another municipality uses the same module (added by hand), and muni-1 uses a hand-made module. + $this->store[self::USAGE]['usage-other'] = ['id' => 'usage-other', 'consumer' => 'muni-2', 'module' => $seven]; + $this->store[self::MODULE]['module-manual'] = ['id' => 'module-manual', 'name' => 'Handmatig', 'externalKey' => 'topdesk:muni-2:9']; + $this->store[self::USAGE]['usage-manual'] = ['id' => 'usage-manual', 'consumer' => 'muni-1', 'module' => 'module-manual']; + + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '8', row: 3)], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $seven))); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven), 'the module carries this municipality\'s import key'); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: 'usage-other')); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: 'usage-manual')); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: 'module-manual')); + $this->assertSame(2, count($this->transitions)); + }//end testAnotherMunicipalitysUsagesAreNeverReconciled() + + /** + * A working pair and a stale pair in the trash under the same import key: the reconciliation acts on the + * working one, as the import's own match does, and never restores the stale one into the archive. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAStaleDuplicateIsNeverRestoredByTheReconciliation(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $usage = $this->usageOf(moduleUuid: $seven); + // A stale module deleted by hand, with its own usage, carries the same import key as the working one. + $this->store[self::MODULE] = ['module-stale' => array_merge($this->store[self::MODULE][$seven], ['id' => 'module-stale'])] + $this->store[self::MODULE]; + $this->store[self::USAGE] = ['usage-stale' => array_merge($this->store[self::USAGE][$usage], ['id' => 'usage-stale', 'module' => 'module-stale'])] + $this->store[self::USAGE]; + $this->states[self::MODULE]['module-stale'] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + $this->states[self::USAGE]['usage-stale'] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + + // APPID 7 is on the archive sheet; APPID 8 is on no sheet. + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertNotContains('restore', array_column($this->transitions, 0), 'the stale duplicate is not restored'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-stale')); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: 'usage-stale')); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $usage)); + $reconciled = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame([['7', 'archived', $seven], ['8', 'deleted', $this->moduleOf(appId: '8')]], array_map(static fn (array $row): array => [$row['appId'], $row['outcome'], $row['moduleUuid']], $reconciled)); + + // A second run finds the working pair archived and the stale pair in the trash: nothing to do. + $this->transitions = []; + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $this->assertSame([], $this->transitions, 'the archived pair is in the state the export asks for, and the stale one is left alone'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-stale')); + }//end testAStaleDuplicateIsNeverRestoredByTheReconciliation() + + /** + * Of two usages under one import key, the one of the module the import would match is reconciled, whatever + * state the usage is in: the module's state ranks first, the usage's second. + * + * The working usage is listed before the one in the trash but belongs to the duplicate module in the trash; + * it is archived as well (a stray usage stays visible otherwise), and the module in the trash is not restored. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testTheModulesStateDecidesWhichDuplicateIsReconciled(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $usage = $this->usageOf(moduleUuid: $seven); + $this->store[self::MODULE]['module-stale'] = array_merge($this->store[self::MODULE][$seven], ['id' => 'module-stale']); + $this->store[self::USAGE]['usage-stale'] = array_merge($this->store[self::USAGE][$usage], ['id' => 'usage-stale', 'module' => 'module-stale']); + $this->states[self::MODULE]['module-stale'] = ['deleted' => ['deletedBy' => 'admin']]; + $this->states[self::USAGE][$usage] = ['deleted' => ['deletedBy' => 'admin']]; + + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '8', row: 3)], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven), 'the working module is archived'); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $usage), 'its usage is restored from the trash and archived'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-stale')); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: 'usage-stale'), 'the working usage of the stale module is archived with the key'); + }//end testTheModulesStateDecidesWhichDuplicateIsReconciled() + + /** + * A usage in the trash that names another consumer is never reconciled, though the trash is not filtered by consumer. + * + * The usage points at a working module that carries this municipality's import key; only the consumer + * check keeps that module out of the scope, and with it out of the trash. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testATrashedUsageOfAnotherConsumerNeverReconcilesItsModule(): void { + $this->importOneSevenAndEight(); + $this->seedOrganisation(uuid: 'muni-2', name: 'Gemeente Anders', type: 'Municipality'); + $seven = $this->moduleOf(appId: '7'); + $this->store[self::MODULE]['module-nine'] = ['id' => 'module-nine', 'name' => 'Negen', 'externalKey' => 'topdesk:muni-1:9', 'externalNumber' => '9']; + $this->store[self::USAGE]['usage-other'] = ['id' => 'usage-other', 'consumer' => 'muni-2', 'module' => 'module-nine']; + $this->states[self::USAGE]['usage-other'] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + + // APPID 9 is on no sheet, so a scope that took the trashed usage in would soft-delete its module. + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '7', row: 3), $this->row(appId: '8', row: 4)], archived: [])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: 'module-nine'), 'the module stays working'); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: 'usage-other'), 'and the other consumer\'s usage stays in the trash'); + $this->assertSame([], $this->transitions); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + }//end testATrashedUsageOfAnotherConsumerNeverReconcilesItsModule() + + /** + * Only a module whose key is exactly this municipality's import key plus an APPID is reconciled. + * + * A module without an external key, one whose key is the bare prefix, and one whose key has another origin + * are used by this municipality but are not what its import created. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testOnlyWhatTheImportCreatedIsReconciled(): void { + $this->importOneSevenAndEight(); + $others = [ + 'module-nokey' => ['id' => 'module-nokey', 'name' => 'Zonder sleutel'], + 'module-bare' => ['id' => 'module-bare', 'name' => 'Kale sleutel', 'externalKey' => 'topdesk:muni-1:'], + 'module-origin' => ['id' => 'module-origin', 'name' => 'Andere herkomst', 'externalKey' => 'xxxxxxx:muni-1:9'], + ]; + foreach ($others as $uuid => $module) { + $this->store[self::MODULE][$uuid] = $module; + $this->store[self::USAGE]['usage-' . $uuid] = ['id' => 'usage-' . $uuid, 'consumer' => 'muni-1', 'module' => $uuid]; + } + + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '7', row: 3), $this->row(appId: '8', row: 4)], archived: [])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame([], $this->transitions, 'nothing the import did not create is archived or deleted'); + $this->assertSame([], $this->states); + }//end testOnlyWhatTheImportCreatedIsReconciled() + + /** + * A delete step that OpenRegister refuses, and a restore step that throws, each fail only their own application. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAFailingDeleteOrRestoreStepFailsOnlyItsApplication(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + + // APPID 7 and 8 are on no sheet: OpenRegister refuses to delete the usage of 7, then deletes 8. + $this->deleteRefusedFor = $this->usageOf(moduleUuid: $seven); + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: [])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $rows = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame([['7', 'failed'], ['8', 'deleted']], array_map(static fn (array $row): array => [$row['appId'], $row['outcome']], $rows)); + $this->assertSame(['step "delete" failed (RuntimeException)'], $rows[0]['reasons']); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $seven), 'the module of the failed application is untouched'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $eight)); + + // APPID 7 and 8 are in the trash and on the archive sheet: the restore of 7 throws, 8 is restored and archived. + $this->deleteRefusedFor = null; + $this->states = []; + foreach ([$seven, $eight] as $uuid) { + $this->states[self::MODULE][$uuid] = ['deleted' => ['deletedBy' => 'admin']]; + $this->states[self::USAGE][$this->usageOf(moduleUuid: $uuid)] = ['deleted' => ['deletedBy' => 'admin']]; + } + + $this->beforeTransition = function (string $verb, string $uuid) use ($seven): void { + if ($verb === 'restore' && $uuid === $this->usageOf(moduleUuid: $seven)) { + throw new RuntimeException('restore failed'); + } + }; + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7', '8'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $rows = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame([['7', 'failed'], ['8', 'archived']], array_map(static fn (array $row): array => [$row['appId'], $row['outcome']], $rows)); + $this->assertSame(['step "restore" failed (RuntimeException)'], $rows[0]['reasons']); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $seven), 'the failed application stays in the trash'); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $eight)); + }//end testAFailingDeleteOrRestoreStepFailsOnlyItsApplication() + + /** + * More usages than one page, and more modules than one chunk, are all reconciled. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testMoreApplicationsThanOnePageAreAllReconciled(): void { + $this->seedOrganisation(uuid: 'muni-1', name: 'Gemeente Voorbeeldstad', type: 'Municipality'); + $count = 501; + for ($index = 1; $index <= $count; $index++) { + $this->store[self::MODULE]['module-' . $index] = ['id' => 'module-' . $index, 'name' => 'Applicatie ' . $index, 'externalKey' => 'topdesk:muni-1:' . $index, 'externalNumber' => (string)$index]; + $this->store[self::USAGE]['usage-' . $index] = ['id' => 'usage-' . $index, 'consumer' => 'muni-1', 'module' => 'module-' . $index]; + } + + // Every application is on no sheet: every one is soft-deleted, the last of both lists included. + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1000')], archived: [])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame($count, $report['summary']['deleted']); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-' . $count)); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: 'usage-' . $count)); + $this->assertCount(($count * 2), $this->transitions); + }//end testMoreApplicationsThanOnePageAreAllReconciled() + + /** + * An archived match wins over a deleted one with the same import key, and a working match over an archived one. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testTheMatchPrefersWorkingThenArchivedThenDeleted(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + $sevenUsage = $this->usageOf(moduleUuid: $seven); + $eightUsage = $this->usageOf(moduleUuid: $eight); + foreach ([['7', $seven, $sevenUsage], ['8', $eight, $eightUsage]] as [$appId, $module, $usage]) { + $this->store[self::MODULE]['stale-module-' . $appId] = array_merge($this->store[self::MODULE][$module], ['id' => 'stale-module-' . $appId]); + $this->store[self::USAGE]['stale-usage-' . $appId] = array_merge($this->store[self::USAGE][$usage], ['id' => 'stale-usage-' . $appId, 'module' => 'stale-module-' . $appId]); + } + + // APPID 7: the real pair is archived, the stale one deleted. APPID 8: the real pair is working, the stale one archived. + $gone = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + $shelved = ['archived' => ['by' => 'admin', 'at' => '2026-10-01T00:00:00+00:00', 'reason' => 'earlier']]; + $this->states[self::MODULE][$seven] = $shelved; + $this->states[self::USAGE][$sevenUsage] = $shelved; + $this->states[self::MODULE]['stale-module-7'] = $gone; + $this->states[self::USAGE]['stale-usage-7'] = $gone; + $this->states[self::MODULE]['stale-module-8'] = $shelved; + $this->states[self::USAGE]['stale-usage-8'] = $shelved; + $counts = [count($this->store[self::MODULE]), count($this->store[self::USAGE])]; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '7', row: 2), $this->row(appId: '8', row: 3, sheet: 'Onbeh Applicaties CMDB')])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['unarchived', 'unchanged'], array_column($report['rows'], 'outcome')); + $this->assertSame([$seven, $eight], array_column($report['rows'], 'moduleUuid')); + $this->assertSame( + [['unarchive', $seven], ['unarchive', $sevenUsage]], + array_map(static fn (array $t): array => [$t[0], $t[2]], $this->transitions), + 'only the preferred pair is revived; nothing is restored from the trash' + ); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'stale-module-7')); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: 'stale-module-8')); + $this->assertSame($counts, [count($this->store[self::MODULE]), count($this->store[self::USAGE])], 'no module or usage is created'); + }//end testTheMatchPrefersWorkingThenArchivedThenDeleted() + + /** + * With keep and without OpenRegister's archive handler a row whose match is archived fails at the module step. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testKeepWithoutTheArchiveServicesFailsARowWhoseMatchIsArchived(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $this->states[self::MODULE][$seven] = ['archived' => ['by' => 'admin', 'at' => '2026-10-01T00:00:00+00:00', 'reason' => 'earlier']]; + $this->archiveServicesAvailable = false; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '7', row: 3, sheet: 'Onbeh Applicaties CMDB')])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['unchanged', 'failed'], array_column($report['rows'], 'outcome')); + $this->assertSame(['step "module" failed (CmdbImportException)'], $report['rows'][1]['reasons']); + $this->assertSame([], $this->transitions); + $this->assertSame(0, $report['summary']['archived'] + $report['summary']['deleted']); + }//end testKeepWithoutTheArchiveServicesFailsARowWhoseMatchIsArchived() + + /** + * Every other usage of a key leaving the export follows the application: a working usage of a module deleted + * by hand, and a second usage of the same module. A stale module in the trash is neither restored nor touched. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testEveryOtherUsageOfAKeyFollowsTheApplication(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + $sevenUsage = $this->usageOf(moduleUuid: $seven); + // APPID 7: a module deleted by hand whose usage stayed working, and a second usage of the working module. + $this->store[self::MODULE]['module-stale'] = array_merge($this->store[self::MODULE][$seven], ['id' => 'module-stale']); + $this->states[self::MODULE]['module-stale'] = ['deleted' => ['deletedBy' => 'admin']]; + $this->store[self::USAGE]['usage-stray'] = array_merge($this->store[self::USAGE][$sevenUsage], ['id' => 'usage-stray', 'module' => 'module-stale']); + $this->store[self::USAGE]['usage-second'] = array_merge($this->store[self::USAGE][$sevenUsage], ['id' => 'usage-second']); + // APPID 8 also has a stale pair in the trash: nothing to follow. + $this->store[self::MODULE]['module-stale-8'] = array_merge($this->store[self::MODULE][$eight], ['id' => 'module-stale-8']); + $this->store[self::USAGE]['usage-stale-8'] = array_merge($this->store[self::USAGE][$this->usageOf(moduleUuid: $eight)], ['id' => 'usage-stale-8', 'module' => 'module-stale-8']); + $this->states[self::MODULE]['module-stale-8'] = ['deleted' => ['deletedBy' => 'admin']]; + $this->states[self::USAGE]['usage-stale-8'] = ['deleted' => ['deletedBy' => 'admin']]; + + // APPID 8 also has a second working module with its own usage, which a first-hit match never reaches. + $this->store[self::MODULE]['module-dup-8'] = array_merge($this->store[self::MODULE][$eight], ['id' => 'module-dup-8']); + $this->store[self::USAGE]['usage-dup-8'] = array_merge($this->store[self::USAGE][$this->usageOf(moduleUuid: $eight)], ['id' => 'usage-dup-8', 'module' => 'module-dup-8']); + + // 7 is on the archive sheet, 8 on no sheet. + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $this->assertSame([[false, true], [false, true]], [$this->stateOf(schema: self::MODULE, uuid: 'module-dup-8'), $this->stateOf(schema: self::USAGE, uuid: 'usage-dup-8')], 'a working duplicate of 8 is deleted with it'); + + foreach ([$sevenUsage, 'usage-stray', 'usage-second'] as $usage) { + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $usage), $usage . ' is archived'); + } + + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-stale'), 'the stale module stays in the trash'); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: 'usage-stale-8'), 'a usage in the trash is never restored'); + $this->assertNotContains('restore', array_column($this->transitions, 0)); + $this->assertSame([1, 1], [$report['summary']['archived'], $report['summary']['deleted']], 'one report row per application'); + + // The same key on no sheet: every usage is soft-deleted, once. + $this->transitions = []; + $this->states = array_replace_recursive($this->states, [self::USAGE => [$sevenUsage => ['archived' => null], 'usage-stray' => ['archived' => null], 'usage-second' => ['archived' => null]], self::MODULE => [$seven => ['archived' => null]]]); + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: [])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + foreach ([$sevenUsage, 'usage-stray', 'usage-second'] as $usage) { + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: $usage), $usage . ' is soft-deleted'); + } + + $deletes = array_column(array_filter($this->transitions, static fn (array $t): bool => $t[0] === 'delete'), 2); + $this->assertSame(count($deletes), count(array_unique($deletes)), 'nothing is deleted twice'); + }//end testEveryOtherUsageOfAKeyFollowsTheApplication() + + /** + * One application that cannot be archived is a failed row; the others are still reconciled. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testAFailingArchiveIsAFailedRowAndTheOthersAreReconciled(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $this->beforeTransition = function (string $verb, string $uuid) use ($seven): void { + if ($uuid === $seven) { + throw new RuntimeException('Schema "Applicatie 7" does not declare x-openregister-archive'); + } + }; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7', '8'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $rows = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame(['failed', 'archived'], array_column($rows, 'outcome')); + $this->assertSame(['step "archive" failed (RuntimeException)'], $rows[0]['reasons']); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $seven)), 'the usage went first'); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $this->moduleOf(appId: '8'))); + $this->assertSame(1, $report['summary']['failed']); + $failed = array_values(array_filter($this->logLines, static fn (string $line): bool => str_starts_with($line, 'CmdbExportImportService: row failed'))); + $this->assertCount(1, $failed); + $this->assertStringNotContainsString('Applicatie 7', $failed[0]); + }//end testAFailingArchiveIsAFailedRowAndTheOthersAreReconciled() + + /** + * Without OpenRegister's archive services an archiving import is refused before the file is read; keep still imports. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testMissingArchiveServicesRefuseAnArchivingImport(): void { + $this->seedOrganisation(uuid: 'muni-1', name: 'Gemeente Voorbeeldstad', type: 'Municipality'); + $this->archiveServicesAvailable = false; + $reader = new class extends CmdbWorkbookReader { + /** + * Constructor. + */ + public function __construct() { + } + + /** + * Never reached. + * + * @param string $path Ignored. + * @param CmdbImportProfile $profile Ignored. + * + * @return array + */ + public function read(string $path, CmdbImportProfile $profile): array { + throw new RuntimeException('the file was read'); + } + }; + + try { + $this->service(reader: $reader)->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $this->fail('ARCHIVE_UNAVAILABLE expected'); + } catch (CmdbImportException $e) { + $this->assertSame('ARCHIVE_UNAVAILABLE', $e->getErrorCode()); + $this->assertSame(503, $e->getHttpStatus()); + } + + $this->assertSame([], $this->saves); + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')]))->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + $this->assertSame(['created'], array_column($report['rows'], 'outcome')); + }//end testMissingArchiveServicesRefuseAnArchivingImport() + + /** + * An archived application that returns is unarchived and a soft-deleted one restored, before the write; no duplicates. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testAReturningApplicationIsUnarchivedOrRestoredNotDuplicated(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7']))->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $modules = count($this->store[self::MODULE]); + $usages = count($this->store[self::USAGE]); + $this->transitions = []; + + $rows = [ + $this->row(appId: '1'), + $this->row(appId: '7', row: 3, sheet: 'Onbeh Applicaties CMDB', cells: ['Applicatie Naam' => 'Terug']), + $this->row(appId: '8', row: 4), + ]; + $report = $this->service(reader: $this->rowsReader(rows: $rows))->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame(['unchanged', 'unarchived', 'restored'], array_column($report['rows'], 'outcome')); + $this->assertSame([1, 1], [$report['summary']['unarchived'], $report['summary']['restored']]); + $this->assertSame([$modules, $usages], [count($this->store[self::MODULE]), count($this->store[self::USAGE])], 'no second module or usage'); + foreach ([$seven, $eight] as $module) { + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $module)); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: $this->usageOf(moduleUuid: $module))); + } + + $this->assertSame('Terug', $this->store[self::MODULE][$seven]['name'], 'the row is updated after the unarchive'); + $this->assertSame(['unarchive', 'unarchive', 'restore', 'restore'], array_column($this->transitions, 0)); + }//end testAReturningApplicationIsUnarchivedOrRestoredNotDuplicated() + + /** + * A usage archived or soft-deleted on its own, next to a working module, is unarchived or restored when its row returns. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testAReturningUsageIsUnarchivedOrRestoredOnItsOwn(): void { + $this->importOneSevenAndEight(); + [$seven, $eight] = [$this->moduleOf(appId: '7'), $this->moduleOf(appId: '8')]; + [$usageSeven, $usageEight] = [$this->usageOf(moduleUuid: $seven), $this->usageOf(moduleUuid: $eight)]; + $this->states[self::USAGE][$usageSeven] = ['archived' => ['by' => 'admin', 'at' => '2026-10-01T00:00:00+00:00', 'reason' => 'by hand']]; + $this->states[self::USAGE][$usageEight] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + $usages = array_keys($this->store[self::USAGE]); + + $rows = [$this->row(appId: '7', sheet: 'Onbeh Applicaties CMDB'), $this->row(appId: '8', row: 3)]; + $report = $this->service(reader: $this->rowsReader(rows: $rows))->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['unarchived', 'restored'], array_column($report['rows'], 'outcome')); + $this->assertSame([$usageSeven, $usageEight], array_column($report['rows'], 'usageUuid')); + $this->assertSame($usages, array_keys($this->store[self::USAGE]), 'no second usage'); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: $usageSeven)); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: $usageEight)); + $this->assertSame([['unarchive', $usageSeven], ['restore', $usageEight]], array_map(static fn (array $t): array => [$t[0], $t[2]], $this->transitions)); + }//end testAReturningUsageIsUnarchivedOrRestoredOnItsOwn() + + /** + * An application soft-deleted while archived is restored and unarchived when it returns, and updated from the restored object. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testAnApplicationDeletedWhileArchivedIsRestoredAndUnarchived(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $usage = $this->usageOf(moduleUuid: $seven); + $marker = [ + 'archived' => ['by' => 'admin', 'at' => '2026-10-01T00:00:00+00:00', 'reason' => 'cmdb-import: on sheet'], + 'deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-02T00:00:00+00:00'], + ]; + $this->states[self::MODULE][$seven] = $marker; + $this->states[self::USAGE][$usage] = $marker; + $counts = [count($this->store[self::MODULE]), count($this->store[self::USAGE])]; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '7', sheet: 'Onbeh Applicaties CMDB', cells: ['Applicatie Naam' => 'Terug'])])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['restored'], array_column($report['rows'], 'outcome')); + $this->assertSame( + [['restore', $seven], ['unarchive', $seven], ['restore', $usage], ['unarchive', $usage]], + array_map(static fn (array $t): array => [$t[0], $t[2]], $this->transitions) + ); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([false, false], $this->stateOf(schema: self::USAGE, uuid: $usage)); + $this->assertSame($counts, [count($this->store[self::MODULE]), count($this->store[self::USAGE])], 'no second module or usage'); + $this->assertSame('Terug', $this->store[self::MODULE][$seven]['name']); + $this->assertSame('7', $this->store[self::MODULE][$seven]['externalNumber'], 'the update starts from the restored object, not from the trash listing'); + }//end testAnApplicationDeletedWhileArchivedIsRestoredAndUnarchived() + + /** + * A soft-deleted application the archive sheet lists is restored and archived, and the trash is read once. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testADeletedApplicationOnTheArchiveSheetIsRestoredAndArchived(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $usage = $this->usageOf(moduleUuid: $seven); + $this->states[self::MODULE][$seven] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-02T00:00:00+00:00']]; + $this->states[self::USAGE][$usage] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-02T00:00:00+00:00']]; + $this->trashReads = 0; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '8', row: 3)], archived: ['7'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1']); + + $this->assertSame( + [['restore', $usage], ['archive', $usage], ['restore', $seven], ['archive', $seven]], + array_map(static fn (array $t): array => [$t[0], $t[2]], $this->transitions) + ); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([true, false], $this->stateOf(schema: self::USAGE, uuid: $usage)); + $reconciled = array_values(array_filter($report['rows'], static fn (array $row): bool => $row['row'] === 0)); + $this->assertSame([['7', 'archived']], array_map(static fn (array $row): array => [$row['appId'], $row['outcome']], $reconciled)); + $this->assertSame(1, $this->trashReads, 'the trash is read once per import, not per row'); + }//end testADeletedApplicationOnTheArchiveSheetIsRestoredAndArchived() + + /** + * A trash listing that throws fails its row and is read again by the next row, which restores instead of duplicating. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testAFailedTrashReadIsNotCachedForTheNextRow(): void { + $this->importOneSevenAndEight(); + $deleted = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-02T00:00:00+00:00']]; + foreach (['7', '8'] as $appId) { + $module = $this->moduleOf(appId: $appId); + $this->states[self::MODULE][$module] = $deleted; + $this->states[self::USAGE][$this->usageOf(moduleUuid: $module)] = $deleted; + } + + $counts = [count($this->store[self::MODULE]), count($this->store[self::USAGE])]; + $this->trashReads = 0; + $this->trashFailures = 1; + + $rows = [$this->row(appId: '8', row: 2), $this->row(appId: '7', row: 3, sheet: 'Onbeh Applicaties CMDB')]; + $report = $this->service(reader: $this->rowsReader(rows: $rows))->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['failed', 'restored'], array_column($report['rows'], 'outcome')); + $this->assertSame(2, $this->trashReads, 'the failed read is not cached'); + $this->assertSame($counts, [count($this->store[self::MODULE]), count($this->store[self::USAGE])], 'no second module or usage'); + }//end testAFailedTrashReadIsNotCachedForTheNextRow() + + /** + * With updateExisting false a returning application is skipped as `exists` and stays archived. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testUpdateExistingFalseLeavesAnArchivedMatchArchived(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1'), $this->row(appId: '8', row: 3)], archived: ['7']))->import(path: '', options: ['municipalityUuid' => 'muni-1']); + $modules = $this->store[self::MODULE]; + $this->transitions = []; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '7', sheet: 'Onbeh Applicaties CMDB')])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'updateExisting' => false, 'missingRecords' => 'keep']); + + $this->assertSame(['skipped'], array_column($report['rows'], 'outcome')); + $this->assertSame(['exists'], $report['rows'][0]['reasons']); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $seven)); + $this->assertSame([], $this->transitions); + $this->assertSame($modules, $this->store[self::MODULE]); + }//end testUpdateExistingFalseLeavesAnArchivedMatchArchived() + + /** + * A module and usage deleted by hand next to the working ones with the same import key stay in the trash. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testAWorkingMatchWinsOverADeletedOneWithTheSameKey(): void { + $this->importOneSevenAndEight(); + $seven = $this->moduleOf(appId: '7'); + $usage = $this->usageOf(moduleUuid: $seven); + // Stored ahead of the working ones, so a first-hit match would find them first. + $this->store[self::MODULE] = ['module-deleted' => array_merge($this->store[self::MODULE][$seven], ['id' => 'module-deleted'])] + $this->store[self::MODULE]; + $this->store[self::USAGE] = ['usage-deleted' => array_merge($this->store[self::USAGE][$usage], ['id' => 'usage-deleted'])] + $this->store[self::USAGE]; + $this->states[self::MODULE]['module-deleted'] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + $this->states[self::USAGE]['usage-deleted'] = ['deleted' => ['deletedBy' => 'admin', 'deletedAt' => '2026-10-01T00:00:00+00:00']]; + $counts = [count($this->store[self::MODULE]), count($this->store[self::USAGE])]; + + $report = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '7', sheet: 'Onbeh Applicaties CMDB', cells: ['Applicatie Naam' => 'Terug'])])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'missingRecords' => 'keep']); + + $this->assertSame(['updated'], array_column($report['rows'], 'outcome')); + $this->assertSame($seven, $report['rows'][0]['moduleUuid']); + $this->assertSame('Terug', $this->store[self::MODULE][$seven]['name']); + $this->assertSame([], $this->transitions, 'nothing is restored'); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: 'module-deleted')); + $this->assertSame([false, true], $this->stateOf(schema: self::USAGE, uuid: 'usage-deleted')); + $this->assertSame($counts, [count($this->store[self::MODULE]), count($this->store[self::USAGE])], 'no module or usage is created'); + }//end testAWorkingMatchWinsOverADeletedOneWithTheSameKey() + + /** + * The fixture round trip of the e2e test: the later export archives 1234 and deletes 2, the original brings both back. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-that-returns-to-the-export-shall-be-unarchived-or-restored-never-duplicated-req-cmdb-016 + */ + public function testTheArchivedApplicationsFixtureArchivesDeletesAndBringsBack(): void { + $path = $this->fixture(); + $options = ['municipalityName' => 'Gemeente Voorbeeldstad']; + $this->service()->import(path: $path, options: $options); + $counts = [count($this->store[self::MODULE]), count($this->store[self::USAGE]), count($this->store[self::CONTACT_PERSON])]; + [$archived, $deleted] = [$this->moduleOf(appId: '1234'), $this->moduleOf(appId: '2')]; + + $report = $this->service()->import(path: CmdbTestSupport::fixtures() . '/topdesk-archived-applications.xlsx', options: $options); + + $this->assertSame(['archived', 'deleted'], array_column($report['rows'], 'outcome')); + $this->assertSame(['1234', '2'], array_column($report['rows'], 'appId')); + $this->assertSame([true, false], $this->stateOf(schema: self::MODULE, uuid: $archived)); + $this->assertSame([false, true], $this->stateOf(schema: self::MODULE, uuid: $deleted)); + $this->assertSame([], $report['importWarnings']); + + $back = $this->service()->import(path: $path, options: $options); + + $this->assertSame(['unarchived', 'restored'], array_column($back['rows'], 'outcome')); + $this->assertSame($counts, [count($this->store[self::MODULE]), count($this->store[self::USAGE]), count($this->store[self::CONTACT_PERSON])]); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $archived)); + $this->assertSame([false, false], $this->stateOf(schema: self::MODULE, uuid: $deleted)); + }//end testTheArchivedApplicationsFixtureArchivesDeletesAndBringsBack() + + /** + * The reconciliation reports its own progress phase and advances per application. + * + * @return void + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-an-application-missing-from-the-cmdb-sheets-shall-be-archived-when-the-archive-sheet-lists-it-and-soft-deleted-when-no-sheet-does-req-cmdb-015 + */ + public function testTheReconciliationHasItsOwnProgressPhase(): void { + $this->importOneSevenAndEight(); + $seen = []; + $this->beforeTransition = function () use (&$seen): void { + $progress = $this->tracker->getProgress(operationId: 'cmdb-reconcile-1'); + $seen[] = [$progress['phase'], $progress['processed_items'], $progress['total_items']]; + }; + + $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1')], archived: ['7', '8'])) + ->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-reconcile-1']); + + // One row, then three usages in scope: APPID 1 (on a CMDB sheet), 7 and 8. + $this->assertSame([['reconciling', 2, 4], ['reconciling', 2, 4], ['reconciling', 3, 4], ['reconciling', 3, 4]], $seen); + $this->assertSame('completed', $this->cache['progress_cmdb-reconcile-1']['status']); + $this->assertSame(4, $this->cache['progress_cmdb-reconcile-1']['processed_items']); + }//end testTheReconciliationHasItsOwnProgressPhase() /** * An unknown Applicatie Status drops only that field and warns with column and value. @@ -1806,7 +3192,7 @@ public function testAnImportLeavesAnAuditRecord(): void { options: ['municipalityUuid' => 'muni-1', 'updateExisting' => false, 'publish' => false, 'operationId' => 'cmdb-audit-01', 'fileName' => 'C:\\Users\\beheer\\export.xlsx'] ); - $audit = '"operationId":"cmdb-audit-01","uid":"admin","fileName":"export.xlsx","municipality":"muni-1","municipalityCreated":false,"updateExisting":false,"publish":false'; + $audit = '"operationId":"cmdb-audit-01","uid":"admin","fileName":"export.xlsx","municipality":"muni-1","municipalityCreated":false,"updateExisting":false,"publish":false,"missingRecords":"archive"'; $started = array_values(array_filter($this->logLines, static fn (string $line): bool => str_starts_with($line, 'CmdbExportImportService: import started'))); $finished = array_values(array_filter($this->logLines, static fn (string $line): bool => str_starts_with($line, 'CmdbExportImportService: import finished'))); $this->assertCount(1, $started); @@ -1879,7 +3265,7 @@ public function testOneBadRowDoesNotStopTheOthers(): void { $this->assertSame(['created', 'failed', 'created'], array_column($report['rows'], 'outcome')); $this->assertStringStartsWith('step "module" failed', $report['rows'][1]['reasons'][0]); - $this->assertSame(['rowsRead' => 3, 'processed' => 3, 'created' => 2, 'updated' => 0, 'unchanged' => 0, 'skipped' => 0, 'failed' => 1, 'warnings' => 0, 'unpublished' => 0], $report['summary']); + $this->assertSame(['rowsRead' => 3, 'processed' => 3, 'created' => 2, 'updated' => 0, 'unchanged' => 0, 'skipped' => 0, 'failed' => 1, 'archived' => 0, 'unarchived' => 0, 'deleted' => 0, 'restored' => 0, 'warnings' => 0, 'unpublished' => 0], $report['summary']); $this->assertCount(2, $this->store[self::MODULE]); }//end testOneBadRowDoesNotStopTheOthers() @@ -2151,7 +3537,7 @@ public function testCancelNeedsARunningImport(): void { }//end testCancelNeedsARunningImport() /** - * A cancel that arrives after the last row's check leaves no flag that a later run with the same id would inherit. + * A cancel that arrives after the last row's check, with records kept, leaves no flag that a later run with the same id would inherit. * * @return void */ @@ -2165,7 +3551,7 @@ public function testACancelAfterTheLastCheckLeavesNoFlag(): void { }; $service = $this->service(reader: $this->rowsReader(rows: [$this->row(appId: '1', row: 2)])); - $report = $service->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-late-cancel-1']); + $report = $service->import(path: '', options: ['municipalityUuid' => 'muni-1', 'operationId' => 'cmdb-late-cancel-1', 'missingRecords' => 'keep']); $this->assertFalse($report['cancelled'], 'the only row was already done'); $this->assertSame('completed', $this->cache['progress_cmdb-late-cancel-1']['status']); diff --git a/tests/e2e/spec-coverage/cmdb-import.spec.ts b/tests/e2e/spec-coverage/cmdb-import.spec.ts index 6251f2d8d..b10deda29 100644 --- a/tests/e2e/spec-coverage/cmdb-import.spec.ts +++ b/tests/e2e/spec-coverage/cmdb-import.spec.ts @@ -28,6 +28,15 @@ * The last test checks, without signing in, that the imported owners are * not readable anonymously: neither through OpenRegister's objects API nor * in an OpenCatalogi search hit (skipped when OpenCatalogi is not installed). + * + * openspec/changes/cmdb-import-archive-reconciliation: a later export that + * moved APPID 1234 to "Gearchiveerde Applicaties" and dropped APPID 2 archives + * the one and moves the other to the trash, the Applications page lists 1234 + * under "Archived", and the original export brings both back. The cleanup + * looks through OpenRegister's archive lens too, so an interrupted run + * leaves no archived application of the run's municipality behind; a + * soft-deleted one cannot be listed and stays in the trash until its purge + * date (design.md, D9). */ import type { APIRequestContext, Locator, Page, Response } from '@playwright/test' @@ -45,10 +54,18 @@ import { resolveConfig, RUN_ID, } from '../workflows/_fixtures.ts' +import { dismissSupportDialog, gotoAppRoute } from './_helpers.ts' const FIXTURES_DIR = path.resolve(__dirname, '../../fixtures/cmdb') const EXPORT_FIXTURE = path.join(FIXTURES_DIR, 'topdesk-export-anonymised.xlsx') const MISSING_COLUMN_FIXTURE = path.join(FIXTURES_DIR, 'topdesk-missing-appid.xlsx') +// The later export: APPID 1234 only on "Gearchiveerde Applicaties", APPID 2 on no sheet. +const ARCHIVED_FIXTURE = path.join( + FIXTURES_DIR, + 'topdesk-archived-applications.xlsx', +) +// The application name of APPID 1234 in the anonymised export. +const ARCHIVED_APPLICATION_NAME = 'Aangetekend Mailen' // The owner values the anonymised export holds (tests/fixtures/cmdb/README.md). const OWNER_VALUES = ['Achternaam', 'Voornaam', 'Teamleider Applicatiebeheer'] @@ -122,6 +139,39 @@ async function objectsOfMunicipality( return rows.filter((row) => JSON.stringify(row).includes(uuid)) } +/** + * The objects of a schema that mention a municipality, through OpenRegister's + * archive and trash lenses: `_archived` `true` lists the archive alone, `any` + * the archive and the working set. `_includeDeleted` is sent for completeness: + * the list cannot return a soft-deleted row even then (design.md, D9). + * + * @param ctx The API context + * @param schema The schema id + * @param lens The list lenses + * @param lens.archived The `_archived` value + * @param lens.includeDeleted Whether the trash is included + * @param uuid The municipality, by default the run's chosen one + */ +async function objectsInLens( + ctx: APIRequestContext, + schema: string, + lens: { archived: 'true' | 'any'; includeDeleted: boolean }, + uuid: string = municipalityUuid, +): Promise>> { + const res = await ctx.get( + `/index.php/apps/openregister/api/objects/${config.register}/${schema}?_limit=5000&_archived=${lens.archived}&_includeDeleted=${lens.includeDeleted}`, + ) + expect( + res.ok(), + `Listing ${schema} with _archived=${lens.archived} failed with ${res.status()}`, + ).toBe(true) + const body = await res.json() + const list = body?.results ?? body ?? [] + return (Array.isArray(list) ? list : []).filter((row) => + JSON.stringify(row).includes(uuid), + ) +} + /** * The organisations of type Municipality with exactly this name. * @@ -312,9 +362,12 @@ test.describe.serial('CMDB import section', () => { config.contactpersoon_schema, config.module_schema, ]) { - for (const row of await objectsOfMunicipality( + // Archived objects too: the archive test may have stopped half-way. A + // soft-deleted one is not listable and stays in the trash until it is purged. + for (const row of await objectsInLens( ctx, schema, + { archived: 'any', includeDeleted: true }, uuid, )) { const id = String( @@ -458,6 +511,153 @@ test.describe.serial('CMDB import section', () => { await ctx.dispose() }) + // @e2e cmdb-export-import::an-application-that-moved-to-the-archive-sheet-is-archived + // @e2e cmdb-export-import::the-applications-page-lists-the-archive-on-request + // @e2e cmdb-export-import::an-archived-application-returns-to-the-export + test('a later export archives what moved to the archive sheet, deletes what left, and the original brings both back', async ({ + page, + }) => { + requireFixture(EXPORT_FIXTURE) + requireFixture(ARCHIVED_FIXTURE) + const ctx = await newApiContext() + const before = await countWritten(ctx) + expect( + before.modules, + 'the first import must have written the modules this test archives and deletes', + ).toBeGreaterThan(0) + const archivedModules = async () => + ( + await objectsInLens(ctx, config.module_schema, { + archived: 'true', + includeDeleted: false, + }) + ).map((row) => String(row.externalNumber ?? '')) + + // 1. The later export, with the default choice: archive or delete. + let section = await gotoCmdbSection(page) + await chooseMunicipality(page) + await expect( + section.locator( + '[data-testid="cmdb-import-missing-records-archive"] input', + ), + ).toBeChecked() + let response = await runImport(page, ARCHIVED_FIXTURE) + expect(response.status()).toBe(200) + + await expect(summaryValue(page, 'rowsRead')).toHaveText('0') + await expect(summaryValue(page, 'archived')).toHaveText('1') + await expect(summaryValue(page, 'deleted')).toHaveText('1') + await expect( + reportRows(page).locator('[data-outcome="archived"]'), + ).toHaveCount(1) + await expect( + reportRows(page).locator('[data-outcome="deleted"]'), + ).toHaveCount(1) + + // 1234 is in the archive, 2 in the trash; neither is in the working set. + expect(await archivedModules()).toEqual(['1234']) + const working = await countWritten(ctx) + expect(working.modules).toBe(before.modules - 2) + expect(working.usages).toBe(before.usages - 2) + // Contact persons are never archived or deleted. + expect(working.contactPersons).toBe(before.contactPersons) + + // 2. The Applications page lists 1234 only under the quick filter "Archived". + await gotoAppRoute(page, '/modules') + await dismissSupportDialog(page) + // Not under the default tab. Nextcloud keeps polls open, so no network + // idle wait: wait until the list shows a working application (the + // export brought more than the two this test took out), then look. + expect( + working.modules, + 'the working set must still hold an application the list can show', + ).toBeGreaterThan(0) + await expect( + page.locator('.faceted-catalog-index__list tbody tr').first(), + ).toBeVisible({ + timeout: 30000, + }) + await expect(page.getByText(ARCHIVED_APPLICATION_NAME)).toHaveCount(0) + await page.getByRole('tab', { name: 'Archived', exact: true }).click() + await expect(page.getByText(ARCHIVED_APPLICATION_NAME).first()).toBeVisible({ + timeout: 30000, + }) + + // 3. The original export brings both back: no duplicates. + section = await gotoCmdbSection(page) + await chooseMunicipality(page) + response = await runImport(page, EXPORT_FIXTURE) + expect(response.status()).toBe(200) + + await expect(summaryValue(page, 'unarchived')).toHaveText('1') + await expect(summaryValue(page, 'restored')).toHaveText('1') + await expect( + section.locator('[data-testid="cmdb-import-error"]'), + ).toHaveCount(0) + expect(await archivedModules()).toEqual([]) + expect(await countWritten(ctx)).toEqual(before) + await ctx.dispose() + }) + + // @e2e cmdb-export-import::the-admin-chooses-to-keep-missing-applications + test('choosing "Keep them as they are" leaves the applications a later export no longer lists as they are', async ({ + page, + }) => { + requireFixture(EXPORT_FIXTURE) + requireFixture(ARCHIVED_FIXTURE) + const ctx = await newApiContext() + const before = await countWritten(ctx) + expect( + before.modules, + 'the first import must have written the modules this test keeps', + ).toBeGreaterThan(0) + + // The same later export as the archive test, which archives 1234 and deletes 2. + const section = await gotoCmdbSection(page) + await chooseMunicipality(page) + const keep = section.locator( + '[data-testid="cmdb-import-missing-records-keep"] input', + ) + await keep.check() + await expect(keep).toBeChecked() + await expect( + section.locator( + '[data-testid="cmdb-import-missing-records-archive"] input', + ), + ).not.toBeChecked() + const response = await runImport(page, ARCHIVED_FIXTURE) + expect(response.status()).toBe(200) + // The request carries the choice, not the default. + expect(response.request().postData() ?? '').toMatch( + /name="missingRecords"\s+keep/, + ) + + await expect(summaryValue(page, 'rowsRead')).toHaveText('0') + // The archived and deleted tiles only exist when the import did either. + await expect( + section.locator('[data-testid="cmdb-import-summary-archived"]'), + ).toHaveCount(0) + await expect( + section.locator('[data-testid="cmdb-import-summary-deleted"]'), + ).toHaveCount(0) + await expect( + reportRows(page).locator('[data-outcome="archived"]'), + ).toHaveCount(0) + await expect( + reportRows(page).locator('[data-outcome="deleted"]'), + ).toHaveCount(0) + + // Both applications are still in the working set; nothing is archived. + expect(await countWritten(ctx)).toEqual(before) + expect( + await objectsInLens(ctx, config.module_schema, { + archived: 'true', + includeDeleted: false, + }), + ).toEqual([]) + await ctx.dispose() + }) + // @e2e cmdb-export-import::a-missing-required-column-is-named-in-the-422-response test('an export without a required column names the column and the sheet', async ({ page, diff --git a/tests/vitest/cmdbArchiveQuickFilter.spec.js b/tests/vitest/cmdbArchiveQuickFilter.spec.js new file mode 100644 index 000000000..f93a42ba1 --- /dev/null +++ b/tests/vitest/cmdbArchiveQuickFilter.spec.js @@ -0,0 +1,160 @@ +/** + * The Applications and Applications in use pages list the archive on request. + * + * OpenRegister leaves archived objects out of every list unless the request + * asks for `_archived`; CnIndexPage spreads the active quick filter's map into + * the list request as it is, so a quick filter `{ _archived: 'true' }` lists + * the archive alone. The CMDB import archives applications that moved to the + * sheet "Gearchiveerde Applicaties", and without this filter an admin could + * not find them in stackiq any more. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-archived-applications-and-usages-shall-be-listable-in-stackiq-and-shall-stay-out-of-opencatalogi-and-portaliq-by-default-req-cmdb-017 + * + * SPDX-FileCopyrightText: 2026 Conduction B.V. + * SPDX-License-Identifier: EUPL-1.2 + */ + +import fs from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' +import { describe, expect, it, vi } from 'vitest' +import { effectScope } from 'vue' +// resolveFilterTokens reads the current user at import time, which needs a +// browser; the tokens this spec merges (`_archived`, `_ids`) resolve no user. +vi.mock('@nextcloud/auth', () => ({ getCurrentUser: () => null })) +// useSelfFetchList is the real function; only the list composable it feeds its +// `fixedFilters` getter to is replaced, so the test can read the options it got. +const captured = vi.hoisted(() => ({ options: null })) +vi.mock( + '../../node_modules/@conduction/nextcloud-vue/src/composables/index.js', + () => ({ + useListView: (objectType, options) => { + captured.options = options + return { refresh: () => {}, searchTerm: { value: '' } } + }, + }), +) +vi.mock( + '../../node_modules/@conduction/nextcloud-vue/src/composables/useObjectSubscription.js', + () => ({ + useObjectSubscription: () => ({}), + }), +) +vi.mock('../../node_modules/@conduction/nextcloud-vue/src/store/index.js', () => ({ + useObjectStore: () => ({ registerObjectType: () => {} }), +})) +import { useSelfFetchList } from '../../node_modules/@conduction/nextcloud-vue/src/components/CnIndexPage/useSelfFetchList.js' +// The REAL implementations from the installed package — not a local restatement. +import { buildQueryString } from '../../node_modules/@conduction/nextcloud-vue/src/utils/headers.js' +import { facetNarrowingFilter } from '../../src/utils/facetNarrowing.js' + +const here = path.dirname(fileURLToPath(import.meta.url)) +const repoRoot = path.resolve(here, '../..') + +/** + * Every page of the manifest and its fragments. + * + * @return {Array} The pages + */ +function pages() { + const files = [ + path.join(repoRoot, 'src/manifest.json'), + ...fs + .readdirSync(path.join(repoRoot, 'src/manifest.d')) + .filter((name) => name.endsWith('.json')) + .map((name) => path.join(repoRoot, 'src/manifest.d', name)), + ] + return files.flatMap( + (file) => JSON.parse(fs.readFileSync(file, 'utf8')).pages ?? [], + ) +} + +describe('the archive quick filter', () => { + it('the Gebruik page lists the archive alone under "Archived"', () => { + const page = pages().find((candidate) => candidate.id === 'Gebruik') + const archived = (page?.config?.quickFilters ?? []).filter( + (tab) => tab.label === 'Archived', + ) + + expect(archived).toHaveLength(1) + expect(archived[0].filter).toEqual({ _archived: 'true' }) + expect(archived[0].default).not.toBe(true) + }) + + it('the Modules page lists the archive under "Archived" without the facet id set', () => { + const page = pages().find((candidate) => candidate.id === 'Modules') + const archived = (page?.config?.quickFilters ?? []).filter( + (tab) => tab.label === 'Archived', + ) + + expect(archived).toHaveLength(1) + expect(archived[0].filter).toEqual({ _archived: 'true', _ids: '' }) + expect(archived[0].default).not.toBe(true) + }) + + // The Applications page sends `_ids` (the working set a facet or search + // matched) and merges the active tab's filter over it. OpenRegister leaves + // archived objects out of that id set, so `_ids` AND `_archived=true` lists + // nothing: with a facet selected, the Archived tab must not send `_ids`. + // The merge is the library's own: useSelfFetchList hands useListView a + // `fixedFilters` getter, which is called here exactly as a fetch calls it. + it('sends no _ids with the Archived tab when a facet narrowed the list', () => { + const page = pages().find((candidate) => candidate.id === 'Modules') + const quickFilters = page.config.quickFilters + const archivedIndex = quickFilters.findIndex( + (tab) => tab.label === 'Archived', + ) + const facetFilter = facetNarrowingFilter(['live-1', 'live-2']) + + /** + * The query string the library builds for the list with one tab active. + * + * @param {number|null} tabIndex The active quick-filter tab + * @return {string} The query string + */ + function queryWithTab(tabIndex) { + const scope = effectScope() + let fixedFilters + captured.options = null + scope.run(() => { + const result = useSelfFetchList( + { + register: 'voorzieningen', + schema: 'module', + filter: facetFilter, + quickFilters, + columns: [], + }, + null, + () => null, + ) + result.activeQuickFilterIndex.value = tabIndex + fixedFilters = captured.options.fixedFilters + }) + const query = buildQueryString(fixedFilters()) + scope.stop() + return query + } + + const archivedQuery = queryWithTab(archivedIndex) + const otherQuery = queryWithTab(archivedIndex === 0 ? 1 : 0) + + expect(new URLSearchParams(archivedQuery).get('_archived')).toBe('true') + expect(archivedQuery).not.toContain('_ids') + // A tab without the override keeps the facet narrowing. + expect(otherQuery).toContain('_ids') + }) + + it('the Modules page note names the Archived tab as the exception to the additive merge', () => { + const page = pages().find((candidate) => candidate.id === 'Modules') + + expect(page._note).toContain('additive AND') + expect(page._note).toMatch(/exception is the Archived tab/) + expect(page._note).toMatch(/ignores the GEMMA facets and the search term/) + }) + + it('uses an icon the app registers', () => { + const icons = fs.readFileSync(path.join(repoRoot, 'src/icons.js'), 'utf8') + expect(icons).toMatch(/^\tArchiveOutline,$/m) + }) +}) diff --git a/tests/vitest/facetedCatalogArchiveNote.spec.js b/tests/vitest/facetedCatalogArchiveNote.spec.js new file mode 100644 index 000000000..23060dc1e --- /dev/null +++ b/tests/vitest/facetedCatalogArchiveNote.spec.js @@ -0,0 +1,123 @@ +// @vitest-environment jsdom +/** + * The Archived tab of the Applications page says it ignores the facets and + * the search term. + * + * The facet sidebar and the search box stay on screen on that tab, but its + * filter carries an empty `_ids` that drops the facet id set. Without a note + * an admin who picks a facet sees an unchanged list and no reason. + * + * @spec openspec/changes/cmdb-import-archive-reconciliation/specs/cmdb-export-import/spec.md#requirement-archived-applications-and-usages-shall-be-listable-in-stackiq-and-shall-stay-out-of-opencatalogi-and-portaliq-by-default-req-cmdb-017 + * + * SPDX-FileCopyrightText: 2026 Conduction B.V. + * SPDX-License-Identifier: EUPL-1.2 + */ + +import { mount } from '@vue/test-utils' +import { createPinia, setActivePinia } from 'pinia' +import { describe, expect, it, vi } from 'vitest' +import { defineComponent, h } from 'vue' + +// CnIndexPage stands in as a component that emits what the real one emits +// when a quick-filter tab is chosen. +vi.mock('@conduction/nextcloud-vue', () => ({ + CnFacetSidebar: defineComponent({ + name: 'CnFacetSidebar', + render: () => h('div'), + }), + CnIndexPage: defineComponent({ + name: 'CnIndexPage', + emits: ['quick-filter-change'], + render() { + return h('div', { class: 'index-page-stub' }) + }, + }), +})) +vi.mock('../../src/modals/SaveFacetViewModal.vue', () => ({ + default: defineComponent({ name: 'SaveFacetViewModal', render: () => h('div') }), +})) + +import FacetedCatalogIndexView from '../../src/views/FacetedCatalogIndexView.vue' + +const NOTE = '.faceted-catalog-index__archive-note' +const QUICK_FILTERS = [ + { label: 'All', filter: {}, default: true }, + { label: 'Archived', filter: { _archived: 'true', _ids: '' } }, +] + +/** + * Mount the view with the facet store's network calls silenced. + * + * @return {object} The wrapper + */ +function mountView(quickFilters = QUICK_FILTERS) { + setActivePinia(createPinia()) + return mount(FacetedCatalogIndexView, { + props: { + schema: 'module', + register: 'voorzieningen', + title: 'Applications', + quickFilters, + }, + global: { + mocks: { + $route: { query: {} }, + $router: { push: vi.fn() }, + t: (app, text) => text, + }, + stubs: { + NcTextField: true, + NcButton: true, + NcActions: true, + NcActionButton: true, + NcActionCaption: true, + }, + plugins: [ + (app) => { + app.config.globalProperties.t = (a, text) => text + }, + ], + }, + }) +} + +describe('FacetedCatalogIndexView on the Archived tab', () => { + it('shows the note only while the Archived tab is active', async () => { + const wrapper = mountView() + const index = wrapper.findComponent({ name: 'CnIndexPage' }) + + expect(wrapper.find(NOTE).exists()).toBe(false) + + await index.vm.$emit('quick-filter-change', 1) + expect(wrapper.find(NOTE).exists()).toBe(true) + expect(wrapper.find(NOTE).text()).toContain('ignores the GEMMA filters') + + await index.vm.$emit('quick-filter-change', 0) + expect(wrapper.find(NOTE).exists()).toBe(false) + }) + + it('follows the default tab at mount, before any tab event', () => { + expect(mountView().find(NOTE).exists()).toBe(false) + + const archivedFirst = [ + { label: 'Archived', filter: { _archived: 'true', _ids: '' } }, + { label: 'All', filter: {} }, + ] + // No default marked: CnIndexPage starts on the first tab. + expect(mountView(archivedFirst).find(NOTE).exists()).toBe(true) + }) + + it('drops the note when a facet or search change remounts the list on its default tab', async () => { + const wrapper = mountView() + const index = wrapper.findComponent({ name: 'CnIndexPage' }) + await index.vm.$emit('quick-filter-change', 1) + expect(wrapper.find(NOTE).exists()).toBe(true) + + // A search term changes the key CnIndexPage is mounted under. + wrapper.vm.facetStore.module.search = 'mail' + await wrapper.vm.$nextTick() + await wrapper.vm.$nextTick() + + expect(wrapper.find(NOTE).exists()).toBe(false) + }) +})