RED-3745 - updated the way of how clone name is generated

This commit is contained in:
Valentin Mihai 2022-04-15 17:54:39 +03:00
parent 75e9a7fb51
commit 7260b87f21
2 changed files with 34 additions and 11 deletions

View File

@ -86,7 +86,13 @@ export class AddEditCloneDossierTemplateDialogComponent extends BaseDialogCompon
validFrom: this.hasValidFrom ? this.form.get('validFrom').value : null,
validTo: this.hasValidTo ? this.form.get('validTo').value : null,
} as IDossierTemplate;
await firstValueFrom(this._dossierTemplatesService.createOrUpdate(dossierTemplate));
if (this.data?.clone) {
const dossierTemplateId = this.dossierTemplate?.dossierTemplateId;
const nameOfClonedTemplate = this._getNameOfClonedTemplate(this.dossierTemplate.name);
await firstValueFrom(this._dossierTemplatesService.clone(dossierTemplate, dossierTemplateId, nameOfClonedTemplate));
} else {
await firstValueFrom(this._dossierTemplatesService.createOrUpdate(dossierTemplate));
}
this._dialogRef.close(true);
} catch (error: any) {
const message =
@ -116,24 +122,35 @@ export class AddEditCloneDossierTemplateDialogComponent extends BaseDialogCompon
private _getTemplateName(): string | null {
if (this.dossierTemplate) {
const name = this.dossierTemplate.name.trim();
const templateName = this.dossierTemplate.name.trim();
if (this.data.clone) {
const prefix = 'Clone of ';
if (name.startsWith(prefix)) {
const regExp = /\(\s*\d+\s*\)$/;
if (regExp.test(name)) {
const lastNumberRegExp = /\d+(?=\D*$)/;
return name.replace(lastNumberRegExp, parseInt(name.match(lastNumberRegExp)[0]) + 1 + '');
const nameOfClonedTemplate = this._getNameOfClonedTemplate(templateName);
const allTemplatesNames = this._dossierTemplatesService.all.map(t => t.name);
let clonesCount = 0;
for (const name of allTemplatesNames) {
const splitName = name.split(nameOfClonedTemplate);
const suffixRegExp = new RegExp(/^\(\s*\d+\s*\)$/);
if (splitName[0] === 'Clone of ' && (splitName[1].trim().match(suffixRegExp) || splitName[1] === '')) {
clonesCount++;
}
return `${name} (1)`;
}
return prefix + name;
if (clonesCount >= 1) {
return `Clone of ${nameOfClonedTemplate} ${clonesCount === 1 ? '(1)' : `(${clonesCount})`}`;
}
return `Clone of ${nameOfClonedTemplate}`;
}
return name;
return templateName;
}
return null;
}
private _getNameOfClonedTemplate(templateName: string): string {
const nameOfClonedTemplate = templateName.split('Clone of ').filter(n => n)[0];
return nameOfClonedTemplate.split(/\(\s*\d+\s*\)$/)[0].trim();
}
private _applyValidityIntervalConstraints(value): boolean {
if (applyIntervalConstraints(value, this._previousValidFrom, this._previousValidTo, this.form, 'validFrom', 'validTo')) {
return true;

View File

@ -70,6 +70,12 @@ export class DossierTemplatesService extends EntitiesService<DossierTemplate, ID
return this._post(body).pipe(switchMap(() => this.loadAll()));
}
clone(@RequiredParam() body: any, dossierTemplateId: string, name: string) {
return this._post(body, `${this._defaultModelPath}/${dossierTemplateId}/clone?nameOfClonedDossierTemplate=${name}`).pipe(
switchMap(() => this.loadAll()),
);
}
refreshDossierTemplate(dossierTemplateId: string): Observable<any> {
return forkJoin([
this._fileAttributesService.loadFileAttributesConfig(dossierTemplateId),