|
| 1 | +/** |
| 2 | + * Copyright (c) 2022, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import { AsgardeoSPAClient, HttpClientInstance } from "@asgardeo/auth-react"; |
| 20 | +import { store } from "@wso2is/admin.core.v1"; |
| 21 | +import { RequestConfigInterface } from "@wso2is/admin.core.v1/hooks/use-request"; |
| 22 | +import { IdentityAppsApiException } from "@wso2is/core/exceptions"; |
| 23 | +import { HttpMethods } from "@wso2is/core/models"; |
| 24 | +import { AxiosError, AxiosResponse } from "axios"; |
| 25 | +import SMSSenderConstants from "../constants/sms-sender-constants"; |
| 26 | +import { NotificationSenderSMSInterface } from "../models/sms-senders"; |
| 27 | + |
| 28 | +const httpClient: HttpClientInstance = AsgardeoSPAClient.getInstance().httpRequest |
| 29 | + .bind(AsgardeoSPAClient.getInstance()); |
| 30 | + |
| 31 | +/** |
| 32 | + * Add sms notification senders with name SMSPublisher. |
| 33 | + * |
| 34 | + * @returns A promise containing the response. |
| 35 | + */ |
| 36 | +const addSMSPublisher = (): Promise<NotificationSenderSMSInterface> => { |
| 37 | + //SMS Notification sender with name SMSPublisher. |
| 38 | + const smsProvider: NotificationSenderSMSInterface = { |
| 39 | + contentType: "FORM", |
| 40 | + name: "SMSPublisher", |
| 41 | + properties: [ |
| 42 | + { |
| 43 | + key: "channel.type", |
| 44 | + value: "choreo" |
| 45 | + } |
| 46 | + ], |
| 47 | + provider: "choreo", |
| 48 | + providerURL: "https://console.choreo.dev/" |
| 49 | + }; |
| 50 | + |
| 51 | + const requestConfig: RequestConfigInterface = { |
| 52 | + data: smsProvider, |
| 53 | + headers: { |
| 54 | + "Accept": "application/json", |
| 55 | + "Access-Control-Allow-Origin": store.getState().config.deployment.clientHost, |
| 56 | + "Content-Type": "application/json" |
| 57 | + }, |
| 58 | + method: HttpMethods.POST, |
| 59 | + url: store.getState().config.endpoints.notificationSendersEndPoint + "/sms" |
| 60 | + }; |
| 61 | + |
| 62 | + return httpClient(requestConfig) |
| 63 | + .then((response: AxiosResponse<NotificationSenderSMSInterface>) => { |
| 64 | + if (response.status !== 201) { |
| 65 | + throw new IdentityAppsApiException( |
| 66 | + SMSSenderConstants.ERROR_IN_CREATING_SMS_NOTIFICATION_SENDER, |
| 67 | + null, |
| 68 | + response.status, |
| 69 | + response.request, |
| 70 | + response, |
| 71 | + response.config); |
| 72 | + } |
| 73 | + |
| 74 | + return Promise.resolve(response.data as NotificationSenderSMSInterface); |
| 75 | + }).catch((error: AxiosError) => { |
| 76 | + throw new IdentityAppsApiException( |
| 77 | + SMSSenderConstants.ERROR_IN_CREATING_SMS_NOTIFICATION_SENDER, |
| 78 | + error.stack, |
| 79 | + error.code, |
| 80 | + error.request, |
| 81 | + error.response, |
| 82 | + error.config); |
| 83 | + }); |
| 84 | +}; |
| 85 | + |
| 86 | +export default addSMSPublisher; |
0 commit comments