Skip to content

Commit 490dbc7

Browse files
committed
Don't let users add duplidate products
1 parent ae1e2f6 commit 490dbc7

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

db.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ const manageUsers = async(data, action) => {
4141
}
4242
}
4343

44+
const addToSet = (data) => {
45+
if(Array.isArray(data.users))
46+
return({$addToSet: { users: {$each: data.users}}})
47+
else
48+
return({$exists: false}, {$addToSet: { users: data.users}})
49+
}
50+
4451
const manageProducts = async(data, action) => {
4552
await connectDb();
4653
try{
47-
const db = mongo.db('AS_TRACKER');
54+
console.log(data.userId)
55+
const db = mongo.db('TESTS');
4856
const collection = db.collection('tasks');
4957
switch(action) {
5058
case 'delete':
@@ -55,18 +63,20 @@ const manageProducts = async(data, action) => {
5563
return {ok: true}
5664
case 'update':
5765
await collection.updateOne(
58-
{ link: data.link },
59-
{
60-
$set: {
61-
link: data.link,
62-
merchant: data.merchant,
63-
initPrice: data.initPrice,
64-
price: data.price,
65-
title: data.title,
66-
},
67-
$addToSet: {users : Array.isArray(data.users) ? {$each: data.users} : data.users},
68-
},
69-
{ upsert: true }
66+
{ link: data.link },
67+
[
68+
{ $set: {
69+
link: data.link, merchant: data.merchant, initPrice: data.initPrice, price: data.price, title: data.title,
70+
users: {$concatArrays: [
71+
{$ifNull: ["$users", []]},
72+
{$filter: {
73+
input: data.users || [{userId: data.userId, tracking_id: data.tracking_id }],
74+
cond: {$not: {$in: ['$$this.userId', {$ifNull: ["$users.userId", []]}]}}
75+
}}
76+
]}
77+
}},
78+
],
79+
{ upsert: true }
7080
);
7181
return {ok: true, tracking_id: data.tracking_id}
7282
case 'read':

0 commit comments

Comments
 (0)