Skip to content

Commit

Permalink
Merge pull request #957 from funmusicplace/various-fixes
Browse files Browse the repository at this point in the history
fix: typecheck on sending error email
  • Loading branch information
simonv3 authored Jan 5, 2025
2 parents b240211 + e068d24 commit 764dd95
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 57 deletions.
71 changes: 35 additions & 36 deletions client/src/components/Header/HeaderSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,41 @@ import { debounce } from "lodash";
const HeaderSearch: React.FC = () => {
const { t } = useTranslation("translation", { keyPrefix: "headerSearch" });

const getOptions = React.useCallback(
debounce(async (searchString: string) => {
const artists = await api.getMany<Artist>(`artists`, {
name: searchString,
});
const trackGroups = await api.getMany<TrackGroup>(`trackGroups`, {
title: searchString,
});
const tracks = await api.getMany<Track>(`tracks`, {
title: searchString,
});
return [
...artists.results.map((r) => ({
artistId: r.urlSlug ?? r.id,
id: r.id,
name: r.name,
isArtist: true,
})),
...trackGroups.results.map((t) => ({
id: t.urlSlug ?? t.id,
artistId: t.artist?.urlSlug ?? t.artistId,
trackGroupId: t.urlSlug ?? t.id,
name: t.title,
isTrackGroup: true,
})),
...tracks.results.map((t) => ({
id: t.id,
trackGroupId: t.trackGroup.urlSlug ?? t.trackGroupId,
artistId: t.trackGroup.artist.urlSlug ?? t.trackGroup.artistId,
name: t.title,
isTrack: true,
})),
];
}, 500),
[]
);
const getOptions = React.useCallback(async (searchString: string) => {
console.log("searching", searchString);
const artists = await api.getMany<Artist>(`artists`, {
name: searchString,
});
const trackGroups = await api.getMany<TrackGroup>(`trackGroups`, {
title: searchString,
});
const tracks = await api.getMany<Track>(`tracks`, {
title: searchString,
});
const results = [
...artists.results.map((r) => ({
artistId: r.urlSlug ?? r.id,
id: r.id,
name: r.name,
isArtist: true,
})),
...trackGroups.results.map((t) => ({
id: t.urlSlug ?? t.id,
artistId: t.artist?.urlSlug ?? t.artistId,
trackGroupId: t.urlSlug ?? t.id,
name: t.title,
isTrackGroup: true,
})),
...tracks.results.map((t) => ({
id: t.id,
trackGroupId: t.trackGroup.urlSlug ?? t.trackGroupId,
artistId: t.trackGroup.artist.urlSlug ?? t.trackGroup.artistId,
name: t.title,
isTrack: true,
})),
];
return results;
}, []);

return (
<AutoComplete
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/common/AutoComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Background from "components/common/Background";
import Button from "./Button";
import styled from "@emotion/styled";
import { useLocation } from "react-router-dom";
import { debounce } from "lodash";

const SearchResultsDiv = styled.div`
position: absolute;
Expand Down Expand Up @@ -124,12 +125,11 @@ const AutoComplete: React.FC<{
);

const searchCallback = React.useCallback(
async (searchString: string) => {
debounce(async (searchString: string) => {
if (searchString && searchString.length > 1) {
setShowSuggestions(true);
setIsSearching(true);
const results = await getOptions(searchString);

const searchResultsMatchSearch = searchResults.find(
(result) =>
result.name.toLowerCase().replaceAll(/\-| /g, "") === searchString
Expand All @@ -148,7 +148,7 @@ const AutoComplete: React.FC<{
setSearchResults([]);
setShowSuggestions(false);
}
},
}, 500),
[getOptions, allowNew]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Client" ADD COLUMN "key" TEXT,
ADD COLUMN "secret" TEXT;
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ model Client {
applicationName String
applicationUrl String
allowedCorsOrigins String[]
secret String?
key String?
updatedAt DateTime
createdAt DateTime
deletedAt DateTime?
Expand Down
7 changes: 5 additions & 2 deletions src/jobs/send-mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const transport: Transporter = !!process.env.SENDGRID_API_KEY
)
: ({ jsonTransport: true } as unknown as Transporter);

export const sendErrorEmail = async (error: unknown) => {
export const sendErrorEmail = async (error: Error) => {
sendMail({
data: {
template: "error-email",
message: {
to: "[email protected]",
},
locals: { error: JSON.stringify(error), time: new Date().toDateString() },
locals: {
error: JSON.stringify(error.stack),
time: new Date().toDateString(),
},
},
});
};
Expand Down
4 changes: 3 additions & 1 deletion src/queues/moving-files-to-backblaze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const moveFilesToBackblazeJob = async (job: Job) => {
await uploadWrapper(bucketName, fileName, stream);
} catch (e) {
console.error(e);
sendErrorEmail(e);
if (e instanceof Error) {
sendErrorEmail(e);
}
logger.error(`Error moving file ${bucketName}/${fileName}`);
}
logger.info(`done transfering ${bucketName}/${fileName}`);
Expand Down
6 changes: 6 additions & 0 deletions src/routers/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ router.post(
async (req, res, next) => {
try {
const { email, password } = req.body;
if (!email || !password) {
res.status(401).json({
error: "Incorrect username or password",
});
return next();
}
const foundUser = await prisma.user.findFirst({
where: {
email: email.toLowerCase(),
Expand Down
33 changes: 18 additions & 15 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 Expand Up @@ -357,14 +360,14 @@ export const basicTrackGroupInclude = {
},
cover: {
include: {
trackGroup: false
}
trackGroup: false,
},
},
artist: {
include: {
user: false
}
}
user: false,
},
},
},
};

Expand Down Expand Up @@ -435,7 +438,7 @@ export const findPurchaseBasedOnTokenAndUpdate = async (
if (!purchase) {
throw new AppError({
httpCode: 404,
description: `Trackgroup Purchase doesn't exist for ${trackGroupId}, ${userId}`,
description: `Trackgroup Purchase doesn't exist for trackgroupId: ${trackGroupId}, userId: ${userId}`,
});
}

Expand Down

0 comments on commit 764dd95

Please sign in to comment.