-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
96 lines (76 loc) · 2.3 KB
/
main.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
import { Application, Router } from "oak";
import { getOrderByHash, updateOrderStatus } from "./database.ts";
const router = new Router();
router.get("/", (ctx) => {
ctx.response.redirect("https://xditya.me");
});
router.get("/order", async (ctx) => {
const url = ctx.request.url;
const searchParams = url.searchParams;
const email = searchParams.get("email");
const hash = searchParams.get("hash");
if (!email || !hash) {
ctx.response.status = 400;
ctx.response.body = { error: "Missing email or hash parameter" };
return;
}
const orderDetails = await getOrderByHash(email, hash);
let dataToReturn;
if (orderDetails?.status === "Placed") {
dataToReturn = {
error: null,
hash: orderDetails?.hash,
items: orderDetails?.items,
totalCost: orderDetails?.totalCost,
};
} else {
dataToReturn = {
error: "Order deliverd!",
};
}
if (!orderDetails) {
ctx.response.status = 404;
ctx.response.body = { error: "Order not found" };
return;
}
ctx.response.status = 200;
ctx.response.body = dataToReturn;
});
router.get("/order/complete", async (ctx) => {
const url = ctx.request.url;
const searchParams = url.searchParams;
const email = searchParams.get("email");
const hash = searchParams.get("hash");
if (!email || !hash) {
ctx.response.status = 400;
ctx.response.body = { error: "Missing email or hash parameter" };
return;
}
const orderDetails = await getOrderByHash(email, hash);
if (!orderDetails) {
ctx.response.status = 404;
ctx.response.body = { error: "Order not found" };
return;
}
await updateOrderStatus(email, hash, "Done");
ctx.response.status = 200;
ctx.response.body = { status: "success" };
});
const app = new Application();
app.use(async (ctx, next) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*");
ctx.response.headers.set(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
ctx.response.headers.set(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
await next();
});
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener("error", (e) => console.log(e));
console.log("> Started listening on PORT 8000!");
await app.listen({ port: 8000 });