full switch to jotai, finishes schema version query fixes
This commit is contained in:
@@ -3,33 +3,70 @@ 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) => {
|
||||
export const saveSchemaDb = async (s: Schema, version: number) => {
|
||||
const sesh = await auth();
|
||||
if (!sesh?.user?.id) return;
|
||||
|
||||
const { id } = await prisma.schema.upsert({
|
||||
const { id, SchemaRevision } = await prisma.schema.upsert({
|
||||
// data: {
|
||||
// ...s,
|
||||
// },
|
||||
create: {
|
||||
...s,
|
||||
version: 0,
|
||||
name: s.name,
|
||||
SchemaRevision: {
|
||||
create: {
|
||||
fields: s.fields,
|
||||
types: s.types,
|
||||
},
|
||||
},
|
||||
authorId: sesh.user.id,
|
||||
id: undefined,
|
||||
},
|
||||
update: s,
|
||||
update: {
|
||||
name: s.name,
|
||||
},
|
||||
where: {
|
||||
id: s.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
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): Promise<Schema | null> => {
|
||||
export const findSchema = async (
|
||||
id: string,
|
||||
version: number,
|
||||
): Promise<Schema | null> => {
|
||||
const schema = await prisma.schema.findFirst({
|
||||
where: {
|
||||
id,
|
||||
@@ -42,17 +79,33 @@ export const findSchema = async (id: string): Promise<Schema | null> => {
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
schema: true,
|
||||
types: true,
|
||||
include: {
|
||||
SchemaRevision: {
|
||||
where: {
|
||||
version,
|
||||
},
|
||||
select: {
|
||||
version: true,
|
||||
fields: true,
|
||||
types: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
// select: {
|
||||
// id: true,
|
||||
// name: true,
|
||||
// },
|
||||
});
|
||||
|
||||
if (!schema) return null;
|
||||
if (!schema?.SchemaRevision[0]) return null;
|
||||
|
||||
return schema as Schema;
|
||||
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) => {
|
||||
@@ -67,9 +120,6 @@ export const createSchema = async (form: FormData) => {
|
||||
const { id } = await prisma.schema.create({
|
||||
data: {
|
||||
name,
|
||||
schema: "{}",
|
||||
types: "{}",
|
||||
version: 0,
|
||||
gameSystemId: gsId,
|
||||
authorId: session.user.id,
|
||||
},
|
||||
|
Reference in New Issue
Block a user