-
Notifications
You must be signed in to change notification settings - Fork 0
/
box-document-numbering.js
250 lines (212 loc) · 7.58 KB
/
box-document-numbering.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* eslint-disable no-ternary */
/* eslint-disable multiline-ternary */
/* eslint-disable lines-around-comment */
/* eslint-disable max-lines-per-function */
/* eslint-disable camelcase */
import express from 'express'
import axios from 'axios'
import fs from 'fs';
const app = express();
const apiUrl = 'https://api.box.com'
const numberPrefix = (process.env.numberPrefix) ? process.env.numberPrefix : "BOX"
const storagePath = (process.env.storagePath) ? process.env.storagePath : "./"
const fileName = "lastIssuedNumber.txt"
var server = app.listen(3000, () => {
var host = server.address().address
var port = server.address().port
console.log("Box Document Numbering started and listening at http://%s:%s. Visit this address with your browser to start migrating", host, port)
})
app.get('/', (req, res) => {
console.log("Starting")
let fileId = req.query.fileId;
const authCode = req.query.authCode
const clientId = ""
const clientSecret = ""
axios.request({
url: `${apiUrl}/oauth2/token/`,
method: "post",
data: `grant_type=authorization_code&client_id=${clientId}&client_secret=${clientSecret}&code=${authCode}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
}
})
.then(response => {
console.log("Getting user details")
let data = {
accessToken: response.data.access_token
}
let options = {
params: {
fields: "name,enterprise"
},
headers: {
Authorization: `Bearer ${data.accessToken}`
}
}
return axios.get(`${apiUrl}/2.0/users/me/`,options)
.then(subResponse => {
data.username = subResponse.data.name
data.enterpriseId = subResponse.data.enterprise.id
return data
})
.catch(error => {
console.log(error)
return Promise.reject(new Error("Error reading your user details"))
})
})
.then(data => {
console.log("Issuing document number")
return issueNumber()
.then(response => {
data.documentNumber = response
return data
})
.catch(error => {
console.log(error)
return Promise.reject(new Error("Unable to issue number to apply."))
})
})
.then(data => {
console.log("Applying metadata")
let options = {
headers: {
Authorization: `Bearer ${data.accessToken}`
}
}
let attributes = {
"generatedAt": (new Date()).toISOString(),
"generatedBy": data.username,
"number": data.documentNumber
}
return axios.post(`${apiUrl}/2.0/files/${fileId}/metadata/enterprise_${data.enterpriseId}/documentNumber`,attributes,options)
.then(() => {
return data
})
.catch(error => {
if (error.response.status == 409) {
return Promise.reject(new Error("This document already has a document number in its metadata. If you need to re-apply a document number, the metadata instance needs to be removed first."))
}
console.log(error)
return Promise.reject(new Error("Error applying metadata"))
})
})
.then(data => {
console.log("Getting file details")
let options = {
headers: {
Authorization: `Bearer ${data.accessToken}`
}
}
return axios.get(`${apiUrl}/2.0/files/${fileId}`,options)
.then(response => {
data.filename = response.data.name
return data
})
.catch(error => {
console.log(error)
return Promise.reject(new Error("Unable to read document name"))
})
})
.then(data => {
console.log("Renaming file")
let options = {
headers: {
Authorization: `Bearer ${data.accessToken}`
}
}
let attributes = {
"name": `${data.filename.replace(/((^\[.*\] )||(^))/,"[" + data.documentNumber + "] ")}`,
}
return axios.put(`${apiUrl}/2.0/files/${fileId}/`,attributes,options)
.then(() => {
return data
})
.catch(error => {
console.log(error)
return Promise.reject(new Error("Unable to rename document"))
})
})
.then(data => {
console.log("Invalidating token")
return axios.request({
url: `${apiUrl}/oauth2/revoke`,
method: "post",
data: `client_id=${clientId}&client_secret=${clientSecret}&code=${authCode}&token=${data.accessToken}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
}
})
.then(() => {
return data
})
.catch(error => {
console.log(error)
return Promise.reject(new Error("Error finishing up while revoking token."))
})
})
.then(data => {
console.log("Returning success status")
res.status(200).send(`Issued and applied document number ${data.documentNumber}`)
})
.catch(error => {
console.log("Returning error to Box user: ", error.message)
res.status(500).send(error.message)
})
})
const issueNumber = () => {
return new Promise((resolve, reject) => {
console.log("Issuing number")
readNumber()
.then(number => {
writeNumber(number)
return number
})
.then((number) => {
resolve(numberPrefix + (number + 1).toString().padStart(6,"0"))
})
.catch(error => {
reject(error)
})
})
}
const readNumber = () => {
return new Promise((resolve) => {
console.log("Reading current number from storage")
fs.promises.stat(`${storagePath}${fileName}`)
.then(() => {
return fs.promises.readFile(`${storagePath}${fileName}`)
.then(buffer => {
console.log("returning from existing file", buffer.toString());
resolve(Number(buffer.toString()));
})
})
.catch(error => {
if (error.code == "ENOENT") {
console.log("readNumber: Creating file")
const initialNumber = 0
return fs.promises.writeFile(`${storagePath}${fileName}`,initialNumber.toString())
.then(() => {
resolve(initialNumber)
})
}
console.log(error)
return Promise.reject(new Error("Error reading previous number"))
})
})
}
const writeNumber = (currentNumber) => {
return new Promise((resolve, reject) => {
console.log("Writing issued number to storage")
const newNumber = Number(currentNumber) + 1
fs.promises.writeFile(`${storagePath}${fileName}`,newNumber.toString())
.then(() => {
resolve()
})
.catch(error => {
console.log(error)
reject(new Error("Error writing issued number. This is an integity risk"))
})
})
}