Ensure deletion of speech times if related faction is deleted

This commit is contained in:
Jim Martens 2021-09-23 17:25:43 +02:00
parent 659c97c285
commit f78f4e64f5
2 changed files with 16 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import {AngularFireDatabase, SnapshotAction} from '@angular/fire/compat/database
import {Observable} from 'rxjs';
import {Faction} from './faction';
import {FactionInBodyService} from '../../shared/faction-in-body.service';
import {SpeechTimeService} from '../../shared/speech-time.service';
@Component({
selector: 'app-factions',
@ -13,7 +14,8 @@ export class FactionsComponent implements OnInit {
public factions: Observable<SnapshotAction<Faction>[]> = new Observable<SnapshotAction<Faction>[]>();
constructor(private database: AngularFireDatabase,
private factionInBodyService: FactionInBodyService) {
private factionInBodyService: FactionInBodyService,
private speechTimeService: SpeechTimeService) {
}
ngOnInit(): void {
@ -33,6 +35,7 @@ export class FactionsComponent implements OnInit {
if (key != null) {
factionRef.remove(key);
this.factionInBodyService.deleteFactions(key);
this.speechTimeService.deleteSpeechTimesOfFaction(key);
}
}
}

View File

@ -34,4 +34,16 @@ export class SpeechTimeService {
Promise.all(promises);
})
}
public deleteSpeechTimesOfFaction(factionKey: string): void {
const speechTimesRef = this.database.list<Speech>('speechTimes');
const affectedTimes = speechTimesRef.query.orderByChild('factionKey').equalTo(factionKey);
affectedTimes.once('value', snapshot => {
const promises: Promise<any>[] = [];
snapshot.forEach((child) => {
promises.push(child.ref.remove());
});
Promise.all(promises);
})
}
}