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 || '', street_address_1: req.body.street_address_1 || '', city1: req.body.city1 || '', state1: req.body.state1 || '', zip_code1: req.body.zip_code1 || '', street_address_2: req.body.street_address_2 || '', city2: req.body.city2 || '', state2: req.body.state2 || '', zip_code2: req.body.zip_code2 || '', street_address_3: req.body.street_address_3 || '', city3: req.body.city3 || '', state3: req.body.state3 || '', zip_code3: req.body.zip_code3 || '', street_address_4: req.body.street_address_4 || '', city4: req.body.city4 || '', state4: req.body.state4 || '', zip_code4: req.body.zip_code4 || '', street_address_5: req.body.street_address_5 || '', city5: req.body.city5 || '', state5: req.body.state5 || '', zip_code5: req.body.zip_code5 || '', 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 || '', emergency_contact2: req.body.emergency_contact2 || '', emergency_contact_name: req.body.emergency_contact_name || '', emergency_contact_phone: req.body.emergency_contact_phone || '', emergency_contact_relationship: req.body.emergency_contact_relationship || '', emergency_contact2_name: req.body.emergency_contact2_name || '', emergency_contact2_phone: req.body.emergency_contact2_phone || '', emergency_contact2_relationship: req.body.emergency_contact2_relationship || '', 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, health_condition: String, allergy_info: req.body.allergy_info || '', meal_requirement: req.body.meal_requirement || '', service_requirement: req.body.service_requirement || '', payment_due_date: req.body.payment_due_date || '', payment_status: req.body.payment_status || '', join_reason: req.body.join_reason || '', discharge_reason: req.body.discharge_reason || '' }); // 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." }); }); };