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