@nka212bg/backend-utils 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/os.js +25 -7
- package/package.json +1 -1
- package/stripe.js +83 -84
package/os.js
CHANGED
|
@@ -29,25 +29,42 @@ exports.rebootServer = ({ password } = {}) => {
|
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
exports.backupSystem = async ({ basePath, archiveName,
|
|
32
|
+
exports.backupSystem = async ({ basePath, archiveName, override, items, count = 5 } = {}) => {
|
|
33
33
|
try {
|
|
34
34
|
const startTime = Date.now();
|
|
35
35
|
|
|
36
36
|
basePath = basePath || `${__basedir}/backups`;
|
|
37
|
-
archiveName = archiveName || new Date().toISOString().split("T")[0];
|
|
38
|
-
const fullPath = `${basePath}/${archiveName}`;
|
|
39
37
|
!existsSync(basePath) && mkdirSync(basePath, { recursive: true });
|
|
40
|
-
|
|
38
|
+
|
|
39
|
+
|
|
41
40
|
const availableBackups = readdirSync(basePath);
|
|
42
|
-
if (availableBackups.length >
|
|
41
|
+
if (availableBackups.length > parseInt(count)) {
|
|
42
|
+
let oldestFile = null;
|
|
43
|
+
let oldestTime = Infinity;
|
|
44
|
+
|
|
45
|
+
availableBackups.forEach(file => {
|
|
46
|
+
file = `${basePath}/${file}`;
|
|
47
|
+
const stats = lstatSync(file);
|
|
48
|
+
|
|
49
|
+
if (stats.isFile() && stats.birthtimeMs < oldestTime) {
|
|
50
|
+
oldestTime = stats.birthtimeMs;
|
|
51
|
+
oldestFile = file;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
rmSync(oldestFile, { recursive: true, force: true });
|
|
55
|
+
}
|
|
43
56
|
|
|
44
|
-
|
|
57
|
+
archiveName = archiveName || new Date().toISOString().split("T")[0];
|
|
58
|
+
const fullPath = `${basePath}/${archiveName}`;
|
|
59
|
+
|
|
60
|
+
if (!override && existsSync(`${fullPath}.zip`)) return console.log("backupSystem info, file awready exists --> ", `${fullPath}.zip`);
|
|
45
61
|
|
|
46
62
|
if (!items) throw "No items";
|
|
47
63
|
if (typeof items == "string") items = [items];
|
|
48
64
|
|
|
49
65
|
const zip = new AdmZip();
|
|
50
|
-
items.forEach((e) =>
|
|
66
|
+
items.forEach((e) => lstatSync(e).isFile() ? zip.addLocalFile(e) : zip.addLocalFolder(e, e.split('/').pop()));
|
|
67
|
+
|
|
51
68
|
zip.writeZip(`${fullPath}.zip`);
|
|
52
69
|
|
|
53
70
|
console.info(`System backup --> time: ${parseInt((Date.now() - startTime) / 1000)}`);
|
|
@@ -61,6 +78,7 @@ exports.osStatus = () => {
|
|
|
61
78
|
|
|
62
79
|
try {
|
|
63
80
|
return {
|
|
81
|
+
serverStart,
|
|
64
82
|
serverUptime: secToTime((now - serverStart) / 1000),
|
|
65
83
|
machineUptime: secToTime(os.uptime()),
|
|
66
84
|
memoryUsage: shortCount(os.totalmem() - os.freemem(), "disk"),
|
package/package.json
CHANGED
package/stripe.js
CHANGED
|
@@ -1,98 +1,97 @@
|
|
|
1
|
-
const
|
|
2
|
-
appInfo: {
|
|
3
|
-
name: "buukmarks.com",
|
|
4
|
-
version: "1.0.0",
|
|
5
|
-
},
|
|
6
|
-
});
|
|
1
|
+
const Stripe = require("stripe");
|
|
7
2
|
|
|
8
|
-
exports
|
|
9
|
-
|
|
10
|
-
};
|
|
3
|
+
module.exports = (key, appInfo) => {
|
|
4
|
+
const stripe = Stripe(key, { appInfo });
|
|
11
5
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} catch (error) {
|
|
17
|
-
return { error };
|
|
18
|
-
}
|
|
19
|
-
};
|
|
6
|
+
return {
|
|
7
|
+
async balance() {
|
|
8
|
+
return await stripe.balance.retrieve();
|
|
9
|
+
},
|
|
20
10
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
11
|
+
async createCustomer({ name, email, source, metadata }) {
|
|
12
|
+
try {
|
|
13
|
+
const data = await stripe.customers.create({ name, email, source, metadata });
|
|
14
|
+
return { data };
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return { error };
|
|
17
|
+
}
|
|
18
|
+
},
|
|
29
19
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
async updateCustomer({ stripe_id, source, metadata }) {
|
|
21
|
+
try {
|
|
22
|
+
const data = await stripe.customers.update(stripe_id, { source, metadata });
|
|
23
|
+
return { data };
|
|
24
|
+
} catch (error) {
|
|
25
|
+
return { error };
|
|
26
|
+
}
|
|
27
|
+
},
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
29
|
+
async getCustomerSubscription(stripe_id) {
|
|
30
|
+
try {
|
|
31
|
+
const customer = await stripe.customers.retrieve(stripe_id);
|
|
32
|
+
const paymentMethod = await stripe.customers.retrievePaymentMethod(customer.id, customer.default_source);
|
|
33
|
+
return { data: { customer, paymentMethod } };
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return { error };
|
|
36
|
+
}
|
|
37
|
+
},
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
async getPayments(user_id) {
|
|
40
|
+
if (!user_id) return { error: "missing_required" };
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
try {
|
|
43
|
+
let { data = [], ...rest } = await stripe.charges.search({
|
|
44
|
+
query: `metadata["user_id"]:"${user_id}"`,
|
|
45
|
+
});
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// raw: e,
|
|
63
|
-
};
|
|
64
|
-
});
|
|
47
|
+
data = data.map((e) => ({
|
|
48
|
+
id: e.id,
|
|
49
|
+
amount: e.amount,
|
|
50
|
+
currency: e.currency,
|
|
51
|
+
paid_on: e.created,
|
|
52
|
+
description: e.description,
|
|
53
|
+
paid_until: e.metadata.paid_until,
|
|
54
|
+
card: {
|
|
55
|
+
brand: e.source.brand.toLowerCase(),
|
|
56
|
+
last4: e.source.last4,
|
|
57
|
+
country: e.source.country,
|
|
58
|
+
},
|
|
59
|
+
receipt_url: e.receipt_url,
|
|
60
|
+
status: e.status,
|
|
61
|
+
}));
|
|
65
62
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
63
|
+
return { data, rest };
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return { error };
|
|
66
|
+
}
|
|
67
|
+
},
|
|
71
68
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
async chargeCustomer({ stripe_id, description, amount, currency, metadata }) {
|
|
70
|
+
amount = Number(amount);
|
|
71
|
+
if (!amount) return { error: "no_amount" };
|
|
75
72
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
try {
|
|
74
|
+
const data = await stripe.charges.create({
|
|
75
|
+
customer: stripe_id,
|
|
76
|
+
description: description || "misc payment",
|
|
77
|
+
currency: currency || "usd",
|
|
78
|
+
amount,
|
|
79
|
+
metadata,
|
|
80
|
+
});
|
|
84
81
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
82
|
+
return { data };
|
|
83
|
+
} catch (error) {
|
|
84
|
+
return { error };
|
|
85
|
+
}
|
|
86
|
+
},
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
async unsubscribeStripe(stripe_id) {
|
|
89
|
+
try {
|
|
90
|
+
const data = await stripe.customers.del(stripe_id);
|
|
91
|
+
return { data };
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return { error };
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
};
|
|
98
97
|
};
|