133 lines
3.9 KiB
JavaScript
133 lines
3.9 KiB
JavaScript
const db = require("../models");
|
|
const Client = db.client;
|
|
var bcrypt = require("bcryptjs");
|
|
// Create and Save a new Client
|
|
exports.createClient = (req, res) => {
|
|
// Validate request
|
|
if (!req.body.name) {
|
|
res.status(400).send({ message: "Content can not be empty!" });
|
|
return;
|
|
}
|
|
// Create a Client
|
|
const client = new Client({
|
|
username: req.body.username || req.body.email || '',
|
|
name: req.body.name || '',
|
|
name_cn: req.body.name_cn || '',
|
|
email: req.body.email || '',
|
|
password: req.body.password ? bcrypt.hashSync(req.body.password, 8) : '',
|
|
mobile_phone: req.body.mobile_phone || '',
|
|
home_phone: req.body.home_phone || '',
|
|
phone: req.body.phone || '',
|
|
language: req.body.language || '',
|
|
status: 'active',
|
|
address: req.body.address1 || '',
|
|
address1: req.body.address2 || '',
|
|
firstname: req.body.firstname || '',
|
|
lastname: req.body.lastname || '',
|
|
birth_date: req.body.birth_date || null,
|
|
create_by: req.body.create_by || '',
|
|
create_date: new Date(),
|
|
edit_by: req.body.edit_by || '',
|
|
edit_date: new Date(),
|
|
note: req.body.note || '',
|
|
care_provider: req.body.care_provider || '',
|
|
emergency_contact: req.body.emergency_contact || '',
|
|
medicare_number: req.body.medicare_number || '',
|
|
medicaid_number: req.body.medicaid_number || '',
|
|
pharmacy: req.body.pharmacy || '',
|
|
type: req.body.type || '',
|
|
pharmacy_id: req.body.pharmacy_id || '',
|
|
pin: req.body.pin || '',
|
|
admission_date: req.body.admission_date || null,
|
|
seating: req.body.seating || '',
|
|
vehicle_no: req.body.vehicle_no || '',
|
|
caller: req.body.caller || '',
|
|
discharge_date: req.body.discharge_date || null,
|
|
placement: req.body.placement || '',
|
|
nickname: req.body.nickname || '',
|
|
groups: req.body.groups || null,
|
|
tags: req.body.tags || null,
|
|
roles: req.body.roles || null,
|
|
private_note: req.body.private_note || '',
|
|
parent_id: '5eee3552b02fac3d4acfd5ea'
|
|
});
|
|
// Save Client in the database
|
|
client
|
|
.save(client)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while creating the Client."
|
|
});
|
|
});
|
|
};
|
|
|
|
exports.getClient = (req, res) => {
|
|
const id = req.params.id;
|
|
Client.findById(id)
|
|
.then(data => {
|
|
if (!data)
|
|
res.status(404).send({ message: "Not found client with id " + id });
|
|
else res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res
|
|
.status(500)
|
|
.send({ message: "Error retrieving client with id=" + id });
|
|
});
|
|
}
|
|
|
|
// Update a Client by the id in the request
|
|
exports.updateClient = (req, res) => {
|
|
if (!req.body) {
|
|
return res.status(400).send({
|
|
message: "Data to update can not be empty!"
|
|
});
|
|
}
|
|
const id = req.params.id;
|
|
if (req.body.password) {
|
|
req.body.password = bcrypt.hashSync(req.body.password, 8);
|
|
}
|
|
Client.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
|
|
.then(data => {
|
|
if (!data) {
|
|
res.status(404).send({
|
|
message: `Cannot update client with id=${id}. Maybe Client was not found!`
|
|
});
|
|
} else res.send({ success: true, message: "Client was updated successfully." });
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
success: false,
|
|
message: "Error updating Client with id=" + id
|
|
});
|
|
});
|
|
};
|
|
|
|
// Get Client with username or email
|
|
exports.getClientsWithNameOrEmail = (req, res) => {
|
|
var params = req.query;
|
|
var condition = {};
|
|
const nameOrEmail = params?.nameOrEmail;
|
|
if (nameOrEmail) {
|
|
condition = { $or: [
|
|
{ email: nameOrEmail },
|
|
{ name: nameOrEmail }
|
|
]};
|
|
}
|
|
Client.find(condition)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while retrieving clients."
|
|
});
|
|
});
|
|
};
|
|
|