This is the code I use to save
export const createExpenseReport = (report) => new Promise((resolve) => {
const newExpenseReport = new ExpenseReport(report);
newExpenseReport.save((err, doc) => {
if (err) throw err;
console.log("document was inserted %s", doc);
resolve(doc);
});
});
this is the connection:
export const ExpenseReport = conn_expenses.model("ExpenseReport", expenseReportSchema);
This is the part where I format the obj to be saved into the database:
const expenses = await getExpenses();
// here maniputate MSG and data from db expenses, advancePayments and user controller(from user machine) to create a report then post it to db expense reports
const addToTotalExpenses = (obj, allExpenses) => {
if (obj.totalVacations.length > 0) {
obj.totalVacations.forEach((vacationExpense) => {
const monthNum = moment(vacationExpense.date[0]).month();
const storedAmount = allExpenses[monthNum];
storedAmount[monthNum + 1] =
parseInt(storedAmount[monthNum + 1]) +
parseInt(vacationExpense.accommodationFees);
});
} else {
obj.totalExpenses.forEach((otherExpense) => {
const monthNum = moment(otherExpense.date).month();
storedAmount[monthNum + 1] =
parseInt(storedAmount[monthNum + 1]) +
parseInt(otherExpense.expense);
});
}
};
const createAnnualExpense = (expenseObj) => {
// the keys are the months.Ex. january: 0 => 1: 0
const allExpenses = [
{ 1: 0 },
{ 2: 0 },
{ 3: 0 },
{ 4: 0 },
{ 5: 0 },
{ 6: 0 },
{ 7: 0 },
{ 8: 0 },
{ 9: 0 },
{ 10: 0 },
{ 11: 0 },
{ 12: 0 },
];
expenses.forEach((expense) => {
addToTotalExpenses(expense, allExpenses);
});
// if (expenseObj.category === "vacationExpenses") {
// addToTotalExpenses(expenseObj, allExpenses);
// return allExpenses;
// }
return allExpenses;
};
// const totalExpenses = createAnnualExpense(expenseObj);
const totalExpenses = createAnnualExpense();
// employee gender
const employees = await getEmployees();
const genderDistribution = employees.reduce(
(genders, person) => {
genders[person.gender]++;
return genders;
},
{ male: 0, female: 0, other: 0 }
);
// contract types
const contractDistribution = employees.reduce(
(contracts, person) => {
if(person.contract) contracts[person.contract]++;
return contracts;
},
{ permanent: 0, temporary: 0 }
);
// employmentStatus
const employmentStatusDistribution = employees.reduce(
(employmentType, person) => {
if(person.employmentType) employmentType[person.employmentType]++;
return employmentType;
},
{ student: 0, fullTime: 0, partTime: 0 }
);
// averangeExpenses
const createAverangeExpenses = ()=>{
const allExpenses = [
{ 1: 0, quantity:0, mean: 0 },
{ 2: 0, quantity:0, mean: 0 },
{ 3: 0, quantity:0, mean: 0 },
{ 4: 0, quantity:0, mean: 0 },
{ 5: 0, quantity:0, mean: 0 },
{ 6: 0, quantity:0, mean: 0 },
{ 7: 0, quantity:0, mean: 0 },
{ 8: 0, quantity:0, mean: 0 },
{ 9: 0, quantity:0, mean: 0 },
{ 10: 0, quantity:0, mean: 0 },
{ 11: 0, quantity:0, mean: 0 },
{ 12: 0, quantity:0, mean: 0 },
];
const addToAverangeExpenses = (obj, allExpenses) => {
if (obj.totalVacations.length > 0) {
obj.totalVacations.forEach((vacationExpense) => {
const monthNum = moment(vacationExpense.date[0]).month();
const storedMonthInfo = allExpenses[monthNum];
storedMonthInfo[monthNum + 1] =
parseInt(allExpenses[monthNum][monthNum + 1]) +
parseInt(vacationExpense.accommodationFees);
storedMonthInfo.quantity ++;
storedMonthInfo.mean = storedMonthInfo[monthNum + 1]/storedMonthInfo.quantity;
});
} else {
obj.totalExpenses.forEach((otherExpense) => {
const monthNum = moment(otherExpense.date).month();
const storedMonthInfo = allExpenses[monthNum];
storedMonthInfo[monthNum + 1] =
parseInt(allExpenses[monthNum][monthNum + 1]) +
parseInt(otherExpense.expense);
storedMonthInfo.quantity ++;
storedMonthInfo.mean = storedMonthInfo[monthNum + 1]/storedMonthInfo.quantity;
});
}
return allExpenses
};
expenses.forEach((expense) => {
addToAverangeExpenses(expense, allExpenses);
});
return allExpenses;
}
const averangeExpenses = createAverangeExpenses();
// salary info should come from the database, the employeesSalaries array is temporary
const employeesSalaries =[ {
person: "Anna Bassi",
salary: "1000 EU"
}, {
person: "Marco AA",
salary: "1000 EU"
}, {
person: "Anna ddd",
salary: "200 EU"
}]
// totalSalary
const getTotalSalary = (employeesSalaries=[])=> employeesSalaries.reduce((total, person)=>total += parseInt(person.salary.split(" ")[0]), 0);
// averangeSalary
const getAverangeSalary = (employeesSalaries=[])=> employeesSalaries.reduce((total, person)=>total += parseInt(person.salary.split(" ")[0])/(employeesSalaries.length-1), 0);
const report = {
totalExpenses: totalExpenses,
averangeExpenses,
employeeGender: genderDistribution,
typeOfContracts: contractDistribution,
employmentStatus: employmentStatusDistribution,
totalSalary: getTotalSalary(employeesSalaries),
averangeSalary: getAverangeSalary(employeesSalaries)
};
console.log("report===>", report)
createExpenseReport(report)