Created
August 13, 2018 11:02
-
-
Save nimatrazmjo/ead58b9ce7081a641f69f7366406b317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let UserPackage = require('../company/userPackages/userPackages.schema'); | |
let Training = require('../trainings/trainings.schema'); | |
let Request = require('../rfq/request/request.schema'); | |
let Job = require('../jobPosting/jobPosting.schema'); | |
let Company = require('../company/company.schema'); | |
let User = require('./user.schema'); | |
module.exports.mergeUser = (req, res) => { | |
let fromUserId = req.params.from; | |
let toUserId = req.params.to; | |
Promise.all([getCompanyId(fromUserId), getCompanyId(toUserId), getUserDetails(toUserId)]) | |
.then(result => { | |
// res.status(200).json(result); | |
if (result.length > 0) { | |
let [fromCompanyDetails, toCompanyDetails, toUserDetails] = result; | |
return Promise.all([ | |
// jobs(fromCompanyDetails, fromUserId, toCompanyDetails, toUserDetails), | |
transferCompanyBusinessUnits(fromCompanyDetails, toCompanyDetails), | |
userPackage(fromUserId, toCompanyDetails, toUserDetails), | |
transferFRQ(fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails), | |
transferTrainings(fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails) | |
]) | |
.then(result => res.status(200).json(result)) | |
.catch(error => res.status(500).json(error)); | |
} else { | |
return res.status(500).json({message: 'Companies Id could not found'}); | |
} | |
}) | |
.catch(err => res.status(500).json(err)); | |
}; | |
async function getCompanyId (userId) { | |
try { | |
const company = await Company.findOne({"owner.id": userId}); | |
if (company) { | |
return {_id: company._id, name: company.name, description: company.description, address: company.address, logo: company.profilePic}; | |
} else { | |
throw new Error('Company does not found'); | |
} | |
} catch (error) { | |
throw new Error(error); | |
} | |
} | |
function getUserDetails (userId) { | |
return new Promise((resolve, reject) => { | |
User.findById(userId) | |
.then(result => resolve({_id: result._id, firstName: result.firstName, lastName: result.lastName})) | |
.catch(error => reject(error)); | |
}); | |
} | |
/** | |
* Merge jobs of company | |
* @param fromCompanyId | |
* @param fromUserId | |
* @param toCompanyId | |
* @param toUserDetails | |
*/ | |
function jobs (...params) { | |
let [fromCompanyDetails, fromUserId, toCompanyDetails, toUserDetails] = params; | |
return Promise.all([mergeJobCompany(fromCompanyDetails, toCompanyDetails), mergeJobUser(fromUserId, toUserDetails)]); | |
// .then(result => result) | |
// .catch(err => err); | |
} | |
function mergeJobCompany (fromCompanyDetails, toCompanyDetails) { | |
// return new Promise((resolve, reject) => { | |
return Company.updateMany({"company.id": fromCompanyDetails._id}, {"company.id": toCompanyDetails._id}); | |
// .then(result => resolve(result)) | |
// .catch(err => reject(err)); | |
// }); | |
} | |
async function mergeJobUser (fromUserId, toUserDetails) { | |
try { | |
await Job.updateMany({ | |
"publishedBy.id": fromUserId | |
}, { | |
"publishedBy.id": toUserDetails._id, | |
"publishedBy.firstName": toUserDetails.firstName, | |
"publishedBy.lastName": toUserDetails.lastName | |
}); | |
await Job.updateMany({ | |
"createdBy.id": fromUserId | |
}, { | |
"createdBy.id": toUserDetails._id, | |
"createdBy.firstName": toUserDetails.firstName, | |
"createdBy.lastName": toUserDetails.lastName | |
}); | |
await Job.updateMany({ | |
"deletedBy.id": fromUserId | |
}, { | |
"deletedBy.id": toUserDetails._id, | |
"deletedBy.firstName": toUserDetails.firstName, | |
"deletedBy.lastName": toUserDetails.lastName | |
}); | |
return true; | |
} catch (error) { | |
throw new Error(error); | |
} | |
} | |
/** | |
* Merge jobs of credits | |
* @param fromUserId | |
* @param toCompanyId | |
* @param toUserDetails | |
*/ | |
function userPackage (...params) { | |
let [fromUserId, toCompanyDetails, toUserDetails] = params; | |
return Promise.all([ | |
changePackageCustomer(fromUserId, toUserDetails, toCompanyDetails), | |
changePackageRequestedBy(fromUserId, toUserDetails) | |
]) | |
.then(result => result) | |
.catch(error => error); | |
} | |
function changePackageCustomer (fromUserId, toUserDetails, toCompanyDetails) { | |
// return new Promise((resolve, reject) => { | |
return UserPackage.updateMany({ | |
"customer.id": fromUserId | |
}, { | |
$set: { | |
customer: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstName, | |
lastName: toUserDetails.lastName, | |
company: { | |
id: toCompanyDetails._id, | |
name: toCompanyDetails.name, | |
address: toCompanyDetails.address | |
} | |
} | |
} | |
}); | |
// .then(result => resolve(result)) | |
// .catch(error => reject(error)); | |
// }); | |
} | |
/** Change package requeste by if the package requested by owner of company */ | |
function changePackageRequestedBy (fromUserId, toUserDetails) { | |
// return new Promise((resolve, reject) => { | |
return UserPackage.updateMany({ | |
"requestedBy.id": fromUserId | |
}, { | |
$set: { | |
requestedBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstName, | |
lastName: toUserDetails.lastName | |
} | |
} | |
}); | |
// .then(result => resolve(result)) | |
// .catch(error => reject(error)); | |
// }); | |
} | |
/** | |
* Merge company data | |
* @param fromCompanyDetails | |
* @param toCompanyDetails | |
*/ | |
async function transferCompanyBusinessUnits (fromCompanyDetails, toCompanyDetails) { | |
try { | |
const fromCompany = await Company.findById(fromCompanyDetails._id); | |
if (fromCompany) { | |
let businessUnit = fromCompany.businessUnits.filter(item => delete item._id); | |
const toCompany = await Company.findOneAndUpdate({ | |
_id: toCompanyDetails._id | |
}, { | |
$addToSet: { | |
businessUnits: businessUnit | |
} | |
}); | |
return toCompany; | |
} | |
} catch (error) { | |
throw new Error(error); | |
} | |
} | |
/** | |
* Merge RFQ's | |
* @param fromUserId | |
* @param fromCompanyDetails | |
* @param toUserDetails | |
* @param toCompanyDetails | |
*/ | |
function transferFRQ (fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails) { | |
return Promise.all([ | |
transferRFQCompany(fromCompanyDetails, toCompanyDetails), | |
replaceRFQCreatedBy(fromUserId, toUserDetails) | |
]); | |
// .then(result => Promise.resolve(result)) | |
// .catch(error => Promise.reject(error)); | |
} | |
function transferRFQCompany (fromCompanyDetails, toCompanyDetails) { | |
// return new Promise((resolve, reject) => { | |
return Request.updateMany({ | |
"company.id": fromCompanyDetails._id | |
}, { | |
$set: { | |
company: { | |
id: toCompanyDetails._id, | |
name: toCompanyDetails.name, | |
logo: toCompanyDetails.logo | |
} | |
} | |
}); | |
// .then(result => resolve(result)) | |
// .catch(error => reject(error)); | |
// }); | |
} | |
function replaceRFQCreatedBy (fromUserId, toUserDetails) { | |
// return new Promise((resolve, reject) => { | |
return Request.updateMany({ | |
"createdBy.id": fromUserId | |
}, { | |
$set: { | |
createdBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstMame, | |
lastName: toUserDetails.lastName | |
} | |
} | |
}); | |
// .then(result => resolve(result)) | |
// .catch(error => reject(error)); | |
// }); | |
} | |
/** | |
* Merge Trainings | |
* @param fromUserId | |
* @param fromCompanyDetails | |
* @param toUserDetails | |
* @param toCompanyDetails | |
*/ | |
function transferTrainings (fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails) { | |
return Promise.all([ | |
transferTrainingCompany(fromCompanyDetails, toCompanyDetails), | |
replaceTrainingCreatedBy(fromUserId, toUserDetails) | |
]); | |
} | |
function transferTrainingCompany (fromCompanyDetails, toCompanyDetails) { | |
return Training.updateMany({ | |
"company.id": fromCompanyDetails._id | |
}, { | |
$set: { | |
"company.id": toCompanyDetails._id, | |
"company.name": toCompanyDetails.name, | |
"company.logo": toCompanyDetails.logo | |
} | |
});f | |
} | |
function replaceTrainingCreatedBy (fromUserId, toUserDetails) { | |
return Training.updateMany({ | |
"createdBy.id": fromUserId._id | |
}, { | |
$set: { | |
createdBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstName, | |
lastName: toUserDetails.lastName | |
} | |
} | |
}); | |
} |
Hey @nimatullah if the 1st question is answered for you, please mark is as accepted answer
https://stackoverflow.com/questions/51818804/promise-all-response
here's the refactored version of your code
https://gist.github.com/shierro/aeb5bad035ae468a0fb1432bcb696e71
there's a lot of typo & extra commas on your snippet, i have cleaned it up for you :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @nimatullah i'll try to refactor your code. basically, you can apply same principle that we applied to function
transferFRQ