97 lines
2 KiB
JavaScript
97 lines
2 KiB
JavaScript
import { getSession } from "next-auth/react";
|
|
import prisma from "./prisma";
|
|
|
|
export const getBorderById = async (id) => {
|
|
return await prisma.borderImage.findFirst({
|
|
where: {
|
|
id: parseInt(id),
|
|
},
|
|
});
|
|
};
|
|
|
|
export const countAllBorders = async () => {
|
|
return await prisma.borderImage.count();
|
|
};
|
|
|
|
export const getAllBorders = async (limit = undefined, cursor = undefined) => {
|
|
const sanitizedLimit = parseInt(limit) || undefined;
|
|
const sanitizedCursor = parseInt(cursor) || 0;
|
|
return await prisma.borderImage.findMany({
|
|
take: sanitizedLimit,
|
|
cursor: {
|
|
id: sanitizedCursor,
|
|
},
|
|
orderBy: {
|
|
id: "asc",
|
|
},
|
|
});
|
|
};
|
|
|
|
export const getUserBorders = async (req) => {
|
|
const session = await getSession({ req });
|
|
if (!session) {
|
|
return undefined;
|
|
}
|
|
|
|
const accountData = await prisma.account.findFirst({
|
|
where: {
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
|
|
const userData = await prisma.applicationUserData.findUnique({
|
|
where: {
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
|
|
if (!!userData) {
|
|
return userData;
|
|
}
|
|
|
|
const result = await prisma.applicationUserData.create({
|
|
data: {
|
|
userId: session.user.id,
|
|
discordId: accountData.providerAccountId,
|
|
},
|
|
});
|
|
return result;
|
|
};
|
|
|
|
export const setUserBorder = async (req, borderId) => {
|
|
const session = await getSession({ req });
|
|
if (!session) {
|
|
return undefined;
|
|
}
|
|
|
|
const accountData = await prisma.account.findFirst({
|
|
where: {
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
|
|
const updateData = await prisma.applicationUserData.upsert({
|
|
create: {
|
|
userId: session.user.id,
|
|
discordId: accountData.providerAccountId,
|
|
borderId: parseInt(borderId),
|
|
},
|
|
update: {
|
|
borderId: parseInt(borderId),
|
|
},
|
|
where: {
|
|
userId: session.user.id,
|
|
},
|
|
});
|
|
return updateData;
|
|
};
|
|
|
|
export const getByDiscordId = async (id) => {
|
|
const userData = await prisma.applicationUserData.findUnique({
|
|
where: {
|
|
discordId: id,
|
|
},
|
|
});
|
|
return userData?.borderId ? `${userData.borderId}` : undefined;
|
|
};
|