borders/lib/borders.js

97 lines
2.0 KiB
JavaScript
Raw Normal View History

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-24 02:58:54 +00:00
export const countAllBorders = async () => {
return await prisma.borderImage.count();
};
2022-04-20 14:01:43 +00:00
export const getAllBorders = async (limit = undefined, cursor = undefined) => {
2022-04-24 02:58:54 +00:00
const sanitizedLimit = parseInt(limit) || undefined;
const sanitizedCursor = parseInt(cursor) || 0;
2022-04-20 14:01:43 +00:00
return await prisma.borderImage.findMany({
2022-04-24 02:58:54 +00:00
take: sanitizedLimit,
cursor: {
id: sanitizedCursor,
},
orderBy: {
id: "asc",
},
2022-04-20 14:01:43 +00:00
});
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,
2022-04-24 02:58:54 +00:00
borderId: parseInt(borderId),
2022-04-11 16:50:50 +00:00
},
2022-04-24 02:58:54 +00:00
update: {
borderId: parseInt(borderId),
2022-04-11 16:50:50 +00:00
},
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
};