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 { getUnlockedAchievements } from "@main/events/user/get-unlocked-achievements";
import { Game } from "@main/entity";
import { achievementsLogger } from "../logger";
import { publishNewAchievementNotification } from "../notifications";
import { SubscriptionRequiredError } from "@shared";
import { achievementsLogger } from "../logger";
const saveAchievementsOnLocal = async (
objectId: string,
@ -120,10 +121,14 @@ export const mergeAchievements = async (
}
if (game.remoteId) {
await HydraApi.put("/profile/games/achievements", {
id: game.remoteId,
achievements: mergedLocalAchievements,
})
await HydraApi.put(
"/profile/games/achievements",
{
id: game.remoteId,
achievements: mergedLocalAchievements,
},
{ needsSubscription: !newAchievements.length }
)
.then((response) => {
return saveAchievementsOnLocal(
response.objectId,
@ -133,7 +138,13 @@ export const mergeAchievements = async (
);
})
.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(
game.objectID,

View file

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

View file

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