Skip to content

Commit

Permalink
reset sorttitle when user unmaps data
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfagun74 committed Sep 26, 2024
1 parent 736b584 commit 3904233
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
31 changes: 6 additions & 25 deletions src/modules/games/files.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ export class FilesService implements OnApplicationBootstrap {
gameToIndex.size = file.size;
gameToIndex.file_path = `${file.path}`;
gameToIndex.title = this.extractTitle(file.path);
gameToIndex.sort_title = this.createSortTitle(gameToIndex.title);
gameToIndex.sort_title = this.gamesService.generateSortTitle(
gameToIndex.title,
);
gameToIndex.release_date = this.extractReleaseYear(file.path);
gameToIndex.version = this.extractVersion(file.path);
gameToIndex.early_access = this.extractEarlyAccessFlag(
Expand Down Expand Up @@ -191,7 +193,9 @@ export class FilesService implements OnApplicationBootstrap {

gameToUpdate.file_path = updatesToApply.file_path;
gameToUpdate.title = updatesToApply.title;
gameToUpdate.sort_title = this.createSortTitle(updatesToApply.title);
gameToUpdate.sort_title = this.gamesService.generateSortTitle(
updatesToApply.title,
);
gameToUpdate.release_date = updatesToApply.release_date;
gameToUpdate.size = updatesToApply.size;
gameToUpdate.version = updatesToApply.version;
Expand Down Expand Up @@ -715,27 +719,4 @@ export class FilesService implements OnApplicationBootstrap {
size: rangeSize,
};
}

private createSortTitle(title: string): string {
// List of leading articles to be removed
const articles: string[] = ["the", "a", "an"];

// Convert the title to lowercase
let sortTitle: string = toLower(title).trim();

// Remove any leading article
for (const article of articles) {
const articleWithSpace = `${article} `;
if (sortTitle.startsWith(articleWithSpace)) {
sortTitle = sortTitle.substring(articleWithSpace.length);
break;
}
}

// Remove special characters except alphanumeric and spaces and Replace multiple spaces with a single space and trim
return sortTitle
.replace(/[^a-z0-9\s]/g, "")
.replace(/\s+/g, " ")
.trim();
}
}
26 changes: 24 additions & 2 deletions src/modules/games/games.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Repository,
} from "typeorm";

import { isEmpty, kebabCase, sample } from "lodash";
import { isEmpty, kebabCase, sample, toLower } from "lodash";
import { FindOptions } from "../../globals";
import { DeveloperMetadata } from "../metadata/developers/developer.metadata.entity";
import { GameMetadata } from "../metadata/games/game.metadata.entity";
Expand Down Expand Up @@ -173,7 +173,7 @@ export class GamesService {
this.logger.debug({
message: "Updating User Metadata",
game: game.getLoggableData(),
user_metadata: dto.user_metadata
user_metadata: dto.user_metadata,
});
const updatedUserMetadata = game.user_metadata || new GameMetadata();

Expand Down Expand Up @@ -407,4 +407,26 @@ export class GamesService {

return [GameExistence.EXISTS, foundGame];
}

public generateSortTitle(title: string): string {
// List of leading articles to be removed
const articles: string[] = ["the", "a", "an"];

// Convert the title to lowercase
let sortTitle: string = toLower(title).trim();

// Remove any leading article
for (const article of articles) {
const articleWithSpace = `${article} `;
if (sortTitle.startsWith(articleWithSpace)) {
sortTitle = sortTitle.substring(articleWithSpace.length);
break;
}
}
// Remove special characters except alphanumeric and spaces and Replace multiple spaces with a single space and trim
return sortTitle
.replace(/[^a-z0-9\s]/g, "")
.replace(/\s+/g, " ")
.trim();
}
}
1 change: 1 addition & 0 deletions src/modules/metadata/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export class MetadataService {
game.user_metadata.id,
);
game.user_metadata = null;
game.sort_title = this.gamesService.generateSortTitle(game.title);
this.logger.log({
message: "Deleted user metadata from a game.",
game: game.getLoggableData(),
Expand Down

0 comments on commit 3904233

Please sign in to comment.