|
| 1 | +import axios from 'axios'; |
| 2 | +import { getAxiosConfig, processAuthError } from './auth'; |
| 3 | +import { API_BASE } from '../apiBase'; |
| 4 | + |
| 5 | +const getPush = async ( |
| 6 | + id: string, |
| 7 | + setIsLoading: (isLoading: boolean) => void, |
| 8 | + setData: (data: any) => void, |
| 9 | + setAuth: (auth: boolean) => void, |
| 10 | + setIsError: (isError: boolean) => void, |
| 11 | +): Promise<void> => { |
| 12 | + const url = `${API_BASE}/push/${id}`; |
| 13 | + setIsLoading(true); |
| 14 | + |
| 15 | + try { |
| 16 | + const response = await axios(url, getAxiosConfig()); |
| 17 | + const data = response.data; |
| 18 | + data.diff = data.steps.find((x: any) => x.stepName === 'diff'); |
| 19 | + setData(data); |
| 20 | + } catch (error: any) { |
| 21 | + if (error.response?.status === 401) setAuth(false); |
| 22 | + else setIsError(true); |
| 23 | + } finally { |
| 24 | + setIsLoading(false); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const getPushes = async ( |
| 29 | + setIsLoading: (isLoading: boolean) => void, |
| 30 | + setData: (data: any) => void, |
| 31 | + setAuth: (auth: boolean) => void, |
| 32 | + setIsError: (isError: boolean) => void, |
| 33 | + setErrorMessage: (errorMessage: string) => void, |
| 34 | + query = { |
| 35 | + blocked: true, |
| 36 | + canceled: false, |
| 37 | + authorised: false, |
| 38 | + rejected: false, |
| 39 | + }, |
| 40 | +): Promise<void> => { |
| 41 | + const url = new URL(`${API_BASE}/push`); |
| 42 | + url.search = new URLSearchParams(query as any).toString(); |
| 43 | + |
| 44 | + setIsLoading(true); |
| 45 | + |
| 46 | + try { |
| 47 | + const response = await axios(url.toString(), getAxiosConfig()); |
| 48 | + setData(response.data); |
| 49 | + } catch (error: any) { |
| 50 | + setIsError(true); |
| 51 | + |
| 52 | + if (error.response?.status === 401) { |
| 53 | + setAuth(false); |
| 54 | + setErrorMessage(processAuthError(error)); |
| 55 | + } else { |
| 56 | + const message = error.response?.data?.message || error.message; |
| 57 | + setErrorMessage(`Error fetching pushes: ${message}`); |
| 58 | + } |
| 59 | + } finally { |
| 60 | + setIsLoading(false); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +const authorisePush = async ( |
| 65 | + id: string, |
| 66 | + setMessage: (message: string) => void, |
| 67 | + setUserAllowedToApprove: (userAllowedToApprove: boolean) => void, |
| 68 | + attestation: Array<{ label: string; checked: boolean }>, |
| 69 | +): Promise<void> => { |
| 70 | + const url = `${API_BASE}/push/${id}/authorise`; |
| 71 | + let errorMsg = ''; |
| 72 | + let isUserAllowedToApprove = true; |
| 73 | + await axios |
| 74 | + .post( |
| 75 | + url, |
| 76 | + { |
| 77 | + params: { |
| 78 | + attestation, |
| 79 | + }, |
| 80 | + }, |
| 81 | + getAxiosConfig(), |
| 82 | + ) |
| 83 | + .catch((error: any) => { |
| 84 | + if (error.response && error.response.status === 401) { |
| 85 | + errorMsg = 'You are not authorised to approve...'; |
| 86 | + isUserAllowedToApprove = false; |
| 87 | + } |
| 88 | + }); |
| 89 | + setMessage(errorMsg); |
| 90 | + setUserAllowedToApprove(isUserAllowedToApprove); |
| 91 | +}; |
| 92 | + |
| 93 | +const rejectPush = async ( |
| 94 | + id: string, |
| 95 | + setMessage: (message: string) => void, |
| 96 | + setUserAllowedToReject: (userAllowedToReject: boolean) => void, |
| 97 | +): Promise<void> => { |
| 98 | + const url = `${API_BASE}/push/${id}/reject`; |
| 99 | + let errorMsg = ''; |
| 100 | + let isUserAllowedToReject = true; |
| 101 | + await axios.post(url, {}, getAxiosConfig()).catch((error: any) => { |
| 102 | + if (error.response && error.response.status === 401) { |
| 103 | + errorMsg = 'You are not authorised to reject...'; |
| 104 | + isUserAllowedToReject = false; |
| 105 | + } |
| 106 | + }); |
| 107 | + setMessage(errorMsg); |
| 108 | + setUserAllowedToReject(isUserAllowedToReject); |
| 109 | +}; |
| 110 | + |
| 111 | +const cancelPush = async ( |
| 112 | + id: string, |
| 113 | + setAuth: (auth: boolean) => void, |
| 114 | + setIsError: (isError: boolean) => void, |
| 115 | +): Promise<void> => { |
| 116 | + const url = `${API_BASE}/push/${id}/cancel`; |
| 117 | + await axios.post(url, {}, getAxiosConfig()).catch((error: any) => { |
| 118 | + if (error.response && error.response.status === 401) { |
| 119 | + setAuth(false); |
| 120 | + } else { |
| 121 | + setIsError(true); |
| 122 | + } |
| 123 | + }); |
| 124 | +}; |
| 125 | + |
| 126 | +export { getPush, getPushes, authorisePush, rejectPush, cancelPush }; |
0 commit comments