2022-04-10 18:26:47 +00:00
|
|
|
import { getSession } from "next-auth/react";
|
|
|
|
import prisma from "./prisma";
|
|
|
|
|
2022-04-11 17:33:16 +00:00
|
|
|
export const getBorderById = async (id) => {
|
|
|
|
return await prisma.borderImage.findFirst({
|
|
|
|
where: {
|
2022-04-16 22:29:21 +00:00
|
|
|
id: parseInt(id),
|
|
|
|
},
|
2022-04-11 17:33:16 +00:00
|
|
|
});
|
2022-04-16 22:29:21 +00:00
|
|
|
};
|
|
|
|
|
2022-04-20 14:01:43 +00:00
|
|
|
export const getAllBorders = async (limit = undefined, cursor = undefined) => {
|
|
|
|
return await prisma.borderImage.findMany({
|
|
|
|
take: limit,
|
|
|
|
cursor: cursor,
|
|
|
|
});
|
2022-04-16 22:29:21 +00:00
|
|
|
};
|
2022-04-11 17:33:16 +00:00
|
|
|
|
2022-04-10 18:26:47 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-04-11 16:50:50 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
borderId,
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
userId: session.user.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return updateData;
|
|
|
|
};
|
|
|
|
|
2022-04-10 18:26:47 +00:00
|
|
|
export const getByDiscordId = async (id) => {
|
|
|
|
const userData = await prisma.applicationUserData.findUnique({
|
|
|
|
where: {
|
|
|
|
discordId: id,
|
|
|
|
},
|
|
|
|
});
|
2022-04-11 16:50:50 +00:00
|
|
|
return userData?.borderId ? `${userData.borderId}` : undefined;
|
2022-04-10 18:26:47 +00:00
|
|
|
};
|