| 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 { CustomerIntegration } from "@/types/integrations.d" |
| 4 | import { h } from "vue" |
| 5 | import Api from "@/api" |
| 6 | |
| 7 | export interface DeleteIntegrationParams { |
| 8 | integration: CustomerIntegration |
| 9 | cbBefore?: () => void |
| 10 | cbSuccess?: () => void |
| 11 | cbAfter?: () => void |
| 12 | cbError?: () => void |
| 13 | message: MessageApiInjection |
| 14 | dialog: DialogApiInjection |
| 15 | } |
| 16 | |
| 17 | export function handleDeleteIntegration({ |
| 18 | integration, |
| 19 | cbBefore, |
| 20 | cbSuccess, |
| 21 | cbAfter, |
| 22 | cbError, |
| 23 | dialog, |
| 24 | message |
| 25 | }: DeleteIntegrationParams) { |
| 26 | dialog.warning({ |
| 27 | title: "Confirm", |
| 28 | content: () => |
| 29 | h("div", { |
| 30 | innerHTML: `Are you sure you want to delete the integration: <strong>${integration.integration_service_name}</strong> ?` |
| 31 | }), |
| 32 | positiveText: "Yes I'm sure", |
| 33 | negativeText: "Cancel", |
| 34 | onPositiveClick: () => { |
| 35 | deleteIntegration({ integration, cbBefore, cbSuccess, cbAfter, cbError, dialog, message }) |
| 36 | }, |
| 37 | onNegativeClick: () => { |
| 38 | message.info("Delete canceled") |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | export function deleteIntegration({ |
| 44 | integration, |
| 45 | cbBefore, |
| 46 | cbSuccess, |
| 47 | cbAfter, |
| 48 | cbError, |
| 49 | message |
| 50 | }: DeleteIntegrationParams) { |
| 51 | if (cbBefore && typeof cbBefore === "function") { |
| 52 | cbBefore() |
| 53 | } |
| 54 | |
| 55 | Api.integrations |
| 56 | .deleteIntegration(integration.customer_code, integration.integration_service_name) |
| 57 | .then(res => { |
| 58 | if (res.data.success) { |
| 59 | message.success(res.data?.message || "Customer integration successfully deleted.") |
| 60 | |
| 61 | if (cbSuccess && typeof cbSuccess === "function") { |
| 62 | cbSuccess() |
| 63 | } |
| 64 | } else { |
| 65 | message.error(res.data?.message || "An error occurred. Please try again later.") |
| 66 | |
| 67 | if (cbError && typeof cbError === "function") { |
| 68 | cbError() |
| 69 | } |
| 70 | } |
| 71 | }) |
| 72 | .catch(err => { |
| 73 | if (err.response?.status === 401) { |
| 74 | message.error(err.response?.data?.message || "Agent Delete returned Unauthorized.") |
| 75 | } else { |
| 76 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 77 | } |
| 78 | |
| 79 | if (cbError && typeof cbError === "function") { |
| 80 | cbError() |
| 81 | } |
| 82 | }) |
| 83 | .finally(() => { |
| 84 | if (cbAfter && typeof cbAfter === "function") { |
| 85 | cbAfter() |
| 86 | } |
| 87 | }) |
| 88 | } |