| 1 | "use server"; |
| 2 | |
| 3 | import { logger } from "@/lib/observability/server/logger"; |
| 4 | import { auth } from "@/server/auth"; |
| 5 | import { db } from "@/server/db"; |
| 6 | import { canEditDocument } from "@/server/share/authorization"; |
| 7 | import { normalizeShareEmail } from "@/server/share/utils"; |
| 8 | |
| 9 | type UpdatePresentationThumbnailUrlParams = { |
| 10 | id: string; |
| 11 | thumbnailUrl: string | null; |
| 12 | onlyIfMissing?: boolean; |
| 13 | }; |
| 14 | |
| 15 | export async function updatePresentationThumbnailUrl({ |
| 16 | id, |
| 17 | thumbnailUrl, |
| 18 | onlyIfMissing = false, |
| 19 | }: UpdatePresentationThumbnailUrlParams) { |
| 20 | const actionName = |
| 21 | "presentation.presentationThumbnailActions.updatePresentationThumbnailUrl"; |
| 22 | const span = logger.startSpan(`presentation.server_action.${actionName}`, { |
| 23 | attributes: { |
| 24 | "allweone.scope": "presentation", |
| 25 | "allweone.action.type": "server_action", |
| 26 | "allweone.action.name": actionName, |
| 27 | }, |
| 28 | }); |
| 29 | |
| 30 | try { |
| 31 | const session = await auth(); |
| 32 | |
| 33 | if (!session?.user) { |
| 34 | throw new Error("Unauthorized"); |
| 35 | } |
| 36 | |
| 37 | const canEdit = await canEditDocument(id, { |
| 38 | userId: session.user.id, |
| 39 | userEmail: session.user.email |
| 40 | ? normalizeShareEmail(session.user.email) |
| 41 | : null, |
| 42 | }); |
| 43 | |
| 44 | if (!canEdit) { |
| 45 | return { |
| 46 | success: false, |
| 47 | message: "You do not have permission to edit this presentation", |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | const updateResult = onlyIfMissing |
| 53 | ? await db.baseDocument.updateMany({ |
| 54 | where: { |
| 55 | id, |
| 56 | thumbnailUrl: null, |
| 57 | }, |
| 58 | data: { |
| 59 | thumbnailUrl, |
| 60 | }, |
| 61 | }) |
| 62 | : await db.baseDocument.update({ |
| 63 | where: { id }, |
| 64 | data: { |
| 65 | thumbnailUrl, |
| 66 | }, |
| 67 | }); |
| 68 | |
| 69 | return { |
| 70 | success: true, |
| 71 | message: "Presentation thumbnail updated successfully", |
| 72 | thumbnailUrl, |
| 73 | updated: "count" in updateResult ? updateResult.count > 0 : true, |
| 74 | }; |
| 75 | } catch (error) { |
| 76 | console.error(error); |
| 77 | return { |
| 78 | success: false, |
| 79 | message: "Failed to update presentation thumbnail", |
| 80 | }; |
| 81 | } |
| 82 | } catch (error) { |
| 83 | span.error(error); |
| 84 | throw error; |
| 85 | } finally { |
| 86 | span.end(); |
| 87 | } |
| 88 | } |
| 89 |