diff --git a/app/modules/report-found/service.server.ts b/app/modules/report-found/service.server.ts index 7c88eb03a..6d4b00b75 100644 --- a/app/modules/report-found/service.server.ts +++ b/app/modules/report-found/service.server.ts @@ -47,12 +47,12 @@ export async function createReport({ } export async function sendReportEmails({ - owner, + ownerEmail, message, reporterEmail, qr, }: { - owner: User; + ownerEmail: User["email"]; message: ReportFound["content"]; reporterEmail: ReportFound["email"]; qr: Prisma.QrGetPayload<{ @@ -73,7 +73,7 @@ export async function sendReportEmails({ return await Promise.all([ /** Send email to owner */ sendEmail({ - to: owner.email, + to: ownerEmail, subject, text: item ? `Your ${type} ${normalizedName} has been reported found. The reason is: \n\n| ${message} \n\n For contact use this email: ${reporterEmail}\n\nEmail sent via shelf.nu\n\n` @@ -93,7 +93,7 @@ export async function sendReportEmails({ throw new ShelfError({ cause, message: "Failed to send report emails", - additionalData: { owner, reporterEmail, item, type, normalizedName }, + additionalData: { ownerEmail, reporterEmail, item, type, normalizedName }, label: "Report", }); } diff --git a/app/routes/qr+/$qrId_.contact-owner.tsx b/app/routes/qr+/$qrId_.contact-owner.tsx index 272a3f5d2..71e1489f6 100644 --- a/app/routes/qr+/$qrId_.contact-owner.tsx +++ b/app/routes/qr+/$qrId_.contact-owner.tsx @@ -12,7 +12,6 @@ import { createReport, sendReportEmails, } from "~/modules/report-found/service.server"; -import { getUserByID } from "~/modules/user/service.server"; import { ShelfError, makeShelfError } from "~/utils/error"; import { isFormProcessing } from "~/utils/form"; import { @@ -45,7 +44,20 @@ export async function action({ request, params }: ActionFunctionArgs) { id: qrId, }, include: { - asset: true, + asset: { + include: { + organization: { + select: { + owner: { + select: { + email: true, + id: true, + }, + }, + }, + }, + }, + }, kit: true, }, }) @@ -73,7 +85,7 @@ export async function action({ request, params }: ActionFunctionArgs) { }); } - const owner = await getUserByID(qr.userId); + const ownerEmail = qr?.asset?.organization?.owner.email; const payload = parseData(await request.formData(), NewReportSchema); const { email, content } = payload; @@ -90,12 +102,14 @@ export async function action({ request, params }: ActionFunctionArgs) { * 1. To the owner of the asset * 2. To the person who reported the asset as found */ - await sendReportEmails({ - owner, - qr, - message: report.content, - reporterEmail: report.email, - }); + if (ownerEmail) { + await sendReportEmails({ + ownerEmail, + qr, + message: report.content, + reporterEmail: report.email, + }); + } return json(data({ report })); } catch (cause) {