Skip to content

Commit 5c3f51b

Browse files
authored
Merge pull request DSpace#2647 from mspalti/access-conditions
File edit component updated to work for forms without access conditions.
2 parents eb433c8 + 6fff475 commit 5c3f51b

2 files changed

Lines changed: 68 additions & 41 deletions

File tree

src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ describe('SubmissionSectionUploadFileEditComponent test suite', () => {
8080
const fileData: any = mockUploadFiles[0];
8181
const pathCombiner = new JsonPatchOperationPathCombiner('sections', sectionId, 'files', fileIndex);
8282

83+
let noAccessConditionsMock = Object.assign({}, mockFileFormData);
84+
delete noAccessConditionsMock.accessConditions;
85+
8386
beforeEach(waitForAsync(() => {
8487
TestBed.configureTestingModule({
8588
imports: [
@@ -299,6 +302,28 @@ describe('SubmissionSectionUploadFileEditComponent test suite', () => {
299302

300303
}));
301304

305+
it('should update Bitstream data properly when access options are omitted', fakeAsync(() => {
306+
compAsAny.formRef = {formGroup: null};
307+
compAsAny.fileData = fileData;
308+
compAsAny.pathCombiner = pathCombiner;
309+
formService.validateAllFormFields.and.callFake(() => null);
310+
formService.isValid.and.returnValue(of(true));
311+
formService.getFormData.and.returnValue(of(noAccessConditionsMock));
312+
const response = [
313+
Object.assign(mockSubmissionObject, {
314+
sections: {
315+
upload: {
316+
files: mockUploadFiles
317+
}
318+
}
319+
})
320+
];
321+
operationsService.jsonPatchByResourceID.and.returnValue(of(response));
322+
comp.saveBitstreamData();
323+
tick();
324+
expect(uploadService.updateFileData).toHaveBeenCalled();
325+
}));
326+
302327
it('should not save Bitstream File data properly when form is not valid', fakeAsync(() => {
303328
compAsAny.formRef = {formGroup: null};
304329
compAsAny.pathCombiner = pathCombiner;

src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -416,56 +416,58 @@ export class SubmissionSectionUploadFileEditComponent
416416
this.operationsBuilder.remove(this.pathCombiner.getPath(path));
417417
});
418418
const accessConditionsToSave = [];
419-
formData.accessConditions
420-
.map((accessConditions) => accessConditions.accessConditionGroup)
421-
.filter((accessCondition) => isNotEmpty(accessCondition))
422-
.forEach((accessCondition) => {
423-
let accessConditionOpt;
424-
425-
this.availableAccessConditionOptions
426-
.filter((element) => isNotNull(accessCondition.name) && element.name === accessCondition.name[0].value)
427-
.forEach((element) => accessConditionOpt = element);
428-
429-
if (accessConditionOpt) {
430-
const currentAccessCondition = Object.assign({}, accessCondition);
431-
currentAccessCondition.name = this.retrieveValueFromField(accessCondition.name);
432-
433-
/* When start and end date fields are deactivated, their values may be still present in formData,
434-
therefore it is necessary to delete them if they're not allowed by the current access condition option. */
435-
if (!accessConditionOpt.hasStartDate) {
436-
delete currentAccessCondition.startDate;
437-
} else if (accessCondition.startDate) {
438-
const startDate = this.retrieveValueFromField(accessCondition.startDate);
439-
// Clamp the start date to the maximum, if any, since the
440-
// datepicker sometimes exceeds it.
441-
let startDateDate = new Date(startDate);
442-
if (accessConditionOpt.maxStartDate) {
419+
if (formData.hasOwnProperty('accessConditions')) {
420+
formData.accessConditions
421+
.filter((accessConditions) => isNotNull(accessConditions))
422+
.map((accessConditions) => accessConditions.accessConditionGroup)
423+
.filter((accessCondition) => isNotEmpty(accessCondition))
424+
.forEach((accessCondition) => {
425+
let accessConditionOpt;
426+
427+
this.availableAccessConditionOptions
428+
.filter((element) => isNotNull(accessCondition.name) && element.name === accessCondition.name[0].value)
429+
.forEach((element) => accessConditionOpt = element);
430+
431+
if (accessConditionOpt) {
432+
const currentAccessCondition = Object.assign({}, accessCondition);
433+
currentAccessCondition.name = this.retrieveValueFromField(accessCondition.name);
434+
435+
/* When start and end date fields are deactivated, their values may be still present in formData,
436+
therefore it is necessary to delete them if they're not allowed by the current access condition option. */
437+
if (!accessConditionOpt.hasStartDate) {
438+
delete currentAccessCondition.startDate;
439+
} else if (accessCondition.startDate) {
440+
const startDate = this.retrieveValueFromField(accessCondition.startDate);
441+
// Clamp the start date to the maximum, if any, since the
442+
// datepicker sometimes exceeds it.
443+
let startDateDate = new Date(startDate);
444+
if (accessConditionOpt.maxStartDate) {
443445
const maxStartDateDate = new Date(accessConditionOpt.maxStartDate);
444446
if (startDateDate > maxStartDateDate) {
445-
startDateDate = maxStartDateDate;
447+
startDateDate = maxStartDateDate;
446448
}
449+
}
450+
currentAccessCondition.startDate = dateToISOFormat(startDateDate);
447451
}
448-
currentAccessCondition.startDate = dateToISOFormat(startDateDate);
449-
}
450-
if (!accessConditionOpt.hasEndDate) {
451-
delete currentAccessCondition.endDate;
452-
} else if (accessCondition.endDate) {
453-
const endDate = this.retrieveValueFromField(accessCondition.endDate);
454-
// Clamp the end date to the maximum, if any, since the
455-
// datepicker sometimes exceeds it.
456-
let endDateDate = new Date(endDate);
457-
if (accessConditionOpt.maxEndDate) {
452+
if (!accessConditionOpt.hasEndDate) {
453+
delete currentAccessCondition.endDate;
454+
} else if (accessCondition.endDate) {
455+
const endDate = this.retrieveValueFromField(accessCondition.endDate);
456+
// Clamp the end date to the maximum, if any, since the
457+
// datepicker sometimes exceeds it.
458+
let endDateDate = new Date(endDate);
459+
if (accessConditionOpt.maxEndDate) {
458460
const maxEndDateDate = new Date(accessConditionOpt.maxEndDate);
459461
if (endDateDate > maxEndDateDate) {
460-
endDateDate = maxEndDateDate;
462+
endDateDate = maxEndDateDate;
461463
}
464+
}
465+
currentAccessCondition.endDate = dateToISOFormat(endDateDate);
462466
}
463-
currentAccessCondition.endDate = dateToISOFormat(endDateDate);
467+
accessConditionsToSave.push(currentAccessCondition);
464468
}
465-
accessConditionsToSave.push(currentAccessCondition);
466-
}
467-
});
468-
469+
});
470+
}
469471
if (isNotEmpty(accessConditionsToSave)) {
470472
this.operationsBuilder.add(this.pathCombiner.getPath('accessConditions'), accessConditionsToSave, true);
471473
}

0 commit comments

Comments
 (0)