main
ts 148 lines 3.55 KB
Raw
1 import type { DialogApiInjection } from "naive-ui/es/dialog/src/DialogProvider"
2 import type { MessageApiInjection } from "naive-ui/es/message/src/MessageProvider"
3 import type { Agent } from "@/types/agents.d"
4 import { h } from "vue"
5 import Api from "@/api"
6 import dayjs from "@/utils/dayjs"
7
8 export function isAgentOnline(lastSeen: string) {
9 const lastSeenDate = dayjs(lastSeen)
10 if (!lastSeenDate.isValid()) return false
11 if (!dayjs().isAfter(lastSeenDate)) return false
12
13 return lastSeenDate.isAfter(dayjs().subtract(1, "h"))
14 }
15
16 export interface ToggleAgentCriticalParams {
17 agentId: string
18 criticalStatus: boolean
19 cbBefore?: () => void
20 cbSuccess?: () => void
21 cbAfter?: () => void
22 cbError?: () => void
23 message: MessageApiInjection
24 }
25
26 export function toggleAgentCritical({
27 agentId,
28 criticalStatus,
29 cbBefore,
30 cbSuccess,
31 cbAfter,
32 cbError,
33 message
34 }: ToggleAgentCriticalParams) {
35 if (cbBefore && typeof cbBefore === "function") {
36 cbBefore()
37 }
38 const method = criticalStatus ? "markNonCritical" : "markCritical"
39
40 Api.agents[method](agentId)
41 .then(res => {
42 if (res.data.success) {
43 message.success("Agent Criticality Updated Successfully")
44
45 if (cbSuccess && typeof cbSuccess === "function") {
46 cbSuccess()
47 }
48 } else {
49 message.error(res.data?.message || "Failed to Update Agent Criticality.")
50
51 if (cbError && typeof cbError === "function") {
52 cbError()
53 }
54 }
55 })
56 .catch(err => {
57 if (err.response?.status === 401) {
58 message.error(err.response?.data?.message || "Agent Criticality Update returned Unauthorized.")
59 } else {
60 message.error(err.response?.data?.message || "Failed to Update Agent Criticality")
61 }
62
63 if (cbError && typeof cbError === "function") {
64 cbError()
65 }
66 })
67 .finally(() => {
68 if (cbAfter && typeof cbAfter === "function") {
69 cbAfter()
70 }
71 })
72 }
73
74 export interface DeleteAgentParams {
75 agent: Agent
76 cbBefore?: () => void
77 cbSuccess?: () => void
78 cbAfter?: () => void
79 cbError?: () => void
80 message: MessageApiInjection
81 dialog: DialogApiInjection
82 }
83
84 export function handleDeleteAgent({
85 agent,
86 cbBefore,
87 cbSuccess,
88 cbAfter,
89 cbError,
90 dialog,
91 message
92 }: DeleteAgentParams) {
93 dialog.warning({
94 title: "Confirm",
95 content: () =>
96 h("div", {
97 innerHTML: `Are you sure you want to delete the Agent:<br/><strong>${agent.hostname}</strong> ?`
98 }),
99 positiveText: "Yes I'm sure",
100 negativeText: "Cancel",
101 onPositiveClick: () => {
102 deleteAgent({ agent, cbBefore, cbSuccess, cbAfter, cbError, dialog, message })
103 },
104 onNegativeClick: () => {
105 message.info("Delete canceled")
106 }
107 })
108 }
109
110 export function deleteAgent({ agent, cbBefore, cbSuccess, cbAfter, cbError, message }: DeleteAgentParams) {
111 if (cbBefore && typeof cbBefore === "function") {
112 cbBefore()
113 }
114
115 Api.agents
116 .deleteAgent(agent.agent_id)
117 .then(res => {
118 if (res.data.success) {
119 message.success(res.data?.message || "Agent was successfully deleted.")
120
121 if (cbSuccess && typeof cbSuccess === "function") {
122 cbSuccess()
123 }
124 } else {
125 message.error(res.data?.message || "An error occurred. Please try again later.")
126
127 if (cbError && typeof cbError === "function") {
128 cbError()
129 }
130 }
131 })
132 .catch(err => {
133 if (err.response?.status === 401) {
134 message.error(err.response?.data?.message || "Agent Delete returned Unauthorized.")
135 } else {
136 message.error(err.response?.data?.message || "An error occurred. Please try again later.")
137 }
138
139 if (cbError && typeof cbError === "function") {
140 cbError()
141 }
142 })
143 .finally(() => {
144 if (cbAfter && typeof cbAfter === "function") {
145 cbAfter()
146 }
147 })
148 }