enh
This commit is contained in:
parent
3d7e023965
commit
d234c8a00f
3 changed files with 57 additions and 12 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
import { router, protectedProcedure } from '../trpc-index';
|
import { router, protectedProcedure } from '../trpc-index';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq, and } from 'drizzle-orm';
|
||||||
|
import { z } from 'zod';
|
||||||
import { db } from '../../db/db_index';
|
import { db } from '../../db/db_index';
|
||||||
import { users, userDetails, userCreds } from '../../db/schema';
|
import { users, userDetails, userCreds, notifCreds } from '../../db/schema';
|
||||||
import { ApiError } from '../../lib/api-error';
|
import { ApiError } from '../../lib/api-error';
|
||||||
import { jwtSecret } from 'src/lib/env-exporter';
|
import { jwtSecret } from 'src/lib/env-exporter';
|
||||||
import { generateSignedUrlFromS3Url } from '../../lib/s3-client';
|
import { generateSignedUrlFromS3Url } from '../../lib/s3-client';
|
||||||
|
|
@ -107,4 +108,40 @@ export const userRouter = router({
|
||||||
isComplete: !!(user.name && user.email && creds),
|
isComplete: !!(user.name && user.email && creds),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
savePushToken: protectedProcedure
|
||||||
|
.input(z.object({ token: z.string() }))
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
const userId = ctx.user.userId;
|
||||||
|
const { token } = input;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
throw new ApiError('User not authenticated', 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this exact token already exists for this user
|
||||||
|
const existing = await db.query.notifCreds.findFirst({
|
||||||
|
where: and(
|
||||||
|
eq(notifCreds.userId, userId),
|
||||||
|
eq(notifCreds.token, token)
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
// Update lastVerified timestamp
|
||||||
|
await db
|
||||||
|
.update(notifCreds)
|
||||||
|
.set({ lastVerified: new Date() })
|
||||||
|
.where(eq(notifCreds.id, existing.id));
|
||||||
|
} else {
|
||||||
|
// Insert new token
|
||||||
|
await db.insert(notifCreds).values({
|
||||||
|
userId,
|
||||||
|
token,
|
||||||
|
lastVerified: new Date(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
@ -27,6 +27,7 @@ import WebViewWrapper from "@/components/WebViewWrapper";
|
||||||
import BackHandlerWrapper from "@/components/BackHandler";
|
import BackHandlerWrapper from "@/components/BackHandler";
|
||||||
import AddToCartDialog from "@/src/components/AddToCartDialog";
|
import AddToCartDialog from "@/src/components/AddToCartDialog";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import NotifChecker from "@/services/notif-service/notif-checker";
|
||||||
|
|
||||||
export default function RootLayout() {
|
export default function RootLayout() {
|
||||||
const colorScheme = useColorScheme();
|
const colorScheme = useColorScheme();
|
||||||
|
|
@ -57,7 +58,8 @@ export default function RootLayout() {
|
||||||
<FirstUserWrapper>
|
<FirstUserWrapper>
|
||||||
<AuthProvider>
|
<AuthProvider>
|
||||||
<NotificationProvider>
|
<NotificationProvider>
|
||||||
<PaperProvider>
|
<NotifChecker />
|
||||||
|
<PaperProvider>
|
||||||
<LocationTestWrapper>
|
<LocationTestWrapper>
|
||||||
<RefreshProvider queryClient={queryClient}>
|
<RefreshProvider queryClient={queryClient}>
|
||||||
<BackHandlerWrapper />
|
<BackHandlerWrapper />
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import { MyText } from "common-ui";
|
||||||
import { View, Linking } from "react-native";
|
import { View, Linking } from "react-native";
|
||||||
import { tw } from "common-ui";
|
import { tw } from "common-ui";
|
||||||
import { MyButton } from "common-ui";
|
import { MyButton } from "common-ui";
|
||||||
import { useAuth } from "@/components/context/auth-context";
|
import { useAuth } from "@/src/contexts/AuthContext";
|
||||||
|
import { trpc } from "@/src/trpc-client";
|
||||||
|
|
||||||
interface Props {}
|
interface Props {}
|
||||||
|
|
||||||
|
|
@ -13,17 +14,22 @@ function NotifChecker(props: Props) {
|
||||||
const {} = props;
|
const {} = props;
|
||||||
const [showPermissionDialog, setShowPermissionDialog] = React.useState(false);
|
const [showPermissionDialog, setShowPermissionDialog] = React.useState(false);
|
||||||
|
|
||||||
const {isLoggedIn} = useAuth();
|
const { isAuthenticated } = useAuth();
|
||||||
// const { data: hasPushToken, isLoading, isError } = useHasPushToken({enabled: isLoggedIn});
|
const savePushTokenMutation = trpc.user.user.savePushToken.useMutation();
|
||||||
const hasPushToken = false;
|
|
||||||
// const { mutate: addPushToken } = useAddPushToken();
|
|
||||||
const addPushToken = (input:any) => {};
|
|
||||||
const { notifPermission, expoPushToken } = useNotification();
|
const { notifPermission, expoPushToken } = useNotification();
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if(isLoggedIn && !hasPushToken && notifPermission =='granted') {
|
if (isAuthenticated && expoPushToken && notifPermission === 'granted') {
|
||||||
addPushToken(expoPushToken!);
|
savePushTokenMutation.mutate(
|
||||||
|
{ token: expoPushToken },
|
||||||
|
{
|
||||||
|
onError: (error) => {
|
||||||
|
console.error('Failed to save push token:', error);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},[isLoggedIn, hasPushToken])
|
}, [isAuthenticated, expoPushToken, notifPermission]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (notifPermission === "denied") {
|
if (notifPermission === "denied") {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue