const bcrypt = require('bcrypt'); const crypto = require('crypto'); const moment = require('moment'); const { sendMail } = require('../config/mail.config'); // πŸ” Generate OTP (panjang default 6 digit) function generateOTP(length = 6) { const digits = '0123456789'; let otp = ''; for (let i = 0; i < length; i++) { otp += digits[Math.floor(Math.random() * 10)]; } return otp; } // πŸ“€ Kirim OTP via WhatsApp atau Email async function sendOTP(type, otp, via, phone, email) { if (via === 'EMAIL') { const subject = `${type} Kode Verifikasi`; const html = `

Kode verifikasi Anda adalah:

${otp}

Gunakan kode ini untuk melanjutkan proses.

`; await sendMail(email, subject, html); } else if (via === 'WHATSAPP') { // Simulasi WhatsApp - bisa diintegrasi ke API WhatsApp asli console.log(`[WhatsApp] Kirim OTP ke ${phone}: ${otp}`); // TODO: Ganti dengan pengiriman lewat API WhatsApp nyata } else { throw new Error('Metode pengiriman OTP tidak valid.'); } } // πŸ” Kirim Link Reset Password via Email const sendForgotPassword = async (email, token) => { const resetLink = `http://localhost:3000/auth/reset/${token}`; const subject = 'Reset Password'; const html = `

Reset Password

Halo, User!

Here’s the link to reset your Cashfio Account:

${resetLink}

For your security, do not share the link with anyone!


Copyright Cashfio – ${new Date().getFullYear()}
`; await sendMail({ to: email, subject, html }); }; const normalizePhone = (phone) => { return phone ? phone.replace(/[^+\d]/g, '') : null; }; module.exports = { generateOTP, sendOTP, sendForgotPassword, normalizePhone };