Skip to content

Commit e87a855

Browse files
committed
Update tracking list acc to db change
1 parent 61fa34b commit e87a855

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

bot.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ bot.command("track", async (ctx) => {
104104

105105
bot.command("list", async (ctx) => {
106106
try {
107-
const products = await manageProducts({ userId: ctx.from.id }, "read");
107+
const products = await manageProducts({ 'users.userId': ctx.from.id }, "read");
108108
const list = products.result
109109
.map(
110110
(product) =>
111-
`<b>${product.title}</b>\nLast Price: ${product.price}\nLink: <a href="${product.link}">${product.merchant}</a>\nTo stop send /stop_${product.tracking_id}`
111+
`<b>${product.title}</b>\nLast Price: ${product.price}\nLink: <a href="${product.link}">${product.merchant}</a>\nTo stop send /stop_${product.users.filter(u => u.userId == ctx.from.id)[0].tracking_id}`
112112
)
113113
.join("\n\n");
114114
ctx.reply(`Here is your tracking list:\n\n${list}`, {
@@ -184,10 +184,10 @@ bot.command("users", async (ctx) => {
184184
bot.command("stats", async (ctx) => {
185185
const users = await manageUsers({}, "read");
186186
const products = await manageProducts({}, "read");
187-
let userCount = 0;
187+
let productCount = 0;
188188
products.result.map((p) => (p += item.users.length));
189189
ctx.reply(
190-
`Total Users: ${users.result.length}\nTotal Products: ${userCount}`
190+
`Total Users: ${users.result.length}\nTotal Products: ${productCount}`
191191
);
192192
});
193193

zzzz.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {manageProducts} from './db.js'
2+
import { productCommonUrl } from './utils.js'
3+
import fs from 'fs'
4+
import { DB_URL } from "./config.js";
5+
import { MongoClient } from "mongodb";
6+
var mongo;
7+
8+
// Check if mongodb is connected or not
9+
const isDbConnected = () =>
10+
!!mongo && !!mongo.topology && mongo.topology.isConnected();
11+
12+
// Connect to mongodb
13+
const connectDb = async () => {
14+
if (!isDbConnected()) {
15+
try {
16+
mongo = await MongoClient.connect(DB_URL, {
17+
useNewUrlParser: true,
18+
useUnifiedTopology: true,
19+
});
20+
console.log("Connected to Database!");
21+
} catch (e) {
22+
console.log("Failed to connected to Database!");
23+
}
24+
}
25+
};
26+
27+
28+
const func = async () => {
29+
await connectDb();
30+
const collection = mongo.db("AS_TRACKER").collection("tasks");
31+
const result = await collection.find({}).toArray();
32+
console.log(result[0].users.length);
33+
let count = 0;
34+
result.map((item) => {
35+
count += item.users.length
36+
});
37+
38+
console.log(count);
39+
}
40+
// let data = await manageProducts({}, 'read')
41+
// data.result.map((product,i) => {
42+
// const url = productCommonUrl(product.link);
43+
// data.result[i].link = url
44+
// })
45+
// fs.writeFileSync('updated.json', JSON.stringify(data.result));
46+
// const data = fs.readFileSync('updated.json').toString();
47+
// let products = JSON.parse(data);
48+
// let arr = [];
49+
// const duplicate = products.filter(product => {
50+
// const same = products.filter(p => {
51+
// return p.link === product.link
52+
// })
53+
// const users = same.map((p) => ({ userId: p.userId, tracking_id: p.tracking_id}));
54+
// arr.push({
55+
// link: product.link,
56+
// merchant: product.merchant,
57+
// initPrice: product.initPrice,
58+
// price: product.price,
59+
// title: product.title,
60+
// users
61+
62+
// })
63+
// // same.map((p, index) => {
64+
// // delete products[index]
65+
// // })
66+
// })
67+
// fs.writeFileSync('duplicate.json', JSON.stringify(arr));
68+
69+
// (async () => await func())()
70+
const deleteData = async () => {
71+
await connectDb();
72+
const db = mongo.db('TESTS').collection('tasks')
73+
await db.deleteOne({users: null})
74+
}
75+
(async () => await deleteData())()

0 commit comments

Comments
 (0)