Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
shahriar
instagram-service
Commits
1a740823
Commit
1a740823
authored
Aug 21, 2021
by
shahriar
☹
Browse files
return 404 if user not found
parent
a128cea4
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/lottery/lottery.controller.ts
View file @
1a740823
...
...
@@ -37,8 +37,6 @@ export class LotteryController {
@
ApiTags
(
'
Lottery/weekly
'
)
@
Post
(
'
weekly/change-status
'
)
async
changeStatus
(@
Body
()
changeStatusDto
:
ChangeStatusDto
)
{
console
.
log
(
'
here
'
);
return
await
this
.
lotteryService
.
changeStatus
(
changeStatusDto
)
}
}
src/lottery/lottery.service.ts
View file @
1a740823
import
{
HttpException
,
Injectable
}
from
'
@nestjs/common
'
import
{
InjectModel
}
from
'
@nestjs/mongoose
'
import
{
Model
,
ObjectId
,
Types
}
from
'
mongoose
'
import
{
of
}
from
'
rxjs
'
import
{
CommentDocument
}
from
'
src/instagram/models/comment.schema
'
import
{
FollowerDocument
}
from
'
src/instagram/models/follower.schema
'
import
{
LikeDocument
}
from
'
src/instagram/models/like.schema
'
import
{
LottoryResultDocument
}
from
'
src/instagram/models/LottoryResult.schema
'
import
{
UserDocument
}
from
'
src/instagram/models/user.schema
'
import
addToStoryData
from
'
../instagram/values/add-to-story-data
'
import
{
ChangeStatusDto
}
from
'
./dto/change-status-dto
'
import
{
ScoreService
}
from
'
./score.service
'
import
*
as
_
from
"
lodash
"
import
{
StoryMentionDocument
}
from
'
src/instagram/models/storyMention.schema
'
import
{
Injectable
,
NotFoundException
}
from
'
@nestjs/common
'
;
import
{
InjectModel
}
from
'
@nestjs/mongoose
'
;
import
{
Model
,
Types
}
from
'
mongoose
'
;
import
{
CommentDocument
}
from
'
src/instagram/models/comment.schema
'
;
import
{
FollowerDocument
}
from
'
src/instagram/models/follower.schema
'
;
import
{
LikeDocument
}
from
'
src/instagram/models/like.schema
'
;
import
{
LottoryResultDocument
}
from
'
src/instagram/models/LottoryResult.schema
'
;
import
{
UserDocument
}
from
'
src/instagram/models/user.schema
'
;
import
addToStoryData
from
'
../instagram/values/add-to-story-data
'
;
import
{
ChangeStatusDto
}
from
'
./dto/change-status-dto
'
;
import
{
ScoreService
}
from
'
./score.service
'
;
import
{
StoryMentionDocument
}
from
'
src/instagram/models/storyMention.schema
'
;
@
Injectable
()
export
class
LotteryService
{
constructor
(
private
scoreService
:
ScoreService
,
@
InjectModel
(
'
User
'
)
private
userModel
:
Model
<
UserDocument
>
,
@
InjectModel
(
'
Follower
'
)
private
followerModel
:
Model
<
FollowerDocument
>
,
@
InjectModel
(
'
Like
'
)
private
likeModel
:
Model
<
LikeDocument
>
,
@
InjectModel
(
'
Comment
'
)
private
commentModel
:
Model
<
CommentDocument
>
,
@
InjectModel
(
'
LottryResult
'
)
private
lotteryResultModel
:
Model
<
LottoryResultDocument
>
,
@
InjectModel
(
'
StoryMention
'
)
private
storyMentionModel
:
Model
<
StoryMentionDocument
>
,
)
{
}
constructor
(
private
scoreService
:
ScoreService
,
@
InjectModel
(
'
User
'
)
private
userModel
:
Model
<
UserDocument
>
,
@
InjectModel
(
'
Follower
'
)
private
followerModel
:
Model
<
FollowerDocument
>
,
@
InjectModel
(
'
Like
'
)
private
likeModel
:
Model
<
LikeDocument
>
,
@
InjectModel
(
'
Comment
'
)
private
commentModel
:
Model
<
CommentDocument
>
,
@
InjectModel
(
'
LottryResult
'
)
private
lotteryResultModel
:
Model
<
LottoryResultDocument
>
,
@
InjectModel
(
'
StoryMention
'
)
private
storyMentionModel
:
Model
<
StoryMentionDocument
>
,
)
{}
async
getUserScore
(
username
:
string
,
profileUsername
:
string
,
postArray
:
string
[])
{
let
foundUser
=
await
this
.
userModel
.
findOne
({
username
})
let
likesScore
=
await
this
.
scoreService
.
getUserLikesScore
(
foundUser
.
_id
,
profileUsername
,
postArray
)
let
commentScore
=
await
this
.
scoreService
.
getUserCommentsScore
(
foundUser
.
_id
,
profileUsername
,
postArray
)
let
addToStoryScore
=
await
this
.
scoreService
.
getTagsScore
(
username
)
async
getUserScore
(
username
:
string
,
profileUsername
:
string
,
postArray
:
string
[],
)
{
const
foundUser
=
await
this
.
userModel
.
findOne
({
username
});
const
likesScore
=
await
this
.
scoreService
.
getUserLikesScore
(
foundUser
.
_id
,
profileUsername
,
postArray
,
);
const
commentScore
=
await
this
.
scoreService
.
getUserCommentsScore
(
foundUser
.
_id
,
profileUsername
,
postArray
,
);
const
addToStoryScore
=
await
this
.
scoreService
.
getTagsScore
(
username
);
return
{
likesScore
,
commentScore
,
addToStoryScore
,
totalScore
:
likesScore
+
commentScore
+
addToStoryScore
}
return
{
likesScore
,
commentScore
,
addToStoryScore
,
totalScore
:
likesScore
+
commentScore
+
addToStoryScore
,
};
}
async
addResultsToDB
(
profileUsername
:
string
,
postArray
:
string
[])
{
const
foundUser_idsList
=
await
this
.
getUserList
();
let
index
=
0
;
for
await
(
const
user_id
of
foundUser_idsList
)
{
const
userScore
=
await
this
.
scoreService
.
calculateUserScore
(
user_id
.
toString
(),
profileUsername
,
postArray
,
);
if
(
userScore
>
0
)
{
console
.
log
(
userScore
,
user_id
.
toString
());
}
const
foundUserLastCount
=
await
this
.
lotteryResultModel
.
countDocuments
({
user_id
:
new
Types
.
ObjectId
(
user_id
),
});
const
newChances
=
userScore
-
foundUserLastCount
;
for
(
let
u
=
0
;
u
<
newChances
;
u
++
)
{
await
this
.
lotteryResultModel
.
create
({
_id
:
new
Types
.
ObjectId
(),
index
:
await
this
.
codeGenerator
(),
user_id
:
new
Types
.
ObjectId
(
user_id
),
status
:
'
valid
'
,
});
}
console
.
log
(
`
${
index
}
/
${
foundUser_idsList
.
length
}
`
);
index
++
;
}
async
addResultsToDB
(
profileUsername
:
string
,
postArray
:
string
[])
{
let
foundUser_idsList
=
await
this
.
getUserList
()
let
index
=
0
for
await
(
const
user_id
of
foundUser_idsList
)
{
let
userScore
=
await
this
.
scoreService
.
calculateUserScore
(
user_id
.
toString
(),
profileUsername
,
postArray
)
if
(
userScore
>
0
)
{
console
.
log
(
userScore
,
user_id
.
toString
());
}
let
foundUserLastCount
=
await
this
.
lotteryResultModel
.
countDocuments
({
user_id
:
new
Types
.
ObjectId
(
user_id
)
})
let
newChances
=
userScore
-
foundUserLastCount
for
(
let
u
=
0
;
u
<
newChances
;
u
++
)
{
await
this
.
lotteryResultModel
.
create
({
_id
:
new
Types
.
ObjectId
(),
index
:
await
this
.
codeGenerator
(),
user_id
:
new
Types
.
ObjectId
(
user_id
),
status
:
'
valid
'
,
})
}
console
.
log
(
`
${
index
}
/
${
foundUser_idsList
.
length
}
`
)
index
++
}
console
.
log
(
'
end
'
)
return
'
successfull
'
console
.
log
(
'
end
'
);
return
'
successfull
'
;
}
async
getResultDb
()
{
return
await
this
.
lotteryResultModel
.
find
({
status
:
'
online
'
})
.
populate
(
'
user_id
'
,
{
username
:
1
},
'
User
'
)
.
sort
({
status
:
1
})
.
select
({
username
:
1
,
index
:
1
,
status
:
1
});
}
async
changeStatus
(
changeStatus
:
ChangeStatusDto
)
{
const
foundUser
=
await
this
.
userModel
.
findOne
({
username
:
changeStatus
.
username
,
});
if
(
!
foundUser
)
{
throw
new
NotFoundException
(
`user with username :
${
changeStatus
.
username
}
not found`
,
);
}
async
getResultDb
()
{
return
await
this
.
lotteryResultModel
.
find
({
status
:
"
online
"
}).
populate
(
"
user_id
"
,
{
"
username
"
:
1
},
"
User
"
).
sort
({
"
status
"
:
1
})
.
select
({
username
:
1
,
index
:
1
,
status
:
1
})
const
foundLottryResults
=
await
this
.
lotteryResultModel
.
find
({
user_id
:
foundUser
.
_id
,
});
for
await
(
const
result
of
foundLottryResults
)
{
result
.
status
=
'
online
'
;
await
result
.
save
();
}
await
this
.
userModel
.
findOneAndUpdate
(
{
username
:
changeStatus
.
username
},
{
mobile
:
changeStatus
.
mobile
},
);
return
{
message
:
'
status changed to online successfully
'
,
};
}
async
changeStatus
(
changeStatus
:
ChangeStatusDto
)
{
let
foundUser
=
await
this
.
userModel
.
findOne
({
username
:
changeStatus
.
username
})
let
foundLottryResults
=
await
this
.
lotteryResultModel
.
find
({
user_id
:
foundUser
.
_id
})
for
await
(
const
result
of
foundLottryResults
)
{
result
.
status
=
"
online
"
await
result
.
save
()
}
await
this
.
userModel
.
findOneAndUpdate
({
username
:
changeStatus
.
username
},
{
mobile
:
changeStatus
.
mobile
})
return
{
message
:
"
status changed to online successfully
"
}
}
async
codeGenerator
()
{
let
lastIndex
=
await
this
.
lotteryResultModel
.
find
().
sort
({
createdAt
:
-
1
})
async
codeGenerator
()
{
const
lastIndex
=
await
this
.
lotteryResultModel
.
find
()
.
sort
({
createdAt
:
-
1
});
if
(
lastIndex
[
0
])
{
let
lastChanceIndex
=
lastIndex
[
0
].
index
let
code
=
lastChanceIndex
.
split
(
'
LT_
'
)[
1
]
let
res
=
`LT_
${(
Number
(
code
)
+
1
)}
`
return
res
}
else
{
return
`LT_
${
1000
}
`
}
if
(
lastIndex
[
0
])
{
const
lastChanceIndex
=
lastIndex
[
0
].
index
;
const
code
=
lastChanceIndex
.
split
(
'
LT_
'
)[
1
];
const
res
=
`LT_
${
Number
(
code
)
+
1
}
`
;
return
res
;
}
else
{
return
`LT_
${
1000
}
`
;
}
}
async
getUserList
()
{
let
foundUsernameInComment
=
await
this
.
commentModel
.
distinct
(
'
user_id
'
)
let
foundUsernameInLike
=
await
this
.
likeModel
.
distinct
(
'
user_id
'
)
let
foundUsernamesInStoryData
=
new
Array
<
any
>
()
for
await
(
const
iterator
of
addToStoryData
.
addToStoryData
)
{
let
foundUser
=
await
this
.
userModel
.
findOne
({
username
:
iterator
.
username
})
if
(
!
foundUser
)
{
let
createdUser
=
await
this
.
userModel
.
create
({
_id
:
new
Types
.
ObjectId
(),
username
:
iterator
.
username
})
foundUsernamesInStoryData
.
push
(
createdUser
.
_id
.
toString
())
}
else
{
foundUsernamesInStoryData
.
push
(
foundUser
.
_id
.
toString
())
}
}
let
userListTemp
=
foundUsernameInComment
.
concat
(
foundUsernameInLike
)
let
array
=
new
Array
<
string
>
()
userListTemp
.
forEach
(
user
=>
{
array
.
push
(
user
.
toString
())
})
let
userListTemp2
=
array
.
concat
(
foundUsernamesInStoryData
)
let
userList
=
[...
new
Set
(
userListTemp2
)]
return
userList
async
getUserList
()
{
const
foundUsernameInComment
=
await
this
.
commentModel
.
distinct
(
'
user_id
'
);
const
foundUsernameInLike
=
await
this
.
likeModel
.
distinct
(
'
user_id
'
);
const
foundUsernamesInStoryData
=
new
Array
<
any
>
();
for
await
(
const
iterator
of
addToStoryData
.
addToStoryData
)
{
const
foundUser
=
await
this
.
userModel
.
findOne
({
username
:
iterator
.
username
,
});
if
(
!
foundUser
)
{
const
createdUser
=
await
this
.
userModel
.
create
({
_id
:
new
Types
.
ObjectId
(),
username
:
iterator
.
username
,
});
foundUsernamesInStoryData
.
push
(
createdUser
.
_id
.
toString
());
}
else
{
foundUsernamesInStoryData
.
push
(
foundUser
.
_id
.
toString
());
}
}
const
userListTemp
=
foundUsernameInComment
.
concat
(
foundUsernameInLike
);
const
array
=
new
Array
<
string
>
();
userListTemp
.
forEach
((
user
)
=>
{
array
.
push
(
user
.
toString
());
});
const
userListTemp2
=
array
.
concat
(
foundUsernamesInStoryData
);
const
userList
=
[...
new
Set
(
userListTemp2
)];
return
userList
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment