|
| 1 | +import { |
| 2 | + useGenericGetRequestQuery, |
| 3 | + useLazyGenericGetRequestQuery, |
| 4 | + useLazyGenericPostRequestQuery, |
| 5 | +} from 'src/store/api/app.js' |
| 6 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 7 | +import { |
| 8 | + CBadge, |
| 9 | + CButton, |
| 10 | + CCallout, |
| 11 | + CCard, |
| 12 | + CCardBody, |
| 13 | + CCardHeader, |
| 14 | + CCardTitle, |
| 15 | + CCol, |
| 16 | + CForm, |
| 17 | + CLink, |
| 18 | + CRow, |
| 19 | + CSpinner, |
| 20 | +} from '@coreui/react' |
| 21 | +import { Form } from 'react-final-form' |
| 22 | +import { RFFSelectSearch } from 'src/components/forms/index.js' |
| 23 | +import React, { useEffect } from 'react' |
| 24 | +import { CippCallout } from 'src/components/layout/index.js' |
| 25 | +import { CippCodeBlock } from 'src/components/utilities' |
| 26 | +import { CellDate } from 'src/components/tables' |
| 27 | + |
| 28 | +/** |
| 29 | + * Sets the notification settings. |
| 30 | + * @returns {JSX.Element} The notification settings component. |
| 31 | + */ |
| 32 | +export function SettingsPartner() { |
| 33 | + const webhookConfig = useGenericGetRequestQuery({ |
| 34 | + path: '/api/ExecPartnerWebhook', |
| 35 | + params: { Action: 'ListSubscription' }, |
| 36 | + }) |
| 37 | + const webhookEvents = useGenericGetRequestQuery({ |
| 38 | + path: '/api/ExecPartnerWebhook', |
| 39 | + params: { Action: 'ListEventTypes' }, |
| 40 | + }) |
| 41 | + const [submitWebhook, webhookCreateResult] = useLazyGenericPostRequestQuery() |
| 42 | + const [sendTest, sendTestResult] = useLazyGenericGetRequestQuery() |
| 43 | + const [checkTest, checkTestResult] = useLazyGenericGetRequestQuery() |
| 44 | + |
| 45 | + const onSubmit = (values) => { |
| 46 | + const shippedValues = { |
| 47 | + EventType: values?.EventType?.map((event) => event.value), |
| 48 | + } |
| 49 | + submitWebhook({ |
| 50 | + path: '/api/ExecPartnerWebhook?Action=CreateSubscription', |
| 51 | + values: shippedValues, |
| 52 | + }).then((res) => { |
| 53 | + webhookConfig.refetch() |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if ( |
| 59 | + sendTestResult.isSuccess && |
| 60 | + sendTestResult?.data?.Results?.correlationId && |
| 61 | + !checkTestResult?.data?.Results?.results |
| 62 | + ) { |
| 63 | + setTimeout( |
| 64 | + checkTest({ |
| 65 | + path: '/api/ExecPartnerWebhook', |
| 66 | + params: { |
| 67 | + Action: 'ValidateTest', |
| 68 | + CorrelationId: sendTestResult?.data?.Results?.correlationId, |
| 69 | + }, |
| 70 | + }), |
| 71 | + 1000, |
| 72 | + ) |
| 73 | + } |
| 74 | + }, [sendTestResult, checkTest, checkTestResult]) |
| 75 | + |
| 76 | + return ( |
| 77 | + <CCard className="h-100"> |
| 78 | + <CCardHeader></CCardHeader> |
| 79 | + <CCardBody> |
| 80 | + <> |
| 81 | + <CButton |
| 82 | + size="sm" |
| 83 | + onClick={() => webhookConfig.refetch()} |
| 84 | + className="mb-2" |
| 85 | + disabled={webhookConfig.isFetching} |
| 86 | + > |
| 87 | + {webhookConfig.isFetching ? ( |
| 88 | + <> |
| 89 | + <CSpinner className="me-2" size="sm" /> Loading... |
| 90 | + </> |
| 91 | + ) : ( |
| 92 | + <> |
| 93 | + <FontAwesomeIcon icon="sync" className="me-2" /> |
| 94 | + Refresh |
| 95 | + </> |
| 96 | + )} |
| 97 | + </CButton> |
| 98 | + |
| 99 | + {!webhookConfig.isFetching && webhookConfig.error && ( |
| 100 | + <CippCallout color="danger">Error loading data</CippCallout> |
| 101 | + )} |
| 102 | + {webhookConfig.isSuccess && ( |
| 103 | + <> |
| 104 | + <h3 className="underline mb-5"> Webhook Configuration</h3> |
| 105 | + <CRow> |
| 106 | + <CCol sm={12} md={6} lg={8} className="mb-3"> |
| 107 | + Subscribe to Microsoft Partner center webhooks to enable automatic tenant |
| 108 | + onboarding and alerting. Updating the settings will replace any existing webhook |
| 109 | + subscription with one pointing to CIPP. Refer to the{' '} |
| 110 | + <CLink |
| 111 | + href="https://learn.microsoft.com/en-us/partner-center/developer/partner-center-webhooks" |
| 112 | + target="_blank" |
| 113 | + > |
| 114 | + Microsoft Partner Center documentation |
| 115 | + </CLink>{' '} |
| 116 | + for more information on the webhook types. |
| 117 | + </CCol> |
| 118 | + </CRow> |
| 119 | + <CRow> |
| 120 | + <CCol sm={12} md={6} className="mb-3"> |
| 121 | + <p className="fw-lighter">Webhook URL</p> |
| 122 | + <CippCodeBlock |
| 123 | + code={webhookConfig?.data?.Results?.webhookUrl ?? 'No webhook URL found'} |
| 124 | + language="plain" |
| 125 | + showLineNumbers={false} |
| 126 | + /> |
| 127 | + </CCol> |
| 128 | + <CCol sm={12} md={6} className="mb-3"> |
| 129 | + <p className="fw-lighter">Last Updated</p> |
| 130 | + <CellDate |
| 131 | + cell={webhookConfig?.data?.Results?.lastModifiedTimestamp} |
| 132 | + format="short" |
| 133 | + /> |
| 134 | + </CCol> |
| 135 | + <CCol sm={12} md={12} className="mb-3"> |
| 136 | + <p className="fw-lighter">Subscribed Events</p> |
| 137 | + <Form |
| 138 | + onSubmit={onSubmit} |
| 139 | + initialValues={{ |
| 140 | + EventType: webhookConfig?.data?.Results?.webhookEvents.map((event) => ({ |
| 141 | + label: event, |
| 142 | + value: event, |
| 143 | + })), |
| 144 | + }} |
| 145 | + render={({ handleSubmit }) => ( |
| 146 | + <> |
| 147 | + <CForm onSubmit={handleSubmit}> |
| 148 | + <RFFSelectSearch |
| 149 | + name="EventType" |
| 150 | + label="Event Types" |
| 151 | + values={webhookEvents.data?.Results?.map((event) => ({ |
| 152 | + name: event, |
| 153 | + value: event, |
| 154 | + }))} |
| 155 | + multi={true} |
| 156 | + refreshFunction={() => webhookEvents.refetch()} |
| 157 | + helpText="Select the events you want to receive notifications for." |
| 158 | + /> |
| 159 | + <CButton |
| 160 | + type="submit" |
| 161 | + color="primary" |
| 162 | + className="my-3" |
| 163 | + disabled={webhookCreateResult.isFetching} |
| 164 | + > |
| 165 | + {webhookCreateResult.isFetching ? ( |
| 166 | + <> |
| 167 | + <CSpinner size="sm" className="me-2" /> |
| 168 | + Saving... |
| 169 | + </> |
| 170 | + ) : ( |
| 171 | + 'Save' |
| 172 | + )} |
| 173 | + </CButton> |
| 174 | + </CForm> |
| 175 | + </> |
| 176 | + )} |
| 177 | + /> |
| 178 | + {webhookCreateResult.isSuccess && ( |
| 179 | + <CippCallout color="info" dismissible> |
| 180 | + {webhookCreateResult?.data?.Results} |
| 181 | + </CippCallout> |
| 182 | + )} |
| 183 | + </CCol> |
| 184 | + </CRow> |
| 185 | + <h3 className="underline mb-5">Webhook Test</h3> |
| 186 | + <CRow> |
| 187 | + <CCol sm={12} md={12} className="mb-3"> |
| 188 | + <CButton |
| 189 | + className="me-3" |
| 190 | + onClick={() => |
| 191 | + sendTest({ |
| 192 | + path: '/api/ExecPartnerWebhook', |
| 193 | + params: { Action: 'SendTest' }, |
| 194 | + }) |
| 195 | + } |
| 196 | + > |
| 197 | + {sendTestResult.isFetching ? ( |
| 198 | + <> |
| 199 | + <CSpinner size="sm" className="me-2" /> |
| 200 | + Running Test... |
| 201 | + </> |
| 202 | + ) : ( |
| 203 | + 'Start Test' |
| 204 | + )} |
| 205 | + </CButton> |
| 206 | + {checkTestResult.isFetching && !checkTestResult?.data?.Results?.result && ( |
| 207 | + <> |
| 208 | + <CSpinner size="sm" className="me-2" /> Waiting for results |
| 209 | + </> |
| 210 | + )} |
| 211 | + </CCol> |
| 212 | + </CRow> |
| 213 | + <CRow> |
| 214 | + {checkTestResult.isSuccess && ( |
| 215 | + <> |
| 216 | + <CCol sm={12} md={4} className="mb-3"> |
| 217 | + <p className="fw-lighter">Status</p> |
| 218 | + {checkTestResult?.data?.Results.status} |
| 219 | + </CCol> |
| 220 | + {Array.isArray(checkTestResult?.data?.Results?.results) && ( |
| 221 | + <> |
| 222 | + <CCol sm={12} md={4} className="mb-3"> |
| 223 | + <p className="fw-lighter">Status Code</p> |
| 224 | + <FontAwesomeIcon |
| 225 | + icon={ |
| 226 | + checkTestResult?.data?.Results?.results[0].responseCode == 200 |
| 227 | + ? 'check-circle' |
| 228 | + : 'times-circle' |
| 229 | + } |
| 230 | + color={ |
| 231 | + checkTestResult?.data?.Results?.results[0].responseCode == 200 |
| 232 | + ? 'green' |
| 233 | + : 'red' |
| 234 | + } |
| 235 | + className="me-2" |
| 236 | + /> |
| 237 | + {checkTestResult?.data?.Results?.results[0].responseCode} |
| 238 | + </CCol> |
| 239 | + {checkTestResult?.data?.Results?.results[0].responseMessage !== '' && ( |
| 240 | + <CCol sm={12} md={4} className="mb-3"> |
| 241 | + <p className="fw-lighter">Response Message</p> |
| 242 | + {checkTestResult.data.Results.results[0].responseMessage} |
| 243 | + </CCol> |
| 244 | + )} |
| 245 | + <CCol sm={12} md={4} className="mb-3"> |
| 246 | + <p className="fw-lighter">Date/Time</p> |
| 247 | + <CellDate |
| 248 | + cell={Date( |
| 249 | + Date.parse( |
| 250 | + checkTestResult?.data?.Results?.results[0].dateTimeUtc + 'Z', |
| 251 | + ), |
| 252 | + ).toLocaleString()} |
| 253 | + format="short" |
| 254 | + /> |
| 255 | + </CCol> |
| 256 | + </> |
| 257 | + )} |
| 258 | + </> |
| 259 | + )} |
| 260 | + </CRow> |
| 261 | + </> |
| 262 | + )} |
| 263 | + </> |
| 264 | + </CCardBody> |
| 265 | + </CCard> |
| 266 | + ) |
| 267 | +} |
0 commit comments