diff --git a/build/pass/Dockerfile b/build/pass/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..a6f854e50d47fcb410506c95892ada146bef33f0 --- /dev/null +++ b/build/pass/Dockerfile @@ -0,0 +1,8 @@ +FROM node:19-bullseye + +VOLUME ["/data"] +RUN mkdir /data && chown 1000:1000 /data + +USER 1000 + +CMD [ "bash", "-c", "npm i && npm run dev"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c163b658bda658df8b72cc01a51699e9e8d992e3..2939cc0ac2fa4e61493981f9710f84f8f40c057f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,17 +3,18 @@ version: '3' volumes: nodecache: {} redisdata: {} + mgpassdata: {} services: express: - image: node:19-bullseye - command: bash -c "npm i && npm run dev" - user: 1000:1000 + build: + context: ./build/pass environment: - NODE_ENV=development working_dir: /app volumes: - ./pass:/app + - mgpassdata:/data ports: - 8080:3000 redis: diff --git a/pass/app/Order.js b/pass/app/Order.js index 3f8e31200da9b8378d88d100bd2b87dc98f53f99..fafabd5844a0201f95baefd572320a3e54ec6d33 100644 --- a/pass/app/Order.js +++ b/pass/app/Order.js @@ -1,6 +1,7 @@ const config = require("config"); const Crypto = require("./Crypto"); const dayjs = require("dayjs"); +const path = require("path"); class Order { static get STORAGE_KEY_PREFIX() { @@ -44,6 +45,7 @@ class Order { * Data populated by context */ #order_date; + #order_path; #create_mode; #redis_client; @@ -60,6 +62,17 @@ class Order { this.#expires_at = dayjs(expires_at); this.#order_date = dayjs.unix(this.#order_id.substr(0, 10)); + this.#order_path = config.get("storage.data_path"); + if (process.env.NODE_ENV === "development") { + this.#order_path = path.join(this.#order_path, "development"); + } + this.#order_path = path.join( + this.#order_path, + this.#order_date.format("YYYY"), + this.#order_date.format("MM"), + "orders" + ); + this.#amount = parseInt(amount); this.#unit_size = parseInt(unit_size); this.#price_per_unit = parseFloat(price_per_unit); @@ -130,9 +143,20 @@ class Order { }); let redis_key = Order.STORAGE_KEY_PREFIX + order_id; + + let order_file = path.join( + this.#order_path, + this.#order_id.toString() + ".json" + ); redis_client.hgetall(redis_key).then((order_data) => { if (Object.keys(order_data).length === 0) { - return reject("Could not find Order in our database!"); + // Checking FS for order + let fs = require("fs"); + if (fs.existsSync(order_file)) { + order_data = JSON.parse(fs.readFileSync(order_file)); + } else { + return reject("Could not find Order in our database! Checking FS"); + } } let loaded_order = new Order( order_data.order_id, @@ -169,21 +193,20 @@ class Order { if (this.#create_mode && key_exists) { return Promise.reject("Cannot create a Order that already exists!"); } + let stored_data = { + order_id: this.#order_id, + expires_at: this.#expires_at.format("YYYY-MM-DD"), + amount: this.#amount, + unit_size: this.#unit_size, + price_per_unit: this.#price_per_unit, + encrypted_sales_receipts: JSON.stringify(this.#encrypted_sales_receipts), + signatures: JSON.stringify(this.#signatures), + payment_completed: this.#payment_completed, + payment_method_link: JSON.stringify(this.#payment_method_link), + }; let storage_promise = this.#redis_client - .hmset(redis_key, { - order_id: this.#order_id, - expires_at: this.#expires_at.format("YYYY-MM-DD"), - amount: this.#amount, - unit_size: this.#unit_size, - price_per_unit: this.#price_per_unit, - encrypted_sales_receipts: JSON.stringify( - this.#encrypted_sales_receipts - ), - signatures: JSON.stringify(this.#signatures), - payment_completed: this.#payment_completed, - payment_method_link: JSON.stringify(this.#payment_method_link), - }) + .hmset(redis_key, stored_data) .then((result) => { this.#create_mode = false; }) @@ -192,6 +215,23 @@ class Order { }); await storage_promise; + + // If the Order payment is completed store the order additionally directly to harddisk + let fs = require("fs"); + let order_file = path.join( + this.#order_path, + this.#order_id.toString() + ".json" + ); + // Create directory if it does not exist + if (!fs.existsSync(path.dirname(order_file))) { + fs.mkdirSync(path.dirname(order_file), { recursive: true }); + } + storage_promise = fs.writeFileSync( + order_file, + JSON.stringify(stored_data, null, 4) + ); + + await storage_promise; } async delete() {