Created
August 13, 2018 13:58
-
-
Save shierro/aeb5bad035ae468a0fb1432bcb696e71 to your computer and use it in GitHub Desktop.
slight refactor of https://gist.github.com/nimatullah/ead58b9ce7081a641f69f7366406b317
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
/* eslint-disable no-underscore-dangle, no-param-reassign */ | |
const UserPackage = require('../company/userPackages/userPackages.schema'); | |
const Training = require('../trainings/trainings.schema'); | |
const Request = require('../rfq/request/request.schema'); | |
const Job = require('../jobPosting/jobPosting.schema'); | |
const Company = require('../company/company.schema'); | |
const User = require('./user.schema'); | |
module.exports.mergeUser = (req, res) => { | |
const fromUserId = req.params.from; | |
const toUserId = req.params.to; | |
Promise.all([ | |
getCompanyId(fromUserId), | |
getCompanyId(toUserId), | |
getUserDetails(toUserId), | |
]) | |
.then(result => { | |
// res.status(200).json(result); | |
if (result.length > 0) { | |
const [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(result2 => res.status(200).json(result2)) | |
.catch(error => res.status(500).json(error)); | |
} | |
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, | |
}; | |
} | |
throw new Error('Company does not found'); | |
} catch (error) { | |
throw new Error(error); | |
} | |
} | |
function getUserDetails(userId) { | |
return User.findById(userId); | |
} | |
/** | |
* Merge jobs of company | |
* @param fromCompanyId | |
* @param fromUserId | |
* @param toCompanyId | |
* @param toUserDetails | |
*/ | |
function jobs(...params) { | |
const [ | |
fromCompanyDetails, | |
fromUserId, | |
toCompanyDetails, | |
toUserDetails, | |
] = params; | |
return Promise.all([ | |
mergeJobCompany(fromCompanyDetails, toCompanyDetails), | |
mergeJobUser(fromUserId, toUserDetails), | |
]); | |
} | |
function mergeJobCompany(fromCompanyDetails, toCompanyDetails) { | |
return Company.updateMany( | |
{ 'company.id': fromCompanyDetails._id }, | |
{ 'company.id': toCompanyDetails._id }, | |
); | |
} | |
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) { | |
const [fromUserId, toCompanyDetails, toUserDetails] = params; | |
return Promise.all([ | |
changePackageCustomer(fromUserId, toUserDetails, toCompanyDetails), | |
changePackageRequestedBy(fromUserId, toUserDetails), | |
]); | |
} | |
function changePackageCustomer(fromUserId, toUserDetails, toCompanyDetails) { | |
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, | |
}, | |
}, | |
}, | |
}, | |
); | |
} | |
/** Change package requeste by if the package requested by owner of company */ | |
function changePackageRequestedBy(fromUserId, toUserDetails) { | |
return UserPackage.updateMany( | |
{ | |
'requestedBy.id': fromUserId, | |
}, | |
{ | |
$set: { | |
requestedBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstName, | |
lastName: toUserDetails.lastName, | |
}, | |
}, | |
}, | |
); | |
} | |
/** | |
* Merge company data | |
* @param fromCompanyDetails | |
* @param toCompanyDetails | |
*/ | |
async function transferCompanyBusinessUnits( | |
fromCompanyDetails, | |
toCompanyDetails, | |
) { | |
try { | |
const fromCompany = await Company.findById(fromCompanyDetails._id); | |
if (fromCompany) { | |
const businessUnit = fromCompany.businessUnits.filter( | |
item => delete item._id, | |
); | |
const toCompany = await Company.findOneAndUpdate( | |
{ | |
_id: toCompanyDetails._id, | |
}, | |
{ | |
$addToSet: { | |
businessUnits: businessUnit, | |
}, | |
}, | |
); | |
return toCompany; | |
} | |
return false; | |
} 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), | |
]); | |
} | |
function transferRFQCompany(fromCompanyDetails, toCompanyDetails) { | |
return Request.updateMany( | |
{ | |
'company.id': fromCompanyDetails._id, | |
}, | |
{ | |
$set: { | |
company: { | |
id: toCompanyDetails._id, | |
name: toCompanyDetails.name, | |
logo: toCompanyDetails.logo, | |
}, | |
}, | |
}, | |
); | |
} | |
function replaceRFQCreatedBy(fromUserId, toUserDetails) { | |
return Request.updateMany( | |
{ | |
'createdBy.id': fromUserId, | |
}, | |
{ | |
$set: { | |
createdBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstMame, | |
lastName: toUserDetails.lastName, | |
}, | |
}, | |
}, | |
); | |
} | |
/** | |
* 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, | |
}, | |
}, | |
); | |
} | |
function replaceTrainingCreatedBy(fromUserId, toUserDetails) { | |
return Training.updateMany( | |
{ | |
'createdBy.id': fromUserId._id, | |
}, | |
{ | |
$set: { | |
createdBy: { | |
id: toUserDetails._id, | |
firstName: toUserDetails.firstName, | |
lastName: toUserDetails.lastName, | |
}, | |
}, | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment