fix: activate/deactivate agent
This commit is contained in:
parent
5c7f835e4c
commit
0a836c78bb
@ -13,8 +13,15 @@ class CRMAgent(Document):
|
|||||||
def update_agent_role(user, new_role):
|
def update_agent_role(user, new_role):
|
||||||
"""
|
"""
|
||||||
Update the role of the user to Agent
|
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)
|
user_doc = frappe.get_doc("User", user)
|
||||||
|
|
||||||
if new_role == "Sales Manager":
|
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.remove_roles("Sales Manager", "System Manager")
|
||||||
|
|
||||||
user_doc.save()
|
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">
|
<h2 class="flex gap-2 text-xl font-semibold leading-none h-5">
|
||||||
{{ __('Agents') }}
|
{{ __('Agents') }}
|
||||||
</h2>
|
</h2>
|
||||||
<div class="flex item-center space-x-2">
|
<div class="flex item-center space-x-2 mr-2">
|
||||||
<FormControl
|
<FormControl
|
||||||
v-model="search"
|
v-model="search"
|
||||||
:placeholder="'Search'"
|
:placeholder="'Search'"
|
||||||
@ -49,24 +49,41 @@
|
|||||||
>
|
>
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<Avatar :image="agent.image" :label="agent.agent_name" size="xl" />
|
<Avatar :image="agent.image" :label="agent.agent_name" size="xl" />
|
||||||
<div class="ml-3">
|
<div class="flex flex-col gap-1 ml-3">
|
||||||
<div class="text-base text-ink-gray-9">
|
<div class="flex items-center gap-2 text-base text-ink-gray-9 h-4">
|
||||||
{{ agent.agent_name }}
|
{{ agent.agent_name }}
|
||||||
|
<Badge
|
||||||
|
v-if="!agent.is_active"
|
||||||
|
variant="subtle"
|
||||||
|
theme="gray"
|
||||||
|
size="sm"
|
||||||
|
label="Inactive"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-1 text-base text-ink-gray-7">
|
<div class="text-base text-ink-gray-5">
|
||||||
{{ agent.name }}
|
{{ agent.name }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Dropdown
|
<div class="flex gap-1 items-center flex-row-reverse">
|
||||||
:options="getDropdownOptions(agent)"
|
<Dropdown
|
||||||
:button="{
|
:options="getMoreOptions(agent)"
|
||||||
label: roleMap[getUserRole(agent.name)],
|
:button="{
|
||||||
iconRight: 'chevron-down',
|
icon: 'more-horizontal',
|
||||||
variant: 'ghost',
|
variant: 'ghost',
|
||||||
}"
|
}"
|
||||||
placement="right"
|
placement="right"
|
||||||
/>
|
/>
|
||||||
|
<Dropdown
|
||||||
|
:options="getDropdownOptions(agent)"
|
||||||
|
:button="{
|
||||||
|
label: roleMap[getUserRole(agent.name)],
|
||||||
|
iconRight: 'chevron-down',
|
||||||
|
variant: 'ghost',
|
||||||
|
}"
|
||||||
|
placement="right"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</li>
|
</li>
|
||||||
<!-- Load More Button -->
|
<!-- Load More Button -->
|
||||||
<div class="flex justify-center">
|
<div class="flex justify-center">
|
||||||
@ -86,7 +103,14 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import LucideCheck from '~icons/lucide/check'
|
import LucideCheck from '~icons/lucide/check'
|
||||||
import { usersStore } from '@/stores/users'
|
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'
|
import { ref, h, watch } from 'vue'
|
||||||
|
|
||||||
const { users, getUserRole } = usersStore()
|
const { users, getUserRole } = usersStore()
|
||||||
@ -94,8 +118,7 @@ const { users, getUserRole } = usersStore()
|
|||||||
const agents = createListResource({
|
const agents = createListResource({
|
||||||
doctype: 'CRM Agent',
|
doctype: 'CRM Agent',
|
||||||
cache: 'CRM Agents',
|
cache: 'CRM Agents',
|
||||||
fields: ['name', 'image', 'agent_name'],
|
fields: ['name', 'image', 'is_active', 'agent_name'],
|
||||||
filters: { is_active: ['=', 1] },
|
|
||||||
auto: true,
|
auto: true,
|
||||||
start: 0,
|
start: 0,
|
||||||
pageLength: 20,
|
pageLength: 20,
|
||||||
@ -107,6 +130,25 @@ const roleMap = {
|
|||||||
'Sales User': __('Regular Access'),
|
'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) {
|
function getDropdownOptions(agent) {
|
||||||
const agentRole = getUserRole(agent.name)
|
const agentRole = getUserRole(agent.name)
|
||||||
return [
|
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('')
|
const search = ref('')
|
||||||
watch(search, (newValue) => {
|
watch(search, (newValue) => {
|
||||||
agents.filters = {
|
agents.filters = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user