Commit 4ea17049 authored by soheib's avatar soheib
Browse files

update get userlist of get story mention data

parent 79eb8386
...@@ -13,13 +13,12 @@ export class LottoryResult { ...@@ -13,13 +13,12 @@ export class LottoryResult {
@Prop({ type: Types.ObjectId, ref: 'User' }) @Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId user_id: Types.ObjectId
// @Prop()
// tagged_user: string;
@Prop() @Prop()
status: string status: string
@Prop() @Prop()
chance: string date: Date
} }
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult); export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
import { ApiProperty } from "@nestjs/swagger" import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty } from "class-validator" import { IsDateString, IsNotEmpty } from "class-validator"
export class CalculateScoreDto{ export class CalculateScoreDto{
@ApiProperty({ example: "azadi.gold" }) @ApiProperty({ example: "azadi.gold" })
...@@ -9,6 +9,4 @@ export class CalculateScoreDto{ ...@@ -9,6 +9,4 @@ export class CalculateScoreDto{
@ApiProperty({ example: ['GKCUev0s5dJO','CSCUev0swJO','CSCUev0swJO'] }) @ApiProperty({ example: ['GKCUev0s5dJO','CSCUev0swJO','CSCUev0swJO'] })
@IsNotEmpty() @IsNotEmpty()
post_array: string[] post_array: string[]
//add_to_story_data: Array<any>
} }
\ No newline at end of file
import { ApiProperty } from "@nestjs/swagger" import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty } from "class-validator" import { IsDateString, IsNotEmpty } from "class-validator"
export class WeeklySearchDto { export class WeeklySearchDto {
......
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, NotFoundException } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose'
import { Model, Types } from 'mongoose'; import { Model, Types } from 'mongoose'
import { CommentDocument } from 'src/instagram/models/comment.schema'; import { CommentDocument } from 'src/instagram/models/comment.schema'
import { FollowerDocument } from 'src/instagram/models/follower.schema'; import { FollowerDocument } from 'src/instagram/models/follower.schema'
import { LikeDocument } from 'src/instagram/models/like.schema'; import { LikeDocument } from 'src/instagram/models/like.schema'
import { LottoryResultDocument } from 'src/instagram/models/LottoryResult.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 addToStoryData from '../instagram/values/add-to-story-data'
import { ChangeStatusDto } from './dto/change-status-dto'; import { ChangeStatusDto } from './dto/change-status-dto'
import { ScoreService } from './score.service'; import { ScoreService } from './score.service'
import { StoryMentionDocument } from 'src/instagram/models/storyMention.schema'; import { StoryMentionDocument } from 'src/instagram/models/storyMention.schema'
@Injectable() @Injectable()
export class LotteryService { export class LotteryService {
...@@ -27,63 +27,60 @@ export class LotteryService { ...@@ -27,63 +27,60 @@ export class LotteryService {
private lotteryResultModel: Model<LottoryResultDocument>, private lotteryResultModel: Model<LottoryResultDocument>,
@InjectModel('StoryMention') @InjectModel('StoryMention')
private storyMentionModel: Model<StoryMentionDocument>, private storyMentionModel: Model<StoryMentionDocument>,
) {} ) { }
async getUserScore( async getUserScore(
username: string, username: string,
profileUsername: string, profileUsername: string,
postArray: string[], postArray: string[],
) { ) {
const foundUser = await this.userModel.findOne({ username }); const foundUser = await this.userModel.findOne({ username })
const likesScore = await this.scoreService.getUserLikesScore( const likesScore = await this.scoreService.getUserLikesScore(
foundUser._id, foundUser._id,
profileUsername, profileUsername,
postArray, postArray,
); )
const commentScore = await this.scoreService.getUserCommentsScore( const commentScore = await this.scoreService.getUserCommentsScore(
foundUser._id, foundUser._id,
profileUsername, profileUsername,
postArray, postArray,
); )
const addToStoryScore = await this.scoreService.getTagsScore(username); const addToStoryScore = await this.scoreService.getTagsScore(username)
return { return {
likesScore, likesScore,
commentScore, commentScore,
addToStoryScore, addToStoryScore,
totalScore: likesScore + commentScore + addToStoryScore, totalScore: likesScore + commentScore + addToStoryScore,
}; }
} }
async addResultsToDB(profileUsername: string, postArray: string[]) { async addResultsToDB(profileUsername: string, postArray: string[]) {
const foundUser_idsList = await this.getUserList(); const foundUser_idsList = await this.getUserList()
let index = 0; let index = 0
for await (const user_id of foundUser_idsList) { for await (const user_id of foundUser_idsList) {
const userScore = await this.scoreService.calculateUserScore( const userScore = await this.scoreService.calculateUserScore(
user_id.toString(), user_id.toString(),
profileUsername, profileUsername,
postArray, postArray,
); )
if (userScore > 0) {
console.log(userScore, user_id.toString());
}
const foundUserLastCount = await this.lotteryResultModel.countDocuments({ const foundUserLastCount = await this.lotteryResultModel.countDocuments({
user_id: new Types.ObjectId(user_id), user_id: new Types.ObjectId(user_id),
}); })
const newChances = userScore - foundUserLastCount; const newChances = userScore - foundUserLastCount
for (let u = 0; u < newChances; u++) { for (let u = 0; u < newChances; u++) {
await this.lotteryResultModel.create({ await this.lotteryResultModel.create({
_id: new Types.ObjectId(), _id: new Types.ObjectId(),
index: await this.codeGenerator(), index: await this.codeGenerator(),
user_id: new Types.ObjectId(user_id), user_id: new Types.ObjectId(user_id),
status: 'valid', status: 'valid',
}); })
} }
console.log(`${index}/${foundUser_idsList.length}`); console.log(`${index}/${foundUser_idsList.length}`)
index++; index++
} }
console.log('end'); console.log('end')
return 'successfull'; return 'successfull'
} }
async getResultDb() { async getResultDb() {
...@@ -91,7 +88,7 @@ export class LotteryService { ...@@ -91,7 +88,7 @@ export class LotteryService {
.find({ status: 'online' }) .find({ status: 'online' })
.populate('user_id', { username: 1 }, 'User') .populate('user_id', { username: 1 }, 'User')
.sort({ status: 1 }) .sort({ status: 1 })
.select({ username: 1, index: 1, status: 1 }); .select({ username: 1, index: 1, status: 1 })
} }
async changeStatus(changeStatus: ChangeStatusDto) { async changeStatus(changeStatus: ChangeStatusDto) {
...@@ -100,68 +97,59 @@ export class LotteryService { ...@@ -100,68 +97,59 @@ export class LotteryService {
$regex: changeStatus.username, $regex: changeStatus.username,
$options: 'i', $options: 'i',
}, },
}); })
if (!foundUser) { if (!foundUser) {
throw new NotFoundException( throw new NotFoundException(
`user with username : ${changeStatus.username} not found`, `user with username : ${changeStatus.username} not found`,
); )
} }
const foundLottryResults = await this.lotteryResultModel.find({ const foundLottryResults = await this.lotteryResultModel.find({
user_id: foundUser._id, user_id: foundUser._id,
}); })
for await (const result of foundLottryResults) { for await (const result of foundLottryResults) {
result.status = 'online'; result.status = 'online'
await result.save(); await result.save()
} }
await this.userModel.findOneAndUpdate( await this.userModel.findOneAndUpdate(
{ username: changeStatus.username }, { username: changeStatus.username },
{ mobile: changeStatus.mobile }, { mobile: changeStatus.mobile },
); )
return { return {
message: 'status changed to online successfully', message: 'status changed to online successfully',
}; }
} }
async codeGenerator() { async codeGenerator() {
const lastIndex = await this.lotteryResultModel const lastIndex = await this.lotteryResultModel
.find() .find()
.sort({ createdAt: -1 }); .sort({ createdAt: -1 })
if (lastIndex[0]) { if (lastIndex[0]) {
const lastChanceIndex = lastIndex[0].index; const lastChanceIndex = lastIndex[0].index
const code = lastChanceIndex.split('LT_')[1]; const code = lastChanceIndex.split('LT_')[1]
const res = `LT_${Number(code) + 1}`; const res = `LT_${Number(code) + 1}`
return res; return res
} else { } else {
return `LT_${1000}`; return `LT_${1000}`
} }
} }
async getUserList() { async getUserList() {
const foundUsernameInComment = await this.commentModel.distinct('user_id'); const foundUsernameInComment = await this.commentModel.distinct('user_id')
const foundUsernameInLike = await this.likeModel.distinct('user_id'); const foundUsernameInLike = await this.likeModel.distinct('user_id')
const foundUsernamesInStoryData = new Array<any>(); const foundUsernameInStoryMentions = await this.storyMentionModel.distinct('user_id')
for await (const iterator of addToStoryData.addToStoryData) { const foundUsernamesInStoryData = new Array<string>()
const foundUser = await this.userModel.findOne({ foundUsernameInStoryMentions.forEach(user_id=>{
username: iterator.username, foundUsernamesInStoryData.push(user_id.toString())
}); })
if (!foundUser) {
const createdUser = await this.userModel.create({ const userListTemp = foundUsernameInComment.concat(foundUsernameInLike)
_id: new Types.ObjectId(), const array = new Array<string>()
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) => { userListTemp.forEach((user) => {
array.push(user.toString()); array.push(user.toString())
}); })
const userListTemp2 = array.concat(foundUsernamesInStoryData); const userListTemp2 = array.concat(foundUsernamesInStoryData)
const userList = [...new Set(userListTemp2)]; const userList = [...new Set(userListTemp2)]
return userList; return userList
} }
} }
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