xonecas.io
4436
Sl

sesame-labs-coding-assignment / commit 3946df6

Use crypto to generate coupon

Sean2023-01-17 3946df677112fdfe2a0f81cbd179d7f5aeb3ec5e parent 6cdc4e1

3 files changed

api/package-lock.json+0−10
@@ -11,7 +11,6 @@
1111 "dependencies": {
1212 "@koa/cors": "^4.0.0",
1313 "@prisma/client": "^4.8.1",
14- "@types/http-status-codes": "^1.2.0",
1514 "@types/koa-session": "^5.10.6",
1615 "axios": "^1.2.2",
1716 "dotenv": "^16.0.3",
@@ -213,15 +212,6 @@
213212 "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.1.tgz",
214213 "integrity": "sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ=="
215214 },
216- "node_modules/@types/http-status-codes": {
217- "version": "1.2.0",
218- "resolved": "https://registry.npmjs.org/@types/http-status-codes/-/http-status-codes-1.2.0.tgz",
219- "integrity": "sha512-vjpjevMaxtrtdrrV/TQNIFT7mKL8nvIKG7G/LjMDZdVvqRxRg5SNfGkeuSaowVc0rbK8xDA2d/Etunyb5GyzzA==",
220- "deprecated": "This is a stub types definition for http-status-codes (https://github.com/prettymuchbryce/node-http-status). http-status-codes provides its own type definitions, so you don\\'t need @types/http-status-codes installed!",
221- "dependencies": {
222- "http-status-codes": "*"
223- }
224- },
225215 "node_modules/@types/keygrip": {
226216 "version": "1.0.2",
227217 "resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.2.tgz",
api/src/discord/index.ts+2−4
@@ -3,6 +3,7 @@ import Koa from "koa";
33 import axios from "axios";
44 import { stringify } from "querystring";
55 import { StatusCodes } from "http-status-codes";
6+import { getCouponCode } from "../quest/util";
67
78 const router = new Router({ prefix: "/discord" });
89
@@ -123,10 +124,7 @@ router.get("/verify", async (ctx: Koa.ParameterizedContext, next: Koa.Next) => {
123124 });
124125
125126 // 10 random characters alphanumeric string
126- const coupon = Math.random()
127- .toString(36)
128- .substring(2, 12)
129- .toLocaleUpperCase();
127+ const coupon = getCouponCode();
130128
131129 console.log(coupon);
132130
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+};