- Backend: WebSocket plugin, chat/pricing/timer/extension/closure/notification services - Client app: ChatBloc, pricing dialog, chat screen with message status, extension/goodbye flow, history - Mitra app: MitraChatBloc, ExtensionBloc, chat screen, extension accept/reject, history - Control center: free trial, extension timeout, early end config toggles - DB migration: chat_messages, session_closures, session_extensions, customer_transactions tables Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
105 lines
4.4 KiB
JavaScript
105 lines
4.4 KiB
JavaScript
import { authenticate, requirePermission } from '../../plugins/auth.js'
|
|
import { getCcUserByFirebaseUid } from '../../services/cc-user.service.js'
|
|
import {
|
|
getAnonymityConfig, setAnonymityConfig,
|
|
getMaxCustomersPerMitra, setMaxCustomersPerMitra,
|
|
getFreeTrialConfig, setFreeTrialConfig,
|
|
getExtensionTimeoutConfig, setExtensionTimeoutConfig,
|
|
getEarlyEndConfig, setEarlyEndConfig,
|
|
} from '../../services/config.service.js'
|
|
|
|
const attachCcUser = async (request, reply) => {
|
|
const user = await getCcUserByFirebaseUid(request.firebaseUser.uid)
|
|
if (!user) return reply.code(403).send({ success: false, error: { code: 'FORBIDDEN', message: 'Not a control center user' } })
|
|
request.ccUser = user
|
|
}
|
|
|
|
export const internalConfigRoutes = async (app) => {
|
|
app.get('/anonymity', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'read')],
|
|
}, async (request, reply) => {
|
|
const config = await getAnonymityConfig()
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.patch('/anonymity', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'update')],
|
|
}, async (request, reply) => {
|
|
const { anonymity_enabled } = request.body ?? {}
|
|
if (typeof anonymity_enabled !== 'boolean') {
|
|
return reply.code(422).send({ success: false, error: { code: 'VALIDATION_ERROR', message: 'anonymity_enabled must be a boolean' } })
|
|
}
|
|
const config = await setAnonymityConfig(anonymity_enabled)
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.get('/max-customers-per-mitra', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'read')],
|
|
}, async (request, reply) => {
|
|
const config = await getMaxCustomersPerMitra()
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.patch('/max-customers-per-mitra', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'update')],
|
|
}, async (request, reply) => {
|
|
const { max_customers_per_mitra } = request.body ?? {}
|
|
if (typeof max_customers_per_mitra !== 'number' || max_customers_per_mitra < 1) {
|
|
return reply.code(422).send({ success: false, error: { code: 'VALIDATION_ERROR', message: 'max_customers_per_mitra must be a positive number' } })
|
|
}
|
|
const config = await setMaxCustomersPerMitra(max_customers_per_mitra)
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
// --- Phase 3: Free Trial ---
|
|
app.get('/free-trial', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'read')],
|
|
}, async (request, reply) => {
|
|
const config = await getFreeTrialConfig()
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.patch('/free-trial', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'update')],
|
|
}, async (request, reply) => {
|
|
const { enabled, duration_minutes } = request.body ?? {}
|
|
const config = await setFreeTrialConfig({ enabled, duration_minutes })
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
// --- Phase 3: Extension Timeout ---
|
|
app.get('/extension-timeout', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'read')],
|
|
}, async (request, reply) => {
|
|
const config = await getExtensionTimeoutConfig()
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.patch('/extension-timeout', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'update')],
|
|
}, async (request, reply) => {
|
|
const { extension_timeout_seconds } = request.body ?? {}
|
|
if (typeof extension_timeout_seconds !== 'number' || extension_timeout_seconds < 10) {
|
|
return reply.code(422).send({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Must be a number >= 10' } })
|
|
}
|
|
const config = await setExtensionTimeoutConfig(extension_timeout_seconds)
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
// --- Phase 3: Early End ---
|
|
app.get('/early-end', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'read')],
|
|
}, async (request, reply) => {
|
|
const config = await getEarlyEndConfig()
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
|
|
app.patch('/early-end', {
|
|
preHandler: [authenticate, attachCcUser, requirePermission('config', 'update')],
|
|
}, async (request, reply) => {
|
|
const { mitra_enabled, customer_enabled } = request.body ?? {}
|
|
const config = await setEarlyEndConfig({ mitra_enabled, customer_enabled })
|
|
return reply.send({ success: true, data: config })
|
|
})
|
|
}
|