deleteOne: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const post = await ctx.prisma.post.findUnique({
where: { id: input.id },
select: { authorId: true, images: true },
});
// backend validation for user authorization to delete post
if (post?.authorId !== ctx.session.user.id) {
throw new TRPCError({
code: "CONFLICT",
message: "You are not authorized to delete this post",
});
}
// delete images from s3 bucket
for (const image of post.images) {
s3.deleteObject(
{
Bucket: env.AWS_BUCKET_NAME,
Key: `${image.userId}/${image?.postId as string}/${image.id}`,
},
(err) => {
if (err) {
console.log(err);
}
}
);
}
return ctx.prisma.post.delete({
where: { id: input.id },
});
}),
deleteOne: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const post = await ctx.prisma.post.findUnique({
where: { id: input.id },
select: { authorId: true, images: true },
});
// backend validation for user authorization to delete post
if (post?.authorId !== ctx.session.user.id) {
throw new TRPCError({
code: "CONFLICT",
message: "You are not authorized to delete this post",
});
}
// delete images from s3 bucket
for (const image of post.images) {
s3.deleteObject(
{
Bucket: env.AWS_BUCKET_NAME,
Key: `${image.userId}/${image?.postId as string}/${image.id}`,
},
(err) => {
if (err) {
console.log(err);
}
}
);
}
return ctx.prisma.post.delete({
where: { id: input.id },
});
}),