Commit 7d1337da authored by soheib's avatar soheib
Browse files

add getting posts from db

parent 58873aad
......@@ -2,11 +2,5 @@ import { ApiProperty } from "@nestjs/swagger"
import { IsDateString, IsNotEmpty } from "class-validator"
export class CalculateScoreDto{
@ApiProperty({ example: "azadi.gold" })
@IsNotEmpty()
profile_username: string
@ApiProperty({ example: ['GKCUev0s5dJO','CSCUev0swJO','CSCUev0swJO'] })
@IsNotEmpty()
post_array: string[]
}
\ No newline at end of file
......@@ -6,11 +6,5 @@ export class WeeklySearchDto {
@ApiProperty({ example: "kakasrm" })
@IsNotEmpty()
username: string
@ApiProperty({ example: "azadi.gold" })
@IsNotEmpty()
profile_username: string
@ApiProperty({ example: ['GKCUev0s5dJO','CSCUev0swJO','CSCUev0swJO'] })
@IsNotEmpty()
post_array: string[]
}
\ No newline at end of file
import { Body, Query, Controller, Get, Post } from '@nestjs/common';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { ApiBody, ApiTags } from '@nestjs/swagger'
import { CalculateScoreDto } from './dto/calculate-score-dto'
import { ChangeStatusDto } from './dto/change-status-dto'
import { WeeklySearchDto } from './dto/weekly-search-dto'
import { LotteryService } from './lottery.service';
......@@ -15,16 +14,13 @@ export class LotteryController {
@ApiTags('Lottery/weekly')
@Post('weekly/search')
async getUserResults(@Body() searchDto: WeeklySearchDto) {
return await this.lotteryService.getUserScore(searchDto.username,
searchDto.profile_username, searchDto.post_array)
return await this.lotteryService.getUserScore(searchDto.username)
}
@ApiBody({ type: CalculateScoreDto })
@ApiTags('Lottery/weekly')
@Post('weekly/calculate-score')
async addResultsToDb(@Body() calculateScoreDto: CalculateScoreDto) {
return await this.lotteryService.addResultsToDB(calculateScoreDto.profile_username,
calculateScoreDto.post_array)
@Get('weekly/calculate-score')
async addResultsToDb() {
return await this.lotteryService.addResultsToDB()
}
@ApiTags('Lottery/weekly')
......
......@@ -2,15 +2,14 @@ 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 { Cron, CronExpression } from '@nestjs/schedule'
import { PostDocument } from 'src/instagram/models/post.schema'
@Injectable()
export class LotteryService {
......@@ -18,8 +17,8 @@ export class LotteryService {
private scoreService: ScoreService,
@InjectModel('User')
private userModel: Model<UserDocument>,
@InjectModel('Follower')
private followerModel: Model<FollowerDocument>,
@InjectModel('Post')
private postModel: Model<PostDocument>,
@InjectModel('Like')
private likeModel: Model<LikeDocument>,
@InjectModel('Comment')
......@@ -30,26 +29,23 @@ export class LotteryService {
private storyMentionModel: Model<StoryMentionDocument>,
) { }
async getUserScore(
username: string,
profileUsername: string,
postArray: string[],
) {
async getUserScore(username: string) {
const foundUser = await this.userModel.findOne({ username })
let postArray = await this.getPosts()
const likesScore = await this.scoreService.getUserLikesScore(
foundUser._id,
profileUsername,
"azadi.gold",
postArray,
)
const commentScore = await this.scoreService.getUserCommentsScore(
foundUser._id,
profileUsername,
"azadi.gold",
postArray,
)
const addToStoryScore = await this.scoreService.getTagsScore(username)
let foundIndexes = await this.lotteryResultModel.find({user_id: foundUser._id})
let chances = new Array<string>()
foundIndexes.forEach(result=>{
let foundIndexes = await this.lotteryResultModel.find({ user_id: foundUser._id })
let chances = new Array<string>()
foundIndexes.forEach(result => {
chances.push(result.index)
})
return {
......@@ -57,13 +53,13 @@ export class LotteryService {
commentScore,
addToStoryScore,
totalScore: likesScore + commentScore + addToStoryScore,
lottry_chances : chances
lottry_chances: chances
}
}
async addResultsToDB(profileUsername: string, postArray: string[]) {
async addResultsToDB(profileUsername = "azadi.gold") {
const foundUser_idsList = await this.getUserList()
let postArray = await this.getPosts()
let index = 0
for await (const user_id of foundUser_idsList) {
const userScore = await this.scoreService.calculateUserScore(
......@@ -71,8 +67,6 @@ export class LotteryService {
profileUsername,
postArray,
)
console.log('=========',userScore);
const foundUserLastCount = await this.lotteryResultModel.countDocuments({
user_id: new Types.ObjectId(user_id),
})
......@@ -162,11 +156,14 @@ export class LotteryService {
return userList
}
async getPosts() {
return await this.postModel.distinct('url') as string[]
}
@Cron(CronExpression.EVERY_3_HOURS)
async cronCalculateScore() {
console.log('start cron ======> calculate daily score');
let postArray = ["CSlqcUEAZsU"]
console.log('start cron ======> calculate daily score')
let account = "azadi.gold"
await this.addResultsToDB(account, postArray)
await this.addResultsToDB(account)
}
}
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