-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBlueprintUserData.ts
56 lines (50 loc) · 2.18 KB
/
BlueprintUserData.ts
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
import express from 'express';
import type { Response } from 'express';
import yaml from 'js-yaml';
import _ from 'lodash';
import { db } from '../db/Connection';
import type { BlueprintUserDataInstance } from '../db/models/BlueprintUserDataModel';
import { getArchiveFileContent, browseArchiveTree } from '../handler/SourceHandler';
import type {
PutBlueprintUserDataLayoutRequestBody,
GetBlueprintUserDataLayoutResponse
} from './BlueprintUserData.types';
const router = express.Router();
router.use(express.json());
router.get('/layout/:blueprintId', (req, res: Response<GetBlueprintUserDataLayoutResponse>, next) => {
db.BlueprintUserData.findOne<BlueprintUserDataInstance>({
where: { ...req.params, ..._.pick(req.user, 'username') }
})
.then(blueprintData => {
if (blueprintData) {
return res.send(blueprintData.layout);
}
return browseArchiveTree(req).then(data => {
if (data !== null) {
const layoutFilePath = _.chain(data)
.get('children[0].children')
// @ts-ignore FIXME: Property 'find' does not exist on type 'LoDashExplicitWrapper<any>'
.find({ title: 'info.yaml' })
.get('key')
.value();
return getArchiveFileContent(req, data.timestamp, layoutFilePath)
.then(yaml.load)
.then(layout => res.send(layout));
}
return res.status(404).send({});
});
})
.catch(next);
});
router.put<{ blueprint: string }, never, PutBlueprintUserDataLayoutRequestBody>(
'/layout/:blueprint',
(req, res, next) => {
db.BlueprintUserData.findOrCreate<BlueprintUserDataInstance>({
where: { blueprintId: req.params.blueprint, username: req.user!.username },
defaults: { blueprintId: req.params.blueprint, username: req.user!.username, layout: {} }
})
.then(([blueprintData]) => blueprintData.update({ layout: req.body }).then(() => res.sendStatus(200)))
.catch(next);
}
);
export default router;