131 lines
3.6 KiB
TypeScript
Executable File
131 lines
3.6 KiB
TypeScript
Executable File
import 'server-only';
|
|
import { db } from '~/server/db';
|
|
import * as schema from '~/server/db/schema';
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
export const getUsers = async () => {
|
|
try {
|
|
const result = await db.select({
|
|
id: schema.users.id,
|
|
name: schema.users.name,
|
|
message: schema.users.message,
|
|
}).from(schema.users);
|
|
return result;
|
|
} catch (error) {
|
|
console.error("Error fetching users", error);
|
|
throw new Error("Failed to fetch users");
|
|
}
|
|
};
|
|
|
|
export const getUser = async (userId: number) => {
|
|
try {
|
|
const result = await db.select({
|
|
id: schema.users.id,
|
|
name: schema.users.name,
|
|
message: schema.users.message,
|
|
pushToken: schema.users.pushToken,
|
|
}).from(schema.users)
|
|
.where(eq(schema.users.id, userId));
|
|
return result;
|
|
} catch (error) {
|
|
console.error("Error fetching user", error);
|
|
throw new Error("Failed to fetch user");
|
|
}
|
|
};
|
|
|
|
export const getMessage = async (userId: number) => {
|
|
try {
|
|
let message = 1;
|
|
if (userId === 1)
|
|
message = 2;
|
|
const result = await db.select({
|
|
receivedMessage: schema.users.message,
|
|
}).from(schema.users)
|
|
.where(eq(schema.users.id, message))
|
|
return result;
|
|
} catch (error) {
|
|
console.error("Error fetching message", error);
|
|
throw new Error("Failed to fetch message");
|
|
}
|
|
};
|
|
|
|
export const setMessage = async (userId: number, message: string) => {
|
|
try {
|
|
await db.update(schema.users)
|
|
.set({ message: message })
|
|
.where(eq(schema.users.id, userId));
|
|
const otherUserId = userId === 1 ? 2 : 1;
|
|
const otherUser = await getUser(otherUserId);
|
|
|
|
if (otherUser?.[0]?.pushToken) {
|
|
await sendPushNotification(otherUser[0].pushToken, message);
|
|
} else {
|
|
console.log(`Other user with id ${otherUserId} does not have a push token`);
|
|
}
|
|
} catch (error) {
|
|
console.error("Error setting message", error);
|
|
throw new Error("Failed to set message");
|
|
}
|
|
};
|
|
|
|
export const getCountdown = async () => {
|
|
try {
|
|
const result = await db.select({
|
|
countdown: schema.countdown.date,
|
|
}).from(schema.countdown)
|
|
.where(eq(schema.countdown.id, 1))
|
|
return result;
|
|
} catch (error) {
|
|
console.error("Error fetching countdown", error);
|
|
throw new Error("Failed to fetch countdown");
|
|
}
|
|
};
|
|
|
|
export const setCountdown = async (date: Date) => {
|
|
try {
|
|
await db.update(schema.countdown)
|
|
.set({ date: date })
|
|
.where(eq(schema.countdown.id, 1));
|
|
} catch (error) {
|
|
console.error("Error setting countdown", error);
|
|
throw new Error("Failed to set countdown");
|
|
}
|
|
};
|
|
|
|
export const updateUserPushToken = async (userId: number, pushToken: string) => {
|
|
try {
|
|
await db.update(schema.users)
|
|
.set({ pushToken: pushToken })
|
|
.where(eq(schema.users.id, userId));
|
|
console.log(`Push token updated for userId ${userId}`);
|
|
} catch (error) {
|
|
console.error("Error updating push token", error);
|
|
throw new Error("Failed to update push token");
|
|
}
|
|
};
|
|
|
|
export const sendPushNotification = async (expoPushToken: string, message: string) => {
|
|
const notificationMessage = {
|
|
to: expoPushToken,
|
|
sound: 'default',
|
|
title: 'New message!',
|
|
body: message,
|
|
data: { message }, // Extra data you might want to send
|
|
};
|
|
|
|
try {
|
|
await fetch('https://exp.host/--/api/v2/push/send', {
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Accept-Encoding': 'gzip, deflate',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(notificationMessage),
|
|
});
|
|
console.log('Push notification sent successfully');
|
|
} catch (error) {
|
|
console.error('Error sending push notification', error);
|
|
}
|
|
};
|