add countdown apis
This commit is contained in:
@@ -26,6 +26,29 @@ export const getUser = async (userId: number) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getRelationship = async (userId: number) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const userRelationship = await db.select()
|
||||
.from(schema.userRelationships)
|
||||
.where(eq(schema.userRelationships.userId, user.id))
|
||||
.limit(1)
|
||||
.then(results => results[0]);
|
||||
if (!userRelationship) throw new Error('No relationships found for user');
|
||||
const relationship = await db.select()
|
||||
.from(schema.relationships)
|
||||
.where(eq(schema.relationships.id, userRelationship.relationshipId))
|
||||
.limit(1)
|
||||
.then(results => results[0] as Relationship);
|
||||
if (!relationship) throw new Error('Relationship not found');
|
||||
return relationship;
|
||||
} catch (error) {
|
||||
console.error('Error getting relationship:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getInitialDataByAppleId = async (appleId: string) => {
|
||||
try {
|
||||
const users = await db.select().from(schema.users)
|
||||
@@ -358,3 +381,53 @@ export const sendMessage = async (message: Message) => {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const setCountdown = async (userId: number, countdown: Countdown) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const relationship = await getRelationship(userId);
|
||||
if (!relationship) throw new Error("Relationship not found");
|
||||
const existingCountdown: Countdown | null = await getCountdown(userId);
|
||||
let result;
|
||||
if (existingCountdown !== null) {
|
||||
result = await db.update(schema.countdowns)
|
||||
.set({ title: countdown.title, date: countdown.date })
|
||||
.where(eq(schema.countdowns.id, existingCountdown.id))
|
||||
.returning();
|
||||
} else {
|
||||
result = await db.insert(schema.countdowns)
|
||||
.values({
|
||||
relationshipId: relationship.id,
|
||||
title: countdown.title,
|
||||
date: countdown.date,
|
||||
}).returning();
|
||||
}
|
||||
return result[0] as Countdown;
|
||||
} catch (error) {
|
||||
console.error('Error setting countdown:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getCountdown = async (userId: number) => {
|
||||
try {
|
||||
const user = await getUser(userId);
|
||||
if (!user) throw new Error("User not found");
|
||||
const relationship = await getRelationship(userId);
|
||||
if (!relationship) throw new Error("Relationship not found");
|
||||
const countdown = await db.select()
|
||||
.from(schema.countdowns)
|
||||
.where(eq(schema.countdowns.relationshipId, relationship.id))
|
||||
.limit(1)
|
||||
.then(results => results[0] as Countdown);
|
||||
if (!countdown) {
|
||||
console.log('No countdown found');
|
||||
return null;
|
||||
}
|
||||
return countdown;
|
||||
} catch (error) {
|
||||
console.error('Error getting countdown:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user