Skip to content

Commit fb727c3

Browse files
committed
fix bug
1 parent ea5e328 commit fb727c3

2 files changed

Lines changed: 69 additions & 42 deletions

File tree

src/modules/games/files.service.spec.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -466,15 +466,48 @@ describe("FilesService", () => {
466466
});
467467

468468
describe("upsertReleaseRecord", () => {
469-
it("should upsert game versions atomically via query builder", async () => {
470-
const execute = jest.fn().mockResolvedValue(undefined);
471-
const orUpdate = jest.fn().mockReturnValue({ execute });
472-
const values = jest.fn().mockReturnValue({ orUpdate });
473-
const into = jest.fn().mockReturnValue({ values });
474-
const insert = jest.fn().mockReturnValue({ into });
475-
const qb = { insert };
469+
it("should create a new release row when none exists", async () => {
470+
gameVersionRepository.findOne.mockResolvedValueOnce(null);
471+
gameVersionRepository.save.mockResolvedValueOnce(undefined);
476472

477-
gameVersionRepository.createQueryBuilder.mockReturnValue(qb as any);
473+
await (service as any).upsertReleaseRecord(9, {
474+
file_path: "/tmp/test-files/Game (v2).zip",
475+
version: "v2",
476+
size: 2000n,
477+
release_date: new Date("2025-01-01T00:00:00.000Z"),
478+
early_access: true,
479+
type: "WINDOWS_PORTABLE",
480+
});
481+
482+
expect(gameVersionRepository.findOne).toHaveBeenCalledWith(
483+
expect.objectContaining({
484+
where: {
485+
game: { id: 9 },
486+
file_path: "/tmp/test-files/Game (v2).zip",
487+
},
488+
withDeleted: true,
489+
}),
490+
);
491+
expect(gameVersionRepository.save).toHaveBeenCalledWith(
492+
expect.objectContaining({
493+
id: undefined,
494+
game: { id: 9 },
495+
file_path: "/tmp/test-files/Game (v2).zip",
496+
version: "v2",
497+
size: 2000n,
498+
early_access: true,
499+
type: "WINDOWS_PORTABLE",
500+
deleted_at: null,
501+
}),
502+
);
503+
});
504+
505+
it("should update an existing release row by reusing its id", async () => {
506+
gameVersionRepository.findOne.mockResolvedValueOnce({
507+
id: 42,
508+
file_path: "/tmp/test-files/Game (v2).zip",
509+
});
510+
gameVersionRepository.save.mockResolvedValueOnce(undefined);
478511

479512
await (service as any).upsertReleaseRecord(9, {
480513
file_path: "/tmp/test-files/Game (v2).zip",
@@ -485,12 +518,14 @@ describe("FilesService", () => {
485518
type: "WINDOWS_PORTABLE",
486519
});
487520

488-
expect(gameVersionRepository.createQueryBuilder).toHaveBeenCalled();
489-
expect(orUpdate).toHaveBeenCalledWith(
490-
expect.arrayContaining(["deleted_at", "updated_at"]),
491-
["game_id", "file_path"],
521+
expect(gameVersionRepository.save).toHaveBeenCalledWith(
522+
expect.objectContaining({
523+
id: 42,
524+
game: { id: 9 },
525+
file_path: "/tmp/test-files/Game (v2).zip",
526+
deleted_at: null,
527+
}),
492528
);
493-
expect(execute).toHaveBeenCalled();
494529
});
495530
});
496531

src/modules/games/files.service.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -416,36 +416,28 @@ export class FilesService implements OnApplicationBootstrap {
416416
): Promise<void> {
417417
const now = new Date();
418418

419-
await this.gameVersionRepository
420-
.createQueryBuilder()
421-
.insert()
422-
.into(GameVersion)
423-
.values({
424-
game: { id: gameId } as GamevaultGame,
419+
const existingVersion = await this.gameVersionRepository.findOne({
420+
where: {
421+
game: { id: gameId },
425422
file_path: indexedGame.file_path,
426-
version: indexedGame.version,
427-
size: indexedGame.size,
428-
release_date: indexedGame.release_date,
429-
early_access: indexedGame.early_access,
430-
type: indexedGame.type,
431-
indexed_at: now,
432-
deleted_at: null,
433-
updated_at: now,
434-
})
435-
.orUpdate(
436-
[
437-
"version",
438-
"size",
439-
"release_date",
440-
"early_access",
441-
"type",
442-
"indexed_at",
443-
"deleted_at",
444-
"updated_at",
445-
],
446-
["game_id", "file_path"],
447-
)
448-
.execute();
423+
},
424+
withDeleted: true,
425+
});
426+
427+
const versionPatch = Object.assign(new GameVersion(), {
428+
id: existingVersion?.id,
429+
game: { id: gameId } as GamevaultGame,
430+
file_path: indexedGame.file_path,
431+
version: indexedGame.version,
432+
size: indexedGame.size,
433+
release_date: indexedGame.release_date,
434+
early_access: indexedGame.early_access,
435+
type: indexedGame.type,
436+
indexed_at: now,
437+
deleted_at: null,
438+
});
439+
440+
await this.gameVersionRepository.save(versionPatch);
449441
}
450442

451443
/** Returns all versions from normalized storage, falling back to legacy columns. */

0 commit comments

Comments
 (0)