185 lines
5.6 KiB
JavaScript
185 lines
5.6 KiB
JavaScript
const db = require("../models");
|
|
const Customer = db.customer;
|
|
var bcrypt = require("bcryptjs");
|
|
const { splitSite } = require("../middlewares");
|
|
// Create and Save a new Customer
|
|
exports.createCustomer = (req, res) => {
|
|
// Validate request
|
|
if (!req.body.name) {
|
|
res.status(400).send({ message: "Content can not be empty!" });
|
|
return;
|
|
}
|
|
const site = splitSite.findSiteNumber(req);
|
|
// Create a Customer
|
|
const customer = new Customer({
|
|
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',
|
|
address1: req.body.address1 || '',
|
|
address2: req.body.address2 || '',
|
|
address3: req.body.address3 || '',
|
|
address4: req.body.address4 || '',
|
|
address5: req.body.address5 || '',
|
|
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 || '',
|
|
avatar: req.body.avatar || '',
|
|
special_needs: req.body.special_needs || '',
|
|
pickup_status: req.body.pickup_status || '',
|
|
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 || '',
|
|
table_id: req.body.table_id || '',
|
|
groups: req.body.groups || null,
|
|
tags: req.body.tags || null,
|
|
roles: req.body.roles || null,
|
|
apartment: req.body.apartment || '',
|
|
private_note: req.body.private_note || '',
|
|
parent_id: '5eee3552b02fac3d4acfd5ea',
|
|
site,
|
|
disability: req.body.disability || false,
|
|
weight: req.body.weight || '',
|
|
height: req.body.height || '',
|
|
gender: req.body.gender || '',
|
|
text_msg_enabled: req.body.text_msg_enabled || false
|
|
});
|
|
// Save Customer in the database
|
|
customer
|
|
.save(customer)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while creating the Customer."
|
|
});
|
|
});
|
|
};
|
|
// Retrieve all Customers from the database.
|
|
exports.getAllCustomers = (req, res) => {
|
|
var condition = {};
|
|
condition = splitSite.splitSiteGet(req, condition);
|
|
Customer.find(condition)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while retrieving customers."
|
|
});
|
|
});
|
|
};
|
|
// Retrieve all Active Customer from the database.
|
|
exports.getAllActiveCustomers = (req, res) => {
|
|
var condition = { status: 'active' };
|
|
condition = splitSite.splitSiteGet(req, condition);
|
|
Customer.find(condition)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while retrieving Customers."
|
|
});
|
|
});
|
|
};
|
|
// Get One Customer by Id
|
|
exports.getCustomer = (req, res) => {
|
|
const id = req.params.id;
|
|
Customer.findById(id)
|
|
.then(data => {
|
|
if (!data)
|
|
res.status(404).send({ message: "Not found Customer with id " + id });
|
|
else res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res
|
|
.status(500)
|
|
.send({ message: "Error retrieving Customer with id=" + id });
|
|
});
|
|
};
|
|
// Update a Customer by the id in the request
|
|
exports.updateCustomer = (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);
|
|
}
|
|
Customer.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
|
|
.then(data => {
|
|
if (!data) {
|
|
res.status(404).send({
|
|
message: `Cannot update customer with id=${id}. Maybe Customer was not found!`
|
|
});
|
|
} else res.send({ success: true, message: "Customer was updated successfully." });
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
success: false,
|
|
message: "Error updating Customer with id=" + id
|
|
});
|
|
});
|
|
};
|
|
// Soft Delete a Customer with the specified id in the request
|
|
exports.deactivateCustomer = (req, res) => {
|
|
|
|
};
|
|
// Delete a Customer by id
|
|
exports.deleteCustomer = (req, res) => {
|
|
|
|
};
|
|
// Get Customer with username or email
|
|
exports.getCustomersWithNameOrEmail = (req, res) => {
|
|
var params = req.query;
|
|
var condition = {};
|
|
const nameOrEmail = params?.nameOrEmail;
|
|
if (nameOrEmail) {
|
|
condition = { $or: [
|
|
{ email: nameOrEmail },
|
|
{ name: nameOrEmail }
|
|
]};
|
|
}
|
|
condition = splitSite.splitSiteGet(req, condition);
|
|
Customer.find(condition)
|
|
.then(data => {
|
|
res.send(data);
|
|
})
|
|
.catch(err => {
|
|
res.status(500).send({
|
|
message:
|
|
err.message || "Some error occurred while retrieving customers."
|
|
});
|
|
});
|
|
};
|