Skip to content

Commit

Permalink
Update DreamService to only deal with non-draft dreams
Browse files Browse the repository at this point in the history
  • Loading branch information
bombies committed Nov 11, 2023
1 parent 4abbd19 commit add5b64
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/app/api/me/dreams/dreams.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const FetchDreamsSchema = zfd.formData({
})

export type PostDreamDto = {
id: string,
title: string,
description: string,
comments?: string | null,
Expand All @@ -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!`),
Expand Down
73 changes: 52 additions & 21 deletions src/app/api/me/dreams/dreams.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
})

Expand Down Expand Up @@ -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,
Expand All @@ -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}
]
},
})

Expand Down

0 comments on commit add5b64

Please sign in to comment.