fixed link sharing page not showing other users scores when logged in

This commit is contained in:
Polaris 2024-08-26 13:08:43 -04:00
parent 7841fe8ff9
commit 90ae8fc48c
3 changed files with 44 additions and 17 deletions

View File

@ -1,6 +1,4 @@
"use server"; "use server";
import { getAuth } from "@/auth/queries/getauth";
import { artemis, daphnis } from "@/lib/prisma"; import { artemis, daphnis } from "@/lib/prisma";
import type * as Prisma from "@prisma/client"; import type * as Prisma from "@prisma/client";
@ -11,14 +9,40 @@ type LinkSharingToken = {
playlogId: number; playlogId: number;
}; };
export async function getSharedSong() { export async function getSharedSong(token: string) {
const { user } = await getAuth();
try { try {
const linkSharingToken = await daphnis.linkSharingToken.findFirst({
where: {
token: token,
},
select: {
playlogId: true,
userId: true,
},
});
const user = await daphnis.user.findUnique({
where: {
id: linkSharingToken!.userId,
},
select: {
id: true,
UserId: true,
},
});
if (!user) {
throw new Error("User not found");
}
if (!linkSharingToken) {
throw new Error("Invalid or expired token");
}
const songs: ChuniScorePlaylog[] = const songs: ChuniScorePlaylog[] =
await artemis.chuni_score_playlog.findMany({ await artemis.chuni_score_playlog.findMany({
where: { where: {
user: user?.UserId, user: user.UserId,
}, },
orderBy: { orderBy: {
userPlayDate: "desc", userPlayDate: "desc",

View File

@ -18,7 +18,6 @@ export default async function Share({
}) { }) {
const { token, id } = params; const { token, id } = params;
// Verify the token
const tokenResult = await shareScore(token); const tokenResult = await shareScore(token);
if (tokenResult.error) { if (tokenResult.error) {
@ -27,10 +26,13 @@ export default async function Share({
const playlogId = parseInt(id); const playlogId = parseInt(id);
const songsData = await getSharedSong(); const songsData = await getSharedSong(token);
const playlogIds = await getPlaylogId(playlogId); const playlogIds = await getPlaylogId(playlogId);
// Filter songsData to match the playlogIds if (!songsData) {
return <p>Error: Something went wrong</p>;
}
const chunithm: chunithm[] = songsData.filter((song) => const chunithm: chunithm[] = songsData.filter((song) =>
playlogIds.includes(song.id), playlogIds.includes(song.id),
); );
@ -42,7 +44,7 @@ export default async function Share({
{chunithm.map((song) => ( {chunithm.map((song) => (
<div key={song.id} className="w-full"> <div key={song.id} className="w-full">
<span className="text-center text-xl font-bold text-primary text-white"> <span className="text-center text-xl font-bold text-primary text-white">
Song: {song.title} {song.title}
</span> </span>
</div> </div>
))} ))}
@ -52,7 +54,7 @@ export default async function Share({
{chunithm.map((song) => ( {chunithm.map((song) => (
<div key={song.id} className="w-full"> <div key={song.id} className="w-full">
<span className="text-xl font-bold text-primary text-white"> <span className="text-xl font-bold text-primary text-white">
Artist: {song.artist} {song.artist}
</span> </span>
</div> </div>
))} ))}

View File

@ -21,15 +21,16 @@ const ActionsCell: React.FC<ActionsCellProps> = ({ row }) => {
const router = useRouter(); const router = useRouter();
const handleGenerateShareToken = async () => { const handleGenerateShareToken = async () => {
const { token, error } = await generateShareToken(row.original.id); const response = await generateShareToken(row.original.id);
if (error) { if (response.error) {
setError(error); setError(response.error);
} else { } else if (response.token) {
const newTab = window.open(`/${token}/${row.original.id}`); const shareUrl = `/${response.token}/${row.original.id}`;
const newTab = window.open(shareUrl, "_blank");
if (newTab) { if (newTab) {
newTab.focus(); newTab.focus();
} else { } else {
router.push(`/${token}/${row.original.id}`); router.push(shareUrl);
} }
} }
}; };