feat(mensagens): adiciona suporte para disparo em massa para pre-matriculas
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 2m18s
Details
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 2m18s
Details
This commit is contained in:
parent
14d3d7c17f
commit
b38a7abf35
|
|
@ -31,15 +31,16 @@ const Messages: React.FC<MessagesProps> = ({ data, updateData }) => {
|
|||
|
||||
const [dbClasses, setDbClasses] = useState<any[]>(data?.classes || []);
|
||||
const [dbCourses, setDbCourses] = useState<any[]>(data?.courses || []);
|
||||
const [dbPreMatriculas, setDbPreMatriculas] = useState<any[]>([]);
|
||||
|
||||
|
||||
React.useEffect(() => {
|
||||
Promise.all([
|
||||
fetch('/api/turmas').catch(() => ({ ok: false, json: async () => ({}) })),
|
||||
fetch('/api/cursos').catch(() => ({ ok: false, json: async () => ({}) })),
|
||||
|
||||
fetch('/api/prematricula/inscricoes').catch(() => ({ ok: false, json: async () => ({}) }))
|
||||
]).then(async (responses) => {
|
||||
const [resT, resC] = responses;
|
||||
const [resT, resC, resP] = responses;
|
||||
if (resT && resT.ok) {
|
||||
const jsonT = await resT.json();
|
||||
if (jsonT.turmas) setDbClasses(jsonT.turmas.map((t: any) => ({
|
||||
|
|
@ -52,7 +53,10 @@ const Messages: React.FC<MessagesProps> = ({ data, updateData }) => {
|
|||
id: c.id, name: c.nome, monthlyFee: Number(c.mensalidade || 0), registrationFee: Number(c.taxa_matricula || 0)
|
||||
})));
|
||||
}
|
||||
|
||||
if (resP && resP.ok) {
|
||||
const jsonP = await resP.json();
|
||||
if (jsonP.inscricoes) setDbPreMatriculas(jsonP.inscricoes);
|
||||
}
|
||||
}).catch(console.error);
|
||||
}, []);
|
||||
|
||||
|
|
@ -240,6 +244,8 @@ const Messages: React.FC<MessagesProps> = ({ data, updateData }) => {
|
|||
}
|
||||
|
||||
let targetStudents = [];
|
||||
let isPreMatricula = false;
|
||||
|
||||
if (targetType === 'todos') {
|
||||
targetStudents = data.students || [];
|
||||
} else if (targetType === 'turma') {
|
||||
|
|
@ -248,37 +254,62 @@ const Messages: React.FC<MessagesProps> = ({ data, updateData }) => {
|
|||
} else if (targetType === 'aluno') {
|
||||
if (!targetId) return showAlert('Aviso', 'Selecione um aluno.', 'warning');
|
||||
targetStudents = (data.students || []).filter(s => s.id === targetId);
|
||||
} else if (targetType === 'prematricula_todos') {
|
||||
targetStudents = dbPreMatriculas;
|
||||
isPreMatricula = true;
|
||||
} else if (targetType === 'prematricula_turma') {
|
||||
if (!targetId) return showAlert('Aviso', 'Selecione uma turma.', 'warning');
|
||||
targetStudents = dbPreMatriculas.filter(p => p.turmaId === targetId);
|
||||
isPreMatricula = true;
|
||||
} else if (targetType === 'prematricula_aluno') {
|
||||
if (!targetId) return showAlert('Aviso', 'Selecione uma pré-matrícula.', 'warning');
|
||||
targetStudents = dbPreMatriculas.filter(p => p.id === targetId);
|
||||
isPreMatricula = true;
|
||||
}
|
||||
|
||||
const validStudents = targetStudents.filter(a => a.phone || a.guardianPhone);
|
||||
if (validStudents.length === 0) {
|
||||
return showAlert('Erro', 'Nenhum aluno com telefone cadastrado foi selecionado.', 'error');
|
||||
let payloadAlunos = [];
|
||||
|
||||
if (isPreMatricula) {
|
||||
const validPre = targetStudents.filter(p => p.telefone);
|
||||
if (validPre.length === 0) {
|
||||
return showAlert('Erro', 'Nenhuma pré-matrícula com telefone válido foi selecionada.', 'error');
|
||||
}
|
||||
payloadAlunos = validPre.map(p => ({
|
||||
nome: p.nome.split(' ')[0],
|
||||
telefone: p.telefone,
|
||||
matricula: 'Pré-Matrícula'
|
||||
}));
|
||||
} else {
|
||||
const validStudents = targetStudents.filter(a => a.phone || a.guardianPhone);
|
||||
if (validStudents.length === 0) {
|
||||
return showAlert('Erro', 'Nenhum aluno com telefone cadastrado foi selecionado.', 'error');
|
||||
}
|
||||
|
||||
payloadAlunos = validStudents.flatMap(a => {
|
||||
const entries = [];
|
||||
|
||||
// Entrada para o Aluno
|
||||
if (a.phone) {
|
||||
entries.push({
|
||||
nome: a.name.split(' ')[0],
|
||||
telefone: a.phone,
|
||||
matricula: a.enrollmentNumber || '—'
|
||||
});
|
||||
}
|
||||
|
||||
// Entrada para o Responsável (apenas se for um número diferente ou se o aluno não tiver número)
|
||||
if (a.guardianPhone && a.guardianPhone !== a.phone) {
|
||||
entries.push({
|
||||
nome: (a.guardianName || a.name).split(' ')[0],
|
||||
telefone: a.guardianPhone,
|
||||
matricula: a.enrollmentNumber || '—'
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
});
|
||||
}
|
||||
|
||||
const payloadAlunos = validStudents.flatMap(a => {
|
||||
const entries = [];
|
||||
|
||||
// Entrada para o Aluno
|
||||
if (a.phone) {
|
||||
entries.push({
|
||||
nome: a.name.split(' ')[0],
|
||||
telefone: a.phone,
|
||||
matricula: a.enrollmentNumber || '—'
|
||||
});
|
||||
}
|
||||
|
||||
// Entrada para o Responsável (apenas se for um número diferente ou se o aluno não tiver número)
|
||||
if (a.guardianPhone && a.guardianPhone !== a.phone) {
|
||||
entries.push({
|
||||
nome: (a.guardianName || a.name).split(' ')[0],
|
||||
telefone: a.guardianPhone,
|
||||
matricula: a.enrollmentNumber || '—'
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
});
|
||||
|
||||
setIsSendingMass(true);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
|
|
@ -371,21 +402,26 @@ const Messages: React.FC<MessagesProps> = ({ data, updateData }) => {
|
|||
value={targetType}
|
||||
onChange={(e) => { setTargetType(e.target.value); setTargetId(''); }}
|
||||
>
|
||||
<option value="todos">Todos os Alunos</option>
|
||||
<option value="turma">Uma Turma</option>
|
||||
<option value="aluno">Um Aluno</option>
|
||||
<option value="todos">Todos os Alunos Ativos</option>
|
||||
<option value="turma">Alunos (Por Turma)</option>
|
||||
<option value="aluno">Alunos (Individual)</option>
|
||||
<option value="prematricula_todos">Pré-Matrículas (Todos)</option>
|
||||
<option value="prematricula_turma">Pré-Matrículas (Por Turma)</option>
|
||||
<option value="prematricula_aluno">Pré-Matrículas (Individual)</option>
|
||||
</select>
|
||||
|
||||
{targetType !== 'todos' && (
|
||||
{targetType !== 'todos' && targetType !== 'prematricula_todos' && (
|
||||
<select
|
||||
className="w-full px-3 py-2.5 border border-emerald-200 rounded-xl text-sm bg-white font-bold"
|
||||
value={targetId}
|
||||
onChange={(e) => setTargetId(e.target.value)}
|
||||
>
|
||||
<option value="">-- Selecione --</option>
|
||||
{targetType === 'turma'
|
||||
{(targetType === 'turma' || targetType === 'prematricula_turma')
|
||||
? dbClasses?.map(c => <option key={c.id} value={c.id}>{c.name}</option>)
|
||||
: data.students?.map(s => <option key={s.id} value={s.id}>{s.name}</option>)
|
||||
: targetType === 'prematricula_aluno'
|
||||
? dbPreMatriculas?.map(p => <option key={p.id} value={p.id}>{p.nome} ({p.telefone})</option>)
|
||||
: data.students?.map(s => <option key={s.id} value={s.id}>{s.name}</option>)
|
||||
}
|
||||
</select>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Reference in New Issue