|
| 1 | +import { Evoai, EvoaiSettings } from "@/types/evolution.types"; |
| 2 | + |
| 3 | +import { api } from "../api"; |
| 4 | +import { useManageMutation } from "../mutateQuery"; |
| 5 | + |
| 6 | +interface CreateEvoaiParams { |
| 7 | + instanceName: string; |
| 8 | + token: string; |
| 9 | + data: Evoai; |
| 10 | +} |
| 11 | + |
| 12 | +const createEvoai = async ({ instanceName, token, data }: CreateEvoaiParams) => { |
| 13 | + const response = await api.post(`/evoai/create/${instanceName}`, data, { |
| 14 | + headers: { apikey: token }, |
| 15 | + }); |
| 16 | + return response.data; |
| 17 | +}; |
| 18 | + |
| 19 | +interface UpdateEvoaiParams { |
| 20 | + instanceName: string; |
| 21 | + evoaiId: string; |
| 22 | + data: Evoai; |
| 23 | +} |
| 24 | +const updateEvoai = async ({ instanceName, evoaiId, data }: UpdateEvoaiParams) => { |
| 25 | + const response = await api.put( |
| 26 | + `/evoai/update/${evoaiId}/${instanceName}`, |
| 27 | + data, |
| 28 | + ); |
| 29 | + return response.data; |
| 30 | +}; |
| 31 | + |
| 32 | +interface DeleteEvoaiParams { |
| 33 | + instanceName: string; |
| 34 | + evoaiId: string; |
| 35 | +} |
| 36 | +const deleteEvoai = async ({ instanceName, evoaiId }: DeleteEvoaiParams) => { |
| 37 | + const response = await api.delete(`/evoai/delete/${evoaiId}/${instanceName}`); |
| 38 | + return response.data; |
| 39 | +}; |
| 40 | + |
| 41 | +interface SetDefaultSettingsEvoaiParams { |
| 42 | + instanceName: string; |
| 43 | + token: string; |
| 44 | + data: EvoaiSettings; |
| 45 | +} |
| 46 | +const setDefaultSettingsEvoai = async ({ |
| 47 | + instanceName, |
| 48 | + token, |
| 49 | + data, |
| 50 | +}: SetDefaultSettingsEvoaiParams) => { |
| 51 | + const response = await api.post(`/evoai/settings/${instanceName}`, data, { |
| 52 | + headers: { apikey: token }, |
| 53 | + }); |
| 54 | + return response.data; |
| 55 | +}; |
| 56 | + |
| 57 | +interface ChangeStatusEvoaiParams { |
| 58 | + instanceName: string; |
| 59 | + token: string; |
| 60 | + remoteJid: string; |
| 61 | + status: string; |
| 62 | +} |
| 63 | +const changeStatusEvoai = async ({ |
| 64 | + instanceName, |
| 65 | + token, |
| 66 | + remoteJid, |
| 67 | + status, |
| 68 | +}: ChangeStatusEvoaiParams) => { |
| 69 | + const response = await api.post( |
| 70 | + `/evoai/changeStatus/${instanceName}`, |
| 71 | + { remoteJid, status }, |
| 72 | + { headers: { apikey: token } }, |
| 73 | + ); |
| 74 | + return response.data; |
| 75 | +}; |
| 76 | + |
| 77 | +export function useManageEvoai() { |
| 78 | + const setDefaultSettingsEvoaiMutation = useManageMutation( |
| 79 | + setDefaultSettingsEvoai, |
| 80 | + { invalidateKeys: [["evoai", "fetchDefaultSettings"]] }, |
| 81 | + ); |
| 82 | + const changeStatusEvoaiMutation = useManageMutation(changeStatusEvoai, { |
| 83 | + invalidateKeys: [ |
| 84 | + ["evoai", "getEvoai"], |
| 85 | + ["evoai", "fetchSessions"], |
| 86 | + ], |
| 87 | + }); |
| 88 | + const deleteEvoaiMutation = useManageMutation(deleteEvoai, { |
| 89 | + invalidateKeys: [ |
| 90 | + ["evoai", "getEvoai"], |
| 91 | + ["evoai", "fetchEvoai"], |
| 92 | + ["evoai", "fetchSessions"], |
| 93 | + ], |
| 94 | + }); |
| 95 | + const updateEvoaiMutation = useManageMutation(updateEvoai, { |
| 96 | + invalidateKeys: [ |
| 97 | + ["evoai", "getEvoai"], |
| 98 | + ["evoai", "fetchEvoai"], |
| 99 | + ["evoai", "fetchSessions"], |
| 100 | + ], |
| 101 | + }); |
| 102 | + const createEvoaiMutation = useManageMutation(createEvoai, { |
| 103 | + invalidateKeys: [["evoai", "fetchEvoai"]], |
| 104 | + }); |
| 105 | + |
| 106 | + return { |
| 107 | + setDefaultSettingsEvoai: setDefaultSettingsEvoaiMutation, |
| 108 | + changeStatusEvoai: changeStatusEvoaiMutation, |
| 109 | + deleteEvoai: deleteEvoaiMutation, |
| 110 | + updateEvoai: updateEvoaiMutation, |
| 111 | + createEvoai: createEvoaiMutation, |
| 112 | + }; |
| 113 | +} |
0 commit comments