Skip to content

Commit

Permalink
fix: clearer code for finding trackgroup by urlSlug
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Jan 5, 2025
1 parent 5e2f932 commit e068d24
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/utils/trackGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ export const deleteTrackGroup = async (
};

export const findTrackGroupIdForSlug = async (
id: string,
trackGroupIdOrSlug: string,
artistId?: string
) => {
let foundId: number | undefined = Number(id);
let foundtrackGroupId: number | undefined = Number(trackGroupIdOrSlug);

if (Number.isNaN(foundId) || (Number.isFinite(+foundId) && artistId)) {
if (
Number.isNaN(foundtrackGroupId) ||
(Number.isFinite(+foundtrackGroupId) && artistId)
) {
if (!artistId) {
throw new Error(
"Searching for a TrackGroup by slug requires an artistId"
Expand All @@ -110,22 +113,22 @@ export const findTrackGroupIdForSlug = async (
if (parsedArtistId) {
const trackGroup = await prisma.trackGroup.findFirst({
where: {
urlSlug: { equals: id, mode: "insensitive" },
urlSlug: { equals: trackGroupIdOrSlug, mode: "insensitive" },
artistId: parsedArtistId,
},
});
foundId = trackGroup ? trackGroup.id : undefined;
foundtrackGroupId = trackGroup ? trackGroup.id : undefined;
} else {
logger.error(
`findTrackGroupIdForSlug: returning undefined for id: ${id} artistId: ${artistId}`
logger.info(
`findTrackGroupIdForSlug: returning undefined for trackGroupId: ${trackGroupIdOrSlug}, artistId: ${artistId}`
);
return undefined;
}
} else {
foundId = Number(id);
foundtrackGroupId = Number(trackGroupIdOrSlug);
}

return foundId;
return foundtrackGroupId;
};

export const trackGroupSingleInclude = (options: {
Expand Down

0 comments on commit e068d24

Please sign in to comment.