update the agent code for admin
This commit is contained in:
@@ -733,8 +733,23 @@ module.exports.change_form_status = [
|
||||
representation.editAccess = false
|
||||
user.isAgent = true
|
||||
user.representation_id = representation._id
|
||||
const agentsCount = await Representation.find({ status: 'accepted' })
|
||||
representation.agent_code = 'ASG' + agentsCount.length.toString().padStart(2, '0')
|
||||
// Use agent_code from request if provided, otherwise generate a new one
|
||||
if (req.body.agent_code) {
|
||||
// Check if the agent code already exists
|
||||
const existingRepresentation = await Representation.findOne({
|
||||
agent_code: req.body.agent_code,
|
||||
_id: { $ne: representation._id }
|
||||
})
|
||||
|
||||
if (existingRepresentation) {
|
||||
return res406(res, 'این کد نمایندگی قبلا برای نماینده دیگری استفاده شده است')
|
||||
}
|
||||
|
||||
representation.agent_code = req.body.agent_code
|
||||
} else {
|
||||
const agentsCount = await Representation.find({ status: 'accepted' })
|
||||
representation.agent_code = 'ASG' + agentsCount.length.toString().padStart(2, '0')
|
||||
}
|
||||
}
|
||||
|
||||
// save changes
|
||||
@@ -1077,7 +1092,8 @@ module.exports.update_representation_by_admin = [
|
||||
computer_saling_exp,
|
||||
computer_saling_description,
|
||||
other_representations,
|
||||
more_descriptions
|
||||
more_descriptions,
|
||||
agent_code
|
||||
} = req.body
|
||||
const data = {
|
||||
_updatedBy: user_id,
|
||||
@@ -1125,9 +1141,14 @@ module.exports.update_representation_by_admin = [
|
||||
computer_saling_exp,
|
||||
computer_saling_description,
|
||||
other_representations,
|
||||
more_descriptions
|
||||
more_descriptions,
|
||||
agent_code
|
||||
}
|
||||
try {
|
||||
// Check if agent_code is provided in the request body
|
||||
if (agent_code) {
|
||||
data.agent_code = agent_code
|
||||
}
|
||||
await Representation.findByIdAndUpdate(representation_id, data)
|
||||
return res.json({ message: 'با موفقیت آپدیت شد' })
|
||||
} catch (e) {
|
||||
@@ -1232,9 +1253,22 @@ module.exports.add_agent_by_admin = [
|
||||
moment(Date.now()).format('jMMM').toUpperCase() +
|
||||
allRepresentations.length.toString().padStart(4, '0')
|
||||
|
||||
// generate agent code with new format
|
||||
const agentsCount = await Representation.find({ status: 'accepted' })
|
||||
data.agent_code = 'ASG' + moment(Date.now()).format('YY') + agentsCount.length.toString().padStart(3, '0')
|
||||
// generate agent code with new format or use the one provided by admin
|
||||
if (req.body.agent_code) {
|
||||
// Check if the agent code already exists
|
||||
const existingRepresentation = await Representation.findOne({
|
||||
agent_code: req.body.agent_code
|
||||
})
|
||||
|
||||
if (existingRepresentation) {
|
||||
return res406(res, 'این کد نمایندگی قبلا برای نماینده دیگری استفاده شده است')
|
||||
}
|
||||
|
||||
data.agent_code = req.body.agent_code
|
||||
} else {
|
||||
const agentsCount = await Representation.find({ status: 'accepted' })
|
||||
data.agent_code = 'ASG' + moment(Date.now()).format('YY') + agentsCount.length.toString().padStart(3, '0')
|
||||
}
|
||||
|
||||
data.representation_code = representation_code
|
||||
|
||||
@@ -1270,3 +1304,37 @@ module.exports.add_agent_by_admin = [
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.update_agent_code = [
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { user_id } = req
|
||||
const representation_id = req.params?.id
|
||||
const { newAgentCode } = req.body
|
||||
|
||||
if (!user_id || !representation_id) return res406(res, 'invalid user id')
|
||||
if (!newAgentCode) return res406(res, 'کد نمایندگی جدید الزامی است')
|
||||
|
||||
// Check if the agent code already exists in another representation
|
||||
const existingRepresentation = await Representation.findOne({
|
||||
agent_code: newAgentCode,
|
||||
_id: { $ne: representation_id }
|
||||
})
|
||||
|
||||
if (existingRepresentation) {
|
||||
return res406(res, 'این کد نمایندگی قبلا برای نماینده دیگری استفاده شده است')
|
||||
}
|
||||
|
||||
const representation = await Representation.findById(representation_id)
|
||||
if (!representation) return res404(res, 'نماینده مورد نظر یافت نشد')
|
||||
|
||||
representation.agent_code = newAgentCode
|
||||
representation._updatedBy = user_id
|
||||
await representation.save()
|
||||
|
||||
return res.json({ message: 'کد نمایندگی با موفقیت بروزرسانی شد' })
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -231,6 +231,7 @@ router.get('/agentPr/:agent_id', hasPermission('agents'), pieceRequestController
|
||||
router.put('/updateAgent/:id', hasPermission('agentUpdate'), representationController.update_representation_by_admin)
|
||||
router.post('/revokeAgent/:id', hasPermission('agentUpdate'), representationController.revoke_representation_by_admin)
|
||||
router.get('/revokedAgents', representationController.get_revoked_agents_for_admin)
|
||||
router.put('/updateAgentCode/:id', hasPermission('agentUpdate'), representationController.update_agent_code)
|
||||
|
||||
// update files
|
||||
router.post(
|
||||
|
||||
Reference in New Issue
Block a user