fix: activate/deactivate agent
(cherry picked from commit 0a836c78bb45a5e0eaa27142ea07fc7b063b5b09)
This commit is contained in:
parent
dd4f5c6f80
commit
a86884e40e
@ -13,8 +13,15 @@ class CRMAgent(Document):
|
||||
def update_agent_role(user, new_role):
|
||||
"""
|
||||
Update the role of the user to Agent
|
||||
:param user: The name of the user
|
||||
:param new_role: The new role to assign (Sales Manager or Sales User)
|
||||
"""
|
||||
|
||||
frappe.only_for("Sales Manager")
|
||||
|
||||
if new_role not in ["Sales Manager", "Sales User"]:
|
||||
frappe.throw("Cannot assign this role")
|
||||
|
||||
user_doc = frappe.get_doc("User", user)
|
||||
|
||||
if new_role == "Sales Manager":
|
||||
@ -25,3 +32,15 @@ def update_agent_role(user, new_role):
|
||||
user_doc.remove_roles("Sales Manager", "System Manager")
|
||||
|
||||
user_doc.save()
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def update_agent_status(agent, status):
|
||||
"""
|
||||
Activate or deactivate the agent
|
||||
:param agent: The name of the agent
|
||||
:param status: The status to set (1 for active, 0 for inactive)
|
||||
"""
|
||||
frappe.only_for("Sales Manager")
|
||||
|
||||
frappe.db.set_value("CRM Agent", agent, "is_active", status)
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<h2 class="flex gap-2 text-xl font-semibold leading-none h-5">
|
||||
{{ __('Agents') }}
|
||||
</h2>
|
||||
<div class="flex item-center space-x-2">
|
||||
<div class="flex item-center space-x-2 mr-2">
|
||||
<FormControl
|
||||
v-model="search"
|
||||
:placeholder="'Search'"
|
||||
@ -49,24 +49,41 @@
|
||||
>
|
||||
<div class="flex items-center">
|
||||
<Avatar :image="agent.image" :label="agent.agent_name" size="xl" />
|
||||
<div class="ml-3">
|
||||
<div class="text-base text-ink-gray-9">
|
||||
<div class="flex flex-col gap-1 ml-3">
|
||||
<div class="flex items-center gap-2 text-base text-ink-gray-9 h-4">
|
||||
{{ agent.agent_name }}
|
||||
<Badge
|
||||
v-if="!agent.is_active"
|
||||
variant="subtle"
|
||||
theme="gray"
|
||||
size="sm"
|
||||
label="Inactive"
|
||||
/>
|
||||
</div>
|
||||
<div class="mt-1 text-base text-ink-gray-7">
|
||||
<div class="text-base text-ink-gray-5">
|
||||
{{ agent.name }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Dropdown
|
||||
:options="getDropdownOptions(agent)"
|
||||
:button="{
|
||||
label: roleMap[getUserRole(agent.name)],
|
||||
iconRight: 'chevron-down',
|
||||
variant: 'ghost',
|
||||
}"
|
||||
placement="right"
|
||||
/>
|
||||
<div class="flex gap-1 items-center flex-row-reverse">
|
||||
<Dropdown
|
||||
:options="getMoreOptions(agent)"
|
||||
:button="{
|
||||
icon: 'more-horizontal',
|
||||
variant: 'ghost',
|
||||
}"
|
||||
placement="right"
|
||||
/>
|
||||
<Dropdown
|
||||
:options="getDropdownOptions(agent)"
|
||||
:button="{
|
||||
label: roleMap[getUserRole(agent.name)],
|
||||
iconRight: 'chevron-down',
|
||||
variant: 'ghost',
|
||||
}"
|
||||
placement="right"
|
||||
/>
|
||||
</div>
|
||||
</li>
|
||||
<!-- Load More Button -->
|
||||
<div class="flex justify-center">
|
||||
@ -86,7 +103,14 @@
|
||||
<script setup>
|
||||
import LucideCheck from '~icons/lucide/check'
|
||||
import { usersStore } from '@/stores/users'
|
||||
import { Avatar, createListResource, FormControl, toast, call } from 'frappe-ui'
|
||||
import {
|
||||
Avatar,
|
||||
Badge,
|
||||
createListResource,
|
||||
FormControl,
|
||||
toast,
|
||||
call,
|
||||
} from 'frappe-ui'
|
||||
import { ref, h, watch } from 'vue'
|
||||
|
||||
const { users, getUserRole } = usersStore()
|
||||
@ -94,8 +118,7 @@ const { users, getUserRole } = usersStore()
|
||||
const agents = createListResource({
|
||||
doctype: 'CRM Agent',
|
||||
cache: 'CRM Agents',
|
||||
fields: ['name', 'image', 'agent_name'],
|
||||
filters: { is_active: ['=', 1] },
|
||||
fields: ['name', 'image', 'is_active', 'agent_name'],
|
||||
auto: true,
|
||||
start: 0,
|
||||
pageLength: 20,
|
||||
@ -107,6 +130,25 @@ const roleMap = {
|
||||
'Sales User': __('Regular Access'),
|
||||
}
|
||||
|
||||
function getMoreOptions(agent) {
|
||||
let options = [
|
||||
{
|
||||
label: __('Activate'),
|
||||
icon: 'check-circle',
|
||||
onClick: () => updateStatus(agent, true),
|
||||
condition: () => !agent.is_active,
|
||||
},
|
||||
{
|
||||
label: __('Deactivate'),
|
||||
icon: 'x-circle',
|
||||
onClick: () => updateStatus(agent, false),
|
||||
condition: () => agent.is_active,
|
||||
},
|
||||
]
|
||||
|
||||
return options.filter((option) => option.condition())
|
||||
}
|
||||
|
||||
function getDropdownOptions(agent) {
|
||||
const agentRole = getUserRole(agent.name)
|
||||
return [
|
||||
@ -171,6 +213,24 @@ function updateRole(agent, newRole) {
|
||||
})
|
||||
}
|
||||
|
||||
function updateStatus(agent, status) {
|
||||
const currentStatus = agent.is_active
|
||||
if (currentStatus === status) return
|
||||
|
||||
call('crm.fcrm.doctype.crm_agent.crm_agent.update_agent_status', {
|
||||
agent: agent.name,
|
||||
status,
|
||||
}).then(() => {
|
||||
toast.success(
|
||||
__('{0} has been {1}', [
|
||||
agent.agent_name,
|
||||
status ? 'activated' : 'deactivated',
|
||||
]),
|
||||
)
|
||||
agents.reload()
|
||||
})
|
||||
}
|
||||
|
||||
const search = ref('')
|
||||
watch(search, (newValue) => {
|
||||
agents.filters = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user