-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfind-invoices
executable file
·153 lines (124 loc) · 3.77 KB
/
find-invoices
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
#! /usr/bin/env bash
#
# This script will use available API endpoints to find invoices in whatever
# environment you specify.
#
# It requires that you supply it with an active JWT. You can obtain your JWT
# by logging into the appropriate office environment, and inspecting your
# cookies. You want the contents of the cookie named 'XYZ_session_token' where
# XYZ is the name of your app: mil or office. It's a very long string.
#
# Valid environments: staging, prod, experimental
# Valid queue names: new, ppm, hhg_active, hhg_in_transit, hhg_delivered, all
#
function usage() {
echo "usage: $0 [environment; default=staging] [queue; default=hhg_delivered]"
exit 1
}
environment="${1:-staging}"
case "$environment" in
loadtest)
url_env="${environment}."
;;
demo)
url_env="${environment}."
;;
exp)
url_env="${environment}."
;;
stg)
url_env="${environment}."
;;
prd)
url_env=""
;;
*)
echo "error: unknown environment: $1"
usage
;;
esac
queue="${2:-hhg_delivered}"
# Ensure JQ is available
if ! command -v jq &> /dev/null; then
echo "error: jq not installed, install with 'brew install jq'" 1>&2
exit 1
fi
# A place to store the user's JWT for use in making API calls
session_file=$(mktemp)
function milmove_api() {
url="$1"
session_token=$(cat "$session_file")
cookie="session_token=${session_token};"
json=$(curl -s "${url}" \
-H 'accept-encoding: gzip, deflate, br' \
-H 'accept-language: en-US,en;q=0.9' \
-H 'user-agent: curl; scripts/find-invoices' \
-H 'accept: */*' \
-H "cookie: $cookie" \
--compressed)
ret=$?
if [[ $ret -ne 0 ]]; then
echo "error: curl to url failed: $url" 1>&2
exit 1
fi
if [[ "$json" == "Unauthorized" ]]; then
echo "Token has expired, please paste in a valid JWT for $environment (input will be hidden):" 1>&2
# Prevent terminal from limiting input length
stty -icanon
read -r -s session_token
echo "$session_token" > "$session_file"
# Revert terminal setting
stty icanon
# retry command recursively
json=$(milmove_api "$url")
fi
echo "$json"
}
# Given a queue name, get a list of moves
function get_moves() {
queue="$1"
json=$(milmove_api "https://office.${url_env}move.mil/internal/queues/${queue}")
move_ids=$(echo "$json" | jq '.[] | .id')
echo "$move_ids"
}
# Given a Move ID, get a list of shipments
function get_shipments() {
move_id="$1"
json=$(milmove_api "https://office.${url_env}move.mil/internal/moves/${move_id}")
shipments=$(echo "$json" | jq '.shipments')
echo "$shipments"
}
# Given a Shipment ID, get invoices
function get_invoice() {
shipment_id="$1"
json=$(milmove_api "https://my.${url_env}move.mil/api/v1/shipments/${shipment_id}/invoices")
echo "$json"
}
####################
# Main
move_ids=$(get_moves "$queue")
for move_id_quoted in $move_ids; do
move_id=$(echo "$move_id_quoted" | grep -o -e '[^"]\+')
echo "Checking move id: $move_id"
shipments=$(get_shipments "$move_id")
if [[ "$shipments" == "null" ]]; then
echo " No shipment..."
continue
fi
shipment_ids=$(echo "$shipments" | jq '.[] | .id')
for shipment_id_quoted in $shipment_ids; do
shipment_id=$(echo "$shipment_id_quoted" | grep -o -e '[^"]\+')
echo " Checking shipment id ${shipment_id}"
invoices=$(get_invoice "$shipment_id")
if [[ "$invoices" != "[]" ]]; then
echo " Invoices:"
OLD_IFS="$IFS"
IFS=''
echo "$invoices" | jq -C '.[]' | while read -r invoice_line; do
echo " ${invoice_line}"
done
IFS="$OLD_IFS"
fi
done
done
rm "$session_file"