Commit 251575ce authored by velayat's avatar velayat
Browse files

change lottry result api

parent 476384c7
...@@ -5,10 +5,14 @@ export type LottoryResultDocument = LottoryResult & Document; ...@@ -5,10 +5,14 @@ export type LottoryResultDocument = LottoryResult & Document;
@Schema({ timestamps: true }) @Schema({ timestamps: true })
export class LottoryResult { export class LottoryResult {
@Prop()
index: number;
@Prop() @Prop()
username: string; username: string;
@Prop() @Prop()
index: number; tagged_user: string;
@Prop()
status: string;
} }
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult); export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
...@@ -505,7 +505,7 @@ export class AppService implements OnApplicationBootstrap { ...@@ -505,7 +505,7 @@ export class AppService implements OnApplicationBootstrap {
(response.followed_before_mentions = userRes.followed_before), (response.followed_before_mentions = userRes.followed_before),
(response.pending_mentions = userRes.pending_mentions), (response.pending_mentions = userRes.pending_mentions),
(response.last_update = userRes['updatedAt']), (response.last_update = userRes['updatedAt']),
(response.score = userRes.score); (response.score = userRes.score);
userIndexs.forEach((index) => { userIndexs.forEach((index) => {
response.lottory_chances_codes.push(index.index.toString()); response.lottory_chances_codes.push(index.index.toString());
}); });
...@@ -552,16 +552,43 @@ export class AppService implements OnApplicationBootstrap { ...@@ -552,16 +552,43 @@ export class AppService implements OnApplicationBootstrap {
} }
async addResultsToDB() { async addResultsToDB() {
await this.lotoryResultModel.deleteMany(); // await this.lotoryResultModel.deleteMany();
const comptitionArray = new Array<any>(); const comptitionArray = new Array<any>();
const foundUsernames = await this.resultModel.find().sort({ score: -1 }); const foundUsernames = await this.resultModel
console.log(foundUsernames); .find({})
.sort({ score: -1 });
let index = 1; let index = 1;
for await (const user of foundUsernames) { for await (const user of foundUsernames) {
for (let u = 0; u < user.score; u++) { for (let u = 0; u < user.valid_users.length; u++) {
comptitionArray.push({ username: user.username, index }); const isChanceExist = await this.lotoryResultModel
.findOne({
username: user.username,
tagged_user: user.valid_users[u],
})
.exec();
if (!isChanceExist)
comptitionArray.push({
index,
username: user.username,
tagged_user: user.valid_users[u],
status:"valid"
});
index++; index++;
} }
const isUserChanceExist = await this.lotoryResultModel.findOne({
username: user.username,
tagged_user: user.username,
});
if (!isUserChanceExist)
comptitionArray.push({
index,
username: user.username,
tagged_user: user.username,
status:"valid"
});
index++;
} }
await this.lotoryResultModel.insertMany(comptitionArray); await this.lotoryResultModel.insertMany(comptitionArray);
return 'successfull'; return 'successfull';
...@@ -570,7 +597,7 @@ export class AppService implements OnApplicationBootstrap { ...@@ -570,7 +597,7 @@ export class AppService implements OnApplicationBootstrap {
async getResultDb() { async getResultDb() {
return await this.lotoryResultModel return await this.lotoryResultModel
.find() .find()
.select({ username: 1, index: 1 }); .select({ username: 1,tagged_user:1, index: 1,status:1 });
} }
} }
......
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