Skip to content

Commit

Permalink
[BETA] added sort by relevance
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Nov 23, 2024
1 parent ba2136f commit 984ec4f
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/workshop/bandage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface BandageSearch {
User?: { name: { contains: string } }
}

export const sort_keys = ['popular_up', 'date_up', 'name_up'];
export const sort_keys = ['popular_up', 'date_up', 'name_up', 'relevant_up'];

const constructSort = (sort?: string): Prisma.BandageOrderByWithRelationInput => {
/* generate sort rule */
Expand Down Expand Up @@ -62,12 +62,14 @@ export class BandageService {
return await this.prisma.bandage.count();
}

async getBandages(session: Session,
async getBandages(
session: Session,
take: number,
page: number,
search?: string,
filters?: number[],
sort?: string) {
sort?: string
) {

/* get workshop list */

Expand Down Expand Up @@ -100,6 +102,10 @@ export class BandageService {
AND: filters_rule,
};

if (sort === sort_keys[3]) {
return this.getBandagesRelevance(session, take, page, where, available);
}

const data = await this.prisma.bandage.findMany({
where: where,
include: {
Expand All @@ -117,6 +123,37 @@ export class BandageService {
return { data: result, totalCount: count, next_page: result.length ? page + 1 : page };
}

async getBandagesRelevance(
session: Session,
take: number,
page: number,
where: Prisma.BandageWhereInput,
available: boolean
) {
const data = await this.prisma.bandage.findMany({
where: where,
include: {
User: { include: { UserSettings: true } },
stars: true,
categories: true
}
});

const count = await this.prisma.bandage.count({ where: where });

const getRelevance = (bandage: { stars: any[]; creationDate: Date; }) => {
const stars = bandage.stars.length;
const daysSinceCreation =
(Date.now() - new Date(bandage.creationDate).getTime()) / (1000 * 60 * 60 * 24);
return stars / Math.pow(daysSinceCreation + 1, 1.5);
}

const startIndex = page * take;
const ratedWorks = data.sort((a, b) => getRelevance(b) - getRelevance(a)).slice(startIndex, startIndex + take);
const result = generate_response(ratedWorks, session, available);
return { data: result, totalCount: count, next_page: result.length ? page + 1 : page };
}

async setStar(session: Session, set: boolean, id: string) {
/* set star to bandage by external id */

Expand Down

0 comments on commit 984ec4f

Please sign in to comment.