-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
187 lines (177 loc) · 10.5 KB
/
server.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
import { config } from './src/config.js';
import http from 'http';
import { auth } from './src/auth.js';
import url from 'url';
import { createClient } from 'redis';
import { createWebhook, getVerification } from './src/sheerid.js';
import { nano } from './src/nano.js';
import { getCartDiscounts, createCartDiscount, createDiscountCode, applyDiscount, getCart, getCarts } from './src/discount.js';
import fetch from 'node-fetch';
const token = await auth();
const makeid = (length) => {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return (result.substring(0, length / 2) + nano() + result.substring(length / 2, length)).toUpperCase();
}
const verificationStatus = async (verificationId) => {
return await getVerification(verificationId);
}
const onSuccess = async (cartId, verificationData) => {
return await redis.set(`cart-${cartId}`, JSON.stringify(verificationData));
}
const updateCart = async (sessionId, cartId) => {
const existing = await redis.get(`cartid-${sessionId}`);
if (existing) {
return;
}
const code = "ST"+makeid(6)
console.log('updating cart', cartId, code);
const cart = await getCart(token, cartId);
const res = await createDiscountCode(token, "Student Discount", config.CART_DISCOUNT_ID, code);
if (res) {
console.log(res);
const res2 = await applyDiscount(token, cartId, cart.version, code);
console.log(res2);
await redis.set(`cartid-${sessionId}`, cartId);
}
}
const redis = createClient();
redis.on('error', (err) => console.log('Redis Client Error', err));
console.log('connecting to Redis...');
await redis.connect();
http.createServer(async (req, res) => {
if (req.url === '/' && req.method === 'GET') {
console.log('get /');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('SheerID - commercetools Demo server');
} else if (req.url.startsWith('/api/create-webhook')) {
const q = url.parse(req.url, true).query;
const r = await createWebhook(q.pid);
res.end(JSON.stringify(r));
} else if (req.url === '/api/cart-discounts' && req.method === 'GET') {
console.log('get /api/cart-discounts');
res.writeHead(200, {'Content-Type': 'application/json'});
const discounts = await getCartDiscounts(token.access_token);
console.log('number of discounts:', discounts.total);
const o = {};
discounts.results.forEach(element => {
console.log(element);
o[element.id] = element.name.en;
});
res.end(JSON.stringify(o));
} else if (req.url.startsWith('/api/verify') && req.method === 'GET') {
const q = url.parse(req.url, true).query;
console.log('get /api/verify', q.cid);
let cartJson = await redis.get(`cart-${q.cid}`)
if (cartJson === null) {
cartJson = `{}`;
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(cartJson);
} else if (req.url.startsWith('/api/getcarts') && req.method === 'GET') {
const q = url.parse(req.url, true).query;
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(await getCarts(token)));
} else if (req.url.startsWith('/api/update') && req.method === 'GET') {
const q = url.parse(req.url, true).query;
console.log('get /api/update', q.cid);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(`{}`);
try {
updateCart(q.cid, q.cart);
} catch (e) {
console.log(e);
}
// return;
// const storedCart = await redis.get(`cart-${q.cid}`);
// if (!storedCart) {
// return;
// }
// const rb = await fetch("https://api.europe-west1.gcp.commercetools.com/sunrise-spa/graphql", {
// "headers": {
// "authorization": "Bearer "+q.t,
// "content-type": "application/json",
// },
// "method": "POST",
// "body": "{\"operationName\":\"myCart\",\"variables\":{\"locale\":\"en\"},\"query\":\"query myCart($locale: Locale!) {\\n myCart: me {\\n activeCart {\\n cartId: id\\n version\\n lineItems {\\n lineId: id\\n name(locale: $locale)\\n productSlug(locale: $locale)\\n quantity\\n price {\\n value {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n discounted {\\n value {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n discount {\\n name(locale: $locale)\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n totalPrice {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n variant {\\n sku\\n images {\\n url\\n __typename\\n }\\n attributesRaw {\\n name\\n value\\n attributeDefinition {\\n type {\\n name\\n __typename\\n }\\n name\\n label(locale: $locale)\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n totalPrice {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n shippingInfo {\\n shippingMethod {\\n methodId: id\\n name\\n localizedDescription(locale: $locale)\\n __typename\\n }\\n price {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n __typename\\n }\\n taxedPrice {\\n totalGross {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n totalNet {\\n centAmount\\n currencyCode\\n fractionDigits\\n __typename\\n }\\n __typename\\n }\\n discountCodes {\\n discountCode {\\n codeId: id\\n code\\n name(locale: $locale)\\n __typename\\n }\\n __typename\\n }\\n shippingAddress {\\n firstName\\n lastName\\n streetName\\n additionalStreetInfo\\n postalCode\\n city\\n country\\n phone\\n email\\n __typename\\n }\\n billingAddress {\\n firstName\\n lastName\\n streetName\\n additionalStreetInfo\\n postalCode\\n city\\n country\\n phone\\n email\\n __typename\\n }\\n __typename\\n }\\n __typename\\n }\\n}\"}",
// });
// const r = await rb.json();
// const activeCart = await r.data.myCart.activeCart;
// console.log('cart version:', JSON.stringify(activeCart.version));
// const code = "ST"+makeid(6)
// console.log('updating cart', activeCart.cartId, code);
// const dc = await createDiscountCode(token, "Student Discount", config.CART_DISCOUNT_ID, code);
// console.log('discountcode:', dc);
// if (dc) {
// const qi = JSON.stringify({
// "operationName":"mutateCart",
// "variables":{
// "actions":[{"addDiscountCode":{"code": dc.code }}],
// "version":activeCart.version,
// "id":activeCart.cartId,
// },
// "query":"mutation mutateCart($actions: [MyCartUpdateAction!]!, $version: Long!, $id: String!) {\n updateMyCart(actions: $actions, version: $version, id: $id) {\n id\n version\n lineItems {\nlineId: id\nquantity\n__typename\n}\n__typename\n}\n}",
// })
// console.log('q', qi);
// fetch("https://api.europe-west1.gcp.commercetools.com/sunrise-spa/graphql", {
// "headers": {
// "authorization": "Bearer "+q.t,
// "content-type": "application/json",
// },
// "method": "POST",
// "body": qi,
// }).then(async(res)=>{
// const r = await res.json();
// console.log(r)
// });
// }
} else if (req.url === '/api/success-webhook' && req.method === 'POST') {
console.log('post /api/success-webhook');
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
res.end('OK');
if (body != undefined && body.length > 0) {
const res = JSON.parse(body);
verificationStatus(res.verificationId).then((r) => {
try {
if (r.personInfo?.metadata != undefined) {
const cartId = r.personInfo.metadata.cartid;
console.log(`saving ${cartId} cart id`);
onSuccess(cartId, r);
} else {
console.log('no metadata', r);
}
} catch(err) {
console.log('error:', err);
}
});
}
});
} else if (req.url === '/api/webhook' && req.method === 'POST') {
console.log('post /api/cart-discounts');
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
res.end('OK');
if (body != undefined && body.length > 0) {
const r = JSON.parse(body);
console.log('processing name', r.name);
createCartDiscount(token, r);
}
});
} else {
console.log('404', req);
res.statusCode = 404;
res.end('404: File Not Found');
}
}).listen(config.PORT);
console.log(`server is running on http://localhost:${config.PORT}/`);