-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.mjs
192 lines (163 loc) · 5.6 KB
/
server.mjs
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
import cors from 'cors'
import express from 'express'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const app = express()
const readManifest = (path) => {
return new Promise((resolve) => {
fs.readFile(path, 'utf-8', (e, data) => {
if (e) {
console.error('An error occurred reading the file', e)
resolve({})
} else if (data) {
resolve(JSON.parse(data))
} else {
console.error('Unexpected empty buffer')
resolve({})
}
})
})
}
const renderEntrypoint = (name, manifest, entry) => {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UI Extension: ${name}</title>
<script type="module" src="/dist/${name}/${manifest[entry].slice(2)}"></script>
</head>
<body></body>
</html>`
}
app.use(cors())
app.use('/dist', express.static(path.join(__dirname, '/dist')))
app.get('/', (_, response) => {
response.sendFile(path.join(__dirname, '/index.html'))
})
app.get('/extension/:uuid', async (request, response) => {
const uuid = request.params.uuid
const known = await readManifest(path.join(__dirname, '/cases.json'))
const record = known.items.find(r => r.uuid === uuid)
if (record) {
const manifest = await readManifest(path.join(__dirname, `/dist/${record.name}/manifest.json`))
response.send(renderEntrypoint(record.name, manifest, record.script))
} else {
response.sendStatus(404)
}
})
app.get('/extension/:uuid/stylesheet', async (request, response) => {
const uuid = request.params.uuid
const known = await readManifest(path.join(__dirname, '/cases.json'))
const record = known.items.find(r => r.uuid === uuid)
if (record && record.stylesheet) {
const manifest = await readManifest(path.join(__dirname, `/dist/${record.name}/manifest.json`))
response.sendFile(path.join(__dirname, 'dist', record.name, manifest[record.stylesheet]))
} else {
response.sendStatus(404)
}
})
// fiscalReceipts routes
app.post('/receipts-count', async (request, response) => {
response.status(200).json({ count: 2 })
})
const receipts = [{
id: 645,
details: {
receiptTime: '17.11.2024 11:51',
shiftNumber: 16,
machineNumber: 'KZN030315',
taxSystem: 'OSN',
onlinePayment: true,
fnNumber: '7380440801381848',
kktRegistrationNumber: '0007642722037997',
fdNumber: 41859,
fpd: 2975038937,
ffdVersion: '1.2',
},
}, {
id: 813,
details: {
receiptTime: '28.10.2024 10:32',
shiftNumber: 18,
machineNumber: 'KZN1001202',
taxSystem: 'OSN',
onlinePayment: true,
fnNumber: '7380440800998420',
kktRegistrationNumber: '0007642686026725',
fdNumber: 4696,
fpd: 3632111203,
ffdVersion: '1.2',
},
}]
app.post('/receipts', express.urlencoded(), async (request, response) => {
const payload = JSON.parse(request.body.payload)
const responseReceipts = receipts.map(receipt => {
return {
...receipt,
id: `ORDER${payload.order_number}_${receipt.id}`,
}
})
response.status(200).json({ receipts: responseReceipts })
})
// orderNotes routes
const notes = [{
id: 1,
author: 'Василий Петров',
date: '2024-10-15T16:00:00',
text: 'Клиент просил оставить заказ у двери',
}, {
id: 2,
author: 'Николай Понкратов',
date: '2024-10-12T00:00:00',
text: 'Просил оповестить, как появятся мандарины, хочет добавить к заказу',
}]
app.post('/notes-count', async (request, response) => {
response.status(200).json({ count: notes.length })
})
app.post('/notes', async (request, response) => {
response.status(200).json({ notes })
})
app.post('/notes/new', express.urlencoded(), async (request, response) => {
const payload = JSON.parse(request.body.payload)
const resultNotes = [payload.note, ...notes]
response.status(200).json({ notes: resultNotes })
})
// customerINN routes
app.post('/customer/by-inn', express.urlencoded(), async (request, response) => {
const { inn } = JSON.parse(request.body.payload)
if (inn === '1234567890') {
return response.status(200).json({
data: {
name: 'МФО ТомскАсбоцементПивБанк',
bank: 'Сбербанк России, ОАО, г. Москва',
bankAccount: '415219379646',
bankAddress: '877568, Липецкая область, город Клин, наб. Косиора, 61',
legalName: 'ОАО Глав',
legalAddress: '877568, Липецкая область, город Клин, наб. Косиора, 61',
corrAccount: '601630812474',
OGRN: '1027700132195',
OGRNIP: '304770000000571',
INN: '2872865074',
OKPO: '10720877',
BIK: '38630490',
KPP: '287201001',
certificate: {
date: '15.03.2020',
number: '78-20-567890',
},
},
})
}
response.status(400)
})
const server = app.listen(3000, () => {
console.log('Serving on port 3000')
})
process.on('SIGINT', () => {
server.close(() => {
console.log('Server has been stopped')
process.exit(0)
})
})