Commit c22fd7fd authored by velayat's avatar velayat
Browse files

soheib cant push his changes so i did it for him

parent c670bdce
This diff is collapsed.
export class CalculateScoreDto{
profile_username: string
post_array: string[]
export class CalculateScoreDto{
profile_username: string
post_array: string[]
}
\ No newline at end of file
export class ChangeStatusDto{
mobile: string
username: string
export class ChangeStatusDto{
mobile: string
username: string
}
\ No newline at end of file
export class WeeklySearchDto {
username: string
profile_username: string
post_array: string[]
export class WeeklySearchDto {
username: string
profile_username: string
post_array: string[]
}
\ No newline at end of file
import { Body, Query, Controller, Get, Post } from '@nestjs/common';
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';
@Controller('lottery')
export class LotteryController {
constructor(private lotteryService: LotteryService) { }
//weekly lottery apies:
@Post('weekly/search')
async getUserResults(@Body() searchDto: WeeklySearchDto) {
return await this.lotteryService.getUserScore(searchDto.username,
searchDto.profile_username, searchDto.post_array)
}
@Post('weekly/calculate-score')
async addResultsToDb(@Body() calculateScoreDto: CalculateScoreDto) {
return await this.lotteryService.addResultsToDB(calculateScoreDto.profile_username,
calculateScoreDto.post_array)
}
@Get('weekly/get-lottory-result')
async getResultDb() {
return await this.lotteryService.getResultDb()
}
@Post('weekly/change-status')
async changeStatus(@Body() changeStatusDto: ChangeStatusDto) {
console.log('here');
return await this.lotteryService.changeStatus(changeStatusDto)
}
}
import { Body, Query, Controller, Get, Post } from '@nestjs/common';
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';
@Controller('lottery')
export class LotteryController {
constructor(private lotteryService: LotteryService) { }
//weekly lottery apies:
@Post('weekly/search')
async getUserResults(@Body() searchDto: WeeklySearchDto) {
return await this.lotteryService.getUserScore(searchDto.username,
searchDto.profile_username, searchDto.post_array)
}
@Post('weekly/calculate-score')
async addResultsToDb(@Body() calculateScoreDto: CalculateScoreDto) {
return await this.lotteryService.addResultsToDB(calculateScoreDto.profile_username,
calculateScoreDto.post_array)
}
@Get('weekly/get-lottory-result')
async getResultDb() {
return await this.lotteryService.getResultDb()
}
@Post('weekly/change-status')
async changeStatus(@Body() changeStatusDto: ChangeStatusDto) {
console.log('here');
return await this.lotteryService.changeStatus(changeStatusDto)
}
}
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
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 { UserSchema } from 'src/instagram/models/user.schema';
import { LotteryController } from './lottery.controller';
import { LotteryService } from './lottery.service';
import { ScoreService } from './score.service';
@Module({
imports: [
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
]),
MongooseModule.forFeature([
{ name: 'Follower', schema: FollowerSchema },
]),
MongooseModule.forFeature([
{ name: 'Like', schema: LikeSchema },
]),
MongooseModule.forFeature([
{ name: 'Comment', schema: CommentSchema },
]),
MongooseModule.forFeature([
{ name: 'LottryResult', schema: LottoryResultSchema },
]),
],
controllers: [LotteryController],
providers: [LotteryService, ScoreService]
})
export class LotteryModule { }
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
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 { UserSchema } from 'src/instagram/models/user.schema';
import { LotteryController } from './lottery.controller';
import { LotteryService } from './lottery.service';
import { ScoreService } from './score.service';
@Module({
imports: [
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
]),
MongooseModule.forFeature([
{ name: 'Follower', schema: FollowerSchema },
]),
MongooseModule.forFeature([
{ name: 'Like', schema: LikeSchema },
]),
MongooseModule.forFeature([
{ name: 'Comment', schema: CommentSchema },
]),
MongooseModule.forFeature([
{ name: 'LottryResult', schema: LottoryResultSchema },
]),
],
controllers: [LotteryController],
providers: [LotteryService, ScoreService]
})
export class LotteryModule { }
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 { 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';
@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>
) { }
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 addToStoryScore = await this.scoreService.getTagsScore(username)
return { likesScore, commentScore, addToStoryScore, totalScore: likesScore + commentScore + addToStoryScore }
}
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>()
foundUsernameInComment.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.push(user_id)
}
})
foundUsernameInLike.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.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 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'
})
index++;
console.log(`user_id:${user_id} , index: ${index}, score:${userScore}`);
}
console.log(`user index:${count} , total items: ${totalItems}`);
count++
}
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});
}
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"
}
}
}
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 { 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';
@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>
) { }
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 addToStoryScore = await this.scoreService.getTagsScore(username)
return { likesScore, commentScore, addToStoryScore, totalScore: likesScore + commentScore + addToStoryScore }
}
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>()
foundUsernameInComment.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.push(user_id)
}
})
foundUsernameInLike.forEach((user_id) => {
if (!foundUser_id.includes(user_id)) {
foundUser_id.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 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'
})
index++;
console.log(`user_id:${user_id} , index: ${index}, score:${userScore}`);
}
console.log(`user index:${count} , total items: ${totalItems}`);
count++
}
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});
}
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"
}
}
}
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 addToStoryData from '../instagram/values/add-to-story-data'
@Injectable()
export class ScoreService {
constructor(
@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<CommentDocument>
) { }
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
}
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 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 => {
score += element.post_score
})
return score
}
async getTagsScore(username: string) {
let addToStoryScore = 0
let foundUser = addToStoryData.addToStoryData.find((user) => {
return user.username === username
})
if (foundUser) {
addToStoryScore = foundUser.count
}
return addToStoryScore
}
async calculateUserScore(username: string, profileUsername: string, postArray: string[]) {
let likesScore = await this.getUserLikesScore(username, profileUsername, postArray)
let commentScore = await this.getUserCommentsScore(username, profileUsername, postArray)
let addToStoryScore = await this.getTagsScore(username)
return likesScore + commentScore + addToStoryScore
}
}
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 addToStoryData from '../instagram/values/add-to-story-data'
@Injectable()
export class ScoreService {
constructor(
@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<CommentDocument>
) { }
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
}
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 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 => {
score += element.post_score
})
return score
}
async getTagsScore(username: string) {
let addToStoryScore = 0
let foundUser = addToStoryData.addToStoryData.find((user) => {
return user.username === username
})
if (foundUser) {
addToStoryScore = foundUser.count
}
return addToStoryScore
}
async calculateUserScore(username: string, profileUsername: string, postArray: string[]) {
let likesScore = await this.getUserLikesScore(username, profileUsername, postArray)
let commentScore = await this.getUserCommentsScore(username, profileUsername, postArray)
let addToStoryScore = await this.getTagsScore(username)
return likesScore + commentScore + addToStoryScore
}
}
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
})
await app.listen(4002).then(() => {
console.log(`server start on port 4002`);
})
}
bootstrap();
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
})
await app.listen(4002).then(() => {
console.log(`server start on port 4002`);
})
}
bootstrap();
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type RequestDocument = Request & Document;
@Schema({ timestamps: true })
export class Request {
@Prop()
_id: Types.ObjectId;
@Prop()
cursor: string;
@Prop()
type: string;
@Prop()
post_short_code: string;
@Prop()
account_username: string;
}
export const RequestSchema = SchemaFactory.createForClass(Request);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type RequestDocument = Request & Document;
@Schema({ timestamps: true })
export class Request {
@Prop()
_id: Types.ObjectId;
@Prop()
cursor: string;
@Prop()
type: string;
@Prop()
post_short_code: string;
@Prop()
account_username: string;
}
export const RequestSchema = SchemaFactory.createForClass(Request);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type ResultDocument = Result & Document;
@Schema({ timestamps: true })
export class Result {
@Prop()
username: string;
@Prop()
valid_mentions: number;
@Prop()
mentions_before: number;
@Prop()
followed_before: number;
@Prop()
pending_mentions: number;
@Prop()
score: number;
@Prop()
valid_users: Array<string>;
@Prop()
mentions_before_users: Array<string>;
@Prop()
followed_before_users: Array<string>;
@Prop()
pending_users: Array<string>;
}
export const ResultSchema = SchemaFactory.createForClass(Result);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type ResultDocument = Result & Document;
@Schema({ timestamps: true })
export class Result {
@Prop()
username: string;
@Prop()
valid_mentions: number;
@Prop()
mentions_before: number;
@Prop()
followed_before: number;
@Prop()
pending_mentions: number;
@Prop()
score: number;
@Prop()
valid_users: Array<string>;
@Prop()
mentions_before_users: Array<string>;
@Prop()
followed_before_users: Array<string>;
@Prop()
pending_users: Array<string>;
}
export const ResultSchema = SchemaFactory.createForClass(Result);
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