Commit 027de527 authored by soheib's avatar soheib
Browse files

change lottery service result

parent c670bdce
File added
......@@ -10,6 +10,7 @@ import { FollowerSchema } from './instagram/models/follower.schema'
import { LikeSchema } from './instagram/models/like.schema'
import { CommentSchema } from './instagram/models/comment.schema'
import { LottoryResultSchema } from './instagram/models/LottoryResult.schema'
import { PostSchema } from './instagram/models/post.schema'
@Module({
imports: [
......@@ -31,6 +32,9 @@ import { LottoryResultSchema } from './instagram/models/LottoryResult.schema'
MongooseModule.forFeature([
{ name: 'LottryResult', schema: LottoryResultSchema },
]),
MongooseModule.forFeature([
{ name: 'Post', schema: PostSchema },
]),
InstagramModule,
LotteryModule,
......
......@@ -16,7 +16,10 @@ export class LottoryResult {
// @Prop()
// tagged_user: string;
@Prop()
status: string;
status: string
@Prop()
chance: string
}
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
export default {
addToStoryData : [
{
username: "soma.ye2103",
count: 5
username: "e.lin800",
count: 2
},
{
username: "zahra_51512",
username: "tybh2405 ",
count: 1
},
{
username: "nafas3633m",
username: "maryam_mohamadi.x",
count: 1
},
{
username: "jafar.amr",
username: "zahra.salaree",
count: 1
},
{
username: "sougool5382",
count: 1
username: "massi_a_6363 ",
count: 3
},
{
username: "maryam.maryammy",
count: 1
username: "mohana0012",
count: 5
},
{
username: "mona_vahed.68",
count: 2
},
{
username: "nafas3633m",
count: 5
},
{
username: "shml0oo",
count: 2
},
{
username: "toktam.yobi80",
count: 1
count: 7
},
{
username: "roghayeh_b8082",
count: 5
},
{
username: "mohseneidyzadeh",
count: 1
},
{
username: "turky.yyy",
username: "banoo_bahareh_62",
count: 1
},
{
username: "liligoli",
username: "sogand.mri12",
count: 1
},
{
username: "soh.ila50",
username: "shima.ahmadikia",
count: 1
},
{
username: "_mrs_kz_z",
username: "miss_fatemeh6902",
count: 1
},
]
......
......@@ -4,6 +4,7 @@ import { CommentSchema } from 'src/instagram/models/comment.schema';
import { FollowerSchema } from 'src/instagram/models/follower.schema';
import { LikeSchema } from 'src/instagram/models/like.schema';
import { LottoryResultSchema } from 'src/instagram/models/LottoryResult.schema';
import { PostSchema } from 'src/instagram/models/post.schema'
import { UserSchema } from 'src/instagram/models/user.schema';
import { LotteryController } from './lottery.controller';
import { LotteryService } from './lottery.service';
......@@ -17,6 +18,9 @@ import { ScoreService } from './score.service';
MongooseModule.forFeature([
{ name: 'Follower', schema: FollowerSchema },
]),
MongooseModule.forFeature([
{ name: 'Post', schema: PostSchema },
]),
MongooseModule.forFeature([
{ name: 'Like', schema: LikeSchema },
]),
......
import { HttpException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { HttpException, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Model, 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 { 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 { 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 { ScoreService } from './score.service'
import * as _ from "lodash"
@Injectable()
......@@ -29,8 +30,9 @@ export class LotteryService {
) { }
async getUserScore(username: string, profileUsername: string, postArray: string[]) {
let likesScore = await this.scoreService.getUserLikesScore(username, profileUsername, postArray)
let commentScore = await this.scoreService.getUserCommentsScore(username, profileUsername, postArray)
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)
return { likesScore, commentScore, addToStoryScore, totalScore: likesScore + commentScore + addToStoryScore }
......@@ -38,54 +40,66 @@ export class LotteryService {
}
async addResultsToDB(profileUsername: string, postArray: string[]) {
await this.lotteryResultModel.deleteMany({})
let foundPost
let foundUsernameInComment = await this.commentModel.distinct('user_id')
let foundUsernameInLike = await this.likeModel.distinct('user_id')
let foundUser_id = new Array<any>()
let foundUser_idsList = new Array<any>()
foundUsernameInComment.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.push(user_id)
if (!foundUser_idsList.includes(user_id)) {
foundUser_idsList.push(user_id)
}
})
foundUsernameInLike.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.push(user_id)
if (!foundUser_idsList.includes(user_id)) {
foundUser_idsList.push(user_id)
}
})
console.log(foundUser_id);
const comptitionArray = new Array<any>();
let index = 1000;
let totalItems = foundUser_id.length
let count = 0
for await (const user_id of foundUser_id) {
let user_index = 0
for await (const user_id of foundUser_idsList) {
let userScore = await this.scoreService.calculateUserScore(user_id, profileUsername, postArray)
for (let u = 0; u < userScore; u++) {
await this.lotteryResultModel.create({
_id: new Types.ObjectId(),
index: `LT_${index}`,
user_id: user_id,
status: 'valid'
let foundIndex = await this.lotteryResultModel.findOne({
$and: [
{
user_id: new Types.ObjectId(user_id)
}, {
index: `${u}`,
}]
})
index++;
console.log(`user_id:${user_id} , index: ${index}, score:${userScore}`);
if (!foundIndex) {
await this.lotteryResultModel.create({
_id: new Types.ObjectId(),
index: `${user_index}`,
user_id: user_id,
status: 'valid',
chance: await this.codeGenerator()
})
}
}
console.log(`user index:${count} , total items: ${totalItems}`);
count++
user_index++
}
return 'successfull'
}
async codeGenerator() {
let lastIndex = await this.lotteryResultModel.find().sort({ createdAt: -1 })
if (lastIndex[0]) {
let temp = lastIndex[0].chance
let code =Number(temp.split('LT_')[1])
return `LT_${code++}`
}
else {
return `LT_${1000}`
}
return 'successfull';
}
async getResultDb() {
return await this.lotteryResultModel
.find().populate("user_id",{"username":1},"User").sort({"status":1})
.select({ username: 1, index: 1 , status: 1});
.find().populate("user_id", { "username": 1 }, "User").sort({ "status": 1 })
.select({ username: 1, index: 1, status: 1 })
}
async changeStatus(changeStatus: ChangeStatusDto){
let foundUser = await this.userModel.findOne({username: changeStatus.username})
async changeStatus(changeStatus: ChangeStatusDto) {
let foundUser = await this.userModel.findOne({ username: changeStatus.username })
let foundLottryResults = await this.lotteryResultModel.find({
user_id: foundUser._id
})
......@@ -93,7 +107,7 @@ export class LotteryService {
result.status = "online"
await result.save()
}
await this.userModel.findOneAndUpdate({username: changeStatus.username},{mobile: changeStatus.mobile})
await this.userModel.findOneAndUpdate({ username: changeStatus.username }, { mobile: changeStatus.mobile })
return {
message: "status changed to online successfully"
}
......
import { HttpException, Injectable } 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 { UserDocument } from 'src/instagram/models/user.schema';
import { HttpException, Injectable } 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 { PostDocument } from 'src/instagram/models/post.schema'
import { UserDocument } from 'src/instagram/models/user.schema'
import addToStoryData from '../instagram/values/add-to-story-data'
......@@ -20,45 +21,60 @@ export class ScoreService {
@InjectModel('Comment')
private commentModel: Model<CommentDocument>,
@InjectModel('LottryResult')
private lotteryResultModel: Model<CommentDocument>
private lotteryResultModel: Model<CommentDocument>,
@InjectModel('Post')
private postModel: Model<PostDocument>,
) { }
async getUserLikesScore(user_id: string, profileUsername: string, postArray: string[]) {
let foundLikes = await this.likeModel.find({ user_id: new Types.ObjectId(user_id) })
return foundLikes.length
let foundAccount = await this.userModel.findOne({ username: profileUsername })
let isfollowerValid = await this.followerModel.findOne({
$and: [{ user_id: new Types.ObjectId(user_id) }
, { account_id: foundAccount._id }]
})
if (isfollowerValid) {
let foundLikes = await this.likeModel.find({ user_id: new Types.ObjectId(user_id) })
return foundLikes.length
}
else {
return 0
}
}
async getUserCommentsScore(user_id: string, profileUsername: string, postArray: string[]) {
let userCommentScore = new Array<{ post_id: string, post_score: number }>()
postArray.forEach((post) => {
userCommentScore.push({
post_id: post,
post_score: 0
})
let foundAccount = await this.userModel.findOne({ username: profileUsername })
let isfollowerValid = await this.followerModel.findOne({
$and: [{ user_id: new Types.ObjectId(user_id) }
, { account_id: foundAccount._id }]
})
let foundUserComments = await this.commentModel.find({ user_id: new Types.ObjectId(user_id) })
for await (const comment of foundUserComments) {
if (isfollowerValid) {
let userCommentScore = new Array<{ post_id: string, post_score: number }>()
for await (const post of postArray) {
let foundPost = await this.postModel.findOne({ url: post })
userCommentScore.push({
post_id: foundPost._id,
post_score: 0
})
}
let foundUserComments = await this.commentModel.find({ user_id: new Types.ObjectId(user_id) })
for await (const comment of foundUserComments) {
userCommentScore.forEach(element => {
if (comment.post_id.toString() === element.post_id.toString() && element.post_score < 3) {
element.post_score++
}
})
}
let score = 0
userCommentScore.forEach(element => {
if (comment.post_id.toString() === element.post_id.toString() && element.post_score < 3) {
element.post_score++
}
score += element.post_score
})
return score
}
else {
return 0
}
let score = 0
userCommentScore.forEach(element => {
score += element.post_score
})
return score
}
async getTagsScore(username: string) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment