RED-6786: faster dictionary saving

This commit is contained in:
Dan Percic 2023-05-26 01:22:33 +03:00
parent fd637deb12
commit eabad1b9bc

View File

@ -103,50 +103,58 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
dossierId: string,
showToast = true,
dictionaryEntryType = DictionaryEntryTypes.ENTRY,
): Promise<unknown> {
) {
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++) {
if (!entries.at(i).trim()) {
const entry = entries.at(i);
if (!entry.trim()) {
continue;
}
entriesToAdd.push(entries.at(i));
}
for (let i = 0; i < initialEntries.length; i++) {
if (entries.includes(initialEntries.at(i))) {
continue;
if (entry.length < MIN_WORD_LENGTH) {
hasInvalidRows = true;
}
deletedEntries.push(initialEntries.at(i));
}
// 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 (!hasNewEntries && !initialEntriesSet.has(entry)) {
hasNewEntries = true;
}
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 {
@ -263,7 +271,7 @@ export class DictionaryService extends EntitiesService<IDictionary, Dictionary>
{ key: 'removeCurrent', value: true },
];
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: 'dictionaryEntryType', value: dictionaryEntryType }];
const url = `${this._defaultModelPath}/delete/${type}/${dossierTemplateId}`;
return this._post(entries, url, queryParams);
return firstValueFrom(this._post(entries, url, queryParams));
}
}