fix: edit messages with votes (#737)
This commit is contained in:
parent
2d47ffbd77
commit
5bb62f1b14
1 changed files with 20 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import 'server-only';
|
import 'server-only';
|
||||||
|
|
||||||
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
import { genSaltSync, hashSync } from 'bcrypt-ts';
|
||||||
import { and, asc, desc, eq, gt, gte } from 'drizzle-orm';
|
import { and, asc, desc, eq, gt, gte, inArray } from 'drizzle-orm';
|
||||||
import { drizzle } from 'drizzle-orm/postgres-js';
|
import { drizzle } from 'drizzle-orm/postgres-js';
|
||||||
import postgres from 'postgres';
|
import postgres from 'postgres';
|
||||||
|
|
||||||
|
|
@ -301,11 +301,28 @@ export async function deleteMessagesByChatIdAfterTimestamp({
|
||||||
timestamp: Date;
|
timestamp: Date;
|
||||||
}) {
|
}) {
|
||||||
try {
|
try {
|
||||||
return await db
|
const messagesToDelete = await db
|
||||||
.delete(message)
|
.select({ id: message.id })
|
||||||
|
.from(message)
|
||||||
.where(
|
.where(
|
||||||
and(eq(message.chatId, chatId), gte(message.createdAt, timestamp)),
|
and(eq(message.chatId, chatId), gte(message.createdAt, timestamp)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const messageIds = messagesToDelete.map((message) => message.id);
|
||||||
|
|
||||||
|
if (messageIds.length > 0) {
|
||||||
|
await db
|
||||||
|
.delete(vote)
|
||||||
|
.where(
|
||||||
|
and(eq(vote.chatId, chatId), inArray(vote.messageId, messageIds)),
|
||||||
|
);
|
||||||
|
|
||||||
|
return await db
|
||||||
|
.delete(message)
|
||||||
|
.where(
|
||||||
|
and(eq(message.chatId, chatId), inArray(message.id, messageIds)),
|
||||||
|
);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
'Failed to delete messages by id after timestamp from database',
|
'Failed to delete messages by id after timestamp from database',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue