Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve as issues #77, #78, #79, #80, #81, #82, #83, #85 #84

Merged
merged 11 commits into from
Jan 20, 2025
Merged
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# Healthway
## Tutorial Instalação para Android

```bash
flutter build apk --split-per-abi
flutter install --use-application-binary=build\app\outputs\flutter-apk\app-arm64-v8a-release.apk
```
16 changes: 16 additions & 0 deletions backend/controllers/nutricionistaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ const nutricionistaController = {
}
},

async getByEmailAndPassword(req, res) {
try {
const { email, senha } = req.body;
const snapshot = await db.collection('nutricionista').where('email', '==', email).where('senha', '==', senha).get();
const paciente = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));

if (paciente.length === 0) {
return res.status(404).json({ error: 'Nutricionista não encontrado.' });
}

res.status(200).json(paciente[0]);
} catch (error) {
res.status(500).json({ error: error.message });
}
},

// Atualizar um nutricionista
async update(req, res) {
try {
Expand Down
44 changes: 39 additions & 5 deletions backend/controllers/pacienteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const pacienteController = {
// Criar um paciente
async create(req, res) {
try {
const paciente = new Paciente(req.body);
await db.collection('paciente').add(paciente.toFirestore());
res.status(201).json({ message: 'Paciente criado com sucesso!' });
const paciente = new Paciente();
paciente.fromJson(req.body);
await db.collection('paciente').add(paciente.toFirestore());
res.status(201).json({ message: 'Paciente criado com sucesso!' });
} catch (error) {
res.status(500).json({ error: error.message });
res.status(500).json({ error: error.message });
}
},

Expand Down Expand Up @@ -40,11 +41,44 @@ const pacienteController = {
}
},

async getByListOfIds(req, res) {
try {
const { ids } = req.body;
const pacientes = [];
for (const id of ids) {
const doc = await db.collection('paciente').doc(id).get();
if (doc.exists) {
pacientes.push({ id: doc.id, ...doc.data() });
}
}
res.status(200).json(pacientes);
} catch (error) {
res.status(500).json({ error: error.message });
}
},

async getByEmailAndPassword(req, res) {
try {
const { email, senha } = req.body;
const snapshot = await db.collection('paciente').where('email', '==', email).where('senha', '==', senha).get();
const paciente = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));

if (paciente.length === 0) {
return res.status(404).json({ error: 'Paciente não encontrado.' });
}

res.status(200).json(paciente[0]);
} catch (error) {
res.status(500).json({ error: error.message });
}
},

// Atualizar um paciente
async update(req, res) {
try {
const { id } = req.params;
const paciente = new Paciente(req.body);
const paciente = new Paciente();
paciente.fromJson(req.body);

await db.collection('paciente').doc(id).update(paciente.toFirestore());
res.status(200).json({ message: 'Paciente atualizado com sucesso!' });
Expand Down
44 changes: 24 additions & 20 deletions backend/model/Nutricionista.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
class Nutricionista {
constructor({ cpf, crn, email, especialidade, foto_perfil, foto_documento, nome }) {
this.cpf = cpf;
this.crn = crn;
this.email = email;
this.especialidade = especialidade;
this.foto_perfil = foto_perfil;
this.foto_documento = foto_documento;
this.nome = nome;
}
constructor({ cpf, crn, email, especialidade, foto_perfil, foto_documento, nome, senha, pacientes }) {
this.cpf = cpf;
this.crn = crn;
this.email = email;
this.especialidade = especialidade;
this.foto_perfil = foto_perfil;
this.foto_documento = foto_documento;
this.nome = nome;
this.senha = senha;
this.pacientes = pacientes;
}

toFirestore() {
return {
cpf: this.cpf,
crn: this.crn,
email: this.email,
especialidade: this.especialidade,
foto_perfil: this.foto_perfil,
foto_documento: this.foto_documento,
nome: this.nome,
};
}
toFirestore() {
return {
cpf: this.cpf,
crn: this.crn,
email: this.email,
especialidade: this.especialidade,
foto_perfil: this.foto_perfil,
foto_documento: this.foto_documento,
nome: this.nome,
senha: this.senha,
pacientes: this.pacientes,
};
}
}

module.exports = Nutricionista;
122 changes: 49 additions & 73 deletions backend/model/Paciente.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,55 @@
class Paciente {
constructor({
id = null,
alergias = [],
altura = 0,
cpf = "",
data_nascimento = new Date(),
email = "",
foto_perfil = null,
nome = "",
peso = 0,
sexo = "",
circunferencia_abdominal = 0,
gordura_corporal = 0,
massa_muscular = 0,
preferencias = {},
senha = "",
}) {
this.id = id; // String (opcional)
this.alergias = alergias; // Lista de Strings
this.altura = altura; // Número (double)
this.cpf = cpf; // String
this.data_nascimento = new Date(data_nascimento); // Date
this.email = email; // String
this.foto_perfil = foto_perfil; // String (opcional)
this.nome = nome; // String
this.peso = peso; // Número (double)
this.sexo = sexo; // String
this.circunferencia_abdominal = circunferencia_abdominal; // Número (double)
this.gordura_corporal = gordura_corporal; // Número (double)
this.massa_muscular = massa_muscular; // Número (double)
this.preferencias = preferencias; // Objeto (Map)
this.senha = senha; // String
}
constructor(alergias, altura, cpf, data_nascimento, email, nome, peso, sexo,
circunferencia_abdominal, gordura_corporal, massa_muscular, preferencias, senha) {
this.alergias = alergias;
this.altura = altura;
this.cpf = cpf;
this.dt_nascimento = data_nascimento;
this.email = email;
this.nome = nome;
this.peso = peso;
this.sexo = sexo;
this.circunferencia_abdominal = circunferencia_abdominal;
this.gordura_corporal = gordura_corporal;
this.massa_muscular = massa_muscular;
this.preferencias = preferencias;
this.senha = senha;

// Método estático para criar um objeto Paciente a partir de JSON
static fromJson(json) {
return new Paciente({
id: json.id || null,
alergias: Array.isArray(json.alergias) ? json.alergias : [],
altura: parseFloat(json.altura || 0),
cpf: json.cpf || "",
data_nascimento: json.data_nascimento || new Date(),
email: json.email || "",
foto_perfil: json.foto_perfil || null,
nome: json.nome || "",
peso: parseFloat(json.peso || 0),
sexo: json.sexo || "",
circunferencia_abdominal: parseFloat(json.circunferencia_abdominal || 0),
gordura_corporal: parseFloat(json.gordura_corporal || 0),
massa_muscular: parseFloat(json.massa_muscular || 0),
preferencias: json.preferencias || {},
senha: json.senha || "",
});
}
}

toFirestore() {
return {
alergias: this.alergias,
altura: this.altura,
cpf: this.cpf,
dt_nascimento: this.dt_nascimento,
email: this.email,
nome: this.nome,
peso: this.peso,
sexo: this.sexo,
circunferencia_abdominal: this.circunferencia_abdominal,
gordura_corporal: this.gordura_corporal,
massa_muscular: this.massa_muscular,
preferencias: this.preferencias,
senha: this.senha
};
}

// Método para converter um objeto Paciente para JSON
toJson() {
return {
id: this.id,
alergias: this.alergias,
altura: this.altura,
cpf: this.cpf,
data_nascimento: this.data_nascimento.toISOString(),
email: this.email,
foto_perfil: this.foto_perfil,
nome: this.nome,
peso: this.peso,
sexo: this.sexo,
circunferencia_abdominal: this.circunferencia_abdominal,
gordura_corporal: this.gordura_corporal,
massa_muscular: this.massa_muscular,
preferencias: this.preferencias,
senha: this.senha,
};
fromJson(json) {
this.alergias = json.alergias || '';
this.altura = json.altura || 0;
this.cpf = json.cpf || '';
this.dt_nascimento = json.dt_nascimento || '';
this.email = json.email || '';
this.nome = json.nome || '';
this.peso = parseFloat(json.peso) || 0;
this.sexo = json.sexo || '';
this.circunferencia_abdominal = parseFloat(json.circunferencia_abdominal) || 0;
this.gordura_corporal = parseFloat(json.gordura_corporal) || 0;
this.massa_muscular = parseFloat(json.massa_muscular) || 0;
this.preferencias = json.preferencias || '';
this.senha = json.senha || '';
}
}
}

module.exports = Paciente; // Exporta a classe para uso em outros módulos
1 change: 1 addition & 0 deletions backend/routes/nutricionistaRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const nutricionistaController = require('../controllers/nutricionistaController'
router.post('/', nutricionistaController.create);
router.get('/', nutricionistaController.getAll);
router.get('/:id', nutricionistaController.getById);
router.post('/login', nutricionistaController.getByEmailAndPassword);
router.put('/:id', nutricionistaController.update);
router.delete('/:id', nutricionistaController.delete);
router.get('/specialty/:specialty', nutricionistaController.getBySpecialty);
Expand Down
2 changes: 2 additions & 0 deletions backend/routes/pacienteRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const pacienteController = require('../controllers/pacienteController');
router.post('/', pacienteController.create);
router.get('/', pacienteController.getAll);
router.get('/:id', pacienteController.getById);
router.post('/list', pacienteController.getByListOfIds);
router.post('/login', pacienteController.getByEmailAndPassword);
router.put('/:id', pacienteController.update);
router.delete('/:id', pacienteController.delete);

Expand Down
50 changes: 50 additions & 0 deletions backend/tests/controllers/nutricionistaController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,54 @@ describe('nutricionistaController', () => {
expect(res.json).toHaveBeenCalledWith({ error: 'Erro ao buscar nutricionistas.' });
});
});

describe('getByEmailAndPassword', () => {
test('deve retornar um nutricionista pelo email e senha', async () => {
const mockData = { id: '1', nome: 'Nutricionista 1', email: 'test@example.com', senha: '123456' };
db.get.mockResolvedValueOnce({
docs: [{ id: mockData.id, data: () => mockData }],
});

const req = { body: { email: 'test@example.com', senha: '123456' } };
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};

await nutricionistaController.getByEmailAndPassword(req, res);

expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockData);
});

test('deve retornar um erro ao não achar nenhum nutricionista com o email e senha', async () => {
db.get.mockResolvedValueOnce({ docs: [] });

const req = { body: { email: 'test@example.com', senha: '123456' } };
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};

await nutricionistaController.getByEmailAndPassword(req, res);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ error: 'Nutricionista não encontrado.' });
});

test('deve retornar um erro ao buscar nutricionista pelo email e senha', async () => {
db.get.mockRejectedValueOnce(new Error('Erro ao buscar nutricionista.'));

const req = { body: { email: 'test@example.com', senha: '123456' } };
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};

await nutricionistaController.getByEmailAndPassword(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: 'Erro ao buscar nutricionista.' });
});
});
});
Loading
Loading