got something working thats only a little hacky
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { Prisma } from '@prisma/client'
|
||||
import type { Prisma, Project as ProjectType } from '@prisma/client'
|
||||
import type { ResolverArgs } from '@redwoodjs/api'
|
||||
import { uploadImage, makeSocialPublicIdServer } from 'src/lib/cloudinary'
|
||||
|
||||
import { db } from 'src/lib/db'
|
||||
import {
|
||||
@@ -10,7 +11,7 @@ import {
|
||||
destroyImage,
|
||||
} from 'src/services/helpers'
|
||||
import { requireAuth } from 'src/lib/auth'
|
||||
import { requireOwnership } from 'src/lib/owner'
|
||||
import { requireOwnership, requireProjectOwnership } from 'src/lib/owner'
|
||||
|
||||
export const projects = ({ userName }) => {
|
||||
if (!userName) {
|
||||
@@ -116,6 +117,107 @@ export const updateProject = async ({ id, input }: UpdateProjectArgs) => {
|
||||
return update
|
||||
}
|
||||
|
||||
export const updateProjectImages = async ({
|
||||
id,
|
||||
mainImage64,
|
||||
socialCard64,
|
||||
}: {
|
||||
id: string
|
||||
mainImage64?: string
|
||||
socialCard64?: string
|
||||
}): Promise<ProjectType> => {
|
||||
requireAuth()
|
||||
const project = await requireProjectOwnership({ projectId: id })
|
||||
const replaceSocialCard = async () => {
|
||||
if (!socialCard64) {
|
||||
return
|
||||
}
|
||||
let publicId = ''
|
||||
let socialCardId = ''
|
||||
try {
|
||||
;({ id: socialCardId, url: publicId } = await db.socialCard.findUnique({
|
||||
where: { projectId: id },
|
||||
}))
|
||||
} catch (e) {
|
||||
const { userName } = await db.user.findUnique({
|
||||
where: { id: project.userId },
|
||||
})
|
||||
publicId = makeSocialPublicIdServer(userName, project.title)
|
||||
}
|
||||
const imagePromise = uploadImage({
|
||||
image64: socialCard64,
|
||||
uploadPreset: 'CadHub_project_images',
|
||||
publicId,
|
||||
invalidate: true,
|
||||
})
|
||||
const saveOrUpdateSocialCard = () => {
|
||||
const data = {
|
||||
outOfDate: false,
|
||||
url: publicId,
|
||||
}
|
||||
if (socialCardId) {
|
||||
return db.socialCard.update({
|
||||
data,
|
||||
where: { projectId: id },
|
||||
})
|
||||
}
|
||||
return db.socialCard.create({
|
||||
data: {
|
||||
...data,
|
||||
project: {
|
||||
connect: {
|
||||
id: id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
const socialCardUpdatePromise = saveOrUpdateSocialCard()
|
||||
const [socialCard] = await Promise.all([
|
||||
socialCardUpdatePromise,
|
||||
imagePromise,
|
||||
])
|
||||
return socialCard
|
||||
}
|
||||
|
||||
const updateMainImage = async (): Promise<ProjectType> => {
|
||||
if (!mainImage64) {
|
||||
return project
|
||||
}
|
||||
const { public_id: mainImage } = await uploadImage({
|
||||
image64: mainImage64,
|
||||
uploadPreset: 'CadHub_project_images',
|
||||
invalidate: true,
|
||||
})
|
||||
const projectPromise = db.project.update({
|
||||
data: {
|
||||
mainImage,
|
||||
},
|
||||
where: { id },
|
||||
})
|
||||
let imageDestroyPromise = new Promise((r) => r(null))
|
||||
if (project.mainImage) {
|
||||
console.log(
|
||||
`image destroyed, publicId: ${project.mainImage}, projectId: ${id}, replacing image is ${mainImage}`
|
||||
)
|
||||
// destroy after the db has been updated
|
||||
imageDestroyPromise = destroyImage({ publicId: project.mainImage })
|
||||
}
|
||||
const [updatedProject] = await Promise.all([
|
||||
projectPromise,
|
||||
imageDestroyPromise,
|
||||
])
|
||||
return updatedProject
|
||||
}
|
||||
|
||||
const [updatedProject] = await Promise.all([
|
||||
updateMainImage(),
|
||||
replaceSocialCard(),
|
||||
])
|
||||
|
||||
return updatedProject
|
||||
}
|
||||
|
||||
export const deleteProject = async ({ id }: Prisma.ProjectWhereUniqueInput) => {
|
||||
requireAuth()
|
||||
await requireOwnership({ projectId: id })
|
||||
|
||||
Reference in New Issue
Block a user