···2323 // Check publication and document ownership in one query
2424 let { data: tokenData } = await supabaseServerClient
2525 .from("permission_tokens")
2626- .select(`
2626+ .select(
2727+ `
2728 id,
2829 leaflets_in_publications(publication, publications!inner(identity_did)),
2930 leaflets_to_documents(document, documents!inner(uri))
3030- `)
3131+ `,
3232+ )
3133 .eq("id", permission_token.id)
3234 .single();
3335···3638 const leafletInPubs = tokenData.leaflets_in_publications || [];
3739 if (leafletInPubs.length > 0) {
3840 if (!identity) {
3939- throw new Error("Unauthorized: You must be logged in to delete a leaflet in a publication");
4141+ throw new Error(
4242+ "Unauthorized: You must be logged in to delete a leaflet in a publication",
4343+ );
4044 }
4145 const isOwner = leafletInPubs.some(
4242- (pub: any) => pub.publications.identity_did === identity.atp_did
4646+ (pub: any) => pub.publications.identity_did === identity.atp_did,
4347 );
4448 if (!isOwner) {
4545- throw new Error("Unauthorized: You must own the publication to delete this leaflet");
4949+ throw new Error(
5050+ "Unauthorized: You must own the publication to delete this leaflet",
5151+ );
4652 }
4753 }
4854···5056 const leafletDocs = tokenData.leaflets_to_documents || [];
5157 if (leafletDocs.length > 0) {
5258 if (!identity) {
5353- throw new Error("Unauthorized: You must be logged in to delete a published leaflet");
5959+ throw new Error(
6060+ "Unauthorized: You must be logged in to delete a published leaflet",
6161+ );
5462 }
5563 for (let leafletDoc of leafletDocs) {
5664 const docUri = leafletDoc.documents?.uri;
5765 // Extract the DID from the document URI (format: at://did:plc:xxx/...)
5858- if (docUri && !docUri.includes(identity.atp_did)) {
5959- throw new Error("Unauthorized: You must own the published document to delete this leaflet");
6666+ if (docUri && identity.atp_did && !docUri.includes(identity.atp_did)) {
6767+ throw new Error(
6868+ "Unauthorized: You must own the published document to delete this leaflet",
6969+ );
6070 }
6171 }
6272 }