Skip to content
Snippets Groups Projects
Commit 06cac29e authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

storing order data on disk when payment is complete

parent 9b24a8bb
No related branches found
No related tags found
No related merge requests found
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
......@@ -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:
......
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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment