daphnis/lib/api.ts

156 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-06-29 05:22:22 +00:00
"use server";
import { getAuth } from "@/auth/queries/getauth";
import { artemis } from "@/lib/prisma";
import { daphnis } from "@/lib/prisma";
export async function getSongsWithTitles(userId: number) {
try {
// Fetch songs from ChuniScorePlaylog
const songs = await artemis.chuni_score_playlog.findMany({
where: {
user: userId,
},
orderBy: {
userPlayDate: "desc",
},
select: {
id: true,
maxCombo: true,
userPlayDate: true,
isFullCombo: true,
playerRating: true,
isAllJustice: true,
score: true,
judgeHeaven: true,
judgeGuilty: true,
judgeJustice: true,
judgeAttack: true,
judgeCritical: true,
isClear: true,
skillId: true,
skillEffect: true,
skillLevel: true,
isNewRecord: true,
musicId: true,
level: true,
rateAir: true,
rateHold: true,
rateFlick: true,
rateSlide: true,
rateTap: true,
romVersion: true,
eventId: true,
characterId: true,
charaIllustId: true,
track: true,
isContinue: true,
isFreeToPlay: true,
playKind: true,
playDate: true,
orderId: true,
sortNumber: true,
user: true,
placeId: true,
ticketId: true,
},
});
const musicIds = songs
.map((song) => song.musicId)
.filter((id) => id !== null) as number[];
const staticMusicInfo = await artemis.chuni_static_music.findMany({
where: {
songId: {
in: musicIds,
},
},
select: {
songId: true,
title: true,
artist: true,
},
});
// Map titles to songs based on musicId
const songsWithTitles = songs.map((song) => ({
...song,
title:
staticMusicInfo.find((title) => title.songId === song.musicId)?.title ||
"Unknown Title",
artist:
staticMusicInfo.find((artist) => artist.songId === song.musicId)
?.artist || "Unknown Artist",
}));
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}
export async function SharingPlaylogId(playlogid: number) {
try {
const tokens = await daphnis.linkSharingToken.findMany({
where: {
playlogId: playlogid,
},
select: {
playlogId: true,
},
});
// Extracting playlogId values from the fetched tokens
const playlogIds = tokens.map((token) => token.playlogId);
return playlogIds; // Returning an array of playlogIds
} catch (error) {
console.error("Error fetching playlogIds:", error);
throw error;
}
}
export const getUsername = async () => {
const { user } = await getAuth(); // Assuming getAuth() returns an auth object with user info
if (user) {
return await daphnis.user.findFirst({
where: {
id: user.id,
username: user.username,
},
});
}
return null; // Return null if no user is found
};
export async function getAllAimeCards() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const aimeUser = await daphnis.user.findMany({
where: {
accessCode: user.accessCode,
},
});
return aimeUser;
}
export async function verifyAimeCodeAgainstArtemis() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const aimeUser = await artemis.aime_card.findFirst({
where: {
access_code: user.accessCode,
},
});
return aimeUser;
}