-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSnapshots.ts
230 lines (209 loc) · 8.84 KB
/
Snapshots.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
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
import type { Request, RequestHandler } from 'express';
import express from 'express';
import { map, omit, union } from 'lodash';
import { existsSync, writeJson } from 'fs-extra';
// eslint-disable-next-line node/no-missing-import
import type { ParamsDictionary } from 'express-serve-static-core';
import { db } from '../db/Connection';
import type { UserAppsInstance } from '../db/models/UserAppsModel';
import type { UserAppsAttributes } from '../db/models/UserAppsModel.types';
import type { Page, PageGroup, Template } from '../handler/templates/types';
import { checkTemplateExists, createTemplate, getUserTemplates } from '../handler/templates/TemplatesHandler';
import { checkPageExists, createPage, getUserPages } from '../handler/templates/PagesHandler';
import { checkPageGroupExists, createPageGroup, getUserPageGroups } from '../handler/templates/PageGroupsHandler';
import { createUserWidgetsSnapshot, restoreUserWidgetsSnapshot } from '../handler/widgets/WidgetsHandler';
import type { BlueprintUserDataInstance } from '../db/models/BlueprintUserDataModel';
import type { BlueprintUserDataAttributes } from '../db/models/BlueprintUserDataModel.types';
import { getResourcePath, getTokenFromHeader } from '../utils';
import { getRBAC, isAuthorized } from '../handler/AuthHandler';
const router = express.Router();
router.use(express.json());
function getSnapshotPermissionValidator(permissionType: 'create' | 'restore'): RequestHandler<never, never> {
return async (req, res, next) => {
const rbac = await getRBAC(getTokenFromHeader(req)!);
const permission = rbac.permissions[`snapshot_${permissionType}`];
if (!isAuthorized(req.user!, permission)) {
res.sendStatus(403);
}
next();
};
}
const dataConflictError = { status: 400, message: 'Snapshot data conflicts with already existing data' };
function handleConflictError(err: any) {
return Promise.reject(err.name === 'SequelizeUniqueConstraintError' ? dataConflictError : err);
}
function defineSnapshotEndpoints<PayloadType>(
path: string,
createHandler: RequestHandler<never, PayloadType>,
restoreHandler: (req: Request<ParamsDictionary, any, PayloadType>) => Promise<unknown>
) {
router.get(path, getSnapshotPermissionValidator('create'), createHandler);
router.post(
path,
getSnapshotPermissionValidator('restore'),
(req: Request<ParamsDictionary, any, PayloadType>, res, next) =>
restoreHandler(req)
.then(() => res.status(201).end())
.catch(next)
);
}
(() => {
const propertiesToOmit = ['id', 'tenant'] as const;
type PropertiesToOmit = typeof propertiesToOmit[number];
type UserAppsSnapshot = Omit<UserAppsAttributes, PropertiesToOmit>[];
defineSnapshotEndpoints<UserAppsSnapshot>(
'/ua',
(req, res, next) => {
db.UserApps.findAll<UserAppsInstance>({
where: {
tenant: req.headers.tenant
},
attributes: Object.keys(omit(db.UserApps.getAttributes(), propertiesToOmit))
})
.then(userApps => {
res.send(userApps);
})
.catch(next);
},
req =>
db
.sequelize!.transaction(transaction =>
Promise.all(
req.body.map(userAppSnapshot =>
db.UserApps.create<UserAppsInstance>(
{ ...userAppSnapshot, tenant: req.headers.tenant as string },
{ transaction }
)
)
)
)
.catch(handleConflictError)
);
})();
const commonPropertiesToOmit = ['custom'] as const;
type CommonPropertiesToOmit = typeof commonPropertiesToOmit[number];
const templatePropertiesToOmit = [...commonPropertiesToOmit, 'name'] as const;
export type TemplatesSnapshot = Omit<Template, typeof templatePropertiesToOmit[number]>[];
defineSnapshotEndpoints<TemplatesSnapshot>(
'/templates',
(_req, res, _next) => {
res.send(getUserTemplates().map(template => omit(template, templatePropertiesToOmit)));
},
req =>
Promise.all(req.body.map(templateData => checkTemplateExists(templateData)))
.catch(message => Promise.reject({ message, status: 400 }))
.then(() =>
Promise.all(
req.body.map(templateData =>
createTemplate(
{ ...templateData, pages: templateData.data.pages },
templateData.updatedBy,
templateData.updatedAt
)
)
)
)
);
export type PagesSnapshot = Omit<Page, CommonPropertiesToOmit>[];
defineSnapshotEndpoints<PagesSnapshot>(
'/pages',
(_req, res, _next) => {
res.send(getUserPages().map(page => omit(page, commonPropertiesToOmit)));
},
req =>
Promise.all(req.body.map(pageData => checkPageExists(pageData)))
.catch(message => Promise.reject({ message, status: 400 }))
.then(() =>
Promise.all(
req.body.map(pageData =>
createPage({ ...pageData, ...pageData.data }, pageData.updatedBy, pageData.updatedAt)
)
)
)
);
export type PageGroupsSnapshot = Omit<PageGroup, CommonPropertiesToOmit>[];
defineSnapshotEndpoints<PageGroupsSnapshot>(
'/page-groups',
(_req, res, _next) => {
res.send(getUserPageGroups().map(pageGroup => omit(pageGroup, commonPropertiesToOmit)));
},
req =>
Promise.all(req.body.map(pageGroupData => checkPageGroupExists(pageGroupData)))
.catch(message => Promise.reject({ message, status: 400 }))
.then(() =>
Promise.all(
req.body.map(pageGroupData =>
createPageGroup(pageGroupData, pageGroupData.updatedBy, pageGroupData.updatedAt)
)
)
)
);
defineSnapshotEndpoints(
'/widgets',
(_req, res, next) => {
const snapshot = createUserWidgetsSnapshot(next);
snapshot.pipe(res);
},
restoreUserWidgetsSnapshot
);
(() => {
const propertiesToOmit = ['id'] as const;
type PropertiesToOmit = typeof propertiesToOmit[number];
type BlueprintLayoutsSnapshot = Omit<BlueprintUserDataAttributes, PropertiesToOmit>[];
defineSnapshotEndpoints<BlueprintLayoutsSnapshot>(
'/blueprint-layouts',
(_req, res, next) => {
db.BlueprintUserData.findAll<BlueprintUserDataInstance>({
attributes: Object.keys(omit(db.BlueprintUserData.getAttributes(), propertiesToOmit))
})
.then(blueprintLayouts => {
res.send(blueprintLayouts);
})
.catch(next);
},
req =>
db
.sequelize!.transaction(transaction =>
Promise.all(
req.body.map(blueprintLayoutSnapshot =>
db.BlueprintUserData.create<BlueprintUserDataInstance>(blueprintLayoutSnapshot, {
transaction
})
)
)
)
.catch(handleConflictError)
);
})();
(() => {
const userConfigurationFiles = ['overrides.json', 'userConfig.json'] as const;
type ConfigurationSnapshot = Partial<Record<typeof userConfigurationFiles[number], any>>;
defineSnapshotEndpoints<ConfigurationSnapshot>(
'/configuration',
(_req, res, _next) => {
const snapshot: ConfigurationSnapshot = {};
userConfigurationFiles.forEach(fileName => {
const filePath = getResourcePath(fileName, true);
if (existsSync(filePath)) {
// eslint-disable-next-line import/no-dynamic-require,global-require
snapshot[fileName] = require(filePath);
}
});
res.send(snapshot);
},
req => {
const requestedFiles = Object.keys(req.body);
if (union(userConfigurationFiles, requestedFiles).length > userConfigurationFiles.length) {
return Promise.reject({ status: 400, message: 'Unsupported configuration file' });
}
const existingFile = requestedFiles.find(fileName => existsSync(getResourcePath(fileName, true)));
if (existingFile) {
return Promise.reject(dataConflictError);
}
return Promise.all(
map(req.body, (fileContent, fileName) => writeJson(getResourcePath(fileName, true), fileContent))
);
}
);
})();
export default router;