From a2a9c8c3c44b4642d7993ef2a353c2a6a2077a29 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sun, 11 Dec 2022 19:20:27 +0100 Subject: [PATCH] Resolved prettier & eslint issues --- src/database/index.ts | 2 +- src/stripe/stripeApi.ts | 2 +- src/webserver/controllers/loginController.ts | 2 +- .../controllers/productController.ts | 2 +- .../controllers/stripePaymentController.ts | 68 ++++++++++++++----- src/webserver/server.ts | 4 -- src/webserver/websocketCartServer.ts | 5 +- 7 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/database/index.ts b/src/database/index.ts index a77d61e..47b20a1 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -8,7 +8,7 @@ const password = configuration.get("database", "password"); const host = configuration.get("database", "host"); const dbName = configuration.get("database", "database"); -let postgresDB = new PostgresDatabase(user, password, host, dbName); +const postgresDB = new PostgresDatabase(user, password, host, dbName); postgresDB.connect(); diff --git a/src/stripe/stripeApi.ts b/src/stripe/stripeApi.ts index 289d028..37376af 100644 --- a/src/stripe/stripeApi.ts +++ b/src/stripe/stripeApi.ts @@ -57,7 +57,7 @@ class StripeApi { }); } - createProduct(cart) { + createProduct() { return; } diff --git a/src/webserver/controllers/loginController.ts b/src/webserver/controllers/loginController.ts index 1718ced..63fa404 100644 --- a/src/webserver/controllers/loginController.ts +++ b/src/webserver/controllers/loginController.ts @@ -1,5 +1,5 @@ import cookie from "cookie"; -import type { Request, Response, NextFunction } from "express"; +import type { Request, Response } from "express"; const cookieOptions = { path: "/", diff --git a/src/webserver/controllers/productController.ts b/src/webserver/controllers/productController.ts index 0022414..c5c0f80 100644 --- a/src/webserver/controllers/productController.ts +++ b/src/webserver/controllers/productController.ts @@ -109,7 +109,7 @@ async function addSku(req: Request, res: Response) { const skus = await productRepository.getSkus(product_id); if (!skus.find((sku) => sku.default_price === true)) { - const setDefaultResponse = await productRepository.setSkuDefaultPrice( + await productRepository.setSkuDefaultPrice( product_id, skus[skus.length - 1].sku_id ); diff --git a/src/webserver/controllers/stripePaymentController.ts b/src/webserver/controllers/stripePaymentController.ts index 2917ba2..d407f85 100644 --- a/src/webserver/controllers/stripePaymentController.ts +++ b/src/webserver/controllers/stripePaymentController.ts @@ -1,11 +1,12 @@ +import Stripe from "stripe"; + +import logger from "../../logger"; import Configuration from "../../config/configuration"; import StripeApi from "../../stripe/stripeApi"; import StripeRepository from "../../stripe/stripeRepository"; import OrderRepository from "../../order"; import CustomerRepository from "../../customer"; -import Stripe from "stripe"; - import type { Request, Response, NextFunction } from "express"; import type { IOrder, ILineItem } from "../../interfaces/IOrder"; import type ICustomer from "../../interfaces/ICustomer"; @@ -22,29 +23,62 @@ async function create(req, res) { const clientId = req?.planetId; const { order_id, customer_no } = req.body; + logger.info("Creating stripe payment intent", { + client_id: clientId, + order_id, + customer_no, + }); + if (!order_id || !customer_no) { + logger.warning("Missing order_id and/or customer_id"); + return res.status(400).send({ success: false, message: "Missing order_id and/or customer_id", }); } - const order: IOrder = await orderRepository.getOrder(order_id); - const customer: ICustomer = await customerRepository.getCustomer(customer_no); - - const sum = order.lineItems?.reduce( - (total, lineItem: ILineItem) => total + lineItem.quantity * lineItem.price, - 0 - ); - - stripeRepository - .createPayment(clientId, sum, order_id, customer) - .then((clientSecret) => - res.send({ - success: true, - clientSecret, - }) + try { + const order: IOrder = await orderRepository.getOrder(order_id); + const customer: ICustomer = await customerRepository.getCustomer( + customer_no ); + + const sum = order.lineItems?.reduce( + (total, lineItem: ILineItem) => + total + lineItem.quantity * lineItem.price, + 0 + ); + + stripeRepository + .createPayment(clientId, sum, order_id, customer) + .then((clientSecret) => { + logger.info("New stripe payment", { + client_id: clientId, + client_secret: clientSecret, + }); + + res.send({ + success: true, + clientSecret, + }); + }); + } catch (error) { + res.statusCode = error?.statusCode || 500; + logger.error("Error creating stripe payment intent", { + error, + client_id: clientId, + customer_no, + order_id, + }); + + res.send({ + success: false, + message: + error?.message || + "Unexcepted error while creating stripe payment intent", + }); + } } async function updatePayment(req: Request, res: Response) { diff --git a/src/webserver/server.ts b/src/webserver/server.ts index efc0ccb..4662d3f 100644 --- a/src/webserver/server.ts +++ b/src/webserver/server.ts @@ -1,5 +1,3 @@ -// import * as global from "../types/global"; -import path from "path"; import express from "express"; import { createServer } from "http"; @@ -29,8 +27,6 @@ app.use(setupCORS); app.use(setupHeaders); app.use(getOrSetCookieForClient); -// parse application/json - const router = express.Router(); router.use(express.json()); diff --git a/src/webserver/websocketCartServer.ts b/src/webserver/websocketCartServer.ts index 7e8db53..7ecde60 100644 --- a/src/webserver/websocketCartServer.ts +++ b/src/webserver/websocketCartServer.ts @@ -1,5 +1,4 @@ import cookie from "cookie"; -import { ClientRequest } from "http"; import { WebSocketServer, Websocket, Request } from "ws"; import WSCart from "../cart/WSCart"; import CartSession from "../cart/CartSession"; @@ -26,9 +25,9 @@ function setupCartWebsocketServer(server) { // setInterval(() => cartSession.listCarts(), 3000); setInterval(() => cartSession.removeIfNotAlive(), 1000); - wss.on("connection", (ws, req) => { + wss.on("connection", (ws: Websocket, req: Request) => { const sessionId = generateUUID(); - let clientId = + const clientId = getCookieValue(req.headers.cookie, "planetId") || getHeaderValue(req.url, "planetId");