Use new transactional endpoint for updating dictionaries

This commit is contained in:
Adina Țeudan 2024-09-23 14:12:29 +03:00
parent 79fb9bd785
commit bda96f952e

View File

@ -1,7 +1,7 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { EntitiesService, getConfig, isIqserDevMode, QueryParam, Toaster } from '@iqser/common-ui';
import { EntitiesService, isIqserDevMode, Toaster } from '@iqser/common-ui';
import { List } from '@iqser/common-ui/lib/utils';
import { Dictionary, DictionaryEntryType, DictionaryEntryTypes, IDictionary, IUpdateDictionary, SuperTypes } from '@red/domain';
import { firstValueFrom, forkJoin, Observable } from 'rxjs';
@ -112,23 +112,18 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
}
entriesToAdd.push(entry);
}
const deletedEntries: Array<string> = [];
const entriesToDelete: Array<string> = [];
const entriesSet = new Set(entries);
for (let i = 0; i < initialEntries.length; i++) {
const entry = initialEntries.at(i);
if (entriesSet.has(entry)) {
continue;
}
deletedEntries.push(entry);
entriesToDelete.push(entry);
}
try {
if (deletedEntries.length) {
await this.#deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId);
}
if (entriesToAdd.length) {
await this.#addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId);
}
await this.#updateEntries(entriesToAdd, entriesToDelete, dossierTemplateId, type, dictionaryEntryType, dossierId);
if (showToast) {
this._toaster.success(_('dictionary-overview.success.generic'));
@ -249,30 +244,20 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
return forkJoin(requests);
}
/**
* Add dictionary entries with entry type.
*/
#addEntries(entries: List, dossierTemplateId: string, type: string, dictionaryEntryType: DictionaryEntryType, dossierId: string) {
const queryParams: List<QueryParam> = [
{ key: 'dossierId', value: dossierId },
{ key: 'dictionaryEntryType', value: dictionaryEntryType },
];
const url = `${this._defaultModelPath}/${type}/${dossierTemplateId}`;
return firstValueFrom(this._post(entries, url, queryParams));
}
/**
* Delete dictionary entries with entry type.
*/
#deleteEntries(entries: List, dossierTemplateId: string, type: string, dictionaryEntryType: DictionaryEntryType, dossierId: string) {
#updateEntries(
entriesToAdd: List,
entriesToDelete: List,
dossierTemplateId: string,
type: string,
dictionaryEntryType: DictionaryEntryType,
dossierId: string,
) {
const queryParams = [
{ key: 'dossierId', value: dossierId },
{ key: 'dictionaryEntryType', value: dictionaryEntryType },
];
const url = `${this._defaultModelPath}/delete/${type}/${dossierTemplateId}`;
return firstValueFrom(this._post(entries, url, queryParams));
const url = `${this._defaultModelPath}/update/${type}/${dossierTemplateId}`;
return firstValueFrom(this._post({ entriesToAdd, entriesToDelete }, url, queryParams));
}
#extractDossierLevelTypes(dossierId: string) {