update the agent code for admin

This commit is contained in:
mahyargdz
2025-05-27 17:14:14 +03:30
parent d24481b598
commit 43e49f96f7
4 changed files with 139 additions and 12 deletions
+59 -2
View File
@@ -160,6 +160,24 @@
</CCardBody>
</CCard>
</CCol>
<CCol lg="12">
<CCard>
<CCardBody>
<h6>تغییر کد نمایندگی</h6>
<el-divider></el-divider>
<CForm @submit.prevent="updateAgentCode">
<CRow>
<CCol md="6">
<CInput v-model="newAgentCode" placeholder="کد نمایندگی جدید" />
</CCol>
<CCol md="6">
<CButton color="primary" type="submit"> ثبت کد جدید </CButton>
</CCol>
</CRow>
</CForm>
</CCardBody>
</CCard>
</CCol>
<CCol lg="6">
<CCard>
@@ -414,13 +432,14 @@ export default {
data() {
return {
title: 'مشاهده درخواست نمایندگی',
agent: null,
agent: {},
agentInbox: null,
agentBr: null,
agentOpr: null,
agentPr: null,
editFiles: '',
validation: {}
validation: {},
newAgentCode: ''
}
},
head() {
@@ -480,6 +499,11 @@ export default {
this.$nuxt.refresh()
}
},
mounted() {
if (this.agent && this.agent.agent_code) {
this.newAgentCode = this.agent.agent_code
}
},
methods: {
goToUpdatePage() {
const title = 'هشدار'
@@ -615,6 +639,39 @@ export default {
},
toggleEditFiles(fieldName) {
this.editFiles === fieldName ? (this.editFiles = '') : (this.editFiles = fieldName)
},
async updateAgentCode() {
try {
const title = 'هشدار'
const msg = 'آیا از تغییر کد نمایندگی اطمینان دارید؟'
await this.$confirm(msg, title, {
confirmButtonText: 'ثبت کد جدید',
cancelButtonText: 'لغو',
type: 'warning'
})
await this.$axios
.put(`/api/admin/updateAgentCode/${this.agent._id}`, { newAgentCode: this.newAgentCode })
.then(res => {
this.$message({
type: 'success',
message: res.data.message
})
this.$nuxt.refresh()
})
.catch(err => {
this.$message({
type: 'error',
message: err.response.data.message
})
})
} catch (e) {
this.$message({
type: 'warning',
message: 'عملیات لغو شد'
})
}
}
}
}
+4 -3
View File
@@ -122,11 +122,11 @@
:class="validation.first_name ? 'err' : null"
/>
<CInput
v-model="registerData.representation_code"
v-model="registerData.agent_code"
label="کد نماینده"
horizontal
:description="validation.representation_code ? validation.representation_code.msg : null"
:class="validation.representation_code ? 'err' : null"
:description="validation.agent_code ? validation.agent_code.msg : null"
:class="validation.agent_code ? 'err' : null"
/>
<CInput
v-model="registerData.last_name"
@@ -294,6 +294,7 @@ export default {
registerData: {
userID: '',
first_name: '',
agent_code: '',
last_name: '',
national_code: '',
province_id: '',
+75 -7
View File
@@ -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)
}
}
]
+1
View File
@@ -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(