dont even know if I changed anything

This commit is contained in:
2024-10-22 16:38:45 -05:00
parent fc8f26dd0b
commit 2c2cb819e1
12 changed files with 202 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
// https://orm.drizzle.team/docs/sql-schema-declaration
import { db } from '~/server/db';
import { sql } from "drizzle-orm";
import {
boolean,
@@ -161,3 +162,4 @@ export const quickReplyOptions = pgTable(
quickReplyIdIndex: index('qr_options_quick_reply_id_idx').on(table.quickReplyId),
})
);

View File

@@ -1,7 +1,7 @@
import 'server-only';
import { db } from '~/server/db';
import * as schema from '~/server/db/schema';
import { eq, and, or, like, not } from 'drizzle-orm';
import { eq, and, or, like, not, desc, sql } from 'drizzle-orm';
import { User,
Relationship,
UserRelationship,
@@ -227,7 +227,7 @@ export const updateRelationshipStatus = async (
}
const relationship = relationships[0] as Relationship;
// Everything above is just getting info
if (userId === relationship.requestorId) {
if (userId === relationship.requestorId && status === 'accepted') {
throw new Error('The requestor cannot accept the relationship they requested.');
}
if (status === 'accepted') {
@@ -255,7 +255,7 @@ export const updateRelationshipStatus = async (
return relationshipData;
} else if (status === 'rejected') {
await db.delete(schema.userRelationships)
.where(eq(schema.userRelationships.id, userRelationship.id));
.where(eq(schema.userRelationships.relationshipId, relationship.id));
await db.delete(schema.relationships)
.where(eq(schema.relationships.id, relationship.id));
return null;
@@ -325,3 +325,36 @@ export const createRelationshipRequest = async (userId: number, partnerId: numbe
}
};
export const getMessages = async (userId: number, limit: number, offset: number) => {
try {
const messages = await db.select().from(schema.messages)
.where(or(
eq(schema.messages.senderId, userId),
eq(schema.messages.receiverId, userId)
))
.limit(limit)
.offset(offset)
.orderBy(desc(schema.messages.createdAt), desc(schema.messages.id));
return messages as Message[];
} catch (error) {
console.error('Error getting messages:', error);
throw error;
}
};
export const sendMessage = async (message: Message) => {
try {
const [newMessage] = await db.insert(schema.messages).values({
senderId: message.senderId,
receiverId: message.receiverId,
text: message.text,
hasLocation: message.hasLocation,
hasMedia: message.hasMedia,
hasQuickReply: message.hasQuickReply,
}).returning();
return newMessage as Message;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};