diff --git a/api/ethos/licensing-notifications.ts b/api/ethos/licensing-notifications.ts new file mode 100644 index 00000000..a2dde1c7 --- /dev/null +++ b/api/ethos/licensing-notifications.ts @@ -0,0 +1,260 @@ +import { createClient } from "@supabase/supabase-js"; +import nodemailer from "nodemailer"; + +const supabase = createClient( + process.env.VITE_SUPABASE_URL || "", + process.env.SUPABASE_SERVICE_ROLE || "", +); + +// Initialize email transporter +function getEmailTransporter() { + return nodemailer.createTransport({ + host: process.env.SMTP_HOST, + port: parseInt(process.env.SMTP_PORT || "465"), + secure: parseInt(process.env.SMTP_PORT || "465") === 465, + auth: { + user: process.env.SMTP_USER, + pass: process.env.SMTP_PASSWORD, + }, + }); +} + +const fromEmail = process.env.SMTP_FROM_EMAIL || "no-reply@aethex.tech"; + +interface LicensingNotification { + type: "agreement_created" | "agreement_approved" | "agreement_pending_approval"; + track_id: string; + artist_id: string; + licensee_id: string; + agreement_id: string; + license_type: string; +} + +export default async function handler(req: any, res: any) { + const { method, body } = req; + + try { + if (method === "POST") { + const { action, agreement_id, license_type } = body; + const authUser = req.headers["x-user-id"]; + + if (!authUser) { + return res.status(401).json({ error: "Unauthorized" }); + } + + if (action === "notify-approval") { + // Notify artist that their licensing agreement was approved + const { data: agreement, error: agError } = await supabase + .from("ethos_licensing_agreements") + .select( + ` + id, + track_id, + licensee_id, + license_type, + ethos_tracks( + id, + title, + user_id, + user_profiles(full_name, email) + ), + user_profiles!licensee_id(full_name, email) + `, + ) + .eq("id", agreement_id) + .single(); + + if (agError || !agreement) { + return res.status(404).json({ error: "Agreement not found" }); + } + + const artist = agreement.ethos_tracks?.user_profiles; + const licensee = agreement.user_profiles; + const trackTitle = agreement.ethos_tracks?.title; + + if (!artist?.email) { + return res.status(400).json({ error: "Artist email not found" }); + } + + // Send approval notification to artist + const transporter = getEmailTransporter(); + await transporter.sendMail({ + from: fromEmail, + to: artist.email, + subject: `Licensing Agreement Approved - "${trackTitle}" 🎵`, + html: ` +
Great news! Your licensing agreement for "${trackTitle}" has been approved.
+ +Licensee: ${licensee?.full_name}
+License Type: ${agreement.license_type.replace(/_/g, " ").toUpperCase()}
+The licensee has approved the agreement and can now use your track according to the license terms. Ensure you review the agreement details and any associated contracts.
+ ++ View Agreement +
+ ++ This is an automated notification from the Ethos Guild. For questions, contact support@aethex.tech. +
+Someone wants to license your track "${trackTitle}"!
+ +Requestor: ${licensee?.full_name}
+License Type: ${agreement.license_type.replace(/_/g, " ").toUpperCase()}
+Review the license request and decide whether to approve or decline. You can set custom terms or accept the default licensing agreement.
+ ++ Review Request +
+ ++ This is an automated notification from the Ethos Guild. For questions, contact support@aethex.tech. +
+We've received a decision on your licensing request for "${trackTitle}".
+ +Status: Request Declined
+Artist: ${artist?.full_name}
+Unfortunately, the artist declined your licensing request for this track. You may explore other licensing opportunities or reach out to the artist directly if you'd like to discuss custom arrangements.
+ + + ++ This is an automated notification from the Ethos Guild. For questions, contact support@aethex.tech. +
+