Add phone number field and WhatsApp/copy message buttons for participants
This commit is contained in:
@@ -46,6 +46,7 @@ export interface Participant {
|
||||
id: number;
|
||||
questionnaire_id: number;
|
||||
name: string;
|
||||
phone: string | null;
|
||||
token: string;
|
||||
created_at: string;
|
||||
}
|
||||
@@ -141,6 +142,13 @@ export function initializeDatabase(): void {
|
||||
)
|
||||
`);
|
||||
|
||||
// Migration: Add phone column to participants if it doesn't exist
|
||||
try {
|
||||
db.exec(`ALTER TABLE participants ADD COLUMN phone TEXT`);
|
||||
} catch (e) {
|
||||
// Column already exists, ignore
|
||||
}
|
||||
|
||||
db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idx_participants_questionnaire ON participants(questionnaire_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_participants_token ON participants(token);
|
||||
@@ -308,10 +316,10 @@ export const questionnaireOps = {
|
||||
|
||||
// Participant operations
|
||||
export const participantOps = {
|
||||
create: (questionnaireId: number, name: string, token: string): number => {
|
||||
create: (questionnaireId: number, name: string, phone: string | null, token: string): number => {
|
||||
const result = db.prepare(
|
||||
'INSERT INTO participants (questionnaire_id, name, token) VALUES (?, ?, ?)'
|
||||
).run(questionnaireId, name, token);
|
||||
'INSERT INTO participants (questionnaire_id, name, phone, token) VALUES (?, ?, ?, ?)'
|
||||
).run(questionnaireId, name, phone, token);
|
||||
return result.lastInsertRowid as number;
|
||||
},
|
||||
|
||||
|
||||
@@ -197,7 +197,7 @@ function generateToken(): string {
|
||||
|
||||
// Add participant to questionnaire
|
||||
router.post('/questionnaires/:id/participants', (req: Request, res: Response) => {
|
||||
const { name } = req.body;
|
||||
const { name, phone } = req.body;
|
||||
const questionnaire = questionnaireOps.findById(parseInt(req.params.id));
|
||||
|
||||
if (!questionnaire) {
|
||||
@@ -210,13 +210,16 @@ router.post('/questionnaires/:id/participants', (req: Request, res: Response) =>
|
||||
return;
|
||||
}
|
||||
|
||||
// Clean phone number (remove spaces, keep + and digits)
|
||||
const cleanPhone = phone?.trim() ? phone.trim().replace(/[^\d+]/g, '') : null;
|
||||
|
||||
// Generate unique token
|
||||
let token = generateToken();
|
||||
while (!participantOps.isTokenAvailable(token)) {
|
||||
token = generateToken();
|
||||
}
|
||||
|
||||
const id = participantOps.create(questionnaire.id, name.trim(), token);
|
||||
const id = participantOps.create(questionnaire.id, name.trim(), cleanPhone, token);
|
||||
const participant = participantOps.findById(id);
|
||||
|
||||
res.json({ success: true, participant });
|
||||
|
||||
Reference in New Issue
Block a user