fix: user data not being correctly loaded on hydra api setup

This commit is contained in:
Zamitto 2024-12-20 14:48:04 -03:00
parent c8d1b2c19a
commit 170ac28bb5
3 changed files with 29 additions and 13 deletions

View file

@ -7,8 +7,9 @@ import { WindowManager } from "../window-manager";
import { HydraApi } from "../hydra-api"; import { HydraApi } from "../hydra-api";
import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements"; import { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
import { Game } from "@main/entity"; import { Game } from "@main/entity";
import { achievementsLogger } from "../logger";
import { publishNewAchievementNotification } from "../notifications"; import { publishNewAchievementNotification } from "../notifications";
import { SubscriptionRequiredError } from "@shared";
import { achievementsLogger } from "../logger";
const saveAchievementsOnLocal = async ( const saveAchievementsOnLocal = async (
objectId: string, objectId: string,
@ -120,10 +121,14 @@ export const mergeAchievements = async (
} }
if (game.remoteId) { if (game.remoteId) {
await HydraApi.put("/profile/games/achievements", { await HydraApi.put(
id: game.remoteId, "/profile/games/achievements",
achievements: mergedLocalAchievements, {
}) id: game.remoteId,
achievements: mergedLocalAchievements,
},
{ needsSubscription: !newAchievements.length }
)
.then((response) => { .then((response) => {
return saveAchievementsOnLocal( return saveAchievementsOnLocal(
response.objectId, response.objectId,
@ -133,7 +138,13 @@ export const mergeAchievements = async (
); );
}) })
.catch((err) => { .catch((err) => {
achievementsLogger.error(err); if (err! instanceof SubscriptionRequiredError) {
achievementsLogger.log(
"Achievements not synchronized on API due to lack of subscription",
game.objectID,
game.title
);
}
return saveAchievementsOnLocal( return saveAchievementsOnLocal(
game.objectID, game.objectID,

View file

@ -23,7 +23,7 @@ interface HydraApiUserAuth {
authToken: string; authToken: string;
refreshToken: string; refreshToken: string;
expirationTimestamp: number; expirationTimestamp: number;
subscription: { expiresAt: Date | null } | null; subscription: { expiresAt: Date | string | null } | null;
} }
export class HydraApi { export class HydraApi {
@ -182,8 +182,6 @@ export class HydraApi {
); );
} }
await getUserData();
const userAuth = await userAuthRepository.findOne({ const userAuth = await userAuthRepository.findOne({
where: { id: 1 }, where: { id: 1 },
relations: { subscription: true }, relations: { subscription: true },
@ -197,6 +195,14 @@ export class HydraApi {
? { expiresAt: userAuth.subscription?.expiresAt } ? { expiresAt: userAuth.subscription?.expiresAt }
: null, : null,
}; };
const updatedUserData = await getUserData();
this.userAuth.subscription = updatedUserData?.subscription
? {
expiresAt: updatedUserData.subscription.expiresAt,
}
: null;
} }
private static sendSignOutEvent() { private static sendSignOutEvent() {
@ -284,10 +290,8 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
} }
if (needsSubscription) { if (needsSubscription && !this.hasActiveSubscription()) {
if (!(await this.hasActiveSubscription())) { throw new SubscriptionRequiredError();
throw new SubscriptionRequiredError();
}
} }
} }

View file

@ -42,6 +42,7 @@ export const getUserData = () => {
}) })
.catch(async (err) => { .catch(async (err) => {
if (err instanceof UserNotLoggedInError) { if (err instanceof UserNotLoggedInError) {
logger.info("User is not logged in", err);
return null; return null;
} }
logger.error("Failed to get logged user"); logger.error("Failed to get logged user");