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 {
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
// @Prop()
// tagged_user: string;
@Prop()
status: string
@Prop()
chance: string
date: Date
}
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty } from "class-validator"
import { IsDateString, IsNotEmpty } from "class-validator"
export class CalculateScoreDto{
@ApiProperty({ example: "azadi.gold" })
......@@ -9,6 +9,4 @@ export class CalculateScoreDto{
@ApiProperty({ example: ['GKCUev0s5dJO','CSCUev0swJO','CSCUev0swJO'] })
@IsNotEmpty()
post_array: string[]
//add_to_story_data: Array<any>
}
\ No newline at end of file
import { ApiProperty } from "@nestjs/swagger"
import { IsNotEmpty } from "class-validator"
import { IsDateString, IsNotEmpty } from "class-validator"
export class WeeklySearchDto {
......
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';
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 {
......@@ -27,63 +27,60 @@ export class LotteryService {
private lotteryResultModel: Model<LottoryResultDocument>,
@InjectModel('StoryMention')
private storyMentionModel: Model<StoryMentionDocument>,
) {}
) { }
async getUserScore(
username: string,
profileUsername: string,
postArray: string[],
) {
const foundUser = await this.userModel.findOne({ username });
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);
)
const addToStoryScore = await this.scoreService.getTagsScore(username)
return {
likesScore,
commentScore,
addToStoryScore,
totalScore: likesScore + commentScore + addToStoryScore,
};
}
}
async addResultsToDB(profileUsername: string, postArray: string[]) {
const foundUser_idsList = await this.getUserList();
let index = 0;
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;
})
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++;
console.log(`${index}/${foundUser_idsList.length}`)
index++
}
console.log('end');
return 'successfull';
console.log('end')
return 'successfull'
}
async getResultDb() {
......@@ -91,7 +88,7 @@ export class LotteryService {
.find({ status: 'online' })
.populate('user_id', { username: 1 }, 'User')
.sort({ status: 1 })
.select({ username: 1, index: 1, status: 1 });
.select({ username: 1, index: 1, status: 1 })
}
async changeStatus(changeStatus: ChangeStatusDto) {
......@@ -100,68 +97,59 @@ export class LotteryService {
$regex: changeStatus.username,
$options: 'i',
},
});
})
if (!foundUser) {
throw new NotFoundException(
`user with username : ${changeStatus.username} not found`,
);
)
}
const foundLottryResults = await this.lotteryResultModel.find({
user_id: foundUser._id,
});
})
for await (const result of foundLottryResults) {
result.status = 'online';
await result.save();
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() {
const lastIndex = await this.lotteryResultModel
.find()
.sort({ createdAt: -1 });
.sort({ createdAt: -1 })
if (lastIndex[0]) {
const lastChanceIndex = lastIndex[0].index;
const code = lastChanceIndex.split('LT_')[1];
const res = `LT_${Number(code) + 1}`;
return res;
const lastChanceIndex = lastIndex[0].index
const code = lastChanceIndex.split('LT_')[1]
const res = `LT_${Number(code) + 1}`
return res
} else {
return `LT_${1000}`;
return `LT_${1000}`
}
}
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>();
const foundUsernameInComment = await this.commentModel.distinct('user_id')
const foundUsernameInLike = await this.likeModel.distinct('user_id')
const foundUsernameInStoryMentions = await this.storyMentionModel.distinct('user_id')
const foundUsernamesInStoryData = new Array<string>()
foundUsernameInStoryMentions.forEach(user_id=>{
foundUsernamesInStoryData.push(user_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;
array.push(user.toString())
})
const userListTemp2 = array.concat(foundUsernamesInStoryData)
const userList = [...new Set(userListTemp2)]
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