@nka212bg/backend-utils 0.1.21 → 0.1.23

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/README.md CHANGED
File without changes
package/db/dbUtils.js CHANGED
File without changes
package/db/mysql2.js CHANGED
File without changes
package/db/sqlite3.js CHANGED
File without changes
package/index.js CHANGED
File without changes
File without changes
File without changes
package/location/index.js CHANGED
File without changes
File without changes
File without changes
File without changes
package/markdown/index.js CHANGED
File without changes
File without changes
package/misc.js CHANGED
@@ -113,31 +113,22 @@ exports.handlebars = ({ template, ...params } = {}) => {
113
113
  return template;
114
114
  };
115
115
 
116
- exports.shortCount = (value, type) => {
116
+ exports.shortCount = (value, bites) => {
117
117
  value = Number(value);
118
- if (isNaN(value)) return value;
118
+ if (!value) return "";
119
119
 
120
120
  let units = {
121
- trillion: "T",
122
- billion: "B",
123
- million: "M",
124
- thousand: "K",
121
+ trillion: bites ? "tb" : "T",
122
+ billion: bites ? "gb" : "B",
123
+ million: bites ? "mb" : "M",
124
+ thousand: bites ? "kb" : "K",
125
125
  };
126
126
 
127
- if (type == "disk") {
128
- units = {
129
- trillion: " tb",
130
- billion: " gb",
131
- million: " mb",
132
- thousand: " kb",
133
- };
134
- }
135
-
136
- if (value > 1000000000000) return (value / 1000000000000).toFixed(1) + units.trillion;
137
- if (value > 1000000000) return (value / 1000000000).toFixed(1) + units.billion;
138
- if (value > 1000000) return (value / 1000000).toFixed(1) + units.million;
139
- if (value > 1000) return (value / 1000).toFixed(1) + units.thousand;
140
- return value + (type == "disk" ? " bites" : "");
127
+ if (value >= 1000000000000) return (value / 1000000000000).toFixed(1) + units.trillion;
128
+ if (value >= 1000000000) return (value / 1000000000).toFixed(1) + units.billion;
129
+ if (value >= 1000000) return (value / 1000000).toFixed(1) + units.million;
130
+ if (value >= 1000) return (value / 1000).toFixed(1) + units.thousand;
131
+ return value + (bites ? "bit" : "");
141
132
  };
142
133
 
143
134
  exports.saveFile = (file, destinationPath, callback) => {
@@ -385,26 +376,37 @@ exports.crypto = ({ salt, embedSalt } = {}) => {
385
376
  }
386
377
  };
387
378
 
388
- exports.secToTime = (seconds) => {
389
- if (!(seconds = parseInt(seconds))) return;
390
-
391
- const years = seconds / 60 / 60 / 24 / 30.5 / 12;
392
- const months = seconds / 60 / 60 / 24 / 30.5;
393
- const days = seconds / 60 / 60 / 24;
394
- const hours = seconds / 60 / 60;
395
- const minutes = seconds / 60;
396
- seconds = parseInt(seconds % 60);
397
-
398
- if (years >= 1) return parseInt(years) + "y " + parseInt(months % 12) + "m " + parseInt(days % 30.5) + "d";
399
- if (months >= 1) return parseInt(months) + "m " + parseInt(days % 30.5) + "d " + parseInt(hours % 24) + "h";
400
- if (days >= 1) return parseInt(days) + "d " + parseInt(hours % 24) + "h";
401
- if (hours >= 1) return parseInt(hours) + "h " + doMinutes(minutes) + "min";
402
- return parseInt(minutes) + "min " + (seconds < 10 ? "0" + seconds : seconds) + "s";
403
-
404
- function doMinutes(minutes) {
405
- minutes = parseInt(minutes % 60);
406
- return minutes < 10 ? "0" + minutes : minutes;
407
- }
379
+ exports.secToTime = (sec) => {
380
+ sec = parseInt(sec);
381
+ if (!sec) return;
382
+
383
+ const SEC_IN_MIN = 60;
384
+ const SEC_IN_HOUR = 3600;
385
+ const SEC_IN_DAY = 86400;
386
+ const SEC_IN_MONTH = SEC_IN_DAY * 30.5;
387
+ const SEC_IN_YEAR = SEC_IN_MONTH * 12;
388
+
389
+ const years = Math.floor(sec / SEC_IN_YEAR);
390
+ sec %= SEC_IN_YEAR;
391
+
392
+ const months = Math.floor(sec / SEC_IN_MONTH);
393
+ sec %= SEC_IN_MONTH;
394
+
395
+ const days = Math.floor(sec / SEC_IN_DAY);
396
+ sec %= SEC_IN_DAY;
397
+
398
+ const hours = Math.floor(sec / SEC_IN_HOUR);
399
+ sec %= SEC_IN_HOUR;
400
+
401
+ const minutes = Math.floor(sec / SEC_IN_MIN);
402
+ const seconds = sec % SEC_IN_MIN;
403
+
404
+ if (years > 0) return `${years}y ${months}m ${days}d`;
405
+ if (months > 0) return `${months}m ${days}d ${hours}h`;
406
+ if (days > 0) return `${days}d ${hours}h`;
407
+ if (hours > 0) return `${hours}h ${String(minutes).padStart(2, "0")}min`;
408
+
409
+ return `${minutes}min ${String(seconds).padStart(2, "0")}s`;
408
410
  };
409
411
 
410
412
  exports.urlMetadata = async ({ url }) => {
package/os.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.21",
4
+ "version": "0.1.23",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",
package/session.js CHANGED
File without changes
package/socket.js CHANGED
File without changes
package/stripe.js CHANGED
@@ -1,98 +1,97 @@
1
- const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY, {
2
- appInfo: {
3
- name: "buukmarks.com",
4
- version: "1.0.0",
5
- },
6
- });
1
+ const Stripe = require("stripe");
7
2
 
8
- exports.balance = async () => {
9
- return await stripe.balance.retrieve();
10
- };
3
+ module.exports = (key, appInfo) => {
4
+ const stripe = Stripe(key, { appInfo });
11
5
 
12
- exports.createCustomer = async ({ name, email, source, metadata }) => {
13
- try {
14
- const data = await stripe.customers.create({ name, email, source, metadata });
15
- return { data };
16
- } catch (error) {
17
- return { error };
18
- }
19
- };
6
+ return {
7
+ async balance() {
8
+ return await stripe.balance.retrieve();
9
+ },
20
10
 
21
- exports.updateCustomer = async ({ stripe_id, source, metadata }) => {
22
- try {
23
- const data = await stripe.customers.update(stripe_id, { source, metadata });
24
- return { data };
25
- } catch (error) {
26
- return { error };
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
- exports.getCustomerSubscription = async (stripe_id) => {
31
- try {
32
- const customer = await stripe.customers.retrieve(stripe_id);
33
- const paymentMethod = await stripe.customers.retrievePaymentMethod(customer.id, customer.default_source);
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
- return { data: { customer, paymentMethod } };
36
- } catch (error) {
37
- return { error };
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
- exports.getPayments = async (user_id) => {
42
- if (!user_id) return { error: "missing_required" };
39
+ async getPayments(user_id) {
40
+ if (!user_id) return { error: "missing_required" };
43
41
 
44
- try {
45
- let { data = [], ...rest } = await stripe.charges.search({ query: `metadata["user_id"]:"${user_id}"` });
42
+ try {
43
+ let { data = [], ...rest } = await stripe.charges.search({
44
+ query: `metadata["user_id"]:"${user_id}"`,
45
+ });
46
46
 
47
- data = data.map((e) => {
48
- return {
49
- id: e.id,
50
- amount: e.amount / 100,
51
- currency: e.currency,
52
- paid_on: e.created,
53
- description: e.description,
54
- paid_until: e.metadata.paid_until,
55
- card: {
56
- brand: e.source.brand.toLowerCase(),
57
- last4: e.source.last4,
58
- country: e.source.country,
59
- },
60
- receipt_url: e.receipt_url,
61
- status: e.status,
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
- return { data, rest };
67
- } catch (error) {
68
- return { error };
69
- }
70
- };
63
+ return { data, rest };
64
+ } catch (error) {
65
+ return { error };
66
+ }
67
+ },
71
68
 
72
- exports.chargeCustomer = async ({ stripe_id, description, amount, currency, metadata }) => {
73
- amount = Number(amount);
74
- if (!amount) return { error: "no_amount" };
69
+ async chargeCustomer({ stripe_id, description, amount, currency, metadata }) {
70
+ amount = Number(amount);
71
+ if (!amount) return { error: "no_amount" };
75
72
 
76
- try {
77
- const data = await stripe.charges.create({
78
- customer: stripe_id,
79
- description: description || "OceanDIV undescribed payment from chat app.",
80
- currency: currency || "usd",
81
- amount,
82
- metadata,
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
- return { data };
86
- } catch (error) {
87
- return { error };
88
- }
89
- };
82
+ return { data };
83
+ } catch (error) {
84
+ return { error };
85
+ }
86
+ },
90
87
 
91
- exports.unsubscribeStripe = async (stripe_id) => {
92
- try {
93
- const data = await stripe.customers.del(stripe_id);
94
- return { data };
95
- } catch (error) {
96
- return { error };
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
  };
package/systemLog.js CHANGED
File without changes