feat: email field for QR review replies, internal note if no email
Some checks failed
Deploy to Production / deploy (push) Has been cancelled

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-21 17:37:22 +03:00
parent d83ffcb3c7
commit 8ab71b1e45
3 changed files with 53 additions and 26 deletions

View File

@@ -56,7 +56,7 @@ const reviewsRoutes: FastifyPluginAsync = async (fastify) => {
)
// ── PATCH /api/hotels/:slug/reviews/:id ───────────────────────────────────
fastify.patch<SlugIdParam & { Body: { reply?: string; isPublic?: boolean; rejected?: boolean } }>(
fastify.patch<SlugIdParam & { Body: { reply?: string; isPublic?: boolean; rejected?: boolean; guestEmail?: string } }>(
'/api/hotels/:slug/reviews/:id',
{ onRequest: [fastify.authenticate] },
async (request, reply) => {
@@ -67,7 +67,7 @@ const reviewsRoutes: FastifyPluginAsync = async (fastify) => {
const hotelId = await getHotelId(slug)
if (!hotelId) return reply.code(404).send({ error: 'Hotel not found' })
const { reply: replyText, isPublic, rejected } = request.body
const { reply: replyText, isPublic, rejected, guestEmail } = request.body
const updates: string[] = []
const vals: unknown[] = [id, hotelId]
@@ -93,16 +93,24 @@ const reviewsRoutes: FastifyPluginAsync = async (fastify) => {
if (!rows[0]) return reply.code(404).send({ error: 'Review not found' })
// Send email to guest when manager adds a reply
if (replyText && rows[0].booking_id) {
const { rows: bRows } = await db.query<{
guest_email: string | null; guest_name: string | null
}>(
`SELECT guest_email, guest_name FROM bookings WHERE id = $1`,
[rows[0].booking_id],
)
const guestEmail = bRows[0]?.guest_email
if (guestEmail) {
const { rows: hRows } = await db.query<{ name: string }>(
if (replyText) {
// Determine email: from booking (direct reviews) or manually entered (QR reviews)
let emailTo: string | null = guestEmail?.trim() || null
let guestFirstName = ''
if (!emailTo && rows[0].booking_id) {
const { rows: bRows } = await db.query<{
guest_email: string | null; guest_name: string | null
}>(
`SELECT guest_email, guest_name FROM bookings WHERE id = $1`,
[rows[0].booking_id],
)
emailTo = bRows[0]?.guest_email ?? null
guestFirstName = extractFirstName(bRows[0]?.guest_name ?? null)
}
if (emailTo) {
const { rows: hRows } = await db.query<{ name: string; color: string }>(
`SELECT h.name,
COALESCE((SELECT value #>> '{}' FROM hotel_settings
WHERE hotel_id = h.id AND key = 'review_brand_color'), '#2563eb') AS color
@@ -110,10 +118,10 @@ const reviewsRoutes: FastifyPluginAsync = async (fastify) => {
[hotelId],
)
sendReviewReplyEmail({
to: guestEmail,
guestFirstName: extractFirstName(bRows[0]?.guest_name ?? null),
hotelName: (hRows[0] as unknown as { name: string; color: string })?.name ?? '',
brandColor: (hRows[0] as unknown as { name: string; color: string })?.color ?? '#2563eb',
to: emailTo,
guestFirstName,
hotelName: hRows[0]?.name ?? '',
brandColor: hRows[0]?.color ?? '#2563eb',
replyText,
originalRating: rows[0].rating,
}).catch(err => console.error('[reviews] reply email error:', err))

View File

@@ -482,7 +482,7 @@ export const api = {
list: (slug: string) =>
req<ReviewApi[]>('GET', `/api/hotels/${slug}/reviews`),
update: (slug: string, id: string, data: { reply?: string; isPublic?: boolean; rejected?: boolean }) =>
update: (slug: string, id: string, data: { reply?: string; isPublic?: boolean; rejected?: boolean; guestEmail?: string }) =>
req<ReviewApi>('PATCH', `/api/hotels/${slug}/reviews/${id}`, data),
},

View File

@@ -74,6 +74,7 @@ export function ReviewsPage() {
const [tab, setTab] = useState<Tab>('pending')
const [replyId, setReplyId] = useState<string | null>(null)
const [replyText, setReplyText] = useState('')
const [replyEmail, setReplyEmail] = useState('')
const [copied, setCopied] = useState(false)
// Settings
@@ -189,10 +190,11 @@ export function ReviewsPage() {
// ── Review actions ──────────────────────────────────────────────────────────
const sendReply = async (id: string) => {
const sendReply = async (id: string, isQr: boolean) => {
if (!replyText.trim() || !slug) return
// Negative review stays private (isPublic=false), just saves the reply
const updated = await api.reviews.update(slug, id, { reply: replyText.trim() })
const data: { reply: string; guestEmail?: string } = { reply: replyText.trim() }
if (isQr && replyEmail.trim()) data.guestEmail = replyEmail.trim()
const updated = await api.reviews.update(slug, id, data)
setReviews(prev => prev.map(r => r.id === id ? updated : r))
setReplyId(null)
setReplyText('')
@@ -921,16 +923,33 @@ export function ReviewsPage() {
)}
{replyId === review.id && (
<div className="mt-3 space-y-2">
{review.source === 'qr' && (
<input
type="email"
className="input text-sm w-full"
placeholder="Email гостя (необязательно — для отправки ответа)"
value={replyEmail}
onChange={e => setReplyEmail(e.target.value)}
autoFocus
/>
)}
<textarea
className="input resize-none text-sm w-full" rows={2}
placeholder="Личный ответ гостю (отправится по email)..."
placeholder={review.source === 'qr'
? replyEmail ? 'Ответ отправится на указанный email...' : 'Внутренняя заметка (без email — не отправится гостю)...'
: 'Личный ответ гостю (отправится по email)...'}
value={replyText}
onChange={e => setReplyText(e.target.value)}
autoFocus
autoFocus={review.source !== 'qr'}
/>
<div className="flex gap-2">
<button onClick={() => sendReply(review.id)} className="btn-primary text-xs py-1.5">Отправить гостю</button>
<button onClick={() => setReplyId(null)} className="btn-secondary text-xs py-1.5">Отмена</button>
<button
onClick={() => sendReply(review.id, review.source === 'qr')}
className="btn-primary text-xs py-1.5"
>
{review.source === 'qr' && replyEmail ? 'Отправить на email' : review.source === 'qr' ? 'Сохранить заметку' : 'Отправить гостю'}
</button>
<button onClick={() => { setReplyId(null); setReplyEmail('') }} className="btn-secondary text-xs py-1.5">Отмена</button>
</div>
</div>
)}
@@ -939,10 +958,10 @@ export function ReviewsPage() {
{!review.isPublic && !review.reply && !review.rejectedAt && replyId !== review.id && (
<>
<button
onClick={() => { setReplyId(review.id); setReplyText('') }}
onClick={() => { setReplyId(review.id); setReplyText(''); setReplyEmail('') }}
className="flex items-center gap-1 px-3 py-1.5 rounded-lg bg-brand-600 text-white text-xs font-medium hover:bg-brand-700 transition-colors"
>
<MessageSquare size={12} />Ответить гостю
<MessageSquare size={12} />{review.source === 'qr' ? 'Добавить заметку / ответить' : 'Ответить гостю'}
</button>
<button
onClick={() => reject(review.id)}