From add5b647de288d741946916a1718d90078821dae Mon Sep 17 00:00:00 2001 From: bombies Date: Sat, 11 Nov 2023 10:28:40 -0500 Subject: [PATCH] Update DreamService to only deal with non-draft dreams --- src/app/api/me/dreams/dreams.dto.ts | 2 + src/app/api/me/dreams/dreams.service.ts | 73 ++++++++++++++++++------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/app/api/me/dreams/dreams.dto.ts b/src/app/api/me/dreams/dreams.dto.ts index d66f484..beb6fb7 100644 --- a/src/app/api/me/dreams/dreams.dto.ts +++ b/src/app/api/me/dreams/dreams.dto.ts @@ -9,6 +9,7 @@ export const FetchDreamsSchema = zfd.formData({ }) export type PostDreamDto = { + id: string, title: string, description: string, comments?: string | null, @@ -28,6 +29,7 @@ export const DREAM_DESC_MAX = 5000 export const DREAM_COMMENTS_MAX = 1000 export const PostDreamSchema = z.object({ + id: z.string(), title: z.string() .min(DREAM_TITLE_MIN, `The title can't be less than ${DREAM_TITLE_MIN} character!`) .max(DREAM_TITLE_MAX, `The title can't be more than ${DREAM_TITLE_MAX} characters!`), diff --git a/src/app/api/me/dreams/dreams.service.ts b/src/app/api/me/dreams/dreams.service.ts index 1769270..0b79ccf 100644 --- a/src/app/api/me/dreams/dreams.service.ts +++ b/src/app/api/me/dreams/dreams.service.ts @@ -28,25 +28,39 @@ class DreamsService { const member = session.user const dreams = await prisma.dream.findMany({ where: { - userId: member.id, - title: searchedTitle && { - contains: searchedTitle, - mode: "insensitive" - }, - tags: tags && { - some: { - id: { - in: tags + AND: [ + {userId: member.id}, + { + title: searchedTitle && { + contains: searchedTitle, + mode: "insensitive" } - } - }, - characters: characters && { - some: { - id: { - in: characters + }, + { + tags: tags && { + some: { + id: { + in: tags + } + } + } + }, + { + characters: characters && { + some: { + id: { + in: characters + } + } } + }, + { + OR: [ + {isDraft: null}, + {isDraft: false} + ] } - } + ], }, include: (tags || characters) && { tags: tags !== undefined, @@ -83,7 +97,11 @@ class DreamsService { return prisma.dream.findFirst({ where: { id: dreamId, - userId: member.id + userId: member.id, + OR: [ + {isDraft: null}, + {isDraft: false} + ] }, include: { tags: options?.withTags, @@ -97,14 +115,19 @@ class DreamsService { if (!dtoValidated.success) return buildFailedValidationResponse(dtoValidated.error) - const dream = await prisma.dream.create({ + const dream = await prisma.dream.update({ + where: { + id: dto.id, + isDraft: true, + }, data: { title: dto.title, description: dto.description, comments: dto.comments, userId: session.user.id, tags: {connect: dto.tags?.map(id => ({id})) ?? []}, - characters: {connect: dto.characters?.map(id => ({id})) ?? []} + characters: {connect: dto.characters?.map(id => ({id})) ?? []}, + isDraft: false, } }) @@ -140,7 +163,11 @@ class DreamsService { const updatedDream = await prisma.dream.update({ where: { userId: session.user.id, - id: dreamId + id: dreamId, + OR: [ + {isDraft: null}, + {isDraft: false} + ] }, data: { ...restDto, @@ -165,7 +192,11 @@ class DreamsService { const dream = await prisma.dream.delete({ where: { id: dreamId, - userId: member.id + userId: member.id, + OR: [ + {isDraft: null}, + {isDraft: false} + ] }, })