как можно сократить код?
Может есть более простое решение?
router.delete('/:id', [auth, permit('admin')], async (req, res) => {
const artist = await Artist.findById(req.params.id);
if (!artist) {
return res.status(404).send({error: 'Artist not found!'});
}
const album = await Album.find({artist: artist._id});
if (!album) {
return res.status(404).send({error: 'Album not found!'});
}
let allTracks = [];
await Promise.all(album.map(async el => {
const track = await Track.find({album: el._id});
allTracks = [...allTracks, ...track];
}));
try {
allTracks.map(async track => {
await TrackHistory.deleteMany({track: track._id});
});
album.map(async el => {
await Track.deleteMany({album: el._id});
});
await Album.deleteMany({artist: artist._id});
await Artist.deleteOne({_id: artist._id});
res.send('Delete successful!');
} catch (e) {
res.status(500);
}
});