xonecas.io
4436
Sl

sesame-labs-coding-assignment / commit b900436

Merge branch 'assignment-self-code-review' into 'main'

Changes from self code review

See merge request xonecas/sesame-labs-coding-assignment!1

xonecas2023-01-25 b90043609fd044b45460434ec081a7548e386946 parent 35be474parent 34b89c8

17 files changed

.gitignore+2−1
@@ -2,4 +2,5 @@
22 .keys
33 node_modules
44 .env
5-api/prisma/*.db
5+api/prisma/*.db
6+api/dist
.nvmrc+1−1
@@ -1 +1 @@
1-v16.17.0
1+v19.4.0
README.md+2−2
@@ -2,7 +2,7 @@
22
33 ### Setting up this project:
44
5-Project was built using the Node stable release at the moment (v16.17.0), please make sure you have a compatible node version. If you use nvm then you can use the command: `nvm use` and it should pick up the `.nvmrc` file included in this repo.
5+Project was built using the Node stable release at the moment (v19.4.0), please make sure you have a compatible node version. If you use nvm then you can use the command: `nvm use` and it should pick up the `.nvmrc` file included in this repo.
66
77 **Installing**
88
@@ -21,7 +21,7 @@ Project was built using the Node stable release at the moment (v16.17.0), please
2121
2222 **Running**
2323
24-1. To start the api server, type this command in the `api` folder: `npx tsc && node dist/`
24+1. To start the api server, type this command in the `api` folder: `npx ts-node ./src`
2525 1. To start the ui server, type this command in the `ui` folder: `npm start`
2626
2727 Note: Using the create react app dev server for convenience, in a production environment i'd use nginx or similar to serve the build folder.
api/.env.sample+1−0
@@ -2,6 +2,7 @@
22 ENVIRONMENT=dev
33 PORT=3001
44 CLIENT_URL="http://localhost:3000/"
5+APP_KEYS='A very good secret'
56
67 # Discord related values:
78 DISCORD_APPLICATION_ID=000000000000000000
api/package-lock.json+158−21
@@ -14,12 +14,14 @@
1414 "@types/koa-session": "^5.10.6",
1515 "axios": "^1.2.2",
1616 "dotenv": "^16.0.3",
17+ "http-status-codes": "^2.2.0",
1718 "koa": "2.14.1",
1819 "koa-bodyparser": "4.3.0",
1920 "koa-json": "^2.0.2",
2021 "koa-logger": "^3.2.1",
2122 "koa-router": "^12.0.0",
22- "koa-session": "^6.3.1"
23+ "koa-session": "^6.3.1",
24+ "ts-node": "^10.9.1"
2325 },
2426 "devDependencies": {
2527 "@types/dotenv": "^8.2.0",
@@ -35,9 +37,41 @@
3537 "typescript": "^4.9.4"
3638 }
3739 },
40+ "node_modules/@cspotcode/source-map-support": {
41+ "version": "0.8.1",
42+ "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
43+ "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==",
44+ "dependencies": {
45+ "@jridgewell/trace-mapping": "0.3.9"
46+ },
47+ "engines": {
48+ "node": ">=12"
49+ }
50+ },
51+ "node_modules/@jridgewell/resolve-uri": {
52+ "version": "3.1.0",
53+ "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz",
54+ "integrity": "sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w==",
55+ "engines": {
56+ "node": ">=6.0.0"
57+ }
58+ },
59+ "node_modules/@jridgewell/sourcemap-codec": {
60+ "version": "1.4.14",
61+ "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz",
62+ "integrity": "sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw=="
63+ },
64+ "node_modules/@jridgewell/trace-mapping": {
65+ "version": "0.3.9",
66+ "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz",
67+ "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==",
68+ "dependencies": {
69+ "@jridgewell/resolve-uri": "^3.0.3",
70+ "@jridgewell/sourcemap-codec": "^1.4.10"
71+ }
72+ },
3873 "node_modules/@koa/cors": {
3974 "version": "4.0.0",
40- "resolved": "https://registry.npmjs.org/@koa/cors/-/cors-4.0.0.tgz",
4175 "integrity": "sha512-Y4RrbvGTlAaa04DBoPBWJqDR5gPj32OOz827ULXfgB1F7piD1MB/zwn8JR2LAnvdILhxUbXbkXGWuNVsFuVFCQ==",
4276 "dependencies": {
4377 "vary": "^1.1.2"
@@ -48,7 +82,6 @@
4882 },
4983 "node_modules/@prisma/client": {
5084 "version": "4.8.1",
51- "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.8.1.tgz",
5285 "integrity": "sha512-d4xhZhETmeXK/yZ7K0KcVOzEfI5YKGGEr4F5SBV04/MU4ncN/HcE28sy3e4Yt8UFW0ZuImKFQJE+9rWt9WbGSQ==",
5386 "hasInstallScript": true,
5487 "dependencies": {
@@ -78,6 +111,26 @@
78111 "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.8.0-61.d6e67a83f971b175a593ccc12e15c4a757f93ffe.tgz",
79112 "integrity": "sha512-MHSOSexomRMom8QN4t7bu87wPPD+pa+hW9+71JnVcF3DqyyO/ycCLhRL1we3EojRpZxKvuyGho2REQsMCvxcJw=="
80113 },
114+ "node_modules/@tsconfig/node10": {
115+ "version": "1.0.9",
116+ "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz",
117+ "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA=="
118+ },
119+ "node_modules/@tsconfig/node12": {
120+ "version": "1.0.11",
121+ "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz",
122+ "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag=="
123+ },
124+ "node_modules/@tsconfig/node14": {
125+ "version": "1.0.3",
126+ "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz",
127+ "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow=="
128+ },
129+ "node_modules/@tsconfig/node16": {
130+ "version": "1.0.3",
131+ "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.3.tgz",
132+ "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ=="
133+ },
81134 "node_modules/@types/accepts": {
82135 "version": "1.3.5",
83136 "resolved": "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz",
@@ -121,7 +174,6 @@
121174 },
122175 "node_modules/@types/dotenv": {
123176 "version": "8.2.0",
124- "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz",
125177 "integrity": "sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==",
126178 "deprecated": "This is a stub types definition. dotenv provides its own type definitions, so you do not need this installed.",
127179 "dev": true,
@@ -182,7 +234,6 @@
182234 },
183235 "node_modules/@types/koa__cors": {
184236 "version": "3.3.0",
185- "resolved": "https://registry.npmjs.org/@types/koa__cors/-/koa__cors-3.3.0.tgz",
186237 "integrity": "sha512-FUN8YxcBakIs+walVe3+HcNP+Bxd0SB8BJHBWkglZ5C1XQWljlKcEFDG/dPiCIqwVCUbc5X0nYDlH62uEhdHMA==",
187238 "dev": true,
188239 "dependencies": {
@@ -191,7 +242,6 @@
191242 },
192243 "node_modules/@types/koa-bodyparser": {
193244 "version": "4.3.0",
194- "resolved": "https://registry.npmjs.org/@types/koa-bodyparser/-/koa-bodyparser-4.3.0.tgz",
195245 "integrity": "sha512-aB/vwwq4G9FAtKzqZ2p8UHTscXxZvICFKVjuckqxCtkX1Ro7F5KHkTCUqTRZFBgDoEkmeca+bFLI1bIsdPPZTA==",
196246 "dev": true,
197247 "dependencies": {
@@ -208,7 +258,6 @@
208258 },
209259 "node_modules/@types/koa-json": {
210260 "version": "2.0.20",
211- "resolved": "https://registry.npmjs.org/@types/koa-json/-/koa-json-2.0.20.tgz",
212261 "integrity": "sha512-RuQ1Vlpsm/EC5wo2oWCgNnwneixnSQ9aHQAE7nwnbSiCibgfUO2wwSQN6rn8SfF97cRdN9hKAtoc9KSIuiGP6Q==",
213262 "dev": true,
214263 "dependencies": {
@@ -217,7 +266,6 @@
217266 },
218267 "node_modules/@types/koa-logger": {
219268 "version": "3.1.2",
220- "resolved": "https://registry.npmjs.org/@types/koa-logger/-/koa-logger-3.1.2.tgz",
221269 "integrity": "sha512-sioTA1xlKYiIgryANWPRHBkG3XGbWftw9slWADUPC+qvPIY/yRLSrhvX7zkJwMrntub5dPO0GuAoyGGf0yitfQ==",
222270 "dev": true,
223271 "dependencies": {
@@ -226,7 +274,6 @@
226274 },
227275 "node_modules/@types/koa-router": {
228276 "version": "7.4.4",
229- "resolved": "https://registry.npmjs.org/@types/koa-router/-/koa-router-7.4.4.tgz",
230277 "integrity": "sha512-3dHlZ6CkhgcWeF6wafEUvyyqjWYfKmev3vy1PtOmr0mBc3wpXPU5E8fBBd4YQo5bRpHPfmwC5yDaX7s4jhIN6A==",
231278 "dev": true,
232279 "dependencies": {
@@ -235,7 +282,6 @@
235282 },
236283 "node_modules/@types/koa-session": {
237284 "version": "5.10.6",
238- "resolved": "https://registry.npmjs.org/@types/koa-session/-/koa-session-5.10.6.tgz",
239285 "integrity": "sha512-p4rgkeRmiJu8XGC3eH2duRCNgnLUl6sjadEXH/AsieH/9fqYfXSZoZNC9CAe+FQK+QmM76hVyvuJ5Jrl5xxNeA==",
240286 "dependencies": {
241287 "@types/cookies": "*",
@@ -289,6 +335,25 @@
289335 "node": ">= 0.6"
290336 }
291337 },
338+ "node_modules/acorn": {
339+ "version": "8.8.1",
340+ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.1.tgz",
341+ "integrity": "sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==",
342+ "bin": {
343+ "acorn": "bin/acorn"
344+ },
345+ "engines": {
346+ "node": ">=0.4.0"
347+ }
348+ },
349+ "node_modules/acorn-walk": {
350+ "version": "8.2.0",
351+ "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz",
352+ "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==",
353+ "engines": {
354+ "node": ">=0.4.0"
355+ }
356+ },
292357 "node_modules/ansi-styles": {
293358 "version": "3.2.1",
294359 "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
@@ -313,6 +378,11 @@
313378 "node": ">= 8"
314379 }
315380 },
381+ "node_modules/arg": {
382+ "version": "4.1.3",
383+ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
384+ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA=="
385+ },
316386 "node_modules/asynckit": {
317387 "version": "0.4.0",
318388 "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
@@ -320,7 +390,6 @@
320390 },
321391 "node_modules/axios": {
322392 "version": "1.2.2",
323- "resolved": "https://registry.npmjs.org/axios/-/axios-1.2.2.tgz",
324393 "integrity": "sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==",
325394 "dependencies": {
326395 "follow-redirects": "^1.15.0",
@@ -578,6 +647,11 @@
578647 "buffer": "^5.1.0"
579648 }
580649 },
650+ "node_modules/create-require": {
651+ "version": "1.1.1",
652+ "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
653+ "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ=="
654+ },
581655 "node_modules/debug": {
582656 "version": "4.3.4",
583657 "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -629,6 +703,14 @@
629703 "npm": "1.2.8000 || >= 1.4.16"
630704 }
631705 },
706+ "node_modules/diff": {
707+ "version": "4.0.2",
708+ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
709+ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
710+ "engines": {
711+ "node": ">=0.3.1"
712+ }
713+ },
632714 "node_modules/dotenv": {
633715 "version": "16.0.3",
634716 "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
@@ -838,6 +920,11 @@
838920 "node": ">= 0.6"
839921 }
840922 },
923+ "node_modules/http-status-codes": {
924+ "version": "2.2.0",
925+ "resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-2.2.0.tgz",
926+ "integrity": "sha512-feERVo9iWxvnejp3SEfm/+oNG517npqL2/PIA8ORjyOZjGC7TwCRQsZylciLS64i6pJ0wRYz3rkXLRwbtFa8Ng=="
927+ },
841928 "node_modules/humanize-number": {
842929 "version": "0.0.2",
843930 "resolved": "https://registry.npmjs.org/humanize-number/-/humanize-number-0.0.2.tgz",
@@ -991,7 +1078,6 @@
9911078 },
9921079 "node_modules/koa": {
9931080 "version": "2.14.1",
994- "resolved": "https://registry.npmjs.org/koa/-/koa-2.14.1.tgz",
9951081 "integrity": "sha512-USJFyZgi2l0wDgqkfD27gL4YGno7TfUkcmOe6UOLFOVuN+J7FwnNu4Dydl4CUQzraM1lBAiGed0M9OVJoT0Kqw==",
9961082 "dependencies": {
9971083 "accepts": "^1.3.5",
@@ -1024,7 +1110,6 @@
10241110 },
10251111 "node_modules/koa-bodyparser": {
10261112 "version": "4.3.0",
1027- "resolved": "https://registry.npmjs.org/koa-bodyparser/-/koa-bodyparser-4.3.0.tgz",
10281113 "integrity": "sha512-uyV8G29KAGwZc4q/0WUAjH+Tsmuv9ImfBUF2oZVyZtaeo0husInagyn/JH85xMSxM0hEk/mbCII5ubLDuqW/Rw==",
10291114 "dependencies": {
10301115 "co-body": "^6.0.0",
@@ -1058,7 +1143,6 @@
10581143 },
10591144 "node_modules/koa-json": {
10601145 "version": "2.0.2",
1061- "resolved": "https://registry.npmjs.org/koa-json/-/koa-json-2.0.2.tgz",
10621146 "integrity": "sha512-8+dz0T2ekDuNN1svYoKPCV2txotQ3Ufg8Fn5bft1T48MPJWiC/HKmkk+3xj9EC/iNZuFYeLRazN2h2o3RSUXuQ==",
10631147 "dependencies": {
10641148 "koa-is-json": "1",
@@ -1067,7 +1151,6 @@
10671151 },
10681152 "node_modules/koa-logger": {
10691153 "version": "3.2.1",
1070- "resolved": "https://registry.npmjs.org/koa-logger/-/koa-logger-3.2.1.tgz",
10711154 "integrity": "sha512-MjlznhLLKy9+kG8nAXKJLM0/ClsQp/Or2vI3a5rbSQmgl8IJBQO0KI5FA70BvW+hqjtxjp49SpH2E7okS6NmHg==",
10721155 "dependencies": {
10731156 "bytes": "^3.1.0",
@@ -1081,7 +1164,6 @@
10811164 },
10821165 "node_modules/koa-router": {
10831166 "version": "12.0.0",
1084- "resolved": "https://registry.npmjs.org/koa-router/-/koa-router-12.0.0.tgz",
10851167 "integrity": "sha512-zGrdiXygGYW8WvrzeGsHZvKnHs4DzyGoqJ9a8iHlRkiwuEAOAPyI27//OlhoWdgFAEIM3qbUgr0KCuRaP/TCag==",
10861168 "dependencies": {
10871169 "http-errors": "^2.0.0",
@@ -1118,7 +1200,6 @@
11181200 },
11191201 "node_modules/koa-session": {
11201202 "version": "6.3.1",
1121- "resolved": "https://registry.npmjs.org/koa-session/-/koa-session-6.3.1.tgz",
11221203 "integrity": "sha512-Y6206QSkDgtIgjTT7ndmIgZjWrOcY3jflfsDjwvxLfYwSiwKGVAi3l1ExhI2KjkEO1uhKyPxcEWhEDn8HaoRUg==",
11231204 "dependencies": {
11241205 "crc": "^3.8.0",
@@ -1130,6 +1211,11 @@
11301211 "node": ">=8.0.0"
11311212 }
11321213 },
1214+ "node_modules/make-error": {
1215+ "version": "1.3.6",
1216+ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
1217+ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw=="
1218+ },
11331219 "node_modules/media-typer": {
11341220 "version": "0.3.0",
11351221 "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
@@ -1192,7 +1278,6 @@
11921278 },
11931279 "node_modules/nodemon": {
11941280 "version": "2.0.20",
1195- "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.20.tgz",
11961281 "integrity": "sha512-Km2mWHKKY5GzRg6i1j5OxOHQtuvVsgskLfigG25yTtbyfRGn/GNvIbRyOf1PSCKJ2aT/58TiuUsuOU5UToVViw==",
11971282 "dev": true,
11981283 "dependencies": {
@@ -1307,7 +1392,6 @@
13071392 },
13081393 "node_modules/prisma": {
13091394 "version": "4.8.1",
1310- "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.8.1.tgz",
13111395 "integrity": "sha512-ZMLnSjwulIeYfaU1O6/LF6PEJzxN5par5weykxMykS9Z6ara/j76JH3Yo2AH3bgJbPN4Z6NeCK9s5fDkzf33cg==",
13121396 "devOptional": true,
13131397 "hasInstallScript": true,
@@ -1565,6 +1649,48 @@
15651649 "nodetouch": "bin/nodetouch.js"
15661650 }
15671651 },
1652+ "node_modules/ts-node": {
1653+ "version": "10.9.1",
1654+ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz",
1655+ "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==",
1656+ "dependencies": {
1657+ "@cspotcode/source-map-support": "^0.8.0",
1658+ "@tsconfig/node10": "^1.0.7",
1659+ "@tsconfig/node12": "^1.0.7",
1660+ "@tsconfig/node14": "^1.0.0",
1661+ "@tsconfig/node16": "^1.0.2",
1662+ "acorn": "^8.4.1",
1663+ "acorn-walk": "^8.1.1",
1664+ "arg": "^4.1.0",
1665+ "create-require": "^1.1.0",
1666+ "diff": "^4.0.1",
1667+ "make-error": "^1.1.1",
1668+ "v8-compile-cache-lib": "^3.0.1",
1669+ "yn": "3.1.1"
1670+ },
1671+ "bin": {
1672+ "ts-node": "dist/bin.js",
1673+ "ts-node-cwd": "dist/bin-cwd.js",
1674+ "ts-node-esm": "dist/bin-esm.js",
1675+ "ts-node-script": "dist/bin-script.js",
1676+ "ts-node-transpile-only": "dist/bin-transpile.js",
1677+ "ts-script": "dist/bin-script-deprecated.js"
1678+ },
1679+ "peerDependencies": {
1680+ "@swc/core": ">=1.2.50",
1681+ "@swc/wasm": ">=1.2.50",
1682+ "@types/node": "*",
1683+ "typescript": ">=2.7"
1684+ },
1685+ "peerDependenciesMeta": {
1686+ "@swc/core": {
1687+ "optional": true
1688+ },
1689+ "@swc/wasm": {
1690+ "optional": true
1691+ }
1692+ }
1693+ },
15681694 "node_modules/tsscmp": {
15691695 "version": "1.0.6",
15701696 "resolved": "https://registry.npmjs.org/tsscmp/-/tsscmp-1.0.6.tgz",
@@ -1587,9 +1713,7 @@
15871713 },
15881714 "node_modules/typescript": {
15891715 "version": "4.9.4",
1590- "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
15911716 "integrity": "sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==",
1592- "dev": true,
15931717 "bin": {
15941718 "tsc": "bin/tsc",
15951719 "tsserver": "bin/tsserver"
@@ -1625,6 +1749,11 @@
16251749 "uuid": "dist/bin/uuid"
16261750 }
16271751 },
1752+ "node_modules/v8-compile-cache-lib": {
1753+ "version": "3.0.1",
1754+ "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
1755+ "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg=="
1756+ },
16281757 "node_modules/vary": {
16291758 "version": "1.1.2",
16301759 "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
@@ -1640,6 +1769,14 @@
16401769 "engines": {
16411770 "node": ">= 4.0.0"
16421771 }
1772+ },
1773+ "node_modules/yn": {
1774+ "version": "3.1.1",
1775+ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
1776+ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
1777+ "engines": {
1778+ "node": ">=6"
1779+ }
16431780 }
16441781 }
16451782 }
api/package.json+3−1
@@ -28,11 +28,13 @@
2828 "@types/koa-session": "^5.10.6",
2929 "axios": "^1.2.2",
3030 "dotenv": "^16.0.3",
31+ "http-status-codes": "^2.2.0",
3132 "koa": "2.14.1",
3233 "koa-bodyparser": "4.3.0",
3334 "koa-json": "^2.0.2",
3435 "koa-logger": "^3.2.1",
3536 "koa-router": "^12.0.0",
36- "koa-session": "^6.3.1"
37+ "koa-session": "^6.3.1",
38+ "ts-node": "^10.9.1"
3739 }
3840 }
api/src/authentication/index.ts+5−4
@@ -1,5 +1,6 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3+import { StatusCodes } from "http-status-codes";
34
45 const router = new Router();
56
@@ -20,9 +21,9 @@ router.post(
2021 user = await ctx.prisma.user.create({
2122 data: { walletAddress: data.walletAddress },
2223 });
23- ctx.status = 201;
24+ ctx.status = StatusCodes.CREATED;
2425 } else {
25- ctx.status = 200;
26+ ctx.status = StatusCodes.OK;
2627 }
2728
2829 if (ctx.session) {
@@ -38,7 +39,7 @@ router.post(
3839 "/deauthenticate",
3940 async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
4041 ctx.session = null;
41- ctx.status = 200;
42+ ctx.status = StatusCodes.OK;
4243 await next();
4344 }
4445 );
@@ -52,7 +53,7 @@ router.get(
5253 where: { walletAddress: ctx.session.user.walletAddress },
5354 });
5455
55- ctx.status = 200;
56+ ctx.status = StatusCodes.OK;
5657 }
5758
5859 await next();
api/src/discord/index.ts+66−60
@@ -1,7 +1,9 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3-import axios from "axios";
3+import axios, { AxiosError } from "axios";
44 import { stringify } from "querystring";
5+import { StatusCodes } from "http-status-codes";
6+import { getCouponCode } from "../quest/util";
57
68 const router = new Router({ prefix: "/discord" });
79
@@ -11,25 +13,25 @@ router.get(
1113 const code = ctx.request.query.code;
1214 const state = Number(ctx.request.query.state);
1315
14- if (code) {
15- const response = await axios
16- .post(
17- "https://discord.com/api/v10/oauth2/token",
18- stringify({
19- client_id: process.env.DISCORD_CLIENT_ID,
20- client_secret: process.env.DISCORD_CLIENT_SECRET,
21- grant_type: "authorization_code",
22- code: code,
23- redirect_uri: process.env.DISCORD_REDIRECT_URL,
24- }),
25- {
26- headers: { "Content-Type": "application/x-www-form-urlencoded" },
27- }
28- )
29- .catch((e) => {
30- console.log(e);
31- throw e;
32- });
16+ if (!code) {
17+ ctx.status = StatusCodes.UNAUTHORIZED;
18+ return await next();
19+ }
20+
21+ try {
22+ const response = await axios.post(
23+ "https://discord.com/api/v10/oauth2/token",
24+ stringify({
25+ client_id: process.env.DISCORD_CLIENT_ID,
26+ client_secret: process.env.DISCORD_CLIENT_SECRET,
27+ grant_type: "authorization_code",
28+ code: code,
29+ redirect_uri: process.env.DISCORD_REDIRECT_URL,
30+ }),
31+ {
32+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
33+ }
34+ );
3335
3436 const discordToken = await ctx.prisma.discordToken.create({
3537 data: {
@@ -46,16 +48,20 @@ router.get(
4648 data: { discordTokenId: discordToken.id },
4749 });
4850
49- ctx.status = 201;
51+ ctx.status = StatusCodes.CREATED;
52+ ctx.redirect("/discord/join");
53+
54+ await next();
55+ } catch (e) {
56+ console.error(e);
57+ throw e;
5058 }
51- ctx.redirect("/discord/join");
52- await next();
5359 }
5460 );
5561
5662 router.get("/join", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
5763 if (!ctx.session) {
58- ctx.status = 403;
64+ ctx.status = StatusCodes.FORBIDDEN;
5965 return await next();
6066 }
6167
@@ -77,32 +83,33 @@ router.get("/join", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
7783 return await next();
7884 }
7985
80- const inviteCode = await axios
81- .post(
82- `https://discord.com/api/v10/channels/${process.env.QUEST_CHANNEL_ID}/invites`,
83- {},
84- {
85- headers: {
86- Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
87- "Content-Type": "application/json",
88- },
89- }
90- )
91- .then((response) => response.data.code)
92- .catch((e) => {
93- console.log(e);
94- throw e;
95- });
96-
97- ctx.session.user = user;
98- ctx.redirect(`http://discord.gg/${inviteCode}`);
86+ try {
87+ const inviteCode = await axios
88+ .post(
89+ `https://discord.com/api/v10/channels/${process.env.QUEST_CHANNEL_ID}/invites`,
90+ {},
91+ {
92+ headers: {
93+ Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
94+ "Content-Type": "application/json",
95+ },
96+ }
97+ )
98+ .then((response) => response.data.code);
99+
100+ ctx.session.user = user;
101+ ctx.redirect(`http://discord.gg/${inviteCode}`);
99102
100- await next();
103+ await next();
104+ } catch (e) {
105+ console.error(e);
106+ throw e;
107+ }
101108 });
102109
103110 router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
104111 if (!ctx.session) {
105- ctx.status = 403;
112+ ctx.status = StatusCodes.FORBIDDEN;
106113 return await next();
107114 }
108115
@@ -112,29 +119,28 @@ router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
112119 Authorization: `Bearer ${ctx.session.user.discordToken.accessToken}`,
113120 "Content-Type": "application/x-www-form-urlencoded",
114121 };
115- await axios
116- .get(`https://discord.com/api/v10/users/@me/guilds/${guildId}/member`, {
117- headers: authorizationHeader,
118- })
119- .catch((e) => {
120- console.log(e);
121- throw e;
122- });
123122
124- // 10 random characters alphanumeric string
125- const coupon = Math.random()
126- .toString(36)
127- .substring(2, 12)
128- .toLocaleUpperCase();
123+ try {
124+ await axios.get(
125+ `https://discord.com/api/v10/users/@me/guilds/${guildId}/member`,
126+ {
127+ headers: authorizationHeader,
128+ }
129+ );
130+ } catch (e) {
131+ console.error(e);
132+ throw e;
133+ }
129134
130- console.log(coupon);
135+ // 10 random characters alphanumeric string
136+ const coupon = getCouponCode();
131137
132138 ctx.body = ctx.session.user = await ctx.prisma.user.update({
133139 where: { walletAddress: ctx.session.user.walletAddress },
134140 include: { discordToken: true },
135141 data: { verified: true, coupon },
136142 });
137- ctx.status = 200;
143+ ctx.status = StatusCodes.OK;
138144
139145 await next();
140146 });
api/src/index.ts+2−2
@@ -15,7 +15,7 @@ async function main() {
1515 dotenv.config();
1616
1717 const app = new Koa();
18- app.keys = ["A very good secret!"];
18+ app.keys = [process.env.APP_KEYS || ""];
1919
2020 // Expose prisma client instance to context:
2121 app.context.prisma = prisma;
@@ -46,5 +46,5 @@ main()
4646 .catch(async (e) => {
4747 await prisma.$disconnect();
4848 console.error(e);
49- process.exit();
49+ process.exit(1);
5050 });
api/src/quest/index.ts+2−1
@@ -1,5 +1,6 @@
11 import Router from "koa-router";
22 import Koa from "koa";
3+import { StatusCodes } from "http-status-codes";
34
45 const router = new Router();
56
@@ -11,7 +12,7 @@ router.get("/quest", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
1112 coverImage: process.env.QUEST_COVER_IMAGE,
1213 };
1314
14- ctx.status = 200;
15+ ctx.status = StatusCodes.OK;
1516 await next();
1617 });
1718
api/src/quest/util.ts+10−0
@@ -0,0 +1,10 @@
1+import { randomInt } from "crypto";
2+
3+const SLUG_ALPHABET = "abcdfghjkmnpqrstvwxz0123456789".split("");
4+
5+export const getCouponCode = (length = 10) => {
6+ return new Array(length)
7+ .fill(0)
8+ .map(() => SLUG_ALPHABET[randomInt(SLUG_ALPHABET.length)])
9+ .join("");
10+};
api/src/routes.ts+2−1
@@ -3,6 +3,7 @@ import Router from "koa-router";
33 import authenticationRouter from "./authentication";
44 import discordRouter from "./discord";
55 import questRouter from "./quest";
6+import { StatusCodes } from "http-status-codes";
67
78 const router = new Router();
89
@@ -11,7 +12,7 @@ router.all(
1112 async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
1213 // Check if server is healthy and has connection to db:
1314 ctx.body = `Server is healthy. \n${await ctx.prisma.user.count()} records found in the db.`;
14- ctx.status = 200;
15+ ctx.status = StatusCodes.OK;
1516
1617 await next();
1718 }
ui/src/components/AuthenticationForm/index.tsx+27−22
@@ -7,6 +7,11 @@ export function AuthenticationForm() {
77 const [isLoading, setIsLoading] = useState(false);
88 const context = useContext(PageContext);
99
10+ if (!context)
11+ throw new Error(
12+ "AuthenticationForm needs to be a child of a PageContext provider"
13+ );
14+
1015 const handleAuthenticateClick = async (e: React.MouseEvent) => {
1116 e.preventDefault();
1217 setIsLoading(true);
@@ -20,17 +25,19 @@ export function AuthenticationForm() {
2025 method: "eth_requestAccounts",
2126 });
2227
23- await axios
24- .post("/authenticate", { walletAddress: accounts[0] })
25- .then((response) => {
26- context?.setUser(response.data);
27- })
28- .catch((error) => {
29- console.log(error);
30- context?.setError("We we're unable to receive your account details.");
31- });
28+ try {
29+ const user = await axios
30+ .post("/authenticate", {
31+ walletAddress: accounts[0],
32+ })
33+ .then((response) => response.data);
34+ context.setUser(user);
35+ } catch (e) {
36+ console.error(e);
37+ context.setError("We we're unable to receive your account details.");
38+ }
3239 } else {
33- context?.setError("Please install the metamask browser extension.");
40+ context.setError("Please install the metamask browser extension.");
3441 }
3542 setIsLoading(false);
3643 };
@@ -39,17 +46,15 @@ export function AuthenticationForm() {
3946 e.preventDefault();
4047 setIsLoading(true);
4148
42- await axios
43- .post("/deauthenticate")
44- .then(() => {
45- context?.setUser(null);
46- })
47- .catch((error) => {
48- console.log(error);
49- context?.setError(
50- "We are not able to reach the api server, please try again later."
51- );
52- });
49+ try {
50+ await axios.post("/deauthenticate");
51+ context.setUser(null);
52+ } catch (e) {
53+ console.error(e);
54+ context.setError(
55+ "We are not able to reach the api server, please try again later."
56+ );
57+ }
5358
5459 setIsLoading(false);
5560 };
@@ -58,7 +63,7 @@ export function AuthenticationForm() {
5863 <div>
5964 {isLoading ? (
6065 <div>Loading...</div>
61- ) : context?.user ? (
66+ ) : context.user ? (
6267 <StyledButton onClick={handleDeauthenticateClick}>
6368 Sign out
6469 </StyledButton>
ui/src/components/Header/index.tsx+3−1
@@ -6,10 +6,12 @@ import { PageContext } from "../Page";
66
77 export default function Header() {
88 const context = useContext(PageContext);
9+ const error = context?.error;
10+
911 return (
1012 <StyledHeader>
1113 <h1>Sesame Labs Coding Assignment</h1>
12- <ErrorContainer>{context?.error}</ErrorContainer>
14+ <ErrorContainer>{error || ""}</ErrorContainer>
1315 <div>
1416 <AuthenticationForm />
1517 </div>
ui/src/components/JoinDiscordServer/index.tsx+28−28
@@ -1,40 +1,35 @@
11 import axios from "axios";
22 import React, { useContext, useState } from "react";
33 import styled from "styled-components";
4-import { StyledButton } from "../../theme/button";
4+import { LinkButton, StyledButton } from "../../theme/button";
55 import { PageContext } from "../Page";
66
77 export default function JoinDiscordGuild() {
88 const context = useContext(PageContext);
9- const [isLoading, setIsLoading] = useState(false);
10-
11- const handleJoinLink = async (e: React.MouseEvent) => {
12- e.preventDefault();
13-
14- window.open(
15- `${process.env.REACT_APP_API_URL}/discord/join`,
16- "_blank",
17- "noopener,noreferrer"
9+ if (!context)
10+ throw new Error(
11+ "JoinDiscordGuild needs to be a child of a PageContext provider"
1812 );
19- };
13+
14+ const [isLoading, setIsLoading] = useState(false);
2015
2116 const handleVerifyClick = async (e: React.MouseEvent) => {
2217 e.preventDefault();
2318 setIsLoading(true);
2419
25- await axios
26- .get("/discord/verify")
27- .then((response) => {
28- setIsLoading(false);
29- context?.setUser(response.data);
30- })
31- .catch((e) => {
32- console.log(e);
33- setIsLoading(false);
34- context?.setError(
35- "We are having trouble reaching discord, please try again later."
36- );
37- });
20+ try {
21+ const user = await axios
22+ .get("/discord/verify")
23+ .then((response) => response.data);
24+ context.setUser(user);
25+ setIsLoading(false);
26+ } catch (e) {
27+ console.error(e);
28+ context.setError(
29+ "We are having trouble reaching discord, please try again later."
30+ );
31+ setIsLoading(false);
32+ }
3833 };
3934
4035 return isLoading ? (
@@ -42,15 +37,20 @@ export default function JoinDiscordGuild() {
4237 ) : (
4338 <div>
4439 <ActionsContainer>
45- {context?.user?.verified ? (
40+ {context.user?.verified ? (
4641 <p>
4742 Verified! your coupon is:{" "}
48- <CouponContainer>{context?.user?.coupon}</CouponContainer>
43+ <CouponContainer>{context.user?.coupon}</CouponContainer>
4944 </p>
5045 ) : (
5146 <>
52- <StyledButton onClick={handleJoinLink}>Join</StyledButton>
53- <StyledButton onClick={handleVerifyClick}>verify</StyledButton>
47+ <LinkButton
48+ href={`${process.env.REACT_APP_API_URL}/discord/join`}
49+ target={"_blank"}
50+ >
51+ Join
52+ </LinkButton>
53+ <StyledButton onClick={handleVerifyClick}>Verify</StyledButton>
5454 </>
5555 )}
5656 </ActionsContainer>
ui/src/theme/button.tsx+21−0
@@ -13,3 +13,24 @@ export const StyledButton = styled.button`
1313 cursor: pointer;
1414 }
1515 `;
16+
17+export const LinkButton = styled.a`
18+ text-align: center;
19+ text-decoration: none;
20+
21+ &,
22+ &:visited {
23+ min-width: 100px;
24+ padding: 0.5em 1em;
25+ background: ${(props) => props.theme.colors.background};
26+ color: ${(props) => props.theme.colors.foreground};
27+ border: 2px solid ${(props) => props.theme.colors.highlight};
28+ border-radius: 4px;
29+ }
30+
31+ &:hover,
32+ &:active {
33+ border-color: ${(props) => props.theme.colors.altHighlight};
34+ cursor: pointer;
35+ }
36+`;
ui/src/types.ts+6−6
@@ -1,12 +1,12 @@
1-export interface UserType {
1+export type UserType = {
22 id?: number;
33 walletAddress: string;
44 discordTokenId?: number;
55 verified: boolean;
66 coupon?: string;
7-}
7+};
88
9-export interface PageContextType {
9+export type PageContextType = {
1010 user: UserType | null;
1111 setUser: Function;
1212 quest: {
@@ -17,13 +17,13 @@ export interface PageContextType {
1717 } | null;
1818 error?: string;
1919 setError: Function;
20-}
20+};
2121
22-export interface ThemeType {
22+export type ThemeType = {
2323 colors: {
2424 background: string;
2525 foreground: string;
2626 highlight: string;
2727 altHighlight: string;
2828 };
29-}
29+};