Files
halobestie-clone/backend/src/services/notification.service.js
ramadhan sjamsani b0502ac92b Phase 3 testing fixes: Fastify 5, SSE→WebSocket+FCM, enums, security, session lifecycle
- Upgrade Fastify 4→5 with all plugins (@fastify/websocket 11, cors 11, sensible 6)
- Migrate all SSE endpoints to WebSocket + FCM push (mitra chat requests, customer pairing status)
- Add flutter_local_notifications for foreground push notifications with sound
- Add splash screen to both apps (hide auth loading flash)
- Introduce constants/enums across entire codebase (no raw string literals)
- Move price tiers from hardcoded array to app_config DB (data-driven, includes 1-min test tier)
- Add session ownership validation on all shared chat routes
- Add ownership checks on endSession, respondToExtension, requestExtension
- Fix session timer: auto-complete expired/stale sessions on server restart
- Add 5-min grace period for abandoned closing sessions
- Fix extension flow: proper session_resumed handling, clearExtensionRequest, closure grace timer cleanup
- Fix chat screens: ConnectChat in initState, session status check on connect
- Fix customer expired view: 5-min countdown, closure state priority over expired state
- Fix mitra extension UI: loading spinner, disable buttons, handle EXTENSION_RESOLVED error
- Fix GoRouter navigation consistency (no more Navigator.pushNamed)
- Fix goodbye view keyboard overflow (SingleChildScrollView)
- Add active session card on customer home screen with refresh on navigate back
- Fix PricingBottomSheet extension mode (RequestExtension instead of new pairing)
- Send session_resumed to both parties on extension accept

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 00:17:25 +08:00

54 lines
1.5 KiB
JavaScript

import admin from 'firebase-admin'
import { getDb } from '../db/client.js'
import { UserType } from '../constants.js'
const sql = getDb()
export const registerDeviceToken = async (userType, userId, fcmToken) => {
const table = userType === UserType.CUSTOMER ? 'customers' : 'mitras'
await sql`
UPDATE ${sql(table)}
SET fcm_token = ${fcmToken}
WHERE id = ${userId}
`
}
export const sendPushNotification = async (recipientType, recipientId, { title, body, data = {} }) => {
const table = recipientType === UserType.CUSTOMER ? 'customers' : 'mitras'
const [user] = await sql`
SELECT fcm_token FROM ${sql(table)} WHERE id = ${recipientId}
`
if (!user?.fcm_token) return false
try {
await admin.messaging().send({
token: user.fcm_token,
notification: { title, body },
data: {
...Object.fromEntries(
Object.entries(data).map(([k, v]) => [k, String(v)])
),
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
android: {
priority: 'high',
notification: { channelId: 'chat_messages' },
},
apns: {
payload: {
aps: { sound: 'default', badge: 1 },
},
},
})
return true
} catch (err) {
console.error(`[FCM] Failed to send to ${recipientType}:${recipientId}:`, err.message)
// Clear invalid token
if (err.code === 'messaging/registration-token-not-registered') {
await sql`UPDATE ${sql(table)} SET fcm_token = NULL WHERE id = ${recipientId}`
}
return false
}
}