Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ <h4 class="modal-title">{{'subscriptions.modal.title' | translate}}</h4>
</fieldset>
}
</div>
@if ((showDeleteInfo$ | async)) {
<p class="text-muted">{{'subscriptions.modal.delete-info' | translate}}</p>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,31 @@ describe('SubscriptionModalComponent', () => {
expect(component.subscriptionForm.controls).toBeTruthy();
});

it('should delete an existing subscription when all frequencies are unchecked', () => {
subscriptionServiceStub.deleteSubscription = jasmine.createSpy('deleteSubscription').and.returnValue(createSuccessfulRemoteDataObject$({}));

component.subscriptionForm = new UntypedFormGroup({});
for (let t of testTypes) {
const formGroup = new UntypedFormGroup({
subscriptionId: new UntypedFormControl(testSubscriptionId),
frequencies: new UntypedFormGroup({
f: new UntypedFormControl(false),
g: new UntypedFormControl(false),
}),
});
component.subscriptionForm.addControl(t, formGroup);
component.subscriptionForm.get('test1').markAsDirty();
component.subscriptionForm.get('test1').markAsTouched();
}

fixture.detectChanges();
component.submit();

expect(subscriptionServiceStub.deleteSubscription).toHaveBeenCalled();
expect(subscriptionServiceStub.createSubscription).not.toHaveBeenCalled();
expect(subscriptionServiceStub.updateSubscription).not.toHaveBeenCalled();
});

});

describe('when no subscription is given', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ export class SubscriptionModalComponent implements OnInit {
for (const f of this.frequencyDefaultValues) {
anyFrequencySelected = anyFrequencySelected || newValue.content.frequencies[f];
}
this.isValid = anyFrequencySelected;
const hasExistingSubscription = this.subscriptionDefaultTypes.some(
(t) => isNotEmpty(newValue[t]?.subscriptionId),
);
this.isValid = anyFrequencySelected || hasExistingSubscription;
});
}

Expand Down Expand Up @@ -217,7 +220,6 @@ export class SubscriptionModalComponent implements OnInit {
).subscribe({
next: (res: PaginatedList<Subscription>) => {
if (res.pageInfo.totalElements > 0) {
this.showDeleteInfo$.next(true);
for (const subscription of res.page) {
const type = subscription.subscriptionType;
const subscriptionGroup: UntypedFormGroup = this.subscriptionForm.get(type) as UntypedFormGroup;
Expand Down Expand Up @@ -245,6 +247,7 @@ export class SubscriptionModalComponent implements OnInit {
const subscriptionTypes: string[] = Object.keys(this.subscriptionForm.controls);
const subscriptionsToBeCreated = [];
const subscriptionsToBeUpdated = [];
const subscriptionsToBeDeleted = [];

subscriptionTypes.forEach((subscriptionType: string) => {
const subscriptionGroup: UntypedFormGroup = this.subscriptionForm.controls[subscriptionType] as UntypedFormGroup;
Expand All @@ -254,9 +257,10 @@ export class SubscriptionModalComponent implements OnInit {
subscriptionType,
subscriptionGroup.controls.frequencies as UntypedFormGroup,
);

if (isNotEmpty(body.id)) {
if (isNotEmpty(body.id) && isNotEmpty(body.subscriptionParameterList)) {
subscriptionsToBeUpdated.push(body);
} else if (isNotEmpty(body.id) && !isNotEmpty(body.subscriptionParameterList)) {
subscriptionsToBeDeleted.push(body);
} else if (isNotEmpty(body.subscriptionParameterList)) {
subscriptionsToBeCreated.push(body);
}
Expand Down Expand Up @@ -304,6 +308,23 @@ export class SubscriptionModalComponent implements OnInit {
));
}

if (isNotEmpty(subscriptionsToBeDeleted)) {
toBeProcessed.push(from(subscriptionsToBeDeleted).pipe(
mergeMap((subscriptionBody: any) => {
return this.subscriptionService.deleteSubscription(subscriptionBody.id).pipe(
getFirstCompletedRemoteData(),
);
}),
tap((res: any) => {
if (res.hasSucceeded) {
this.notificationsService.success(null, this.translate.instant('subscriptions.modal.delete.success'));
} else {
this.notificationsService.error(null, this.translate.instant('subscriptions.modal.delete.error'));
}
}),
));
}

combineLatest([...toBeProcessed]).subscribe((res) => {
this.updated.emit();
this.activeModal.close('updated');
Expand Down
Loading