-
Notifications
You must be signed in to change notification settings - Fork 37
/
service.bal
376 lines (348 loc) · 15.4 KB
/
service.bal
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
// Copyright (c) 2022 WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import ballerina/http;
import ballerina/log;
import ballerina/os;
import ballerina/time;
import ballerina/uuid;
import ballerinax/jaeger as _;
import wso2/client_stubs as stubs;
import wso2/money;
const SESSION_ID_COOKIE = "sessionIdCookie";
const CURRENCY_COOKIE = "currencyCookie";
const SESSION_ID_KEY = "sessionId";
const ENABLE_SINGLE_SHARED_SESSION = "ENABLE_SINGLE_SHARED_SESSION";
final boolean isCymbalBrand = os:getEnv("CYMBAL_BRANDING") == "true";
service class AuthInterceptor {
*http:RequestInterceptor;
resource function 'default [string... path](http:RequestContext ctx, http:Request request)
returns http:NextService|error? {
http:Cookie[] cookies = request.getCookies();
http:Cookie[] sessionIDCookies = cookies.filter(cookie => cookie.name == SESSION_ID_COOKIE);
http:Cookie[] currencyCookies = cookies.filter(cookie => cookie.name == CURRENCY_COOKIE);
string sessionId;
if sessionIDCookies.length() == 0 {
if os:getEnv(ENABLE_SINGLE_SHARED_SESSION) == "true" {
// Hard coded user id, shared across sessions
sessionId = "12345678-1234-1234-1234-123456789123";
} else {
sessionId = uuid:createType1AsString();
}
http:Cookie sessionIdCookie = new (SESSION_ID_COOKIE, sessionId, path = "/");
cookies.push(sessionIdCookie);
} else {
sessionId = sessionIDCookies[0].value;
}
if currencyCookies.length() == 0 {
http:Cookie currencyCookie = new (CURRENCY_COOKIE, "USD", path = "/");
cookies.push(currencyCookie);
}
request.addCookies(cookies);
ctx.set(SESSION_ID_KEY, sessionId);
return ctx.next();
}
}
# This service serves the data required by the UI by communicating with internal gRPC services.
@http:ServiceConfig {
cors: {
allowOrigins: ["http://localhost:3000"],
allowCredentials: true
}
}
@display {
label: "Frontend",
id: "frontend"
}
service http:InterceptableService / on new http:Listener(9098) {
function init() {
log:printInfo("Frontend server started.");
}
# GET method to get the metadata like currency and cart size.
#
# + cookieHeader - header containing the cookie
# + return - `MetadataResponse` if successful or `http:Unauthorized` or `error` if an error occurs
resource function get metadata(@http:Header {name: "Cookie"} string cookieHeader)
returns MetadataResponse|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
http:Cookie|http:Unauthorized currencyCookie = getFromCookieHeader(CURRENCY_COOKIE, cookieHeader);
if currencyCookie is http:Unauthorized {
return currencyCookie;
}
stubs:Cart cart = check getCart(sessionIdCookie.value);
MetadataResponse metadataResponse = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: {
userCurrency: [currencyCookie.value, money:CURRENCY_SYMBOLS.get(currencyCookie.value)],
currencies: check getSupportedCurrencies(),
cartSize: getCartSize(cart),
isCymbalBrand: isCymbalBrand
}
};
return metadataResponse;
}
# GET method which provides products and ads.
#
# + cookieHeader - header containing the cookie
# + return - `HomeResponse` if successful or `http:Unauthorized` or `error` if an error occurs
resource function get .(@http:Header {name: "Cookie"} string cookieHeader)
returns HomeResponse|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
http:Cookie|http:Unauthorized currencyCookie = getFromCookieHeader(CURRENCY_COOKIE, cookieHeader);
if currencyCookie is http:Unauthorized {
return currencyCookie;
}
stubs:Product[] products = check getProducts();
ProductLocalized[] productsLocalized = [];
foreach stubs:Product product in products {
stubs:Money convertedMoney = check convertCurrency(product.price_usd, currencyCookie.value);
productsLocalized.push(toProductLocalized(product, money:renderMoney(convertedMoney)));
}
HomeResponse homeResponse = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: {
products: productsLocalized,
ad: check chooseAd()
}
};
return homeResponse;
}
# GET method providing a specific product.
#
# + id - product id
# + cookieHeader - header containing the cookie
# + return - `ProductResponse` if successful or an `http:Unauthorized` or `error` if an error occurs
resource function get product/[string id](@http:Header {name: "Cookie"} string cookieHeader)
returns ProductResponse|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
http:Cookie|http:Unauthorized currencycookie = getFromCookieHeader(CURRENCY_COOKIE, cookieHeader);
if currencycookie is http:Unauthorized {
return currencycookie;
}
string userId = sessionIdCookie.value;
stubs:Product product = check getProduct(id);
stubs:Money convertedMoney = check convertCurrency(product.price_usd, currencycookie.value);
ProductLocalized productLocal = toProductLocalized(product, money:renderMoney(convertedMoney));
stubs:Product[] recommendations = check getRecommendations(userId, [id]);
ProductResponse productResponse = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: {
product: productLocal,
recommendations: recommendations,
ad: check chooseAd(product.categories)
}
};
return productResponse;
}
# POST method to change the currency.
#
# + request - currency type to change
# + cookieHeader - header containing the cookie
# + return - `http:Response` if successful or an `http:Unauthorized` or `error` if an error occurs
resource function post setCurrency(@http:Payload record {|string currency;|} request, @http:Header {name: "Cookie"} string cookieHeader)
returns http:Response|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
string[] supportedCurrencies = check getSupportedCurrencies();
stubs:Cart cart = check getCart(sessionIdCookie.value);
http:Response response = new;
http:Cookie currencyCookie = new (CURRENCY_COOKIE, request.currency, path = "/");
response.addCookie(currencyCookie);
response.setJsonPayload({
userCurrency: [request.currency, money:CURRENCY_SYMBOLS.get(request.currency)],
currencies: supportedCurrencies,
cartSize: getCartSize(cart),
isCymbalBrand
});
return response;
}
# GET method providing the cart.
#
# + cookieHeader - header containing the cookie
# + return - `CartResponse` if successful or an `http:Unauthorized` or `error` if an error occurs
resource function get cart(@http:Header {name: "Cookie"} string cookieHeader)
returns CartResponse|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
http:Cookie|http:Unauthorized currencyCookie = getFromCookieHeader(CURRENCY_COOKIE, cookieHeader);
if currencyCookie is http:Unauthorized {
return currencyCookie;
}
string userId = sessionIdCookie.value;
stubs:Cart cart = check getCart(userId);
stubs:Product[] recommendations = check getRecommendations(userId, self.getProductIdFromCart(cart));
stubs:Money shippingCost = check getShippingQuote(cart.items, currencyCookie.value);
stubs:Money totalPrice = {
currency_code: currencyCookie.value,
nanos: 0,
units: 0
};
CartItemView[] cartItems = [];
foreach stubs:CartItem {product_id, quantity} in cart.items {
stubs:Product product = check getProduct(product_id);
stubs:Money convertedPrice = check convertCurrency(product.price_usd, currencyCookie.value);
stubs:Money price = money:multiplySlow(convertedPrice, quantity);
string renderedPrice = money:renderMoney(price);
cartItems.push({
product,
price: renderedPrice,
quantity: quantity
});
totalPrice = money:sum(totalPrice, price);
}
totalPrice = money:sum(totalPrice, shippingCost);
int year = time:utcToCivil(time:utcNow()).year;
int[] years = [year, year + 1, year + 2, year + 3, year + 4];
CartResponse cartResponse = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: {
recommendations,
shippingCost: money:renderMoney(shippingCost),
totalCost: money:renderMoney(totalPrice),
items: cartItems,
expirationYears: years
}
};
return cartResponse;
}
# POST method to update the cart with a product.
#
# + request - `AddToCartRequest` containing the product id of the product to be added
# + cookieHeader - header containing the cookie
# + return - `http:Created` if successful or `http:Unauthorized` or `http:BadRequest` or `error` if an error occurs
resource function post cart(@http:Payload AddToCartRequest request,
@http:Header {name: "Cookie"} string cookieHeader) returns http:Created|http:Unauthorized|http:BadRequest|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
string userId = sessionIdCookie.value;
stubs:Product|error product = getProduct(request.productId);
if product is error {
http:BadRequest badRequest = {
body: string `invalid request ${product.message()}`
};
return badRequest;
}
check insertItemToCart(userId, request.productId, request.quantity);
http:Created response = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: "item added to the cart"
};
return response;
}
# POST method to empty the cart.
#
# + cookieHeader - header containing the cookie
# + return - `http:Created` if successful or an `http:Unauthorized` or `error` if an error occurs
resource function post cart/empty(@http:Header {name: "Cookie"} string cookieHeader)
returns http:Created|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
string userId = sessionIdCookie.value;
check emptyCart(userId);
http:Created response = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: "cart emptied"
};
return response;
}
# Post method to checkout the user's cart.
#
# + request - `CheckoutRequest` containing user details
# + cookieHeader - header containing the cookie
# + return - `CheckoutResponse` if successful or an `http:Unauthorized` or `error` if an error occurs
resource function post cart/checkout(@http:Payload CheckoutRequest request,
@http:Header {name: "Cookie"} string cookieHeader) returns CheckoutResponse|http:Unauthorized|error {
http:Cookie|http:Unauthorized sessionIdCookie = getFromCookieHeader(SESSION_ID_COOKIE, cookieHeader);
if sessionIdCookie is http:Unauthorized {
return sessionIdCookie;
}
http:Cookie|http:Unauthorized currencyCookie = getFromCookieHeader(CURRENCY_COOKIE, cookieHeader);
if currencyCookie is http:Unauthorized {
return currencyCookie;
}
string userId = sessionIdCookie.value;
stubs:OrderResult orderResult = check checkoutCart({
email: request.email,
address: {
city: request.city,
country: request.country,
state: request.state,
street_address: request.streetAddress,
zip_code: request.zipCode
},
user_id: userId,
user_currency: currencyCookie.value,
credit_card: {
credit_card_cvv: request.creditCardCvv,
credit_card_expiration_month: request.creditCardExpirationMonth,
credit_card_expiration_year: request.creditCardExpirationYear,
credit_card_number: request.creditCardNumber
}
});
stubs:Product[] recommendations = check getRecommendations(userId);
stubs:Money totalCost = orderResult.shipping_cost;
foreach stubs:OrderItem {item, cost} in orderResult.items {
stubs:Money multiplyRes = money:multiplySlow(cost, item.quantity);
totalCost = money:sum(totalCost, multiplyRes);
}
CheckoutResponse checkoutResponse = {
headers: {
"Set-Cookie": sessionIdCookie.toStringValue()
},
body: {
'order: orderResult,
totalPaid: money:renderMoney(totalCost),
recommendations: recommendations
}
};
return checkoutResponse;
}
function getProductIdFromCart(stubs:Cart cart) returns string[] {
return from stubs:CartItem {product_id} in cart.items
select product_id;
}
public function createInterceptors() returns AuthInterceptor {
return new AuthInterceptor();
}
}