-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashCurlOpenTransaction
More file actions
executable file
·188 lines (160 loc) · 7.86 KB
/
Copy pathbashCurlOpenTransaction
File metadata and controls
executable file
·188 lines (160 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
#include
. bashToken
#Get contents of file for tranId variable
tranId=$(cat bashTransactionId)
#Cut transaction id from tranId variable and set checkTranId to transaction id
checkTranId=$(cut -d '=' -f2 <<< $tranId)
#Bold colors
BWHITE='\033[1;37m'
BGREEN='\033[1;32m'
BRED='\033[1;31m'
BYELLOW='\033[1;33m'
BBLUE='\033[1;34m'
UBLUE='\033[4;34m'
NC='\033[0m' # No color
#Create options for outbound ID, file name and file type
while getopts ":o:f:t:" option; do
case $option in
o) # give outbound id
outboundIds=$OPTARG;;
f) # give name you will assign file when sent
fileName=$OPTARG;;
t) # give type of file
fileType=$OPTARG;;
\?) #Invalid option
echo "ERROR: Invalid option"
exit;;
esac
done
#create listFiles variable
listFiles="$(ls clientResourcesFolder/outboundRoutes.json 2>&1)"
lsError=$(cut -d ':' -f3 <<< $listFiles)
#Check if error exists, display message to run command for getting unread messages if outboundRoutes.json file does not exist
if [[ "$lsError" == " No such file or directory" ]] && ! [[ $outboundIds ]]
then
echo -e "\n${BWHITE}\"sea-client-nodejs/clientResourcesFolder/outboundRoutes.json\"${BYELLOW} does not currently exist to pull ${BWHITE}outbound IDs ${BYELLOW}from ${BBLUE}${UBLUE}please run the below command to pull outbound routes before running this commmand again${NC}${BYELLOW} or enter ${BWHITE}1${BYELLOW} from Menu options when you run ${BGREEN}./bashMenu${BYELLOW}:"
echo -e "\n\n${BWHITE}Command: ${BGREEN}./bashCurlGetOutboundRoutes\n"
exit 1
fi
#If checkTranId is not empty then a transaction is currently open, exit script
if [ "$checkTranId" != '""' ]
then
echo -e "\n${BRED}TransactionId: ${BWHITE}$checkTranId${BRED} is currently open!"
echo -e "\n\n${BYELLOW}You must first close this transaction before you can open another.${NC}\n"
exit 1
fi
#While outboundIds is empty ask user for input and display where outbound IDs can be found
while ! [[ $outboundIds ]]; do
read -p "$(echo -e "\n"${BYELLOW}"Please enter outbound ID/s from ${BWHITE}\"sea-client-nodejs/clientResourcesFolder/outboundRoutes.json\"${BYELLOW}. ${BBLUE}${UBLUE}If you are using more than one outbound ID sepearate them with spaces ${NC}${BWHITE}(e.g. outboundID1 outboundID2 outboundID3)${BYELLOW}: "${NC})" outboundIds
echo -e "\n"
done
#Array to contain outboundIds
oids=( $outboundIds )
#Create counter and set to 1
outboundIdCount=1
#Create allOutboundIds array to contain user input
allOutboundIds=()
#Loop through oids and place each string in allOutbounds array to create proper string for data in curl statement
for ((i=0; i<${#oids[@]}; i++)); do
#If oids greater than outboundIdCount add this string to allOutboundIds array and iterate ouboundIdCount
if [[ ${#oids[@]} -gt $outboundIdCount ]]
then
allOutboundIds+=("\"${oids[i]}\", ")
outboundIdCount=$(expr $outboundIdCount + 1)
#Else add this string
else
allOutboundIds+=("\"${oids[i]}\"")
fi
done
#Set userOutboundIds to the contents within allOutboundIds array
userOutboundIds=$(echo -e ${allOutboundIds[@]})
#While fileName is empty ask user for input and display where outbound IDs can be found
while ! [ $fileName ]; do
read -p "$(echo -e "\n"${BYELLOW}"Please enter the ${BWHITE}file name${BYELLOW} you will assign to this file when it is sent ${BWHITE}(e.g. exampleFile.MOR)${BYELLOW}: "${NC})" fileName
echo -e "\n"
done
#Create array for outboundFileTypes and boolean for fileTypeExists
outboundFileTypes=("BirthInfantDeath" "FetalDeath" "ITOP" "Mortality" "Natality" "Roster" "Other")
fileTypeExists=false
#While fileType is empty ask user for input and display where outbound IDs can be found
while ! [ $fileType ]; do
read -p "$(echo -e "\n"${BYELLOW}"Please enter the ${BWHITE}file type${BYELLOW} you will assign to this file when it is sent ${BWHITE}(e.g. Mortality)${BYELLOW}: "${NC})" fileType
echo -e "\n"
done
#If user string matches "birthinfantdeath" change to "BirthInfantDeath" to avoid
#error at check
if [ "$fileType" = "birthinfantdeath" ]
then
fileType="BirthInfantDeath"
#elif user string matches "fetaldeath" change to "FetaltDeath" to avoid
#error at check
elif [ "$fileType" == "fetaldeath" ]
then
fileType="FetalDeath"
#elif user string matches "itop" change to "ITOP" to avoid
#error at check
elif [ "$fileType" == "itop" ]
then
fileType="ITOP"
#Else change first character to uppercase if lower case to avoid error at check
else
fileType="$(tr '[:lower:]' '[:upper:]' <<< ${fileType:0:1})${fileType:1}"
fi
#Check if fileType matches any index in outboundFileTypes array and if it does set fileTypeExists to true
for ((i=0; i<${#outboundFileTypes[@]}; i++)); do
if [[ ${outboundFileTypes[$i]} == "$fileType" ]]
then
fileTypeExists=true
fi
done
#Open transaction and set transactionInfo variable to output
if [ $fileTypeExists = true ]
then
transactionInfo=$(curl -X 'POST' \
"$envUrl/openTransaction" \
-H 'accept: application/json' \
-H "Authorization: Bearer $token" \
-H 'Content-Type: application/json' \
-d "{
"'"outboundIds"'": [$userOutboundIds],
"'"fileName"'": "'"'$fileName'"'",
"'"fileType"'": "'"'$fileType'"'"
}")
#Else echo ERROR statement and list valid file types and example command
else
echo -e "${BRED}\nERROR: That is an invalid File Type! Please run command again with a valid file type.\n\n${BYELLOW}############### Valid File Types ###############\n\n-BirthInfantDeath\n-FetalDeath\n-ITOP\n-Mortality\n-Natality\n-Roster\n-Other"
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlOpenTransaction -o <outboundIds> -f <fileName> -t <fileType>${NC}"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlOpenTransaction\n${NC}"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Run this command and enter ${BBLUE}2${BWHITE} from Menu options: ${BGREEN}./bashMenu\n${NC}"
exit 1;
fi
#Cut error message if it exists in field 4 with a delimeter of " and set errorMessage variable to output
errorMessage=$(cut -d '"' -f4 <<< $transactionInfo)
currentOpenTransaction=$(cut -d '"' -f8 <<< $transactionInfo)
#If string from field 4 of cut is equal to "Outbound IDs not found in realm by type." echo ERROR message
#with instructions on how to run the command and where to find outbound IDs and how to generate a outboundRoutes.json
#if one does not exist, exit script
if [[ "$errorMessage" != "Transaction Opened" ]]
then
echo -e "\n${BRED}ERROR: $errorMessage Invalid outbound ID/s!"
echo -e "\n\n${BYELLOW}Please run command again with valid outbound ID/s from ${BWHITE}\"sea-client-nodejs/clientResourcesFolder/outboundRoutes.json\"${BYELLOW}."
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlOpenTransaction -o <outboundId> -f <fileName> -t <fileType>\n${NC}"
echo -e "\n\n${NC}${BYELLOW}OR..${NC}"
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlOpenTransaction${NC}"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Run this command and enter ${BBLUE}2${BWHITE} from Menu options: ${BGREEN}./bashMenu\n${NC}"
echo -e "\n\n${UBLUE}If ${NC}${BWHITE}\"sea-client-nodejs/clientResourcesFolder/outboundRoutes.json\" ${UBLUE}does not currently exist for the file type that you are opening a transaction for then you can run one of the following command to generate it:"
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlGetOutboundRoutes -f Mortality${NC}"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Example: ${BGREEN}./bashCurlGetOutboundRoutes\n${NC}"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Run this command and enter ${BBLUE}1${BWHITE} from Menu options: ${BGREEN}./bashMenu\n${NC}"
exit 1
#Else open transaction and display transaction id
else
echo -e "\n${BWHITE}Transaction: \"$currentOpenTransaction\" ${BGREEN}is now open.\n"
echo -e '#!/bin/bash\n\ntransactionId="'$currentOpenTransaction'"' > bashTransactionId
fi