RED-6786: faster dictionary saving
This commit is contained in:
parent
fd637deb12
commit
eabad1b9bc
@ -103,50 +103,58 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
|
|||||||
dossierId: string,
|
dossierId: string,
|
||||||
showToast = true,
|
showToast = true,
|
||||||
dictionaryEntryType = DictionaryEntryTypes.ENTRY,
|
dictionaryEntryType = DictionaryEntryTypes.ENTRY,
|
||||||
): Promise<unknown> {
|
) {
|
||||||
const entriesToAdd: Array<string> = [];
|
const entriesToAdd: Array<string> = [];
|
||||||
const deletedEntries: Array<string> = [];
|
const initialEntriesSet = new Set(initialEntries);
|
||||||
|
let hasInvalidRows = false;
|
||||||
|
let hasNewEntries = false;
|
||||||
for (let i = 0; i < entries.length; i++) {
|
for (let i = 0; i < entries.length; i++) {
|
||||||
if (!entries.at(i).trim()) {
|
const entry = entries.at(i);
|
||||||
|
if (!entry.trim()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
entriesToAdd.push(entries.at(i));
|
if (entry.length < MIN_WORD_LENGTH) {
|
||||||
}
|
hasInvalidRows = true;
|
||||||
for (let i = 0; i < initialEntries.length; i++) {
|
|
||||||
if (entries.includes(initialEntries.at(i))) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
deletedEntries.push(initialEntries.at(i));
|
if (!hasNewEntries && !initialEntriesSet.has(entry)) {
|
||||||
}
|
hasNewEntries = true;
|
||||||
// remove empty lines
|
|
||||||
const invalidRowsExist = entriesToAdd.filter(e => e.length < MIN_WORD_LENGTH);
|
|
||||||
if (invalidRowsExist.length === 0) {
|
|
||||||
// can add at least 1 - block UI
|
|
||||||
const promises: Promise<IDictionary>[] = [];
|
|
||||||
if (deletedEntries.length) {
|
|
||||||
promises.push(firstValueFrom(this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId)));
|
|
||||||
}
|
}
|
||||||
if (entriesToAdd.filter(e => !initialEntries.includes(e)).length) {
|
|
||||||
promises.push(firstValueFrom(this._addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId)));
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await Promise.all(promises);
|
|
||||||
if (showToast) {
|
|
||||||
this._toaster.success(_('dictionary-overview.success.generic'));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} catch (error) {
|
|
||||||
if ((error as HttpErrorResponse).status === 400) {
|
|
||||||
this._toaster.error(_('dictionary-overview.error.400'));
|
|
||||||
} else {
|
|
||||||
this._toaster.error(_('dictionary-overview.error.generic'));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this._toaster.error(_('dictionary-overview.error.entries-too-short'));
|
|
||||||
|
|
||||||
throw new Error('Entries too short');
|
entriesToAdd.push(entry);
|
||||||
|
}
|
||||||
|
if (hasInvalidRows) {
|
||||||
|
this._toaster.error(_('dictionary-overview.error.entries-too-short'));
|
||||||
|
|
||||||
|
throw new Error('Entries too short');
|
||||||
|
}
|
||||||
|
const deletedEntries: 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (deletedEntries.length) {
|
||||||
|
await this._deleteEntries(deletedEntries, dossierTemplateId, type, dictionaryEntryType, dossierId);
|
||||||
|
}
|
||||||
|
if (hasNewEntries) {
|
||||||
|
await this._addEntries(entriesToAdd, dossierTemplateId, type, dictionaryEntryType, dossierId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showToast) {
|
||||||
|
this._toaster.success(_('dictionary-overview.success.generic'));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if ((error as HttpErrorResponse).status === 400) {
|
||||||
|
this._toaster.error(_('dictionary-overview.error.400'));
|
||||||
|
} else {
|
||||||
|
this._toaster.error(_('dictionary-overview.error.generic'));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hasManualType(dossierTemplateId: string): boolean {
|
hasManualType(dossierTemplateId: string): boolean {
|
||||||
@ -263,7 +271,7 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
|
|||||||
{ key: 'removeCurrent', value: true },
|
{ key: 'removeCurrent', value: true },
|
||||||
];
|
];
|
||||||
const url = `${this._defaultModelPath}/${type}/${dossierTemplateId}`;
|
const url = `${this._defaultModelPath}/${type}/${dossierTemplateId}`;
|
||||||
return this._post(entries, url, queryParams);
|
return firstValueFrom(this._post(entries, url, queryParams));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -281,6 +289,6 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
|
|||||||
? [{ 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}/delete/${type}/${dossierTemplateId}`;
|
||||||
return this._post(entries, url, queryParams);
|
return firstValueFrom(this._post(entries, url, queryParams));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user