Skip to content

Commit

Permalink
Merge pull request capsule-corp-ternoa#177 from capsule-corp-ternoa/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Leouarz authored Feb 28, 2022
2 parents b2057fd + ad71d4e commit 8638b9a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/api/controllers/users/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,17 @@ export class Controller {
next(err)
}
}

async getArtistHighLight(
_req: Request,
res: Response,
next: NextFunction
): Promise<void> {
try {
res.json(await UserService.getArtistHighLight());
} catch (err) {
next(err)
}
}
}
export default new Controller();
1 change: 1 addition & 0 deletions src/api/controllers/users/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default express
.patch("/reviewRequested/:id", controller.reviewRequested) // ternoa-api
.get("/top-sellers", controller.getTopSellers) // ternoa-api
.get("/most-followed", controller.getMostFollowed) // ternoa-api
.get("/artist-highlight", controller.getArtistHighLight) // ternoa-api
.get("/", controller.getUsers) // ternoa-api
.get("/verifyTwitter/:id", controller.verifyTwitter) // ternoa-api
.get("/:id", controller.getUser) // ternoa-api
Expand Down
59 changes: 59 additions & 0 deletions src/api/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IUser } from "../../interfaces/IUser";
import UserViewModel from "../../models/userView";
import NftLikeModel from "../../models/nftLike";
import FollowModel from "../../models/follow";
import ArtistHighlightModel from "../../models/artistHighlight";
import QueriesBuilder from "./gqlQueriesBuilder";
import { AccountResponse, Account, CustomResponse } from "../../interfaces/graphQL";
import { TIME_BETWEEN_SAME_USER_VIEWS, TERNOA_API_URL } from "../../utils";
Expand Down Expand Up @@ -156,6 +157,64 @@ export class UserService {
throw err
}
}

/**
* get artist highlight / artist of the week
* artist with most number of follower
* stays for 1 week
* go to next one
* if no artist, purge artist-highlight table
* @throws Will throw an error if db / indexer can't be reached
*/
async getArtistHighLight(): Promise<IUser> {
try{
// get last timestamp from db
const lastArtistHighlight = await ArtistHighlightModel.findOne({}, {}, {sort: {timestamp: -1}});
// if timestamp is less than 7 days ago, return artist
if (lastArtistHighlight && (+new Date() - +lastArtistHighlight.timestamp < 7 * 24 * 3600 * 1000)){
return await this.findUser({id: lastArtistHighlight.walletId});
}else{
let artist = null
let isError = false
while(!artist && !isError){
// get 50 first rankings of artist
const mostFollowed = await this.getMostFollowed({pagination:{page:1, limit: 50}});
let i = 0
for (const user of mostFollowed.data){
i +=1
// check in db that it's not used else go to next
const tmpArtistHighlight = await ArtistHighlightModel.findOne({walletId: user.walletId});
if (tmpArtistHighlight) continue;
// check that he has at least 6 nfts else go to next
const gqlQuery = QueriesBuilder.countCreated({id: user.walletId});
const indexerResponse = await request(indexerUrl, gqlQuery);
if (indexerResponse.nftEntities.totalCount < 6) continue;
// if ok, push in db and return artist
artist = user
break;
}
if (artist) break;
const count = await ArtistHighlightModel.count()
if (count > 0){
// we purge db to start the process again
await ArtistHighlightModel.deleteMany()
}else{
// no artist found, throw error
isError = true
}
}
if (artist){
const newArtistHighlight = new ArtistHighlightModel({walletId: artist.walletId, timestamp: new Date()})
await newArtistHighlight.save()
return artist
}else{
throw new Error("No suitable artist to highlight was found")
}
}
}catch(err){
throw err
}
}
}

export default new UserService();
4 changes: 4 additions & 0 deletions src/interfaces/IArtistHighlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IArtistHighlight {
walletId: string;
timestamp: Date;
}
24 changes: 24 additions & 0 deletions src/models/artistHighlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IArtistHighlight } from "../interfaces/IArtistHighlight";
import mongoose, { PaginateModel } from "mongoose";
import mongoosePaginate from "mongoose-paginate-v2";

const ArtistHighlight = new mongoose.Schema({
walletId: {
type: String,
required: true,
},
timestamp: {
type: Date,
required: true,
},
});

ArtistHighlight.plugin(mongoosePaginate);

const ArtistHighlightModel = mongoose.model<IArtistHighlight & mongoose.Document>(
"ArtistHighlight",
ArtistHighlight,
"artist-highlights"
) as PaginateModel<IArtistHighlight & mongoose.Document>;

export default ArtistHighlightModel;

0 comments on commit 8638b9a

Please sign in to comment.