Fixes broken poppable ref adds Schema page Fixes schema creation not including game system id
131 lines
2.7 KiB
TypeScript
131 lines
2.7 KiB
TypeScript
"use server";
|
|
import { auth } from "@/auth";
|
|
import { isEmailVerified } from "@/util/isEmailVerified";
|
|
import { redirect } from "next/navigation";
|
|
import { prisma } from "@/prisma/prismaClient";
|
|
import { Schema } from "@/types";
|
|
|
|
export const saveSchemaDb = async (s: Schema, version: number) => {
|
|
const sesh = await auth();
|
|
if (!sesh?.user?.id) return;
|
|
|
|
const { id, SchemaRevision } = await prisma.schema.upsert({
|
|
// data: {
|
|
// ...s,
|
|
// },
|
|
create: {
|
|
name: s.name,
|
|
SchemaRevision: {
|
|
create: {
|
|
fields: s.fields,
|
|
types: s.types,
|
|
},
|
|
},
|
|
authorId: sesh.user.id,
|
|
gameSystemId: s.gameSystemId,
|
|
id: undefined,
|
|
},
|
|
update: {
|
|
name: s.name,
|
|
},
|
|
where: {
|
|
id: s.id,
|
|
},
|
|
include: {
|
|
// id: true,
|
|
SchemaRevision: {
|
|
where: {
|
|
version: s.version,
|
|
},
|
|
select: {
|
|
version: true,
|
|
isFinal: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
// const schema2 = await prisma.schema.findUnique({where:{id}})
|
|
if (
|
|
!SchemaRevision.at(0) ||
|
|
SchemaRevision[0].version < version ||
|
|
SchemaRevision[0].isFinal
|
|
) {
|
|
await prisma.schemaRevision.create({
|
|
data: {
|
|
schemaId: id,
|
|
types: s.types,
|
|
fields: s.fields,
|
|
version,
|
|
},
|
|
});
|
|
}
|
|
|
|
redirect(`/game-systems/${s.gameSystemId}/schema/${id}`);
|
|
};
|
|
|
|
export const findSchema = async (
|
|
id: string,
|
|
version: number,
|
|
): Promise<Schema | null> => {
|
|
const schema = await prisma.schema.findFirst({
|
|
where: {
|
|
id,
|
|
},
|
|
// include: {
|
|
// gameSystem: {
|
|
// select: {
|
|
// id: true,
|
|
// name: true,
|
|
// },
|
|
// },
|
|
// },
|
|
include: {
|
|
SchemaRevision: {
|
|
where: {
|
|
version,
|
|
},
|
|
select: {
|
|
version: true,
|
|
fields: true,
|
|
types: true,
|
|
},
|
|
},
|
|
},
|
|
// select: {
|
|
// id: true,
|
|
// name: true,
|
|
// },
|
|
});
|
|
|
|
if (!schema?.SchemaRevision[0]) return null;
|
|
|
|
return {
|
|
fields: schema.SchemaRevision[0].fields,
|
|
types: schema.SchemaRevision[0].types,
|
|
id: schema.id,
|
|
gameSystemId: schema.gameSystemId,
|
|
name: schema.name,
|
|
} as Schema;
|
|
};
|
|
|
|
export const createSchema = async (form: FormData) => {
|
|
const name = form.get("name")?.toString();
|
|
const gsId = form.get("gsId")?.toString();
|
|
|
|
const session = await auth();
|
|
|
|
if (!name || !gsId || !session?.user?.id || !isEmailVerified(session.user.id))
|
|
return;
|
|
|
|
const { id } = await prisma.schema.create({
|
|
data: {
|
|
name,
|
|
gameSystemId: gsId,
|
|
authorId: session.user.id,
|
|
},
|
|
select: { id: true },
|
|
});
|
|
redirect(`/game-systems/${gsId}/schema/${id}`);
|
|
};
|