forked from miroapp/app-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
441 lines (381 loc) · 11.3 KB
/
app.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Require sensitive environment variables (oauthToken, Miro boardID)
require("dotenv").config();
// Require express for server and handlebars for clientside rendering
const express = require("express");
const exphbs = require("express-handlebars");
const app = express();
const { Miro } = require("@mirohq/miro-api");
const miro = new Miro();
const USER_ID = "WE_DONT_NEED_A_REAL_ID_FOR_THIS_EXAMPLE";
// Require body-parser to parse form submissions
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false, parameterLimit: 1000000 }));
app.use(bodyParser.json());
// Require multer and fast-csv for CSV upload functionality
const multer = require("multer");
const csv = require("fast-csv");
const fs = require("fs");
const upload = multer({ dest: "tmp/csv/" });
// Configure express-handlebars as our view engine:
app.engine(
"hbs",
exphbs.engine({
defaultLayout: "main",
extname: ".hbs",
})
);
// Configure handlebars
app.set("view engine", "hbs");
// <-------- ROUTES -------->
// ROUTE (GET): Retrieve access_token from OAuth redirect
app.get("/authorized", async (req, res) => {
try {
await miro.handleAuthorizationCodeRequest(USER_ID, req);
} catch (e) {
console.error(e);
}
res.render("authorizeApp");
});
// ROUTE (POST): Trigger the authorization flow
app.post("/", async function (req, res) {
if (await miro.isAuthorized(USER_ID)) {
res.render("authorizeApp");
return;
}
res.redirect(miro.getAuthUrl());
});
// ROUTE(GET): VIEW UPLOAD .CSV OPTION
app.get("/upload-csv", async (req, res) => {
if (await miro.isAuthorized(USER_ID)) {
res.render("uploadCSV");
} else {
res.redirect(miro.getAuthUrl());
}
});
// ROUTE(POST): UPLOAD .CSV FILE
app.post("/upload-csv", upload.single("csv"), async function (req, res) {
if (!(await miro.isAuthorized(USER_ID))) {
res.redirect(miro.getAuthUrl());
}
let fileRows = ["No file uploaded"];
if (req.file) {
fileRows = [];
// open uploaded file
csv
.parseFile(req.file.path)
.on("data", function (data) {
fileRows.push(data); // push each row
})
.on("end", function () {
fs.unlinkSync(req.file.path); // remove temp file
fileRows.shift(); // remove csv headers (start with actual content)
});
}
res.render("uploadCSV.hbs", { fileRows });
});
// ROUTE(POST): GENERATE CONNECTORS & CREATE STICKY NOTES/TAGS FROM CSV CONTENT,
app.post("/create-from-csv", async function (req, res) {
if (!(await miro.isAuthorized(USER_ID))) {
res.redirect(miro.getAuthUrl());
}
const { content } = req.body;
const TAG_COLORS = ["blue", "red", "yellow", "green"];
const CSV_ROW_STRUCTURE = [
"title",
"description",
"tag",
"tag",
"tag",
"tag",
];
const CSV_ROW_LENGTH = CSV_ROW_STRUCTURE.length;
const NUMBER_OF_TAGS = CSV_ROW_STRUCTURE.filter(
(type) => type === "tag"
).length;
const COLUMN_WIDTH = 400;
const STICKY_WIDTH = COLUMN_WIDTH - 200;
const MAX_STICKIES_PER_COLUMN = 8;
const STICKY_SPACING = 40;
const TABLE_HEIGHT = MAX_STICKIES_PER_COLUMN * STICKY_WIDTH + COLUMN_WIDTH;
const COLUMNS_NEEDED = Math.ceil(
content.length / CSV_ROW_LENGTH / MAX_STICKIES_PER_COLUMN
);
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const createStickiesAndTags = () => {
let x = -(COLUMN_WIDTH / 2);
let y = 0;
const promises = [];
for (let i = 0, limit = content.length; i < limit; i += CSV_ROW_LENGTH) {
y =
((i % (MAX_STICKIES_PER_COLUMN * CSV_ROW_LENGTH)) + 4) * STICKY_SPACING;
if (i % (MAX_STICKIES_PER_COLUMN * CSV_ROW_LENGTH) === 0) {
x += COLUMN_WIDTH;
}
promises.push(
board.createStickyNoteItem({
data: {
content:
`<strong>${content[i]}</strong>` + "<br>" + `${content[i + 1]}`,
shape: "square",
},
style: {
fillColor: "light_pink",
textAlign: "left",
textAlignVertical: "top",
},
geometry: {
width: STICKY_WIDTH,
},
position: {
x,
y,
},
})
);
for (let j = 0; j < NUMBER_OF_TAGS; j++) {
const title = content[i + 2 + j];
const randomNumber = Math.floor(999999 * Math.random()); // add random number to prevent double tags
if (title) {
promises.push(
board.createTag({
fillColor: TAG_COLORS[j],
title: `${title} (${randomNumber})`,
})
);
}
}
}
return promises;
};
const tagStickies = async (stickiesAndTags) => {
const tagAttachmentPromises = [];
const stickyIds = [];
let lastId;
const organized = stickiesAndTags.reduce((accumulator, item) => {
if (item.type === "sticky_note") {
lastId = item.id;
accumulator[lastId] = [item];
stickyIds.push(lastId);
} else {
accumulator[lastId].push(item.id);
}
return accumulator;
}, {});
stickyIds.map((stickyId) => {
const [sticky, ...tags] = organized[stickyId];
tags.map((id) => {
tagAttachmentPromises.push(sticky.attachTag(id));
});
});
await Promise.all(tagAttachmentPromises);
};
const createTable = async () => {
let COLOR = "#1a1a1a";
let DOT_SIZE = 10;
const coordinatesForPoints = [];
const pointIndexesToConnectToEachOther = [];
for (let i = 0; i <= COLUMNS_NEEDED; i++) {
coordinatesForPoints.push(
{ x: COLUMN_WIDTH * i + 1, y: 0 },
{ x: COLUMN_WIDTH * i + 1, y: TABLE_HEIGHT }
);
}
for (let i = 0; i <= COLUMNS_NEEDED * 2 + 1; i += 1) {
// Connectors go from 1 shape to another (so NOT from X/Y to another X/y), so these indexes refer to the indexes of the circles in `coordinatesForPoints`
if (i % 2 === 0) {
pointIndexesToConnectToEachOther.push(
[
i,
i + 2,
{
captions: [
{
content: `COLUMN ${Math.ceil(i / 2) + 1}`,
position: "50%",
textAlignVertical: "top",
},
],
},
], // top left to right
[i + 1, i + 3] // bottom left to right
);
}
if (i <= COLUMNS_NEEDED) {
pointIndexesToConnectToEachOther.push(
[i * 2, i * 2 + 1] // top to bottom
);
}
}
const promises = coordinatesForPoints.map(({ x, y }) =>
board.createShapeItem({
data: {
shape: "circle",
},
style: {
borderColor: COLOR,
borderWidth: "2.0",
},
geometry: {
width: DOT_SIZE,
height: DOT_SIZE,
},
position: {
origin: "center",
x,
y,
},
})
);
const points = await Promise.all(promises);
const style = {
color: COLOR,
fontSize: "14",
strokeColor: COLOR,
strokeWidth: "1.0",
startStrokeCap: "none",
endStrokeCap: "none",
};
const connections = pointIndexesToConnectToEachOther.map(
([a, b, data]) =>
points[b]?.id &&
board.createConnector({
startItem: {
id: points[a].id,
},
endItem: {
id: points[b].id,
},
style,
...data,
})
);
await Promise.all(connections);
};
try {
await createTable();
} catch (e) {
console.error("error creating the table:", e);
}
const stickiesAndTagsCreationPromises = createStickiesAndTags();
let stickiesAndTags = [];
try {
stickiesAndTags = await Promise.all(stickiesAndTagsCreationPromises);
} catch (e) {
console.error("Error creating stickies and tags:", e);
}
if (stickiesAndTags?.length) {
try {
await tagStickies(stickiesAndTags);
} catch (e) {
console.error("error tagging stickies", e);
}
}
// Redirect to 'List Stickies' view on success
res.redirect(301, "/get-sticky");
});
// ROUTE(GET): RENDER HOME VIEW
app.get("/", (req, res) => {
res.render("home");
});
// ROUTE(GET) RETRIEVE STICKY DATA / 'List Stickies'
app.get("/get-sticky", async (req, res) => {
if (await miro.isAuthorized(USER_ID)) {
let board;
let allItems = [];
try {
board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
} catch (e) {
console.error("error getting board", e);
}
try {
allItems = await board.getAllItems({ type: "sticky_note" });
} catch (e) {
console.error(
"error getting all items for board",
{ boardId: process.env.MIRO_BOARD_ID },
e
);
}
const boardItems = [];
for await (const item of allItems) {
boardItems.push(item);
}
res.render("viewCard.hbs", { miroData: boardItems });
} else {
res.redirect(miro.getAuthUrl());
}
});
// ROUTE(GET): RENDER 'CREATE CARD' VIEW
app.get("/create-sticky", async (req, res) => {
if (await miro.isAuthorized(USER_ID)) {
res.render("createCard");
} else {
res.redirect(miro.getAuthUrl());
}
});
// ROUTE(GET): RENDER 'UPDATE CARD' VIEW
app.get("/update-sticky", async (req, res) => {
if (await miro.isAuthorized(USER_ID)) {
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const allItems = await board.getAllItems({ type: "sticky_note" });
const stickies = [];
for await (const item of allItems) {
stickies.push(item);
}
res.render("updateCard", { stickies });
} else {
res.redirect(miro.getAuthUrl());
}
});
// ROUTE(GET): RENDER 'DELETE CARD' VIEW
app.get("/delete-sticky", async (req, res) => {
if (await miro.isAuthorized(USER_ID)) {
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const allItems = await board.getAllItems({ type: "sticky_note" });
const stickies = [];
for await (const item of allItems) {
stickies.push(item);
}
res.render("deleteCard", { stickies });
} else {
res.redirect(miro.getAuthUrl());
}
});
// ROUTE(POST): CREATE STICKY
app.post("/create-sticky", async function (req, res) {
let { title, description, tag } = req.body;
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const stickyNote = await board.createStickyNoteItem({
data: {
content: `${title} ${description}`,
shape: "square",
},
});
if (tag) {
const { id } = await board.createTag({
title: tag,
fillColor: "blue",
});
await stickyNote.attachTag(id.toString());
}
res.redirect(301, "/create-sticky");
});
// ROUTE(POST): UPDATE EXISTING STICKY
app.post("/update-sticky", async function (req, res) {
let { id, content } = req.body;
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const stickyNote = await board.getStickyNoteItem(id);
await stickyNote.update({ data: { content } });
res.redirect(301, "/update-sticky");
});
// ROUTE(POST): DELETE EXISTING STICKY NOTE
app.post("/delete-sticky", async function (req, res) {
let { id } = req.body;
const board = await miro.as(USER_ID).getBoard(process.env.MIRO_BOARD_ID);
const stickyNote = await board.getStickyNoteItem(id);
await stickyNote.delete();
res.redirect(301, "/get-sticky");
});
app.listen(8000, () => {
console.log("The web server has started on port 8000");
});