feat: getMe on launch

This commit is contained in:
Zamitto 2024-10-21 15:01:43 -03:00
parent d801bad49e
commit df5f82d47f
5 changed files with 89 additions and 72 deletions

View file

@ -0,0 +1,43 @@
import type { UserDetails } from "@types";
import { HydraApi } from "../hydra-api";
import {
userAuthRepository,
userSubscriptionRepository,
} from "@main/repository";
import * as Sentry from "@sentry/electron/main";
export const getUserData = () => {
return HydraApi.get<UserDetails>(`/profile/me`).then(async (me) => {
userAuthRepository.upsert(
{
id: 1,
displayName: me.displayName,
profileImageUrl: me.profileImageUrl,
backgroundImageUrl: me.backgroundImageUrl,
userId: me.id,
},
["id"]
);
if (me.subscription) {
await userSubscriptionRepository.upsert(
{
id: 1,
subscriptionId: me.subscription?.id || "",
status: me.subscription?.status || "",
planId: me.subscription?.plan.id || "",
planName: me.subscription?.plan.name || "",
expiresAt: me.subscription?.expiresAt || null,
user: { id: 1 },
},
["id"]
);
} else {
await userSubscriptionRepository.delete({ id: 1 });
}
Sentry.setUser({ id: me.id, username: me.username });
return me;
});
};